Wydzielenie ścieżek do python, virtualenv i pip do zmiennych w env.
[wolnelektury.git] / apps / sponsors / models.py
1 from django.db import models
2 from django.utils.translation import ugettext_lazy as _
3 from django.template.loader import render_to_string
4
5 from sponsors.fields import JSONField
6
7
8 class Sponsor(models.Model):
9     name = models.CharField(_('name'), max_length=120)
10     _description = models.CharField(_('description'), blank=True, max_length=255)
11     logo = models.ImageField(_('logo'), upload_to='sponsors/sponsor/logo')
12     url = models.URLField(_('url'), blank=True, verify_exists=False)
13     
14     def __unicode__(self):
15         return self.name
16
17     def description(self):
18         if len(self._description):
19             return self._description
20         else:
21             return self.name
22
23
24 class SponsorPage(models.Model):
25     name = models.CharField(_('name'), max_length=120)
26     column_width = models.PositiveIntegerField(_('column width'), default=200)
27     sponsors = JSONField(_('sponsors'), default={})
28     _html = models.TextField(blank=True, editable=False)
29     
30     def populated_sponsors(self):
31         result = []
32         for column in self.get_sponsors_value():
33             result_group = {'name': column['name'], 'sponsors': []}
34             sponsor_objects = Sponsor.objects.in_bulk(column['sponsors'])
35             for sponsor_pk in column['sponsors']:
36                 try:
37                     result_group['sponsors'].append(sponsor_objects[sponsor_pk])
38                 except KeyError:
39                     pass
40             result.append(result_group)
41         return result
42     
43     def html(self):
44         return self._html
45     html = property(fget=html)
46
47     def save(self, *args, **kwargs):
48         self._html = render_to_string('sponsors/page.html', {
49             'column_width': self.column_width,
50             'sponsors': self.populated_sponsors(),
51         })
52         return super(SponsorPage, self).save(*args, **kwargs)
53     
54     def __unicode__(self):
55         return self.name
56