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 sponsors.fields import JSONField
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)
14 def __unicode__(self):
17 def description(self):
18 if len(self._description):
19 return self._description
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)
30 def populated_sponsors(self):
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']:
37 result_group['sponsors'].append(sponsor_objects[sponsor_pk])
40 result.append(result_group)
45 html = property(fget=html)
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(),
52 return super(SponsorPage, self).save(*args, **kwargs)
54 def __unicode__(self):