|
1
|
|
|
from django.db import models |
|
2
|
|
|
|
|
3
|
|
|
from sigma_core.models.custom_field import CustomField |
|
4
|
|
|
from sigma_core.models.group_field import GroupField |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Group(models.Model): |
|
8
|
|
|
######################### |
|
9
|
|
|
# Constants and choices # |
|
10
|
|
|
######################### |
|
11
|
|
|
ADMINISTRATOR_RANK = 10 |
|
12
|
|
|
|
|
13
|
|
|
########## |
|
14
|
|
|
# Fields # |
|
15
|
|
|
########## |
|
16
|
|
|
name = models.CharField(max_length=254) |
|
17
|
|
|
is_private = models.BooleanField(default=False) |
|
18
|
|
|
description = models.TextField(blank=True) |
|
19
|
|
|
is_protected = models.BooleanField(default=False) # if True, the Group cannot be deleted |
|
20
|
|
|
|
|
21
|
|
|
# The permission a member has upon joining |
|
22
|
|
|
# A value of -1 means that no one can join the group. |
|
23
|
|
|
# A value of 0 means that anyone can request to join the group |
|
24
|
|
|
default_member_rank = models.SmallIntegerField(default=-1) |
|
25
|
|
|
# Invite new members on the group |
|
26
|
|
|
req_rank_invite = models.SmallIntegerField(default=1) |
|
27
|
|
|
# Remove a member from the group |
|
28
|
|
|
req_rank_kick = models.SmallIntegerField(default=ADMINISTRATOR_RANK) |
|
29
|
|
|
# Upgrade someone rank 0 to rank 1 |
|
30
|
|
|
req_rank_accept_join_requests = models.SmallIntegerField(default=1) |
|
31
|
|
|
# Upgrade other users (up to $yourRank - 1) |
|
32
|
|
|
req_rank_promote = models.SmallIntegerField(default=ADMINISTRATOR_RANK) |
|
33
|
|
|
# Downgrade someone (to rank 1 minimum) |
|
34
|
|
|
req_rank_demote = models.SmallIntegerField(default=ADMINISTRATOR_RANK) |
|
35
|
|
|
# Modify group description |
|
36
|
|
|
req_rank_modify_group_infos = models.SmallIntegerField(default=ADMINISTRATOR_RANK) |
|
37
|
|
|
|
|
38
|
|
|
# Related fields: |
|
39
|
|
|
# - invited_users (model User) |
|
40
|
|
|
# - memberships (model GroupMember) |
|
41
|
|
|
# - users (model User) |
|
42
|
|
|
# - fields (model GroupField) |
|
43
|
|
|
# - subgroups (model Group) |
|
44
|
|
|
# - group_parents (model Group) |
|
45
|
|
|
# TODO: Determine whether 'memberships' fields needs to be retrieved every time or not... |
|
46
|
|
|
|
|
47
|
|
|
@property |
|
48
|
|
|
def subgroups_list(self): |
|
49
|
|
|
return [ga.subgroup for ga in self.subgroups.filter(validated=True).select_related('subgroup')] |
|
50
|
|
|
|
|
51
|
|
|
@property |
|
52
|
|
|
def group_parents_list(self): |
|
53
|
|
|
return [ga.parent_group for ga in self.group_parents.filter(validated=True).select_related('parent_group')] |
|
54
|
|
|
|
|
55
|
|
|
@property |
|
56
|
|
|
def members_count(self): |
|
57
|
|
|
return self.memberships.count() |
|
58
|
|
|
|
|
59
|
|
|
################# |
|
60
|
|
|
# Model methods # |
|
61
|
|
|
################# |
|
62
|
|
|
def can_anyone_join(self): |
|
63
|
|
|
return self.default_member_rank >= 0 |
|
64
|
|
|
|
|
65
|
|
|
def __str__(self): # pragma: no cover |
|
66
|
|
|
return self.name |
|
67
|
|
|
|
|
68
|
|
|
############### |
|
69
|
|
|
# Permissions # |
|
70
|
|
|
############### |
|
71
|
|
|
|
|
72
|
|
|
# Perms for admin site |
|
73
|
|
|
def has_perm(self, perm, obj=None): # pragma: no cover |
|
74
|
|
|
return True |
|
75
|
|
|
|
|
76
|
|
|
def has_module_perms(self, app_label): # pragma: no cover |
|
77
|
|
|
return True |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
class GroupAcknowledgment(models.Model): |
|
81
|
|
|
subgroup = models.ForeignKey(Group, related_name='group_parents') |
|
82
|
|
|
parent_group = models.ForeignKey(Group, related_name='subgroups') |
|
83
|
|
|
validated = models.BooleanField(default=False) |
|
84
|
|
|
delegate_admin = models.BooleanField(default=True) |
|
85
|
|
|
created = models.DateTimeField(auto_now_add=True) |
|
86
|
|
|
updated = models.DateTimeField(auto_now=True) |
|
87
|
|
|
|
|
88
|
|
|
def __str__(self): # pragma: no cover |
|
89
|
|
|
if self.validated: |
|
90
|
|
|
return "Group %s acknowledged by Group %s" % (self.subgroup.__str__(), self.parent_group.__str__()) |
|
91
|
|
|
else: |
|
92
|
|
|
return "Group %s awaiting for acknowledgment by Group %s since %s" % (self.subgroup.__str__(), self.parent_group.__str__(), self.created.strftime("%Y-%m-%d %H:%M")) |
|
93
|
|
|
|