fix clip random

This commit is contained in:
j 2016-02-19 18:19:41 +00:00
parent 2fe857d4fd
commit 3f02f6086a
3 changed files with 45 additions and 9 deletions

View file

@ -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',
),
]

View file

@ -59,7 +59,7 @@ class MetaClip(object):
else: else:
self.hue = self.saturation = self.lightness = 0 self.hue = self.saturation = self.lightness = 0
self.volume = 0 self.volume = 0
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.duration != self.end - self.start: if self.duration != self.end - self.start:
self.update_calculated_values() 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) Clip = type('Clip', (MetaClip,models.Model), attrs)
class Random(models.Model): class ClipRandom(models.Model):
clip = models.ForeignKey(Clip, primary_key=True) id = models.BigIntegerField(primary_key=True)
random = models.BigIntegerField(db_index=True, null=True) clip = models.OneToOneField(Clip)

View file

@ -34,10 +34,17 @@ def update_random_clip_sort():
if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']): if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']):
with transaction.atomic(): with transaction.atomic():
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute('DROP TABLE clip_random;') for row in (
cursor.execute('CREATE TABLE "clip_random" AS SELECT id AS clip_id, row_number() OVER (ORDER BY random()) AS random FROM "clip_clip"') 'DROP TABLE clip_cliprandom',
cursor.execute('ALTER TABLE "clip_random" ADD UNIQUE ("clip_id")') 'CREATE TABLE "clip_cliprandom" AS SELECT id AS clip_id, row_number() OVER (ORDER BY random()) AS id FROM "clip_clip"',
cursor.execute('CREATE INDEX "clip_random_random" ON "clip_random" ("random")') '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') @task(ignore_results=True, queue='default')
def update_clips(public_id): def update_clips(public_id):