diff --git a/pandora/clip/models.py b/pandora/clip/models.py index 1d4b7dd4..46754dd7 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -115,7 +115,6 @@ attrs = { 'aspect_ratio': models.FloatField(default=0), 'item': models.ForeignKey('item.Item', related_name='clips'), - 'random': models.BigIntegerField(default=0, db_index=True, null=True), #seconds 'start': models.FloatField(default=-1, db_index=True), @@ -139,3 +138,7 @@ for name in settings.CONFIG['clipLayers']: Clip = type('Clip', (MetaClip,models.Model), attrs) +class Random(models.Model): + clip = models.ForeignKey(Clip, primary_key=True) + random = models.BigIntegerField(db_index=True, null=True) + diff --git a/pandora/clip/views.py b/pandora/clip/views.py index 0b63539e..595b2684 100644 --- a/pandora/clip/views.py +++ b/pandora/clip/views.py @@ -42,7 +42,7 @@ def order_query(qs, sort): clip_keys = ('public_id', 'start', 'end', 'hue', 'saturation', 'lightness', 'volume', 'duration', 'sortvalue', 'videoRatio', 'director', 'title', - 'random') + 'random__random') key = { 'id': 'public_id', 'in': 'start', @@ -50,6 +50,7 @@ def order_query(qs, sort): 'position': 'start', 'text': 'sortvalue', 'videoRatio': 'aspect_ratio', + 'random': 'random__random', }.get(e['key'], e['key']) if key.startswith('clip:'): key = e['key'][len('clip:'):] diff --git a/pandora/item/tasks.py b/pandora/item/tasks.py index 174e1343..d79cdc38 100644 --- a/pandora/item/tasks.py +++ b/pandora/item/tasks.py @@ -29,14 +29,14 @@ def update_random_sort(): def update_random_clip_sort(): if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']): - random.seed() - from clip.models import Clip - ids = [f['id'] for f in Clip.objects.values('id')] - random.shuffle(ids) - n = 1 - for i in ids: - Clip.objects.filter(pk=i).update(random=n) - n += 1 + 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() @task(ignore_results=True, queue='default') def update_poster(itemId):