forked from rc/aircox
cfr #121 Co-authored-by: Christophe Siraut <d@tobald.eu.org> Co-authored-by: bkfox <thomas bkfox net> Co-authored-by: Thomas Kairos <thomas@bkfox.net> Reviewed-on: rc/aircox#131 Co-authored-by: Chris Tactic <ctactic@noreply.git.radiocampus.be> Co-committed-by: Chris Tactic <ctactic@noreply.git.radiocampus.be>
163 lines
5.4 KiB
Python
163 lines
5.4 KiB
Python
# Generated by Django 4.2.9 on 2024-03-26 02:53
|
|
|
|
import aircox.models.file
|
|
from django.db import migrations, models
|
|
import django.db.models.deletion
|
|
|
|
|
|
sounds_info = {}
|
|
|
|
|
|
def get_sounds_info(apps, schema_editor):
|
|
Sound = apps.get_model("aircox", "Sound")
|
|
objs = Sound.objects.filter().values(
|
|
"pk",
|
|
"episode_id",
|
|
"position",
|
|
"type",
|
|
)
|
|
sounds_info.update({obj["pk"]: obj for obj in objs})
|
|
|
|
|
|
def restore_sounds_info(apps, schema_editor):
|
|
try:
|
|
Sound = apps.get_model("aircox", "Sound")
|
|
EpisodeSound = apps.get_model("aircox", "EpisodeSound")
|
|
TYPE_ARCHIVE = 0x01
|
|
TYPE_REMOVED = 0x03
|
|
|
|
episode_sounds = []
|
|
sounds = []
|
|
for sound in Sound.objects.all():
|
|
info = sounds_info.get(sound.pk)
|
|
if not info:
|
|
continue
|
|
|
|
sound.broadcast = info["type"] == TYPE_ARCHIVE
|
|
sound.is_removed = info["type"] == TYPE_REMOVED
|
|
sounds.append(sound)
|
|
if not sound.is_removed and info["episode_id"]:
|
|
obj = EpisodeSound(
|
|
sound=sound,
|
|
episode_id=info["episode_id"],
|
|
position=info["position"],
|
|
broadcast=sound.broadcast,
|
|
)
|
|
episode_sounds.append(obj)
|
|
|
|
Sound.objects.bulk_update(sounds, ("broadcast", "is_removed"))
|
|
EpisodeSound.objects.bulk_create(episode_sounds)
|
|
|
|
print(f"\n>>> {len(sounds)} Sound have been updated.")
|
|
print(f">>> {len(episode_sounds)} EpisodeSound have been created.")
|
|
except Exception as err:
|
|
print(err)
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("aircox", "0025_sound_is_removed_alter_sound_is_downloadable_and_more"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(get_sounds_info),
|
|
migrations.AlterModelOptions(
|
|
name="sound",
|
|
options={"verbose_name": "Sound file", "verbose_name_plural": "Sound files"},
|
|
),
|
|
migrations.RemoveField(
|
|
model_name="sound",
|
|
name="episode",
|
|
),
|
|
migrations.RemoveField(
|
|
model_name="sound",
|
|
name="position",
|
|
),
|
|
migrations.RemoveField(
|
|
model_name="sound",
|
|
name="type",
|
|
),
|
|
migrations.AddField(
|
|
model_name="sound",
|
|
name="broadcast",
|
|
field=models.BooleanField(
|
|
default=False, help_text="The sound is broadcasted on air", verbose_name="Broadcast"
|
|
),
|
|
),
|
|
migrations.AddField(
|
|
model_name="sound",
|
|
name="description",
|
|
field=models.TextField(blank=True, default="", max_length=256, verbose_name="description"),
|
|
),
|
|
migrations.AlterField(
|
|
model_name="sound",
|
|
name="file",
|
|
field=models.FileField(
|
|
db_index=True, max_length=256, upload_to=aircox.models.file.File._upload_to, verbose_name="file"
|
|
),
|
|
),
|
|
migrations.AlterField(
|
|
model_name="sound",
|
|
name="is_downloadable",
|
|
field=models.BooleanField(
|
|
default=False, help_text="sound can be downloaded by visitors", verbose_name="downloadable"
|
|
),
|
|
),
|
|
migrations.AlterField(
|
|
model_name="sound",
|
|
name="is_public",
|
|
field=models.BooleanField(default=False, help_text="file is publicly accessible", verbose_name="public"),
|
|
),
|
|
migrations.AlterField(
|
|
model_name="sound",
|
|
name="is_removed",
|
|
field=models.BooleanField(
|
|
db_index=True, default=False, help_text="file has been removed from server", verbose_name="removed"
|
|
),
|
|
),
|
|
migrations.AlterField(
|
|
model_name="sound",
|
|
name="name",
|
|
field=models.CharField(db_index=True, max_length=64, verbose_name="name"),
|
|
),
|
|
migrations.AlterField(
|
|
model_name="sound",
|
|
name="program",
|
|
field=models.ForeignKey(
|
|
blank=True,
|
|
null=True,
|
|
on_delete=django.db.models.deletion.SET_NULL,
|
|
to="aircox.program",
|
|
verbose_name="Program",
|
|
),
|
|
),
|
|
migrations.CreateModel(
|
|
name="EpisodeSound",
|
|
fields=[
|
|
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
|
(
|
|
"position",
|
|
models.PositiveSmallIntegerField(
|
|
default=0, help_text="position in the playlist", verbose_name="order"
|
|
),
|
|
),
|
|
(
|
|
"broadcast",
|
|
models.BooleanField(
|
|
blank=None, help_text="The sound is broadcasted on air", verbose_name="Broadcast"
|
|
),
|
|
),
|
|
("episode", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="aircox.episode")),
|
|
("sound", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="aircox.sound")),
|
|
],
|
|
options={
|
|
"verbose_name": "Episode Sound",
|
|
"verbose_name_plural": "Episode Sounds",
|
|
},
|
|
),
|
|
migrations.RunPython(restore_sounds_info),
|
|
]
|