symposion_app/symposion/teams/models.py

78 lines
2 KiB
Python
Raw Normal View History

2012-07-28 21:05:01 +00:00
import datetime
from django.db import models
import reversion
from django.contrib.auth.models import Permission, User
TEAM_ACCESS_CHOICES = [
("open", "open"),
("application", "by application"),
("invitation", "by invitation")
2012-07-28 21:05:01 +00:00
]
class Team(models.Model):
slug = models.SlugField(unique=True)
name = models.CharField(max_length=100)
description = models.TextField(blank=True)
access = models.CharField(max_length=20, choices=TEAM_ACCESS_CHOICES)
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")
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")
2012-07-28 21:05:01 +00:00
created = models.DateTimeField(default=datetime.datetime.now, editable=False)
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
2012-07-28 21:05:01 +00:00
def __unicode__(self):
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")
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")
team = models.ForeignKey(Team, related_name="memberships")
state = models.CharField(max_length=20, choices=MEMBERSHIP_STATE_CHOICES)
2012-07-28 21:05:01 +00:00
message = models.TextField(blank=True)
reversion.register(Membership)