From dc75a730d12172dc5310eed7ae3cd301a01b4d39 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 21 Dec 2022 02:51:32 -0500 Subject: [PATCH] new data model: regenerate social timeline --- legacy/management/commands/backfill_social.py | 78 +++++++++++++++++++ legacy/management/commands/migrate_journal.py | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 legacy/management/commands/backfill_social.py diff --git a/legacy/management/commands/backfill_social.py b/legacy/management/commands/backfill_social.py new file mode 100644 index 00000000..38be0998 --- /dev/null +++ b/legacy/management/commands/backfill_social.py @@ -0,0 +1,78 @@ +from books.models import Book as Legacy_Book +from movies.models import Movie as Legacy_Movie +from music.models import Album as Legacy_Album +from music.models import Song as Legacy_Song +from games.models import Game as Legacy_Game +from common.models import MarkStatusEnum +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 collection.models import Collection as Legacy_Collection +from collection.models import CollectionMark as Legacy_CollectionMark +from catalog.common import * +from catalog.models import * +from catalog.sites import * +from journal.models import * +from social.models import * +# from social import models as social_models +from django.core.management.base import BaseCommand +from django.core.paginator import Paginator +import pprint +from tqdm import tqdm +from django.db.models import Q, Count, Sum +from django.utils import dateparse, timezone +import re +from legacy.models import * +from users.models import User +from django.db import DatabaseError, transaction + +BATCH_SIZE = 1000 + + +template_map = { + ShelfMember: ActivityTemplate.MarkItem, + Collection: ActivityTemplate.CreateCollection, + Like: ActivityTemplate.LikeCollection, + Review: ActivityTemplate.ReviewItem, +} + + +class Command(BaseCommand): + help = 'Backfill user activities' + + def add_arguments(self, parser): + parser.add_argument('--since', help='start date to backfill') + parser.add_argument('--clear', help='clear all user pieces, then exit', action='store_true') + + def clear(self): + print("Deleting migrated user activities") + LocalActivity.objects.all().delete() + + def backfill(self, options): + types = [Collection, Like, Review, ShelfMember] + for typ in types: + print(typ) + template = template_map[typ] + qs = typ.objects.all().filter(owner__is_active=True).order_by('id') + if options['since']: + qs = qs.filter(created_time__gte=options['since']) + else: + qs = qs.filter(created_time__gte='2022-12-01') + with transaction.atomic(): + for piece in tqdm(qs): + params = { + 'owner': piece.owner, + 'visibility': piece.visibility, + 'template': template, + 'action_object': piece, + 'created_time': piece.created_time + } + LocalActivity.objects.create(**params) + + def handle(self, *args, **options): + if options['clear']: + self.clear() + else: + self.backfill(options) + self.stdout.write(self.style.SUCCESS(f'Done.')) diff --git a/legacy/management/commands/migrate_journal.py b/legacy/management/commands/migrate_journal.py index 3732abe6..fbca5047 100644 --- a/legacy/management/commands/migrate_journal.py +++ b/legacy/management/commands/migrate_journal.py @@ -227,7 +227,7 @@ class Command(BaseCommand): self.initshelf() elif options['collection']: if options['clear']: - self.clear([Collection]) + self.clear([Collection, Like]) else: self.collection(options) elif options['review']: