2012-04-26 03:44:16 +00:00
|
|
|
from django.db import models
|
|
|
|
|
from django.core.files import File
|
|
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
|
|
|
|
|
2012-09-28 04:42:13 +00:00
|
|
|
from taggit.managers import TaggableManager
|
2012-04-26 03:44:16 +00:00
|
|
|
import urllib2
|
2012-08-02 01:09:16 +00:00
|
|
|
import os
|
2012-07-25 01:50:28 +00:00
|
|
|
from PIL import Image
|
2012-04-26 03:44:16 +00:00
|
|
|
|
2013-02-22 01:53:18 +01:00
|
|
|
from pinry.core.models import User
|
|
|
|
|
|
2012-04-26 03:44:16 +00:00
|
|
|
|
|
|
|
|
class Pin(models.Model):
|
2012-07-24 23:26:38 +00:00
|
|
|
submitter = models.ForeignKey(User)
|
2012-07-03 02:24:34 +00:00
|
|
|
url = models.TextField(blank=True, null=True)
|
2012-04-26 15:38:21 +00:00
|
|
|
description = models.TextField(blank=True, null=True)
|
2012-07-25 01:50:28 +00:00
|
|
|
image = models.ImageField(upload_to='pins/pin/originals/')
|
|
|
|
|
thumbnail = models.ImageField(upload_to='pins/pin/thumbnails/')
|
2012-07-12 22:49:19 +00:00
|
|
|
published = models.DateTimeField(auto_now_add=True)
|
2012-09-28 04:42:13 +00:00
|
|
|
tags = TaggableManager()
|
2012-04-26 03:44:16 +00:00
|
|
|
|
2012-07-25 01:50:28 +00:00
|
|
|
|
2012-04-26 03:44:16 +00:00
|
|
|
def __unicode__(self):
|
2012-04-26 15:38:21 +00:00
|
|
|
return self.url
|
2012-04-26 03:44:16 +00:00
|
|
|
|
2012-07-25 01:50:28 +00:00
|
|
|
|
2012-05-12 00:27:02 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2012-08-02 01:09:16 +00:00
|
|
|
hash_name = os.urandom(32).encode('hex')
|
|
|
|
|
|
2012-04-26 03:44:16 +00:00
|
|
|
if not self.image:
|
|
|
|
|
temp_img = NamedTemporaryFile()
|
|
|
|
|
temp_img.write(urllib2.urlopen(self.url).read())
|
|
|
|
|
temp_img.flush()
|
2012-08-02 01:09:16 +00:00
|
|
|
image = Image.open(temp_img.name)
|
2012-11-01 03:35:57 +00:00
|
|
|
if image.mode != "RGB":
|
|
|
|
|
image = image.convert("RGB")
|
2012-08-02 01:09:16 +00:00
|
|
|
image.save(temp_img.name, 'JPEG')
|
|
|
|
|
self.image.save(''.join([hash_name, '.jpg']), File(temp_img))
|
2012-07-25 01:50:28 +00:00
|
|
|
|
|
|
|
|
if not self.thumbnail:
|
|
|
|
|
if not self.image:
|
|
|
|
|
image = Image.open(temp_img.name)
|
|
|
|
|
else:
|
2012-07-25 02:28:01 +00:00
|
|
|
super(Pin, self).save()
|
2012-07-25 01:50:28 +00:00
|
|
|
image = Image.open(self.image.path)
|
|
|
|
|
size = image.size
|
2012-08-02 00:45:00 +00:00
|
|
|
prop = 200.0 / float(image.size[0])
|
|
|
|
|
size = (int(prop*float(image.size[0])), int(prop*float(image.size[1])))
|
|
|
|
|
image.thumbnail(size, Image.ANTIALIAS)
|
2012-07-25 01:50:28 +00:00
|
|
|
temp_thumb = NamedTemporaryFile()
|
2012-11-01 03:35:57 +00:00
|
|
|
if image.mode != "RGB":
|
|
|
|
|
image = image.convert("RGB")
|
2012-07-25 01:50:28 +00:00
|
|
|
image.save(temp_thumb.name, 'JPEG')
|
2012-08-02 01:09:16 +00:00
|
|
|
self.thumbnail.save(''.join([hash_name, '.jpg']), File(temp_thumb))
|
2012-07-25 01:50:28 +00:00
|
|
|
|
2012-07-24 23:26:38 +00:00
|
|
|
super(Pin, self).save(*args, **kwargs)
|
2012-05-11 02:08:46 +00:00
|
|
|
|
2012-07-25 01:50:28 +00:00
|
|
|
|
2012-05-11 02:08:46 +00:00
|
|
|
class Meta:
|
|
|
|
|
ordering = ['-id']
|