Completed
Push — master ( ebb95c...951ec2 )
by Camille
01:25
created

GroupMember.has_create_permission()   B

Complexity

Conditions 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 14
c 1
b 0
f 1
rs 8.5454
cc 5
1
from django.db import models
2
from django.http import Http404
3
4
from dry_rest_permissions.generics import allow_staff_or_superuser
5
6
from sigma_core.models.user import User
7
8
class GroupMember(models.Model):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable models does not seem to be defined.
Loading history...
9
    """
10
    Modelize a membership relation between an User and a Group.
11
    """
12
    class Meta:
13
        # TODO: Make a primary key once Django supports it
14
        unique_together = (("user", "group"),)
15
16
    user = models.ForeignKey('User', related_name='memberships')
17
    group = models.ForeignKey('Group', related_name='memberships')
18
    created = models.DateTimeField(auto_now_add=True)
19
    join_date = models.DateField(blank=True, null=True)
20
    leave_date = models.DateField(blank=True, null=True)
21
    perm_rank = models.SmallIntegerField(blank=False, default=1)
22
23
    # Related fields:
24
    #   - values (model GroupMemberValue)
25
26
    def can_invite(self):
27
        return self.perm_rank >= self.group.req_rank_invite
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
28
29
    def can_kick(self):
30
        return self.perm_rank >= self.group.req_rank_kick
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
31
32
    def is_accepted(self):
33
        return self.perm_rank > 0
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
34
35
    def __str__(self):
36
        return "User \"%s\" r%d in Group \"%s\"" % (self.user.__str__(), self.perm_rank, self.group.__str__())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
37
38
    # Perms for admin site
39
    def has_perm(self, perm, obj=None):
40
        return True
41
42
    def has_module_perms(self, app_label):
43
        return True
44
45
    # Permissions
46
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
47
    def has_read_permission(request):
48
        return True
49
50
    def has_object_read_permission(self, request):
51
        return True
52
53
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
54
    def has_write_permission(request):
55
        return True
56
57
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
58
    @allow_staff_or_superuser
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable allow_staff_or_superuser does not seem to be defined.
Loading history...
59
    def has_create_permission(request):
60
        from sigma_core.models.group import Group
61
        if request.data.get('user') and request.user.id != request.data.get('user'):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable request does not seem to be defined.
Loading history...
62
            return False
63
        try:
64
            group_id = request.data.get('group', None)
65
            if group_id:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable group_id does not seem to be defined.
Loading history...
66
                request.group = Group.objects.get(pk=group_id)
67
                return request.group.can_anyone_join() or request.user.is_invited_to_group_id(group_id)
68
            return True
69
        except Group.DoesNotExist:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Group does not seem to be defined.
Loading history...
70
            raise Http404()
71
72
    @allow_staff_or_superuser
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable allow_staff_or_superuser does not seem to be defined.
Loading history...
73
    def has_object_write_permission(self, request):
74
        """
75
        TODO: implement that.
76
        """
77
        return True
78