Fixes.
[wolnelektury.git] / src / sponsors / models.py
index 0565b97..c193f42 100644 (file)
@@ -1,10 +1,10 @@
-# -*- coding: utf-8 -*-
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 import json
 import time
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 import json
 import time
-from StringIO import StringIO
+from io import BytesIO
+from django.core.cache import cache
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 from django.template.loader import render_to_string
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 from django.template.loader import render_to_string
@@ -12,7 +12,6 @@ from PIL import Image
 
 from jsonfield import JSONField
 from django.core.files.base import ContentFile
 
 from jsonfield import JSONField
 from django.core.files.base import ContentFile
-from ssify import flush_ssi_includes
 
 THUMB_WIDTH = 120
 THUMB_HEIGHT = 120
 
 THUMB_WIDTH = 120
 THUMB_HEIGHT = 120
@@ -24,7 +23,7 @@ class Sponsor(models.Model):
     logo = models.ImageField(_('logo'), upload_to='sponsorzy/sponsor/logo')
     url = models.URLField(_('url'), blank=True)
 
     logo = models.ImageField(_('logo'), upload_to='sponsorzy/sponsor/logo')
     url = models.URLField(_('url'), blank=True)
 
-    def __unicode__(self):
+    def __str__(self):
         return self.name
 
     def description(self):
         return self.name
 
     def description(self):
@@ -66,28 +65,29 @@ class SponsorPage(models.Model):
             if simg.size[0] > THUMB_WIDTH or simg.size[1] > THUMB_HEIGHT:
                 size = (
                     min(THUMB_WIDTH,
             if simg.size[0] > THUMB_WIDTH or simg.size[1] > THUMB_HEIGHT:
                 size = (
                     min(THUMB_WIDTH,
-                        simg.size[0] * THUMB_HEIGHT / simg.size[1]),
+                        round(simg.size[0] * THUMB_HEIGHT / simg.size[1])),
                     min(THUMB_HEIGHT,
                     min(THUMB_HEIGHT,
-                        simg.size[1] * THUMB_WIDTH / simg.size[0])
+                        round(simg.size[1] * THUMB_WIDTH / simg.size[0]))
                 )
                 simg = simg.resize(size, Image.ANTIALIAS)
             sprite.paste(simg, (
                 )
                 simg = simg.resize(size, Image.ANTIALIAS)
             sprite.paste(simg, (
-                    (THUMB_WIDTH - simg.size[0]) / 2,
-                    i * THUMB_HEIGHT + (THUMB_HEIGHT - simg.size[1]) / 2,
+                    round((THUMB_WIDTH - simg.size[0]) / 2),
+                    round(i * THUMB_HEIGHT + (THUMB_HEIGHT - simg.size[1]) / 2),
                     ))
                     ))
-        imgstr = StringIO()
+        imgstr = BytesIO()
         sprite.save(imgstr, 'png')
 
         if self.sprite:
             self.sprite.delete(save=False)
         sprite.save(imgstr, 'png')
 
         if self.sprite:
             self.sprite.delete(save=False)
-        self.sprite.save('sponsorzy/sprite/%s-%d.png' % (self.name, time.time()), ContentFile(imgstr.getvalue()), save=False)
+        self.sprite.save('sponsorzy/sprite/%s-%d.png' % (
+            self.name, time.time()), ContentFile(imgstr.getvalue()), save=False)
 
     def html(self):
         return self._html
     html = property(fget=html)
 
     def save(self, *args, **kwargs):
 
     def html(self):
         return self._html
     html = property(fget=html)
 
     def save(self, *args, **kwargs):
-        if isinstance(self.sponsors, basestring):
+        if isinstance(self.sponsors, str):
             # Walkaround for weird jsonfield 'no-decode' optimization.
             self.sponsors = json.loads(self.sponsors)
         self.render_sprite()
             # Walkaround for weird jsonfield 'no-decode' optimization.
             self.sponsors = json.loads(self.sponsors)
         self.render_sprite()
@@ -96,12 +96,8 @@ class SponsorPage(models.Model):
             'page': self
         })
         ret = super(SponsorPage, self).save(*args, **kwargs)
             'page': self
         })
         ret = super(SponsorPage, self).save(*args, **kwargs)
-        self.flush_includes()
+        cache.delete('sponsor_page:' + name)
         return ret
 
         return ret
 
-    def flush_includes(self):
-        flush_ssi_includes(['/sponsors/page/%s.html' % self.name])
-
-    def __unicode__(self):
+    def __str__(self):
         return self.name
         return self.name
-