From 6c3c128dc9a384ece0932292564210bf61ce9ceb Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 17 Sep 2010 11:10:08 +0200 Subject: [PATCH] unique urls --- oxdata/poster/migrations/0004_unique_urls.py | 70 ++++++++++++++++++++ oxdata/poster/models.py | 7 +- oxdata/poster/tasks.py | 2 +- 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 oxdata/poster/migrations/0004_unique_urls.py diff --git a/oxdata/poster/migrations/0004_unique_urls.py b/oxdata/poster/migrations/0004_unique_urls.py new file mode 100644 index 0000000..13df6b1 --- /dev/null +++ b/oxdata/poster/migrations/0004_unique_urls.py @@ -0,0 +1,70 @@ +# encoding: utf-8 +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + +class Migration(SchemaMigration): + + def forwards(self, orm): + + # Removing unique constraint on 'PosterCache', fields ['url'] + db.delete_unique('poster_postercache', ['url']) + + # Adding unique constraint on 'PosterCache', fields ['url', 'movie_id'] + db.create_unique('poster_postercache', ['url', 'movie_id_id']) + + + def backwards(self, orm): + + # Adding unique constraint on 'PosterCache', fields ['url'] + db.create_unique('poster_postercache', ['url']) + + # Removing unique constraint on 'PosterCache', fields ['url', 'movie_id'] + db.delete_unique('poster_postercache', ['url', 'movie_id_id']) + + + models = { + 'lookup.movieid': { + 'Meta': {'object_name': 'MovieId'}, + 'amg_id': ('django.db.models.fields.IntegerField', [], {'default': 'None', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'criterion_id': ('django.db.models.fields.IntegerField', [], {'default': 'None', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'director': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1000', 'blank': 'True'}), + 'episode': ('django.db.models.fields.IntegerField', [], {'default': '-1'}), + 'episode_title': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1000', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'imdb_id': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '7', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'impawards_id': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '255', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'oxdb_id': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '42', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'season': ('django.db.models.fields.IntegerField', [], {'default': '-1'}), + 'series_title': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1000', 'blank': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1000', 'blank': 'True'}), + 'wikipedia_id': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '255', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'year': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '4', 'blank': 'True'}) + }, + 'poster.poster': { + 'Meta': {'object_name': 'Poster'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'movie_id': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'poster'", 'to': "orm['lookup.MovieId']"}), + 'poster': ('django.db.models.fields.files.ImageField', [], {'max_length': '255'}) + }, + 'poster.postercache': { + 'Meta': {'unique_together': "(('movie_id', 'url'),)", 'object_name': 'PosterCache'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'failed': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '255'}), + 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'movie_id': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'postercache'", 'to': "orm['lookup.MovieId']"}), + 'site': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'site_id': ('django.db.models.fields.CharField', [], {'max_length': '42'}), + 'status': ('django.db.models.fields.CharField', [], {'default': "'200'", 'max_length': '1024'}), + 'url': ('django.db.models.fields.CharField', [], {'max_length': '1024'}) + } + } + + complete_apps = ['poster'] diff --git a/oxdata/poster/models.py b/oxdata/poster/models.py index 904f52c..41f50b4 100644 --- a/oxdata/poster/models.py +++ b/oxdata/poster/models.py @@ -50,11 +50,14 @@ def poster_path(url, filename): return os.path.join('posters', h[:2], h[2:4], h[4:6], name) class PosterCache(models.Model): + class Meta: + unique_together = ("movie_id", "url") + 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=1024, unique=True) + url = models.CharField(max_length=1024) site = models.CharField(max_length=255) site_id = models.CharField(max_length=42) image = models.ImageField(max_length=255, upload_to=lambda i, f: poster_path(i.url.encode('utf-8'), f)) @@ -99,7 +102,7 @@ class Poster(models.Model): def getPosterUrls(m): def addPoster(url, site, site_id): - if PosterCache.objects.all().filter(url=url).count() == 0: + if PosterCache.objects.all().filter(url=url, movie_id=m).count() == 0: p = PosterCache(url=url, site=site, site_id=site_id, movie_id=m) p.save() diff --git a/oxdata/poster/tasks.py b/oxdata/poster/tasks.py index cbd1535..91192b4 100644 --- a/oxdata/poster/tasks.py +++ b/oxdata/poster/tasks.py @@ -18,7 +18,7 @@ def cronjob(**kwargs): def getMovieposteredb(imdb_id): m = getMovieIdByImdbId(imdb_id) def addPoster(url, site, site_id): - if PosterCache.objects.all().filter(url=url).count() == 0: + if PosterCache.objects.all().filter(url=url, movie_id=m).count() == 0: p = PosterCache(url=url, site=site, site_id=site_id, movie_id=m) p.save() for poster in ox.web.movieposterdb.getData(imdb_id)['posters']: