From 4c213873356bc74ba094b78dbf030a3cc84d4d93 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 22 Mar 2012 21:19:19 +0100 Subject: [PATCH] update random in transaction. timeline sorting --- pandora/item/tasks.py | 17 +++++++++-------- pandora/item/timelines.py | 6 ++++-- pandora/item/utils.py | 4 +++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/pandora/item/tasks.py b/pandora/item/tasks.py index d79cdc384..4ada59503 100644 --- a/pandora/item/tasks.py +++ b/pandora/item/tasks.py @@ -7,6 +7,7 @@ import random random from django.conf import settings +from django.db import connection, transaction from ox.utils import ET from celery.task import task, periodic_task @@ -16,6 +17,7 @@ import models @periodic_task(run_every=timedelta(days=1)) def cronjob(**kwargs): update_random_sort() + update_random_clip_sort() def update_random_sort(): if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']): @@ -29,14 +31,13 @@ def update_random_sort(): def update_random_clip_sort(): if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']): - from django.db import connection, transaction - cursor = connection.cursor() - cursor.execute('DROP TABLE clip_random;') - cursor.execute('CREATE TABLE "clip_random" AS SELECT id AS clip_id, row_number() OVER (ORDER BY random()) AS random FROM "clip_clip"') - cursor.execute('ALTER TABLE "clip_random" ADD UNIQUE ("clip_id")') - cursor.execute('CREATE INDEX "clip_random_clip_id_idx" ON "clip_random" ("clip_id")') - cursor.execute('CREATE INDEX "clip_random_random_idx" ON "clip_random" ("random")') - transaction.commit_unless_managed() + with transaction.commit_on_success(): + cursor = connection.cursor() + cursor.execute('DROP TABLE clip_random;') + cursor.execute('CREATE TABLE "clip_random" AS SELECT id AS clip_id, row_number() OVER (ORDER BY random()) AS random FROM "clip_clip"') + cursor.execute('ALTER TABLE "clip_random" ADD UNIQUE ("clip_id")') + cursor.execute('CREATE INDEX "clip_random_clip_id_idx" ON "clip_random" ("clip_id")') + cursor.execute('CREATE INDEX "clip_random_random_idx" ON "clip_random" ("random")') @task(ignore_results=True, queue='default') def update_poster(itemId): diff --git a/pandora/item/timelines.py b/pandora/item/timelines.py index d37473c76..238ea9701 100644 --- a/pandora/item/timelines.py +++ b/pandora/item/timelines.py @@ -7,10 +7,12 @@ from glob import glob import Image import ox +from utils import sorted_strings + def getTiles(timeline_prefix, height=64): files = glob('%s%sp*.png' % (timeline_prefix, height)) - return sorted(filter(lambda f: f!='%s%sp.png' % (timeline_prefix, height), files)) + return sorted_strings(filter(lambda f: f!='%s%sp.png' % (timeline_prefix, height), files)) def loadTimeline(timeline_prefix, height=64): files = getTiles(timeline_prefix, height) @@ -35,7 +37,7 @@ def makeTiles(timeline_prefix, height=16, width=3600): timeline = Image.new("RGB", (width, height)) pos = 0 - for f in sorted(files): + for f in sorted_strings(files): part = Image.open(f) part_width = int(part.size[0] / fps) part = part.resize((part_width, height), Image.ANTIALIAS) diff --git a/pandora/item/utils.py b/pandora/item/utils.py index 2ad85a0e5..7e71eeaf6 100644 --- a/pandora/item/utils.py +++ b/pandora/item/utils.py @@ -7,7 +7,7 @@ import unicodedata import ox def safe_filename(filename): - filename = filename.replace('_ ', ': ') + filename = filename.replace(': ', '_ ') filename = filename.replace('/', '_') filename = filename.replace('\\', '_') if filename.endswith('.'): @@ -54,6 +54,8 @@ def sort_string(string): string = re.sub('(\d+)', lambda x: '%010d' % int(x.group(0)), string) return unicodedata.normalize('NFKD', string) +def sorted_strings(strings): + return sorted(strings, cmp=lambda a, b: cmp(sort_string(a), sort_string(b))) def sort_title(title):