Files
Pinry/pinry/core/models.py
Krzysztof Klimonda d0d68545ca Add "origin" field to the Pin model
"origin" is an optional field that stores the URI for the site
that the image has been saved from, it's going to be used only from
bookmarklet. Fixes #63
2013-03-03 08:25:42 -08:00

40 lines
1.1 KiB
Python

import urllib2
from cStringIO import StringIO
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db import models
from django_images.models import Image as BaseImage
from taggit.managers import TaggableManager
from ..users.models import User
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
class Pin(models.Model):
submitter = models.ForeignKey(User)
url = models.URLField(null=True)
origin = models.URLField(null=True)
description = models.TextField(blank=True, null=True)
image = models.ForeignKey(Image, related_name='pin')
published = models.DateTimeField(auto_now_add=True)
tags = TaggableManager()
def __unicode__(self):
return self.url