1 from django.db import models
2 from django.utils.translation import ugettext_lazy as _
3 from django.template.loader import render_to_string
5 from sorl.thumbnail.fields import ImageWithThumbnailsField
6 from sponsors.fields import JSONField
9 class Sponsor(models.Model):
10 name = models.CharField(_('name'), max_length=120)
11 _description = models.CharField(_('description'), blank=True, max_length=255)
12 logo = ImageWithThumbnailsField(
14 upload_to='sponsors/sponsor/logo',
18 'options': ['pad', 'detail'],
20 url = models.URLField(_('url'), blank=True, verify_exists=False)
22 def __unicode__(self):
25 def description(self):
26 if len(self._description):
27 return self._description
32 class SponsorPage(models.Model):
33 name = models.CharField(_('name'), max_length=120)
34 sponsors = JSONField(_('sponsors'), default={})
35 _html = models.TextField(blank=True, editable=False)
37 def populated_sponsors(self):
39 for column in self.get_sponsors_value():
40 result_group = {'name': column['name'], 'sponsors': []}
41 sponsor_objects = Sponsor.objects.in_bulk(column['sponsors'])
42 for sponsor_pk in column['sponsors']:
44 result_group['sponsors'].append(sponsor_objects[sponsor_pk])
47 result.append(result_group)
52 html = property(fget=html)
54 def save(self, *args, **kwargs):
55 self._html = render_to_string('sponsors/page.html', {
56 'column_width': self.column_width,
57 'sponsors': self.populated_sponsors(),
59 return super(SponsorPage, self).save(*args, **kwargs)
61 def __unicode__(self):