Added LT translation (without modeltranslation).
[wolnelektury.git] / apps / sponsors / models.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
7 from django.template.loader import render_to_string
8
9 from sorl.thumbnail.fields import ImageWithThumbnailsField
10 from sponsors.fields import JSONField
11
12
13 class Sponsor(models.Model):
14     name = models.CharField(_('name'), max_length=120)
15     _description = models.CharField(_('description'), blank=True, max_length=255)
16     logo = ImageWithThumbnailsField(
17         _('logo'),
18         upload_to='sponsorzy/sponsor/logo',
19         thumbnail={
20             'size': (120, 60),
21             'extension': 'png',
22             'options': ['pad', 'detail'],
23         })
24     url = models.URLField(_('url'), blank=True, verify_exists=False)
25
26     def __unicode__(self):
27         return self.name
28
29     def description(self):
30         if len(self._description):
31             return self._description
32         else:
33             return self.name
34
35
36 class SponsorPage(models.Model):
37     name = models.CharField(_('name'), max_length=120)
38     sponsors = JSONField(_('sponsors'), default={})
39     _html = models.TextField(blank=True, editable=False)
40
41     def populated_sponsors(self):
42         result = []
43         for column in self.get_sponsors_value():
44             result_group = {'name': column['name'], 'sponsors': []}
45             sponsor_objects = Sponsor.objects.in_bulk(column['sponsors'])
46             for sponsor_pk in column['sponsors']:
47                 try:
48                     result_group['sponsors'].append(sponsor_objects[sponsor_pk])
49                 except KeyError:
50                     pass
51             result.append(result_group)
52         return result
53
54     def html(self):
55         return self._html
56     html = property(fget=html)
57
58     def save(self, *args, **kwargs):
59         self._html = render_to_string('sponsors/page.html', {
60             'sponsors': self.populated_sponsors(),
61         })
62         return super(SponsorPage, self).save(*args, **kwargs)
63
64     def __unicode__(self):
65         return self.name
66