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.
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
7 from django.template.loader import render_to_string
9 from sorl.thumbnail.fields import ImageWithThumbnailsField
10 from sponsors.fields import JSONField
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(
18 upload_to='sponsors/sponsor/logo',
22 'options': ['pad', 'detail'],
24 url = models.URLField(_('url'), blank=True, verify_exists=False)
26 def __unicode__(self):
29 def description(self):
30 if len(self._description):
31 return self._description
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)
41 def populated_sponsors(self):
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']:
48 result_group['sponsors'].append(sponsor_objects[sponsor_pk])
51 result.append(result_group)
56 html = property(fget=html)
58 def save(self, *args, **kwargs):
59 self._html = render_to_string('sponsors/page.html', {
60 'sponsors': self.populated_sponsors(),
62 return super(SponsorPage, self).save(*args, **kwargs)
64 def __unicode__(self):