1 from __future__ import unicode_literals
3 from django.core.urlresolvers import reverse
4 from django.contrib.auth.models import User
5 from django.db import models
6 from django.utils.encoding import python_2_unicode_compatible
7 from django.template.loader import render_to_string
8 from django.utils import translation
9 #from jsonfield import JSONField
13 ('intl', 'International'),
19 ('cz', 'Czech Republic'),
33 ('nl', 'Netherlands'),
41 ('uk', 'United Kingdom'),
45 class Card(models.Model):
46 logo = models.ImageField(upload_to='people/logo', blank=True)
47 country = models.CharField(max_length=64, blank=True, choices=countries)
48 www = models.URLField(blank=True)
49 description = models.TextField(blank=True, default="")
50 projects = models.TextField(blank=True, default="")
52 preview_html = models.TextField(blank=True, default="")
53 preview_html_pl = models.TextField(blank=True, default="")
55 created_at = models.DateTimeField(auto_now_add=True)
60 def save(self, *args, **kwargs):
61 translation.activate('en')
62 self.preview_html = render_to_string(self.preview_html_template, {
65 translation.activate('pl')
66 self.preview_html_pl = render_to_string(self.preview_html_template, {
69 ret = super(Card, self).save(*args, **kwargs)
72 def get_projects(self):
73 for project_line in self.projects.strip().split('\n'):
74 parts = project_line.strip().split(' ', 2)
75 if not parts or not parts[0]:
77 url, lang, desc = (parts + [''] * 2)[:3]
80 def get_preview_html(self):
81 lang = translation.get_language()
83 p = getattr(self, "preview_html_%s" % lang)
87 return self.preview_html
89 @python_2_unicode_compatible
91 preview_html_template = 'organizations/snippets/user.html'
92 user = models.ForeignKey(User, unique=True, primary_key=True)
97 def get_absolute_url(self):
98 return reverse('organizations_user', args=[self.user.pk])
101 @python_2_unicode_compatible
102 class Organization(Card):
103 preview_html_template = 'organizations/snippets/organization.html'
105 name = models.CharField(max_length=1024)
106 #logo = models.ImageField(upload_to='people/logo', blank=True)
107 #country = models.CharField(max_length=64, blank=True, choices=countries)
108 #www = models.URLField(blank=True)
109 #description = models.TextField(blank=True, default="")
110 ##projects = JSONField(default=[])
111 #projects = models.TextField(blank=True, default="")
113 #preview_html = models.TextField(blank=True, default="")
115 #created_at = models.DateTimeField(auto_now_add=True)
120 def get_absolute_url(self):
121 return reverse("organizations_main", args=[self.pk])
123 def get_user_status(self, user):
124 if not user.is_authenticated():
127 member = self.membership_set.get(user=user)
128 except Membership.DoesNotExist:
133 def is_member(self, user):
134 return self.get_user_status(user) in (
135 Membership.Status.OWNER, Membership.Status.REGULAR)
137 def is_owner(self, user):
138 return self.get_user_status(user) == Membership.Status.OWNER
140 def set_user_status(self, user, status):
142 member = self.membership_set.get(user=user)
143 except Membership.DoesNotExist:
144 if status is not None:
145 self.membership_set.create(user=user, status=status)
147 if status is not None:
148 member.status = status
154 class Membership(models.Model):
161 (PENDING, 'Membership pending'),
162 (REGULAR, 'Regular member'),
163 (OWNER, 'Organizaition owner'),
166 organization = models.ForeignKey(Organization)
167 user = models.ForeignKey(User)
168 status = models.CharField(max_length=10, choices=Status.choices, default=Status.REGULAR)
169 created_at = models.DateTimeField(auto_now_add=True)
172 ordering = ('user', 'organization')