bookwyrm/bookwyrm/migrations/0101_auto_20210929_1847.py

57 lines
1.6 KiB
Python
Raw Normal View History

2021-06-19 15:17:27 +00:00
# Generated by Django 3.2 on 2021-05-21 00:17
from django.db import migrations
import bookwyrm
2021-09-29 19:21:19 +00:00
from bookwyrm.connectors.abstract_connector import infer_physical_format
2021-06-19 15:17:27 +00:00
def infer_format(app_registry, schema_editor):
"""set the new physical format field based on existing format data"""
2021-06-19 15:17:27 +00:00
db_alias = schema_editor.connection.alias
editions = (
app_registry.get_model("bookwyrm", "Edition")
.objects.using(db_alias)
2021-06-19 15:37:53 +00:00
.filter(physical_format_detail__isnull=False)
2021-06-19 15:17:27 +00:00
)
for edition in editions:
2021-06-19 15:37:53 +00:00
free_format = edition.physical_format_detail.lower()
2021-09-29 19:21:19 +00:00
edition.physical_format = infer_physical_format(free_format)
2021-10-03 20:26:02 +00:00
edition.save()
2021-06-19 15:17:27 +00:00
2021-08-04 21:12:37 +00:00
2021-06-19 15:36:12 +00:00
def reverse(app_registry, schema_editor):
"""doesn't need to do anything"""
2021-06-19 15:17:27 +00:00
2021-08-04 21:12:37 +00:00
2021-06-19 15:17:27 +00:00
class Migration(migrations.Migration):
dependencies = [
2021-09-29 19:38:31 +00:00
("bookwyrm", "0100_shelf_description"),
2021-06-19 15:17:27 +00:00
]
operations = [
migrations.RenameField(
model_name="edition",
old_name="physical_format",
new_name="physical_format_detail",
),
migrations.AddField(
model_name="edition",
name="physical_format",
field=bookwyrm.models.fields.CharField(
blank=True,
choices=[
2021-09-29 18:49:57 +00:00
("AudiobookFormat", "Audiobook"),
("EBook", "eBook"),
("GraphicNovel", "Graphic novel"),
2021-06-19 15:17:27 +00:00
("Hardcover", "Hardcover"),
("Paperback", "Paperback"),
],
max_length=255,
null=True,
),
),
2021-06-19 15:36:12 +00:00
migrations.RunPython(infer_format, reverse),
2021-06-19 15:17:27 +00:00
]