diff --git a/poster/models.py b/poster/models.py index 91aa580..cba62a4 100644 --- a/poster/models.py +++ b/poster/models.py @@ -1,12 +1,16 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 import os.path +import hashlib + from django.db import models from django.db.models import Q from django.contrib.auth.models import User +from django.core.files.base import ContentFile from oxdata.lookup.models import MovieId + class Poster(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) @@ -14,4 +18,25 @@ class Poster(models.Model): movie_id = models.ForeignKey(MovieId, related_name='poster') url = models.CharField(max_length=255) +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))