models.py 7.51 KB
Newer Older
1
2
3
4
5
from django.db import models
from django.core import validators
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
Skia's avatar
Skia committed
6
from django.db import IntegrityError, transaction
7
from django.core.urlresolvers import reverse
Skia's avatar
Skia committed
8
from django.utils import timezone
9

Skia's avatar
Skia committed
10
from core.models import User, MetaGroup, Group, SithFile
Skia's avatar
Skia committed
11
from subscription.models import Subscriber
12
13
14
15
16
17
18

# Create your models here.

class Club(models.Model):
    """
    The Club class, made as a tree to allow nice tidy organization
    """
Skia's avatar
Skia committed
19
    name = models.CharField(_('name'), max_length=64)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    parent = models.ForeignKey('Club', related_name='children', null=True, blank=True)
    unix_name = models.CharField(_('unix name'), max_length=30, unique=True,
            validators=[
                validators.RegexValidator(
                    r'^[a-z0-9][a-z0-9._-]*[a-z0-9]$',
                    _('Enter a valid unix name. This value may contain only '
                        'letters, numbers ./-/_ characters.')
                    ),
                ],
            error_messages={
                'unique': _("A club with that unix name already exists."),
                },
            )
    address = models.CharField(_('address'), max_length=254)
    # email = models.EmailField(_('email address'), unique=True) # This should, and will be generated automatically
35
    owner_group = models.ForeignKey(Group, related_name="owned_club",
Skia's avatar
Skia committed
36
                                    default=settings.SITH_GROUPS['root']['id'])
Skia's avatar
Skia committed
37
38
    edit_groups = models.ManyToManyField(Group, related_name="editable_club", blank=True)
    view_groups = models.ManyToManyField(Group, related_name="viewable_club", blank=True)
Skia's avatar
Skia committed
39
40
    home = models.OneToOneField(SithFile, related_name='home_of_club', verbose_name=_("home"), null=True, blank=True,
            on_delete=models.SET_NULL)
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    def check_loop(self):
        """Raise a validation error when a loop is found within the parent list"""
        objs = []
        cur = self
        while cur.parent is not None:
            if cur in objs:
                raise ValidationError(_('You can not make loops in clubs'))
            objs.append(cur)
            cur = cur.parent

    def clean(self):
        self.check_loop()

Skia's avatar
Skia committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    def _change_unixname(self, new_name):
        c = Club.objects.filter(unix_name=new_name).first()
        if c is None:
            if self.home:
                self.home.name = new_name
                self.home.save()
        else:
            raise ValidationError(_("A club with that unix_name already exists"))

    def make_home(self):
        if not self.home:
            home_root = SithFile.objects.filter(parent=None, name="clubs").first()
            root = User.objects.filter(username="root").first()
            if home_root and root:
                home = SithFile(parent=home_root, name=self.unix_name, owner=root)
                home.save()
                self.home = home
                self.save()

    def save(self, *args, **kwargs):
        with transaction.atomic():
            creation = False
Skia's avatar
Skia committed
77
78
            old = Club.objects.filter(id=self.id).first()
            if not old:
Skia's avatar
Skia committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
                creation = True
            else:
                if old.unix_name != self.unix_name:
                    self._change_unixname(self.unix_name)
            super(Club, self).save(*args, **kwargs)
            if creation:
                board = MetaGroup(name=self.unix_name+settings.SITH_BOARD_SUFFIX)
                board.save()
                member = MetaGroup(name=self.unix_name+settings.SITH_MEMBER_SUFFIX)
                member.save()
                subscribers = Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first()
                self.make_home()
                self.home.edit_groups = [board]
                self.home.view_groups = [member, subscribers]
                self.home.save()
Skia's avatar
Skia committed
94

95
96
97
    def __str__(self):
        return self.name

98
99
100
    def get_absolute_url(self):
        return reverse('club:club_view', kwargs={'club_id': self.id})

Skia's avatar
Skia committed
101
102
103
    def get_display_name(self):
        return self.name

Skia's avatar
Skia committed
104
105
106
107
    def is_owned_by(self, user):
        """
        Method to see if that object can be super edited by the given user
        """
Skia's avatar
Skia committed
108
        return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)
Skia's avatar
Skia committed
109
110
111
112
113
114

    def can_be_edited_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
        ms = self.get_membership_for(user)
Skia's avatar
Skia committed
115
        if ms is not None:
Skia's avatar
Skia committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            return True
        return False

    def can_be_viewed_by(self, user):
        """
        Method to see if that object can be seen by the given user
        """
        sub = Subscriber.objects.filter(pk=user.pk).first()
        if sub is None:
            return False
        return sub.is_subscribed()

    def get_membership_for(self, user):
        """
        Returns the current membership the given user
        """
        return self.members.filter(user=user.id).filter(end_date=None).first()

134
135
136
137
138
139
140
141
142
143
144
class Membership(models.Model):
    """
    The Membership class makes the connection between User and Clubs

    Both Users and Clubs can have many Membership objects:
       - a user can be a member of many clubs at a time
       - a club can have many members at a time too

    A User is currently member of all the Clubs where its Membership has an end_date set to null/None.
    Otherwise, it's a past membership kept because it can be very useful to see who was in which Club in the past.
    """
Skia's avatar
Skia committed
145
    user = models.ForeignKey(User, verbose_name=_('user'), related_name="memberships", null=False, blank=False)
Skia's avatar
Skia committed
146
    club = models.ForeignKey(Club, verbose_name=_('club'), related_name="members", null=False, blank=False)
Skia's avatar
Skia committed
147
    start_date = models.DateField(_('start date'))
148
    end_date = models.DateField(_('end date'), null=True, blank=True)
149
150
    role = models.IntegerField(_('role'), choices=sorted(settings.SITH_CLUB_ROLES.items()),
            default=sorted(settings.SITH_CLUB_ROLES.items())[0][0])
Skia's avatar
Skia committed
151
    description = models.CharField(_('description'), max_length=128, null=False, blank=True)
152
153

    def clean(self):
Skia's avatar
Skia committed
154
155
156
157
        sub = Subscriber.objects.filter(pk=self.user.pk).first()
        if sub is None or not sub.is_subscribed():
            raise ValidationError(_('User must be subscriber to take part to a club'))
        if Membership.objects.filter(user=self.user).filter(club=self.club).filter(end_date=None).exists():
158
159
            raise ValidationError(_('User is already member of that club'))

Skia's avatar
Skia committed
160
161
162
163
164
    def save(self, *args, **kwargs):
        if not self.id:
            self.start_date = timezone.now()
        return super(Membership, self).save(*args, **kwargs)

165
    def __str__(self):
Skia's avatar
Skia committed
166
        return self.club.name+' - '+self.user.username+' - '+str(settings.SITH_CLUB_ROLES[self.role])+str(
167
168
169
                " - "+str(_('past member')) if self.end_date is not None else ""
                )

Skia's avatar
Skia committed
170
171
172
173
174
175
176
177
178
179
    def is_owned_by(self, user):
        """
        Method to see if that object can be super edited by the given user
        """
        return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)

    def can_be_edited_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
Skia's avatar
Skia committed
180
181
        if user.memberships:
            ms = user.memberships.filter(club=self.club, end_date=None).first()
Skia's avatar
Skia committed
182
183
184
            return (ms and ms.role >= self.role) or user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)
        return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)

Skia's avatar
Skia committed
185
186
187
    def get_absolute_url(self):
        return reverse('club:club_members', kwargs={'club_id': self.club.id})