2012-07-28 21:05:01 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.db import models
|
2015-09-08 20:07:24 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-11-22 12:21:54 +00:00
|
|
|
from django.urls import reverse
|
2012-07-28 21:05:01 +00:00
|
|
|
|
2020-11-22 12:21:54 +00:00
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
from django.contrib.auth import get_user_model
|
2015-09-08 20:07:24 +00:00
|
|
|
|
2016-02-27 22:38:27 +00:00
|
|
|
from reversion import revisions as reversion
|
2012-07-28 21:05:01 +00:00
|
|
|
|
2020-11-22 12:21:54 +00:00
|
|
|
User = get_user_model()
|
|
|
|
|
2012-07-28 21:05:01 +00:00
|
|
|
|
|
|
|
TEAM_ACCESS_CHOICES = [
|
2015-06-26 03:46:09 +00:00
|
|
|
("open", _("open")),
|
|
|
|
("application", _("by application")),
|
|
|
|
("invitation", _("by invitation"))
|
2012-07-28 21:05:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class Team(models.Model):
|
|
|
|
|
2015-06-26 03:46:09 +00:00
|
|
|
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
|
2015-06-26 03:46:09 +00:00
|
|
|
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,
|
2015-06-26 03:46:09 +00:00
|
|
|
related_name="manager_teams",
|
|
|
|
verbose_name=_("Manager permissions"))
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2015-06-26 03:46:09 +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
|
|
|
def get_absolute_url(self):
|
2020-11-22 12:21:54 +00:00
|
|
|
return reverse("team_detail", args=[self.slug])
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2015-07-18 07:09:17 +00:00
|
|
|
def __str__(self):
|
2012-07-28 21:05:01 +00:00
|
|
|
return self.name
|
|
|
|
|
2012-07-28 22:29:19 +00:00
|
|
|
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
|
|
|
|
2012-08-02 17:03:16 +00:00
|
|
|
def applicants(self):
|
|
|
|
return self.memberships.filter(state="applied")
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-02 17:03:16 +00:00
|
|
|
def invitees(self):
|
|
|
|
return self.memberships.filter(state="invited")
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-02 17:03:16 +00:00
|
|
|
def members(self):
|
|
|
|
return self.memberships.filter(state="member")
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-02 17:03:16 +00:00
|
|
|
def managers(self):
|
|
|
|
return self.memberships.filter(state="manager")
|
2012-07-28 22:29:19 +00:00
|
|
|
|
2015-06-26 03:46:09 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Team')
|
|
|
|
verbose_name_plural = _('Teams')
|
2012-07-28 21:05:01 +00:00
|
|
|
|
2017-04-17 12:51:48 +00:00
|
|
|
|
2012-07-28 21:05:01 +00:00
|
|
|
MEMBERSHIP_STATE_CHOICES = [
|
2015-06-26 03:46:09 +00:00
|
|
|
("applied", _("applied")),
|
|
|
|
("invited", _("invited")),
|
|
|
|
("declined", _("declined")),
|
|
|
|
("rejected", _("rejected")),
|
|
|
|
("member", _("member")),
|
|
|
|
("manager", _("manager")),
|
2012-07-28 21:05:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class Membership(models.Model):
|
|
|
|
|
2020-11-22 12:21:54 +00:00
|
|
|
user = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name="memberships",
|
|
|
|
verbose_name=_("User"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
team = models.ForeignKey(
|
|
|
|
Team,
|
|
|
|
related_name="memberships",
|
|
|
|
verbose_name=_("Team"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2015-06-26 03:46:09 +00:00
|
|
|
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
|
|
|
|
2017-04-17 12:51:48 +00:00
|
|
|
|
2012-07-28 21:05:01 +00:00
|
|
|
reversion.register(Membership)
|