From dad202823aa73bbfeea9744a518832ff4a7ee6cc Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 1 Jan 2021 15:36:43 -0800 Subject: [PATCH] Moves review re-structing into separate migration --- bookwyrm/migrations/0030_reviewrating.py | 29 --------- .../migrations/0031_auto_20210101_2109.py | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 bookwyrm/migrations/0031_auto_20210101_2109.py diff --git a/bookwyrm/migrations/0030_reviewrating.py b/bookwyrm/migrations/0030_reviewrating.py index 3658a666e..327c6c6a8 100644 --- a/bookwyrm/migrations/0030_reviewrating.py +++ b/bookwyrm/migrations/0030_reviewrating.py @@ -1,35 +1,7 @@ # Generated by Django 3.0.7 on 2021-01-01 19:05 -from django.db.models.fields.reverse_related import ManyToOneRel -from django.db.models import Q from django.db import migrations, models import django.db.models.deletion -from bookwyrm.management.commands.deduplicate_book_data import update_related - - -def convert_review_rating(app_registry, schema_editor): - ''' take reviews with no content and turn them into ratings ''' - db_alias = schema_editor.connection.alias - reviews = app_registry.get_model('bookwyrm', 'Review') - review_ratings = app_registry.get_model('bookwyrm', 'ReviewRating') - ratings = reviews.objects.using(db_alias).filter( - Q(content__isnull=True) | Q(content='')) - # replace the old review with the rating - for review in ratings: - rating = review_ratings.objects.create( - user=review.user, rating=review.rating, book=review.book) - for field in review._meta.get_fields(): - if isinstance(field, ManyToOneRel) or field.name == 'status_ptr': - continue - value = getattr(review, field.name) - print(review, rating, field.name, value) - try: - setattr(rating, field.name, value) - except TypeError: - getattr(rating, field.name).set(value.all()) - rating.save() - update_related(rating, review) - review.delete() class Migration(migrations.Migration): @@ -48,5 +20,4 @@ class Migration(migrations.Migration): }, bases=('bookwyrm.review',), ), - migrations.RunPython(convert_review_rating), ] diff --git a/bookwyrm/migrations/0031_auto_20210101_2109.py b/bookwyrm/migrations/0031_auto_20210101_2109.py new file mode 100644 index 000000000..8343719f7 --- /dev/null +++ b/bookwyrm/migrations/0031_auto_20210101_2109.py @@ -0,0 +1,61 @@ +# Generated by Django 3.0.7 on 2021-01-01 21:09 + +from django.db.models.fields.reverse_related import ManyToOneRel +from django.db.models import Q +from django.db import migrations + +def convert_review_rating(app_registry, schema_editor): + ''' take reviews with no content and turn them into ratings ''' + db_alias = schema_editor.connection.alias + reviews = app_registry.get_model('bookwyrm', 'Review') + review_ratings = app_registry.get_model('bookwyrm', 'ReviewRating') + ratings = reviews.objects.using(db_alias).filter( + Q(content__isnull=True) | Q(content='')) + # replace the old review with the rating + for review in ratings: + rating = review_ratings.objects.create( + user=review.user, rating=review.rating, book=review.book) + print('-----') + print(rating, review) + for field in review._meta.get_fields(): + if isinstance(field, ManyToOneRel) or field.name in ['status_ptr', 'id', 'remote_id']: + continue + value = getattr(review, field.name) + try: + setattr(rating, field.name, value) + except TypeError: + getattr(rating, field.name).set(value.all()) + rating.save() + + # move related models away from old review + related_models = [ + (r.remote_field.name, r.related_model.__name__) for r in \ + review._meta.related_objects] + for (related_field, related_model_name) in related_models: + related_model = app_registry.get_model('bookwyrm', related_model_name) + related_objs = related_model.objects.using(db_alias).filter( + **{related_field: review}) + for related_obj in related_objs: + print('related_obj', related_obj) + print('field', related_field) + try: + print(rating) + print('before', getattr(related_obj, related_field)) + setattr(related_obj, related_field, rating) + related_obj.save() + print('after', getattr(related_obj, related_field)) + except TypeError: + getattr(related_obj, related_field).add(rating) + getattr(related_obj, related_field).remove(review) + review.delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0030_reviewrating'), + ] + + operations = [ + migrations.RunPython(convert_review_rating), + ]