From 822c13962451861a9c4cf4ffa9c8fd866672a100 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 17 Jan 2010 09:42:11 +1300 Subject: [PATCH] ongoing work --- oxdb/backend/daemon.py | 21 +++++++++++++--- oxdb/backend/models.py | 46 +++++++++++++++++++++++++++++++++++ oxdb/backend/views.py | 3 +++ oxdb/user_management/views.py | 2 ++ 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/oxdb/backend/daemon.py b/oxdb/backend/daemon.py index dd214c2..d1a710c 100644 --- a/oxdb/backend/daemon.py +++ b/oxdb/backend/daemon.py @@ -1,6 +1,16 @@ from carrot.connection import DjangoBrokerConnection -from carrot.messaging import Consumer +from carrot.messaging import Consumer, Publisher +from django.conf import settings +import load +import models + +def send_bg_message(msg): + conn = DjangoBrokerConnection() + publisher = Publisher(connection=conn, exchange="oxdb-bg", + routing_key="oxdb-bg") + publisher.send(msg) + publisher.close() def run(): conn = DjangoBrokerConnection() @@ -8,12 +18,15 @@ def run(): consumer = Consumer(connection=conn, queue="oxdb-bg", exchange="oxdb-bg", routing_key="oxdb-bg") - def handle_background_tasks_callback(message_data, message): + def handle_background_tasks_callback(data, message): print("Got feed import message") - print message_data - if 'loadIMDb' in message_data: + print data + if 'loadIMDb' in data: imdbId = message_data['loadIMDb'] load.loadIMDb(imdbId) + elif 'findMovie' in data: + f = models.File.objects.get(pk=data['findMovie']) + f.findMovie() message.ack() consumer.register_callback(handle_background_tasks_callback) consumer.wait() # Go into the consumer loop. diff --git a/oxdb/backend/models.py b/oxdb/backend/models.py index 17a710d..40a51cb 100644 --- a/oxdb/backend/models.py +++ b/oxdb/backend/models.py @@ -3,6 +3,7 @@ import re import os.path import random +from datetime import datetime from django.db import models from django.db.models import Q @@ -16,6 +17,7 @@ from django.utils import simplejson as json import utils import managers import fields +import load class MovieImdb(models.Model): created = models.DateTimeField(auto_now_add=True) @@ -74,6 +76,39 @@ class MovieOxdb(models.Model): def __unicode__(self): return u"%s (%s)" % (self.title, self.year) +class MovieExtra(models.Model): + created = models.DateTimeField(auto_now_add=True) + modified = models.DateTimeField(auto_now=True) + + title = models.CharField(max_length=1000) + description = models.TextField(blank=True) + contributor = models.CharField(max_length=1000) + +def getMovie(info): + ''' + info dict with: + imdbId, title, director, episode_title, season, series + ''' + if 'imdbId' in info: + try: + movie = models.Movie.byImdbId(imdbId) + except models.Movie.DoesNotExist: + movie = load.loadIMDb(movieId==info['imdbId']) + else: + q = Movie.objects.filter(title=info['title'], year=info['director']) + if q.count() > 1: + movie = q[0] + else: + movie = newMovie(info['title'], info['director'], '') + updated = False + for key in ('episode_title', 'season', 'year'): + if key in info: + setattr(movie.oxdb, key, info[key]) + updated = True + if updated: + movie.save() + return movie + def newMovie(title, director, year): movie = Movie() oxdb = MovieOxdb() @@ -96,6 +131,7 @@ def movie_path(f, size): class Movie(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) + published = models.DateTimeField(default=datetime.now, editable=False) #only movies that have metadata from files are available, #this is indicated by setting available to True @@ -106,10 +142,13 @@ class Movie(models.Model): imdb = models.OneToOneField('MovieImdb', null=True, related_name='movie') oxdb = models.OneToOneField('MovieOxdb', null=True, related_name='movie') + extra = models.OneToOneField('MovieOxdb', null=True, related_name='movie') objects = managers.MovieManager() def get(self, key, default=None): + if self.extra and getattr(self.extra, key): + return getattr(self.extra, key) if self.oxdb and getattr(self.oxdb, key): return getattr(self.oxdb, key) if self.imdb: @@ -949,6 +988,8 @@ FILE_TYPES = ( class File(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) + published = models.DateTimeField(default=datetime.now, editable=False) + verified = models.BooleanField(default=False) oshash = models.CharField(blank=True, unique=True, max_length=16) @@ -1060,6 +1101,11 @@ class File(models.Model): self.path = self.info['path'] self.save() + def findMovie(self): + info = utils.parsePath(self.path) + self.movie = getMovie(info) + self.save() + class Still(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) diff --git a/oxdb/backend/views.py b/oxdb/backend/views.py index a9c3e96..58b1eb7 100644 --- a/oxdb/backend/views.py +++ b/oxdb/backend/views.py @@ -23,6 +23,7 @@ from oxdjango.decorators import login_required_json import models import utils +from daemon import send_bg_message ''' field.length -> movie.sort.all()[0].field @@ -426,6 +427,8 @@ def update_archive(request, archive): print "adding file", oshash, data['path'] f = models.ArchiveFile.get_or_create(archive, oshash) f.update(data) + if not f.movie: + send_bg_message({'findMovie': f.id}) #FIXME: only add if it was not in File else: f = q[0] diff --git a/oxdb/user_management/views.py b/oxdb/user_management/views.py index c3da9bf..3482d60 100644 --- a/oxdb/user_management/views.py +++ b/oxdb/user_management/views.py @@ -68,6 +68,7 @@ def preferences(request): key = request.GET.get('key', None) value = request.GET.get('value', None) user = request.user + response['preferences'] = {} if not key: # return all preferences for current user for p in models.Preference.objects.filter(user=user): response['preferences'][p.key] = p.value @@ -76,6 +77,7 @@ def preferences(request): p, created = models.Preference.objects.get_or_create(user=user, key=key) response['preferences'][key] = p.value else: # set preference + del response['preferences'] response['message'] = '%s saved.' % key if key == 'password': user.set_password(value)