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 tags = models.ManyToManyField('catalogue.Tag')
107 # logo = models.ImageField(upload_to='people/logo', blank=True)
108 # country = models.CharField(max_length=64, blank=True, choices=countries)
109 # www = models.URLField(blank=True)
110 # description = models.TextField(blank=True, default="")
111 # #projects = JSONField(default=[])
112 # projects = models.TextField(blank=True, default="")
114 # preview_html = models.TextField(blank=True, default="")
116 # created_at = models.DateTimeField(auto_now_add=True)
121 def get_absolute_url(self):
122 return reverse("organizations_main", args=[self.pk])
124 def get_user_status(self, user):
125 if not user.is_authenticated():
128 member = self.membership_set.get(user=user)
129 except Membership.DoesNotExist:
134 def is_member(self, user):
135 return self.get_user_status(user) in (
136 Membership.Status.OWNER, Membership.Status.REGULAR)
138 def is_owner(self, user):
139 return self.get_user_status(user) == Membership.Status.OWNER
141 def set_user_status(self, user, status):
143 member = self.membership_set.get(user=user)
144 except Membership.DoesNotExist:
145 if status is not None:
146 self.membership_set.create(user=user, status=status)
148 if status is not None:
149 member.status = status
155 class Membership(models.Model):
162 (PENDING, 'Membership pending'),
163 (REGULAR, 'Regular member'),
164 (OWNER, 'Organizaition owner'),
167 organization = models.ForeignKey(Organization)
168 user = models.ForeignKey(User)
169 status = models.CharField(max_length=10, choices=Status.choices, default=Status.REGULAR)
170 created_at = models.DateTimeField(auto_now_add=True)
173 ordering = ('user', 'organization')