From fa623dc0b07a1d329db7c22d5963814929745497 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 6 Jan 2011 12:23:35 +0530 Subject: [PATCH] add manage command to rebuild cache, only update cache after updating sort values --- pandora/item/management/__init__.py | 0 pandora/item/management/commands/__init__.py | 0 .../item/management/commands/rebuildcache.py | 29 +++++++++++++++++++ pandora/item/models.py | 12 ++++---- pandora/itemlist/models.py | 4 +++ pandora/templates/site.json | 2 +- 6 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 pandora/item/management/__init__.py create mode 100644 pandora/item/management/commands/__init__.py create mode 100644 pandora/item/management/commands/rebuildcache.py diff --git a/pandora/item/management/__init__.py b/pandora/item/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pandora/item/management/commands/__init__.py b/pandora/item/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pandora/item/management/commands/rebuildcache.py b/pandora/item/management/commands/rebuildcache.py new file mode 100644 index 000000000..a3f2417f1 --- /dev/null +++ b/pandora/item/management/commands/rebuildcache.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 + +import os +from os.path import join, dirname, basename, splitext, exists + +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings + +from ... import models + + +class Command(BaseCommand): + """ + rebuild sort/search cache for all items. + """ + help = 'listen to rabbitmq and execute encoding tasks.' + args = '' + + def handle(self, **options): + offset = 0 + chunk = 100 + pos = models.Item.objects.count() + while offset <= models.Item.objects.count(): + for i in models.Item.objects.all().order_by('id')[offset:offset+chunk]: + print pos, i.itemId + i.save() + pos -= 1 + offset += chunk diff --git a/pandora/item/models.py b/pandora/item/models.py index 2b7f9079f..4b350efd4 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -164,7 +164,6 @@ class Item(models.Model): return '/%s' % self.itemId def save(self, *args, **kwargs): - self.json = self.get_json() self.oxdbId = self.oxdb_id() if self.poster: @@ -173,10 +172,11 @@ class Item(models.Model): else: self.poster_height = 128 self.poster_width = 80 - super(Item, self).save(*args, **kwargs) self.update_find() self.update_sort() self.update_facets() + self.json = self.get_json() + super(Item, self).save(*args, **kwargs) def delete(self, *args, **kwargs): self.delete_poster() @@ -298,7 +298,9 @@ class Item(models.Model): return layers def get_json(self, fields=None): - item = {} + item = { + 'id': self.itemId + } item.update(self.external_data) item.update(self.data) for key in site_config['keys'].keys(): @@ -490,10 +492,10 @@ class Item(models.Model): value = self.get(source, None) if isinstance(value, basestring): value = datetime.strptime(value, '%Y-%m-%d') - setattr(s, '%s_desc'%name, value) + setattr(s, name, value) if not value: value = datetime.strptime('9999-12-12', '%Y-%m-%d') - setattr(s, name, value) + setattr(s, '%s_desc'%name, value) #sort keys based on database, these will always be available s.itemId = self.itemId.replace('0x', 'xx') diff --git a/pandora/itemlist/models.py b/pandora/itemlist/models.py index 0320c5785..28c7fc822 100644 --- a/pandora/itemlist/models.py +++ b/pandora/itemlist/models.py @@ -5,6 +5,7 @@ from __future__ import division, with_statement from django.db import models from django.contrib.auth.models import User +from ox.django.fields import DictField class List(models.Model): @@ -16,6 +17,9 @@ class List(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=255) public = models.BooleanField(default=False) + + query = DictField(default={}) + items = models.ManyToManyField('item.Item', related_name='lists', through='ListItem') diff --git a/pandora/templates/site.json b/pandora/templates/site.json index 14b04f78e..269771d12 100644 --- a/pandora/templates/site.json +++ b/pandora/templates/site.json @@ -83,7 +83,7 @@ {"id": "producer", "title": "Producer", "width": 180, "type": "person"}, {"id": "cinematographer", "title": "Cinematographer", "width": 180, "type": "person"}, {"id": "editor", "title": "Editor", "width": 180, "type": "person"}, - {"id": "actor", "title": "Number of Actors", "width": 60, "type": "person"}, + {"id": "numberofactors", "title": "Number of Actors", "width": 60, "key": "actor", "type": "length"}, {"id": "genre", "title": "Genre", "width": 120, "type": "string"}, {"id": "numberofkeywords", "title": "Number of Keywords", "width": 60, "key": "keyword", "type": "length"}, {"id": "wordsinsummary", "title": "Words in Summary", "width": 60, "key": "summary", "type": "words"},