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
8 from django.template.loader import render_to_string
9 from django.utils import translation
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)
86 except AssertionError:
87 return self.preview_html
90 @python_2_unicode_compatible
92 preview_html_template = 'organizations/snippets/user.html'
93 user = models.ForeignKey(User, unique=True, primary_key=True)
98 def get_absolute_url(self):
99 return reverse('organizations_user', args=[self.user.pk])
102 @python_2_unicode_compatible
103 class Organization(Card):
104 preview_html_template = 'organizations/snippets/organization.html'
106 name = models.CharField(max_length=1024)
107 tags = models.ManyToManyField('catalogue.Tag')
108 # logo = models.ImageField(upload_to='people/logo', blank=True)
109 # country = models.CharField(max_length=64, blank=True, choices=countries)
110 # www = models.URLField(blank=True)
111 # description = models.TextField(blank=True, default="")
112 # #projects = JSONField(default=[])
113 # projects = models.TextField(blank=True, default="")
115 # preview_html = models.TextField(blank=True, default="")
117 # created_at = models.DateTimeField(auto_now_add=True)
122 def get_absolute_url(self):
123 return reverse("organizations_main", args=[self.pk])
125 def get_user_status(self, user):
126 if not user.is_authenticated():
129 member = self.membership_set.get(user=user)
130 except Membership.DoesNotExist:
135 def is_member(self, user):
136 return self.get_user_status(user) in (
137 Membership.Status.OWNER, Membership.Status.REGULAR)
139 def is_owner(self, user):
140 return self.get_user_status(user) == Membership.Status.OWNER
142 def set_user_status(self, user, status):
144 member = self.membership_set.get(user=user)
145 except Membership.DoesNotExist:
146 if status is not None:
147 self.membership_set.create(user=user, status=status)
149 if status is not None:
150 member.status = status
156 class Membership(models.Model):
163 (PENDING, 'Membership pending'),
164 (REGULAR, 'Regular member'),
165 (OWNER, 'Organizaition owner'),
168 organization = models.ForeignKey(Organization)
169 user = models.ForeignKey(User)
170 status = models.CharField(max_length=10, choices=Status.choices, default=Status.REGULAR)
171 created_at = models.DateTimeField(auto_now_add=True)
174 ordering = ('user', 'organization')