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()
|
2012-04-26 15:38:21 +00:00
|
|
|
description = models.TextField(blank=True, null=True)
|
2012-04-26 03:44:16 +00:00
|
|
|
image = ImageWithThumbsField(upload_to='pins/pin', sizes=((200,1000),))
|
|
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2012-04-26 15:38:21 +00:00
|
|
|
return self.url
|
2012-04-26 03:44:16 +00:00
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
|
if not self.image:
|
|
|
|
|
temp_img = NamedTemporaryFile()
|
|
|
|
|
temp_img.write(urllib2.urlopen(self.url).read())
|
|
|
|
|
temp_img.flush()
|
|
|
|
|
self.image.save(self.url.split('/')[-1], File(temp_img))
|
|
|
|
|
super(Pin, self).save()
|
2012-05-11 02:08:46 +00:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
ordering = ['-id']
|