2009-07-13 12:32:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
import os.path
|
2009-07-14 17:04:15 +00:00
|
|
|
import hashlib
|
|
|
|
|
2009-07-13 08:09:58 +00:00
|
|
|
from django.db import models
|
2009-07-13 12:32:01 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.contrib.auth.models import User
|
2009-07-14 17:04:15 +00:00
|
|
|
from django.core.files.base import ContentFile
|
2009-07-13 12:32:01 +00:00
|
|
|
|
|
|
|
from oxdata.lookup.models import MovieId
|
|
|
|
|
2009-07-14 17:04:15 +00:00
|
|
|
|
2009-07-13 12:32:01 +00:00
|
|
|
class Poster(models.Model):
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
movie_id = models.ForeignKey(MovieId, related_name='poster')
|
|
|
|
url = models.CharField(max_length=255)
|
2009-07-13 08:09:58 +00:00
|
|
|
|
2009-07-14 17:04:15 +00:00
|
|
|
def poster_path(instance, filename):
|
|
|
|
id = instance.serviceId
|
|
|
|
return os.path.join('poster', instance.service, id[:1], id[:4], id, filename)
|
|
|
|
|
|
|
|
class PosterCache(models.Model):
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
movie_id = models.ForeignKey(MovieId, related_name='postercache')
|
|
|
|
url = models.CharField(max_length=255)
|
|
|
|
service = models.CharField(max_length=255)
|
|
|
|
serviceId = models.CharField(max_length=42)
|
|
|
|
image = models.ImageField(max_length=255, upload_to=poster_path)
|
|
|
|
|
|
|
|
def download(self):
|
|
|
|
if not self.image:
|
|
|
|
import oxlib.net
|
|
|
|
name = "%s.jpg" % hashlib.sha1(self.url).hexdigest()
|
|
|
|
data = oxlib.net.getUrl(self.url)
|
|
|
|
|
|
|
|
self.image.save(name, ContentFile(data))
|
2009-07-13 14:44:09 +00:00
|
|
|
|