From 3f02f6086a8a0f8234701c39193930bc3eb73ff5 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 19 Feb 2016 18:19:41 +0000 Subject: [PATCH] fix clip random --- .../migrations/0003_auto_20160219_1805.py | 30 +++++++++++++++++++ pandora/clip/models.py | 9 +++--- pandora/item/tasks.py | 15 +++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 pandora/clip/migrations/0003_auto_20160219_1805.py diff --git a/pandora/clip/migrations/0003_auto_20160219_1805.py b/pandora/clip/migrations/0003_auto_20160219_1805.py new file mode 100644 index 00000000..8fa025d5 --- /dev/null +++ b/pandora/clip/migrations/0003_auto_20160219_1805.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.2 on 2016-02-19 18:05 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('clip', '0002_auto_20160219_1537'), + ] + + operations = [ + migrations.CreateModel( + name='ClipRandom', + fields=[ + ('id', models.BigIntegerField(primary_key=True, serialize=False)), + ('clip', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='clip.Clip')), + ], + ), + migrations.RemoveField( + model_name='random', + name='clip', + ), + migrations.DeleteModel( + name='Random', + ), + ] diff --git a/pandora/clip/models.py b/pandora/clip/models.py index f5f49f52..51499d13 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -59,7 +59,7 @@ class MetaClip(object): else: self.hue = self.saturation = self.lightness = 0 self.volume = 0 - + def save(self, *args, **kwargs): if self.duration != self.end - self.start: self.update_calculated_values() @@ -215,7 +215,6 @@ for name in [k['id'] for k in settings.CONFIG['layers']]: 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) - +class ClipRandom(models.Model): + id = models.BigIntegerField(primary_key=True) + clip = models.OneToOneField(Clip) diff --git a/pandora/item/tasks.py b/pandora/item/tasks.py index e371057c..00a175e9 100644 --- a/pandora/item/tasks.py +++ b/pandora/item/tasks.py @@ -34,10 +34,17 @@ def update_random_clip_sort(): if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']): with transaction.atomic(): 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_random" ON "clip_random" ("random")') + for row in ( + 'DROP TABLE clip_cliprandom', + 'CREATE TABLE "clip_cliprandom" AS SELECT id AS clip_id, row_number() OVER (ORDER BY random()) AS id FROM "clip_clip"', + 'ALTER TABLE "clip_cliprandom" ADD UNIQUE ("clip_id")', + 'ALTER TABLE "clip_cliprandom" ADD UNIQUE ("id")', + 'ALTER TABLE "clip_cliprandom" ALTER COLUMN "id" SET NOT NULL', + 'ALTER TABLE "clip_cliprandom" ALTER COLUMN "clip_id" SET NOT NULL', + + ): + cursor.execute(row) + @task(ignore_results=True, queue='default') def update_clips(public_id):