symposion_app/symposion/teams/models.py

97 lines
2.9 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2012-07-28 21:05:01 +00:00
import datetime
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
2012-07-28 21:05:01 +00:00
from django.contrib.auth.models import Permission, User
import reversion
2012-07-28 21:05:01 +00:00
TEAM_ACCESS_CHOICES = [
("open", _("open")),
("application", _("by application")),
("invitation", _("by invitation"))
2012-07-28 21:05:01 +00:00
]
@python_2_unicode_compatible
2012-07-28 21:05:01 +00:00
class Team(models.Model):
slug = models.SlugField(unique=True, verbose_name=_("Slug"))
name = models.CharField(max_length=100, verbose_name=_("Name"))
description = models.TextField(blank=True, verbose_name=_("Description"))
access = models.CharField(max_length=20, choices=TEAM_ACCESS_CHOICES,
verbose_name=_("Access"))
2014-07-30 18:19:26 +00:00
2012-08-08 03:28:15 +00:00
# member permissions
permissions = models.ManyToManyField(Permission, blank=True,
related_name="member_teams",
verbose_name=_("Permissions"))
2014-07-30 18:19:26 +00:00
2012-08-08 03:28:15 +00:00
# manager permissions
2014-07-30 18:19:26 +00:00
manager_permissions = models.ManyToManyField(Permission, blank=True,
related_name="manager_teams",
verbose_name=_("Manager permissions"))
2014-07-30 18:19:26 +00:00
created = models.DateTimeField(default=datetime.datetime.now,
editable=False, verbose_name=_("Created"))
2014-07-30 18:19:26 +00:00
2012-08-14 20:59:06 +00:00
@models.permalink
def get_absolute_url(self):
return ("team_detail", [self.slug])
2014-07-30 18:19:26 +00:00
def __str__(self):
2012-07-28 21:05:01 +00:00
return self.name
def get_state_for_user(self, user):
try:
return self.memberships.get(user=user).state
except Membership.DoesNotExist:
return None
2014-07-30 18:19:26 +00:00
def applicants(self):
return self.memberships.filter(state="applied")
2014-07-30 18:19:26 +00:00
def invitees(self):
return self.memberships.filter(state="invited")
2014-07-30 18:19:26 +00:00
def members(self):
return self.memberships.filter(state="member")
2014-07-30 18:19:26 +00:00
def managers(self):
return self.memberships.filter(state="manager")
class Meta:
verbose_name = _('Team')
verbose_name_plural = _('Teams')
2012-07-28 21:05:01 +00:00
MEMBERSHIP_STATE_CHOICES = [
("applied", _("applied")),
("invited", _("invited")),
("declined", _("declined")),
("rejected", _("rejected")),
("member", _("member")),
("manager", _("manager")),
2012-07-28 21:05:01 +00:00
]
class Membership(models.Model):
user = models.ForeignKey(User, related_name="memberships",
verbose_name=_("User"))
team = models.ForeignKey(Team, related_name="memberships",
verbose_name=_("Team"))
state = models.CharField(max_length=20, choices=MEMBERSHIP_STATE_CHOICES,
verbose_name=_("State"))
message = models.TextField(blank=True, verbose_name=_("Message"))
class Meta:
verbose_name = _("Membership")
verbose_name_plural = _("Memberships")
2012-07-28 21:05:01 +00:00
reversion.register(Membership)