mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-15 20:56:34 +00:00
Moves review re-structing into separate migration
This commit is contained in:
parent
385ec4d70a
commit
dad202823a
2 changed files with 61 additions and 29 deletions
|
@ -1,35 +1,7 @@
|
||||||
# Generated by Django 3.0.7 on 2021-01-01 19:05
|
# 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
|
from django.db import migrations, models
|
||||||
import django.db.models.deletion
|
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):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
@ -48,5 +20,4 @@ class Migration(migrations.Migration):
|
||||||
},
|
},
|
||||||
bases=('bookwyrm.review',),
|
bases=('bookwyrm.review',),
|
||||||
),
|
),
|
||||||
migrations.RunPython(convert_review_rating),
|
|
||||||
]
|
]
|
||||||
|
|
61
bookwyrm/migrations/0031_auto_20210101_2109.py
Normal file
61
bookwyrm/migrations/0031_auto_20210101_2109.py
Normal file
|
@ -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),
|
||||||
|
]
|
Loading…
Reference in a new issue