1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
4 from django.core.urlresolvers import reverse
5 from django.contrib.auth.models import User
6 from django.db import models
7 from django.utils.encoding import python_2_unicode_compatible
11 ('intl', 'International'),
17 ('cz', 'Czech Republic'),
31 ('nl', 'Netherlands'),
39 ('uk', 'United Kingdom'),
43 class Card(models.Model):
44 logo = models.ImageField(upload_to='people/logo', blank=True)
45 country = models.CharField(max_length=64, blank=True, choices=countries)
46 www = models.URLField(blank=True)
47 description = models.TextField(blank=True, default="")
48 projects = models.TextField(blank=True, default="")
50 created_at = models.DateTimeField(auto_now_add=True)
55 def get_projects(self):
56 for project_line in self.projects.strip().split('\n'):
57 parts = project_line.strip().split(None, 2)
58 if not parts or not parts[0]:
60 url, lang, desc = (parts + [''] * 2)[:3]
64 @python_2_unicode_compatible
66 user = models.OneToOneField(User, primary_key=True)
69 return self.user.get_full_name()
71 def get_absolute_url(self):
72 return reverse('organizations_user', args=[self.user.pk])
75 @python_2_unicode_compatible
76 class Organization(Card):
77 name = models.CharField(max_length=1024)
82 def get_absolute_url(self):
83 return reverse("organizations_main", args=[self.pk])
85 def get_user_status(self, user):
86 if not user.is_authenticated():
89 member = self.membership_set.get(user=user)
90 except Membership.DoesNotExist:
95 def is_member(self, user):
96 return self.get_user_status(user) in (
97 Membership.Status.OWNER, Membership.Status.REGULAR)
99 def is_owner(self, user):
100 return self.get_user_status(user) == Membership.Status.OWNER
102 def set_user_status(self, user, status):
104 member = self.membership_set.get(user=user)
105 except Membership.DoesNotExist:
106 if status is not None:
107 self.membership_set.create(user=user, status=status)
109 if status is not None:
110 member.status = status
116 class Membership(models.Model):
123 (PENDING, 'Membership pending'),
124 (REGULAR, 'Regular member'),
125 (OWNER, 'Organizaition owner'),
128 organization = models.ForeignKey(Organization)
129 user = models.ForeignKey(User)
130 status = models.CharField(max_length=10, choices=Status.choices, default=Status.REGULAR)
131 created_at = models.DateTimeField(auto_now_add=True)
134 ordering = ('user', 'organization')