ore.models.project.Project.to_dict()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
from django.contrib.auth.models import User
2
from django.db import models
3
4
5
class Project(models.Model):
6
7
    """
8
    Class: Project
9
10
    Fields:
11
     {str}            name     - the name of the project
12
     {User}           owner    - a link to the owner of the project
13
     {User}           users    - a link to the members of the project
14
     {const datetime} created  - timestamp of the moment of project creation (default: now)
15
     {bool}           deleted  - flag indicating whether this project was deleted or not. Simplifies restoration of the
16
                                 project if needed by toggling this member (default: False)
17
    """
18
19
    class Meta:
20
        app_label = 'ore'
21
22
    name = models.CharField(max_length=255)
23
    owner = models.ForeignKey(User, related_name='own_projects')
24
    users = models.ManyToManyField(User, related_name='projects')
25
    modified = models.DateTimeField(auto_now=True)
26
    created = models.DateTimeField(auto_now_add=True, editable=False)
27
    deleted = models.BooleanField(default=False)
28
29
    def __unicode__(self):
30
        return unicode(self.name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unicode does not seem to be defined.
Loading history...
31
32
    def to_dict(self):
33
        """
34
        Method: to_dict
35
36
        Encodes the project as dictionary
37
38
        Returns:
39
         {dict} the project as dictionary
40
        """
41
        return {
42
            'pk': self.pk,
43
            'name': self.name,
44
            'created': self.created
45
        }
46
47
    def is_authorized(self, user):
48
        """
49
        Method: is_authorized
50
51
        A user is athorized to browse a project if he is the owner or member of the project
52
53
        Returns:
54
          {bool}
55
        """
56
        return (self.owner == user) or (
57
            self.users.all().filter(id=user.id).exists())
58