Club model implementation, various other changes

This commit is contained in:
Skia
2016-01-29 15:20:00 +01:00
parent 7f4955d15c
commit 4322318c31
26 changed files with 267 additions and 204 deletions

0
club/__init__.py Normal file
View File

8
club/admin.py Normal file
View File

@ -0,0 +1,8 @@
from django.contrib import admin
from club.models import Club, Membership
admin.site.register(Club)
admin.site.register(Membership)

View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.core.validators
from django.conf import settings
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Club',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
('name', models.CharField(max_length=30, verbose_name='name')),
('unix_name', models.CharField(unique=True, error_messages={'unique': 'A club with that unix name already exists.'}, validators=[django.core.validators.RegexValidator('^[a-z0-9][a-z0-9._-]*[a-z0-9]$', 'Enter a valid unix name. This value may contain only letters, numbers ./-/_ characters.')], verbose_name='unix name', max_length=30)),
('address', models.CharField(max_length=254, verbose_name='address')),
('parent', models.ForeignKey(related_name='children', blank=True, to='club.Club', null=True)),
],
),
migrations.CreateModel(
name='Membership',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
('start_date', models.DateField(auto_now=True, verbose_name='start date')),
('end_date', models.DateField(null=True, verbose_name='end date', blank=True)),
('role', models.IntegerField(verbose_name='role', choices=[(0, 'Membre'), (1, 'Membre actif'), (2, 'Membre du bureau'), (3, 'Responsable info'), (4, 'Secrétaire'), (5, 'Responsable com'), (7, 'Trésorier'), (8, 'Vice-Président'), (9, 'Vice-Président'), (10, 'Président')], default=0)),
('description', models.CharField(max_length=30, verbose_name='description', blank=True)),
('club', models.ForeignKey(related_name='members', to='club.Club')),
('user', models.ForeignKey(related_name='membership', to=settings.AUTH_USER_MODEL)),
],
),
]

View File

77
club/models.py Normal file
View File

@ -0,0 +1,77 @@
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
from core.models import User
# Create your models here.
class Club(models.Model):
"""
The Club class, made as a tree to allow nice tidy organization
"""
name = models.CharField(_('name'), max_length=30)
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
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()
def __str__(self):
return self.name
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.
"""
user = models.ForeignKey(User, related_name="membership", null=False, blank=False)
club = models.ForeignKey(Club, related_name="members", null=False, blank=False)
start_date = models.DateField(_('start date'), auto_now=True)
end_date = models.DateField(_('end date'), null=True, blank=True)
role = models.IntegerField(_('role'), choices=sorted(settings.CLUB_ROLES.items()),
default=sorted(settings.CLUB_ROLES.items())[0][0])
description = models.CharField(_('description'), max_length=30, null=False, blank=True)
def clean(self):
if Membership.objects.filter(user=self.user).filter(club=self.club).filter(end_date=None).exclude(pk=self.pk).exists():
raise ValidationError(_('User is already member of that club'))
def __str__(self):
return self.club.name+' - '+self.user.username+' - '+settings.CLUB_ROLES[self.role]+str(
" - "+str(_('past member')) if self.end_date is not None else ""
)

3
club/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
club/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.