From 3bd28afe93db282be61cbc4a50205a3b8179288e Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 4 Dec 2021 16:06:07 +0100 Subject: [PATCH] Add unique embed_key to List model --- bookwyrm/migrations/0120_list_embed_key.py | 29 ++++++++++++++++++++++ bookwyrm/models/list.py | 9 +++++++ 2 files changed, 38 insertions(+) create mode 100644 bookwyrm/migrations/0120_list_embed_key.py diff --git a/bookwyrm/migrations/0120_list_embed_key.py b/bookwyrm/migrations/0120_list_embed_key.py new file mode 100644 index 00000000..40db1f0f --- /dev/null +++ b/bookwyrm/migrations/0120_list_embed_key.py @@ -0,0 +1,29 @@ +# Generated by Django 3.2.5 on 2021-12-04 10:55 + +from django.db import migrations, models +import uuid + + +def gen_uuid(apps, schema_editor): + """sets an unique UUID for embed_key""" + book_lists = apps.get_model("bookwyrm", "List") + db_alias = schema_editor.connection.alias + for book_list in book_lists.objects.using(db_alias).all(): + book_list.embed_key = uuid.uuid4() + book_list.save(broadcast=False) + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0119_user_feed_status_types"), + ] + + operations = [ + migrations.AddField( + model_name="list", + name="embed_key", + field=models.UUIDField(editable=False, null=True, unique=True), + ), + migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop), + ] diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index 978a7a9b..61cb41ef 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -1,4 +1,6 @@ """ make a list of books!! """ +import uuid + from django.apps import apps from django.db import models from django.db.models import Q @@ -43,6 +45,7 @@ class List(OrderedCollectionMixin, BookWyrmModel): through="ListItem", through_fields=("book_list", "book"), ) + embed_key = models.UUIDField(unique=True, null=True, editable=False) activity_serializer = activitypub.BookList def get_remote_id(self): @@ -105,6 +108,12 @@ class List(OrderedCollectionMixin, BookWyrmModel): group=None, curation="closed" ) + def save(self, *args, **kwargs): + """on save, update embed_key and avoid clash with existing code""" + if not self.embed_key: + self.embed_key = uuid.uuid4() + return super(List, self).save(*args, **kwargs) + class ListItem(CollectionItemMixin, BookWyrmModel): """ok"""