Files
Pinry/pinry/pins/models.py

30 lines
853 B
Python
Raw Normal View History

2012-04-26 03:44:16 +00:00
from django.db import models
from django.template.defaultfilters import slugify
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
from thumbs import ImageWithThumbsField
import urllib2
class Pin(models.Model):
url = models.TextField()
description = models.TextField(blank=True, null=True)
image = ImageWithThumbsField(upload_to='pins/pin', sizes=((200,1000), ))
2012-04-26 03:44:16 +00:00
def __unicode__(self):
return self.url
2012-04-26 03:44:16 +00:00
def save(self, *args, **kwargs):
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()
# pylint: disable-msg=E1101
2012-04-26 03:44:16 +00:00
self.image.save(self.url.split('/')[-1], File(temp_img))
super(Pin, self).save()
class Meta:
ordering = ['-id']