2013-02-25 15:10:15 -08:00
|
|
|
import urllib2
|
2013-02-26 03:27:41 +00:00
|
|
|
from cStringIO import StringIO
|
|
|
|
|
|
2013-02-25 15:10:15 -08:00
|
|
|
from django.core.files.uploadedfile import InMemoryUploadedFile
|
2013-02-23 20:33:10 +01:00
|
|
|
from django.db import models
|
2013-02-24 00:15:59 +01:00
|
|
|
|
2013-02-25 15:10:15 -08:00
|
|
|
from django_images.models import Image as BaseImage
|
2012-09-28 04:42:13 +00:00
|
|
|
from taggit.managers import TaggableManager
|
2012-04-26 03:44:16 +00:00
|
|
|
|
2013-02-24 00:15:59 +01:00
|
|
|
from ..core.models import User
|
2013-02-22 01:53:18 +01:00
|
|
|
|
2012-04-26 03:44:16 +00:00
|
|
|
|
2013-02-25 15:10:15 -08:00
|
|
|
class ImageManager(models.Manager):
|
|
|
|
|
# FIXME: Move this into an asynchronous task
|
|
|
|
|
def create_for_url(self, url):
|
|
|
|
|
file_name = url.split("/")[-1]
|
|
|
|
|
buf = StringIO()
|
|
|
|
|
buf.write(urllib2.urlopen(url).read())
|
|
|
|
|
obj = InMemoryUploadedFile(buf, 'image', file_name, None, buf.tell(), None)
|
|
|
|
|
return Image.objects.create(image=obj)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Image(BaseImage):
|
|
|
|
|
objects = ImageManager()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
|
|
|
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)
|
2013-02-25 04:08:35 +01:00
|
|
|
image = models.ForeignKey(Image, related_name='pin')
|
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
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2012-04-26 15:38:21 +00:00
|
|
|
return self.url
|