cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
group.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.clm.models.group
22 # @author Maciej Nabożny
23 #
24 
25 from django.db import models
26 from clm.models.user import User
27 from clm.utils.exception import CLMException
28 
29 
30 ##
31 #
32 # @model{GROUP}
33 #
34 # Group is an entity uniting Users to enable sharing VMImage resources
35 # between them. Each User may belong to any number of Groups.
36 #
37 class Group(models.Model):
38  ## User being leader of this Group @field
39  leader = models.ForeignKey(User, related_name='group_leader_set')
40  ## Human-friendly name of this group @field
41  name = models.CharField(max_length=45)
42  ## Group's description @field
43  desc = models.TextField(null=True, blank=True)
44  ## "many-to-many" relationship between Group and User defined through UserGroup @field
45  users = models.ManyToManyField(User, through='UserGroup')
46 
47  class Meta:
48  app_label = 'clm'
49 
50  ##
51  #
52  # @returns{string} name of this Group
53  #
54  def __unicode__(self):
55  return self.name
56 
57  @property
58  ##
59  #
60  # @returns{dict} this Group's data
61  # \n fields:
62  # @dictkey{group_id,int} id of this Group
63  # @dictkey{leader_id,int} id of this Group's leader
64  # @dictkey{leader_login,string} login of this Group's leader
65  # @dictkey{leader,string} First name and last name of this Group's leader
66  # @dictkey{name,string} this Group's name
67  # @dictkey{description,string} this Group's description
68  #
69  def dict(self):
70  d = {}
71  d['group_id'] = self.id
72  d['leader_id'] = self.leader.id or ''
73  d['leader_login'] = self.leader.login if self.leader else ''
74  d['leader'] = '%s %s' % (self.leader.first, self.leader.last) if self.leader else ''
75  d['name'] = self.name
76  d['description'] = self.desc
77  return d
78 
79  @staticmethod
80  ##
81  #
82  # @parameter{group_id,int} id of the requested Group
83  #
84  # @returns{Group} instance of the requested Group
85  #
86  # @raises{group_get,CLMException} no such Group
87  #
88  def get(group_id):
89  try:
90  g = Group.objects.get(pk=group_id)
91  except:
92  raise CLMException('group_get')
93 
94  return g
95