Add occupation model to function as history of roles
This commit is contained in:
+45
-7
@@ -4,9 +4,10 @@ from django.db import models
|
||||
from django.utils import timezone
|
||||
# from datetime import timedelta
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
from webapp.utils import month_from_now
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
# from django.contrib.auth.models import User
|
||||
from auditlog.registry import auditlog
|
||||
from phonenumber_field.modelfields import PhoneNumberField
|
||||
# from django.contrib.postgres.fields import JSONField
|
||||
@@ -163,7 +164,7 @@ class Role(PresetRole):
|
||||
Model for Role.
|
||||
|
||||
Model representing an active or historical occupation
|
||||
in an official's history.
|
||||
in the guild.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
@@ -172,12 +173,32 @@ class Role(PresetRole):
|
||||
verbose_name = _('Role')
|
||||
verbose_name_plural = _('Roles')
|
||||
|
||||
start_date = models.DateField(_('Start date'))
|
||||
end_date = models.DateField(_('End date'))
|
||||
committee = models.ForeignKey('Committee', related_name='roles', on_delete=models.SET_NULL, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return '{} (Hallitus: {}) ({})'.format(self.name, _("Yes") if self.is_board else _("No"), self.committee)
|
||||
|
||||
class Official(User):
|
||||
|
||||
class Occupation(models.Model):
|
||||
"""
|
||||
Model for a occupation in guild.
|
||||
|
||||
Model links Official into a role he/she has or has had in the guild.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Occupation')
|
||||
verbose_name_plural = _('Occupations')
|
||||
|
||||
start_date = models.DateField(_('Start date'))
|
||||
end_date = models.DateField(_('End date'))
|
||||
occupation = models.ForeignKey('Role', on_delete=models.SET_NULL, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return '{}: {} - {}'.format(self.occupation.name, self.start_date, self.end_date)
|
||||
|
||||
|
||||
class Official(models.Model):
|
||||
"""Model representing a guild official."""
|
||||
|
||||
class Meta:
|
||||
@@ -186,11 +207,28 @@ class Official(User):
|
||||
verbose_name = _('Official')
|
||||
verbose_name_plural = _('Officials')
|
||||
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
|
||||
phone_number = PhoneNumberField(_('Phone number'))
|
||||
role = models.ManyToManyField('Role', related_name='official')
|
||||
role_history = models.ManyToManyField('Occupation', blank=True)
|
||||
|
||||
@property
|
||||
def current_roles(self):
|
||||
return self.role_history.all().filter(end_date__gte=timezone.now()).filter(start_date__lte=timezone.now())
|
||||
|
||||
def __str__(self):
|
||||
return '{} {}'.format(self.first_name, self.last_name)
|
||||
return '{} {}'.format(self.user.first_name, self.user.last_name)
|
||||
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def create_user_profile(sender, instance, created, **kwargs):
|
||||
if created:
|
||||
Official.objects.create(user=instance)
|
||||
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def save_user_profile(sender, instance, **kwargs):
|
||||
instance.official.save()
|
||||
|
||||
|
||||
auditlog.register(Tag)
|
||||
|
||||
Reference in New Issue
Block a user