1 # -*- coding: utf-8 -*-
2 # This file is part of django-sponsors, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from StringIO import StringIO
7 from django.conf import settings
8 from django.db import models
9 from django.utils.translation import ugettext_lazy as _
10 from django.template.loader import render_to_string
13 from jsonfield import JSONField
14 from django.core.files.base import ContentFile
16 THUMB_WIDTH = getattr(settings, 'SPONSORS_THUMB_WIDTH', 120)
17 THUMB_HEIGHT = getattr(settings, 'SPONSORS_THUMB_HEIGHT', 120)
20 class Sponsor(models.Model):
21 name = models.CharField(_('name'), max_length=120)
22 _description = models.CharField(_('description'), blank=True, max_length=255)
23 logo = models.ImageField(_('logo'), upload_to='sponsorzy/sponsor/logo')
24 url = models.URLField(_('url'), blank=True)
26 def __unicode__(self):
29 def description(self):
30 if len(self._description):
31 return self._description
36 width, height = THUMB_WIDTH, THUMB_HEIGHT
38 simg = Image.open(self.logo.path)
39 width = int(float(simg.size[0]) / simg.size[1] * height)
41 simg = Image.open(self.logo.path)
42 height = int(float(simg.size[1]) / simg.size[0] * width)
46 class SponsorPage(models.Model):
47 name = models.CharField(_('name'), max_length=120)
48 sponsors = JSONField(_('sponsors'), default={})
49 _html = models.TextField(blank=True, editable=False)
50 sprite = models.ImageField(upload_to='sponsorzy/sprite', blank=True)
52 def populated_sponsors(self):
55 for column in self.sponsors:
56 result_group = {'name': column['name'], 'sponsors': []}
57 sponsor_objects = Sponsor.objects.in_bulk(column['sponsors'])
58 for sponsor_pk in column['sponsors']:
60 sponsor = sponsor_objects[sponsor_pk]
64 result_group['sponsors'].append((offset, sponsor))
65 offset -= sponsor.size()[1]
66 result.append(result_group)
69 def render_sprite(self):
71 for column in self.sponsors:
72 sponsor_ids.extend(column['sponsors'])
73 sponsors = Sponsor.objects.in_bulk(sponsor_ids)
76 for sponsor in sponsors.values():
78 total_width = max(total_width, w)
80 sprite = Image.new('RGBA', (total_width, total_height))
82 for i, sponsor_id in enumerate(sponsor_ids):
83 sponsor = sponsors[sponsor_id]
84 simg = Image.open(sponsor.logo.path)
85 thumb_size = sponsor.size()
86 # FIXME: This is too complicated now.
87 if simg.size[0] > thumb_size[0] or simg.size[1] > thumb_size[1]:
90 simg.size[0] * thumb_size[1] / simg.size[1]),
92 simg.size[1] * thumb_size[0] / simg.size[0])
94 simg = simg.resize(size, Image.ANTIALIAS)
96 (thumb_size[0] - simg.size[0]) / 2,
97 offset + (thumb_size[1] - simg.size[1]) / 2,
99 offset += thumb_size[1]
101 sprite.save(imgstr, 'png')
104 self.sprite.delete(save=False)
105 self.sprite.save('sponsorzy/sprite/%s-%d.png' % (self.name, time.time()), ContentFile(imgstr.getvalue()), save=False)
109 html = property(fget=html)
111 def save(self, *args, **kwargs):
113 self._html = render_to_string('sponsors/page.html', {
114 'sponsors': self.populated_sponsors(),
117 return super(SponsorPage, self).save(*args, **kwargs)
119 def __unicode__(self):