Files
Pinry/pinry/pins/models.py

32 lines
978 B
Python
Raw Normal View History

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-07-24 23:26:38 +00:00
from django.contrib.auth.models import User
2012-04-26 03:44:16 +00:00
from thumbs import ImageWithThumbsField
import urllib2
class Pin(models.Model):
2012-07-24 23:26:38 +00:00
submitter = models.ForeignKey(User)
url = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
image = ImageWithThumbsField(upload_to='pins/pin', sizes=((200, 1000),))
published = models.DateTimeField(auto_now_add=True)
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))
2012-07-24 23:26:38 +00:00
super(Pin, self).save(*args, **kwargs)
class Meta:
ordering = ['-id']