From 940f612fb5dd9ac99c964a28cd3b9e9c1ced8120 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 19 Dec 2022 17:50:00 -0500 Subject: [PATCH] new data model: migrate review --- journal/models.py | 4 +- legacy/management/commands/migrate_journal.py | 79 ++++++++++++++----- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/journal/models.py b/journal/models.py index e7d5120c..af62ea14 100644 --- a/journal/models.py +++ b/journal/models.py @@ -67,9 +67,9 @@ class Review(Content): body = MarkdownxField() @staticmethod - def review_item_by_user(item, user, title, body, visibility=0): + def review_item_by_user(item, user, title, body, metadata={}, visibility=0): # allow multiple reviews per item per user. - review = Review.objects.create(owner=user, item=item, title=title, body=body, visibility=visibility) + review = Review.objects.create(owner=user, item=item, title=title, body=body, metadata=metadata, visibility=visibility) """ review = Review.objects.filter(owner=user, item=item).first() if title is None: diff --git a/legacy/management/commands/migrate_journal.py b/legacy/management/commands/migrate_journal.py index d102a5e4..6e1aba1d 100644 --- a/legacy/management/commands/migrate_journal.py +++ b/legacy/management/commands/migrate_journal.py @@ -3,10 +3,10 @@ from movies.models import Movie as Legacy_Movie from music.models import Album as Legacy_Album from games.models import Game as Legacy_Game from common.models import MarkStatusEnum -from books.models import BookMark -from movies.models import MovieMark -from music.models import AlbumMark -from games.models import GameMark +from books.models import BookMark, BookReview +from movies.models import MovieMark, MovieReview +from music.models import AlbumMark, AlbumReview +from games.models import GameMark, GameReview from catalog.common import * from catalog.models import * from catalog.sites import * @@ -31,6 +31,10 @@ model_link = { MovieMark: MovieLink, AlbumMark: AlbumLink, GameMark: GameLink, + BookReview: BookLink, + MovieReview: MovieLink, + AlbumReview: AlbumLink, + GameReview: GameLink, } shelf_map = { @@ -55,28 +59,55 @@ class Command(BaseCommand): parser.add_argument('--movie', dest='types', action='append_const', const=MovieMark) parser.add_argument('--album', dest='types', action='append_const', const=AlbumMark) parser.add_argument('--game', dest='types', action='append_const', const=GameMark) + parser.add_argument('--mark', help='migrate shelves/tags/ratings, then exit', action='store_true') + parser.add_argument('--review', help='migrate reviews, then exit', action='store_true') + parser.add_argument('--collection', help='migrate collections, then exit', action='store_true') parser.add_argument('--id', help='id to convert; or, if using with --max-id, the min id') parser.add_argument('--maxid', help='max id to convert') parser.add_argument('--failstop', help='stop on fail', action='store_true') parser.add_argument('--initshelf', help='initialize shelves for users, then exit', action='store_true') parser.add_argument('--clear', help='clear all user pieces, then exit', action='store_true') - def handle(self, *args, **options): - if options['initshelf']: - print("Initialize shelves") - with transaction.atomic(): - for user in tqdm(User.objects.filter(is_active=True)): - user.shelf_manager.initialize() - return + def initshelf(self): + print("Initialize shelves") + with transaction.atomic(): + for user in tqdm(User.objects.filter(is_active=True)): + user.shelf_manager.initialize() - if options['clear']: - print("Deleting all migrated user pieces") - # Piece.objects.all().delete() - for cls in [Review, Comment, Rating, TagMember, Tag, ShelfLogEntry, ShelfMember]: # Collection - print(cls) - cls.objects.all().delete() - return + def clear(self): + print("Deleting all migrated user pieces") + # Piece.objects.all().delete() + for cls in [Review, Comment, Rating, TagMember, Tag, ShelfLogEntry, ShelfMember]: # Collection + print(cls) + cls.objects.all().delete() + def collection(self, options): + pass + + def review(self, options): + for typ in [GameReview, AlbumReview, BookReview, MovieReview]: + print(typ) + LinkModel = model_link[typ] + qs = typ.objects.all().filter(owner__is_active=True).order_by('id') + if options['id']: + if options['maxid']: + qs = qs.filter(id__gte=int(options['id']), id__lte=int(options['maxid'])) + else: + qs = qs.filter(id=int(options['id'])) + pg = Paginator(qs, BATCH_SIZE) + for p in tqdm(pg.page_range): + with transaction.atomic(): + for entity in pg.get_page(p).object_list: + try: + item_link = LinkModel.objects.get(old_id=entity.item.id) + item = Item.objects.get(uid=item_link.new_uid) + Review.review_item_by_user(item, entity.owner, entity.title, entity.content, {'shared_link': entity.shared_link}, entity.visibility) + except Exception as e: + print(f'Convert failed for {typ} {entity.id}: {e}') + if options['failstop']: + raise(e) + + def mark(self, options): types = options['types'] or [GameMark, AlbumMark, MovieMark, BookMark] print('Preparing cache') tag_cache = {f'{t.owner_id}_{t.title}': t.id for t in Tag.objects.all()} @@ -147,4 +178,16 @@ class Command(BaseCommand): print(f'Convert failed for {typ} {entity.id}: {e}') if options['failstop']: raise(e) + + def handle(self, *args, **options): + if options['initshelf']: + self.initshelf() + elif options['clear']: + self.clear() + elif options['collection']: + self.collection(options) + elif options['review']: + self.review(options) + elif options['mark']: + self.mark(options) self.stdout.write(self.style.SUCCESS(f'Done.'))