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)
78 tags = models.ManyToManyField('catalogue.Tag', blank=True)
83 def get_absolute_url(self):
84 return reverse("organizations_main", args=[self.pk])
86 def get_user_status(self, user):
87 if not user.is_authenticated():
90 member = self.membership_set.get(user=user)
91 except Membership.DoesNotExist:
96 def is_member(self, user):
97 return self.get_user_status(user) in (
98 Membership.Status.OWNER, Membership.Status.REGULAR)
100 def is_owner(self, user):
101 return self.get_user_status(user) == Membership.Status.OWNER
103 def set_user_status(self, user, status):
105 member = self.membership_set.get(user=user)
106 except Membership.DoesNotExist:
107 if status is not None:
108 self.membership_set.create(user=user, status=status)
110 if status is not None:
111 member.status = status
117 class Membership(models.Model):
124 (PENDING, 'Membership pending'),
125 (REGULAR, 'Regular member'),
126 (OWNER, 'Organizaition owner'),
129 organization = models.ForeignKey(Organization)
130 user = models.ForeignKey(User)
131 status = models.CharField(max_length=10, choices=Status.choices, default=Status.REGULAR)
132 created_at = models.DateTimeField(auto_now_add=True)
135 ordering = ('user', 'organization')