2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
|
|
|
#
|
|
|
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
|
|
# http://ae.utbm.fr.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License a published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
|
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
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
|
2016-08-10 14:23:12 +00:00
|
|
|
from django.db import IntegrityError, transaction
|
2016-02-02 15:34:36 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-09-02 19:21:57 +00:00
|
|
|
from django.utils import timezone
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2016-08-10 14:23:12 +00:00
|
|
|
from core.models import User, MetaGroup, Group, SithFile
|
2016-01-29 14:20:00 +00:00
|
|
|
|
|
|
|
# Create your models here.
|
|
|
|
|
|
|
|
class Club(models.Model):
|
|
|
|
"""
|
|
|
|
The Club class, made as a tree to allow nice tidy organization
|
|
|
|
"""
|
2017-05-20 10:36:18 +00:00
|
|
|
id = models.AutoField(primary_key=True, db_index=True)
|
2016-08-13 14:08:02 +00:00
|
|
|
name = models.CharField(_('name'), max_length=64)
|
2016-01-29 14:20:00 +00:00
|
|
|
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
|
2016-02-02 15:34:36 +00:00
|
|
|
owner_group = models.ForeignKey(Group, related_name="owned_club",
|
2016-12-10 00:29:56 +00:00
|
|
|
default=settings.SITH_GROUP_ROOT_ID)
|
2016-02-05 15:59:42 +00:00
|
|
|
edit_groups = models.ManyToManyField(Group, related_name="editable_club", blank=True)
|
|
|
|
view_groups = models.ManyToManyField(Group, related_name="viewable_club", blank=True)
|
2016-11-20 10:56:33 +00:00
|
|
|
home = models.OneToOneField(SithFile, related_name='home_of_club', verbose_name=_("home"), null=True, blank=True,
|
|
|
|
on_delete=models.SET_NULL)
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2017-03-24 08:19:15 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ['name', 'unix_name']
|
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
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()
|
|
|
|
|
2016-08-10 14:23:12 +00:00
|
|
|
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
|
2016-08-13 14:08:02 +00:00
|
|
|
old = Club.objects.filter(id=self.id).first()
|
|
|
|
if not old:
|
2016-08-10 14:23:12 +00:00
|
|
|
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()
|
2016-03-29 10:45:10 +00:00
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-02-02 15:34:36 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('club:club_view', kwargs={'club_id': self.id})
|
|
|
|
|
2016-08-07 18:10:50 +00:00
|
|
|
def get_display_name(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-02-05 15:59:42 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be super edited by the given user
|
|
|
|
"""
|
2016-09-02 19:21:57 +00:00
|
|
|
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)
|
2016-02-05 15:59:42 +00:00
|
|
|
|
|
|
|
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)
|
2017-05-10 17:16:18 +00:00
|
|
|
if ms is not None and ms.role > settings.SITH_MAXIMUM_FREE_ROLE:
|
2016-02-05 15:59:42 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_be_viewed_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be seen by the given user
|
|
|
|
"""
|
2016-12-10 00:58:30 +00:00
|
|
|
sub = User.objects.filter(pk=user.pk).first()
|
2016-02-05 15:59:42 +00:00
|
|
|
if sub is None:
|
|
|
|
return False
|
2017-02-24 01:59:59 +00:00
|
|
|
return sub.is_subscribed
|
2016-02-05 15:59:42 +00:00
|
|
|
|
2017-05-20 10:36:18 +00:00
|
|
|
_memberships = {}
|
2016-02-05 15:59:42 +00:00
|
|
|
def get_membership_for(self, user):
|
|
|
|
"""
|
|
|
|
Returns the current membership the given user
|
|
|
|
"""
|
2017-05-20 10:36:18 +00:00
|
|
|
try:
|
|
|
|
return Club._memberships[self.id][user.id]
|
|
|
|
except:
|
|
|
|
m = self.members.filter(user=user.id).filter(end_date=None).first()
|
|
|
|
try:
|
|
|
|
Club._memberships[self.id][user.id] = m
|
|
|
|
except:
|
|
|
|
Club._memberships[self.id] = {}
|
|
|
|
Club._memberships[self.id][user.id] = m
|
|
|
|
return m
|
2016-02-05 15:59:42 +00:00
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
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.
|
|
|
|
"""
|
2016-09-15 09:06:51 +00:00
|
|
|
user = models.ForeignKey(User, verbose_name=_('user'), related_name="memberships", null=False, blank=False)
|
2016-07-19 17:03:16 +00:00
|
|
|
club = models.ForeignKey(Club, verbose_name=_('club'), related_name="members", null=False, blank=False)
|
2016-12-28 23:42:26 +00:00
|
|
|
start_date = models.DateField(_('start date'), default=timezone.now)
|
2016-01-29 14:20:00 +00:00
|
|
|
end_date = models.DateField(_('end date'), null=True, blank=True)
|
2016-04-20 00:07:01 +00:00
|
|
|
role = models.IntegerField(_('role'), choices=sorted(settings.SITH_CLUB_ROLES.items()),
|
|
|
|
default=sorted(settings.SITH_CLUB_ROLES.items())[0][0])
|
2016-08-13 14:08:02 +00:00
|
|
|
description = models.CharField(_('description'), max_length=128, null=False, blank=True)
|
2016-01-29 14:20:00 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2016-12-10 00:58:30 +00:00
|
|
|
sub = User.objects.filter(pk=self.user.pk).first()
|
2017-02-24 01:59:59 +00:00
|
|
|
if sub is None or not sub.is_subscribed:
|
2016-03-22 08:01:24 +00:00
|
|
|
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():
|
2016-01-29 14:20:00 +00:00
|
|
|
raise ValidationError(_('User is already member of that club'))
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-18 15:47:43 +00:00
|
|
|
return self.club.name+' - '+self.user.username+' - '+str(settings.SITH_CLUB_ROLES[self.role])+str(
|
2016-01-29 14:20:00 +00:00
|
|
|
" - "+str(_('past member')) if self.end_date is not None else ""
|
|
|
|
)
|
|
|
|
|
2016-09-02 19:21:57 +00:00
|
|
|
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
|
|
|
|
"""
|
2016-09-15 09:06:51 +00:00
|
|
|
if user.memberships:
|
|
|
|
ms = user.memberships.filter(club=self.club, end_date=None).first()
|
2016-09-02 19:21:57 +00:00
|
|
|
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)
|
|
|
|
|
2016-02-04 07:59:03 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('club:club_members', kwargs={'club_id': self.club.id})
|
|
|
|
|