def delete_user_threads(sender, **kwargs): recount_categories = set() recount_threads = set() for thread in batch_delete(sender.thread_set.all(), 50): recount_categories.add(thread.category_id) with transaction.atomic(): thread.delete() for post in batch_delete(sender.post_set.all(), 50): recount_categories.add(post.category_id) recount_threads.add(post.thread_id) with transaction.atomic(): post.delete() if recount_threads: changed_threads_qs = Thread.objects.filter(id__in=recount_threads) for thread in batch_update(changed_threads_qs, 50): thread.synchronize() thread.save() if recount_categories: for category in Category.objects.filter(id__in=recount_categories): category.synchronize() category.save()
def sync_users(self, users_to_sync): categories = Category.objects.root_category().get_descendants() message = "Synchronizing %s users...\n" self.stdout.write(message % users_to_sync) synchronized_count = 0 show_progress(self, synchronized_count, users_to_sync) start_time = time.time() for user in batch_update(UserModel.objects.all()): user.threads = user.thread_set.filter( category__in=categories, is_hidden=False, is_unapproved=False, ).count() user.posts = user.post_set.filter( category__in=categories, is_event=False, is_unapproved=False, ).count() user.followers = user.followed_by.count() user.following = user.follows.count() user.save() synchronized_count += 1 show_progress(self, synchronized_count, users_to_sync, start_time) self.stdout.write("\n\nSynchronized %s users" % synchronized_count)
def execute_step(self, user): recount_categories = set() recount_threads = set() deleted_posts = 0 is_completed = False for post in user.post_set.order_by("-id")[:50]: recount_categories.add(post.category_id) recount_threads.add(post.thread_id) with transaction.atomic(): post.delete() deleted_posts += 1 if recount_categories: changed_threads_qs = Thread.objects.filter(id__in=recount_threads) for thread in batch_update(changed_threads_qs, 50): thread.synchronize() thread.save() for category in Category.objects.filter(id__in=recount_categories): category.synchronize() category.save() else: is_completed = True return {"deleted_count": deleted_posts, "is_completed": is_completed}
def execute_step(self, user): recount_forums = set() recount_threads = set() deleted_posts = 0 is_completed = False for post in user.post_set.order_by('-id')[:50]: recount_forums.add(post.forum_id) recount_threads.add(post.thread_id) with transaction.atomic(): post.delete() deleted_posts += 1 if recount_forums: changed_threads_qs = Thread.objects.filter(id__in=recount_threads) for thread in batch_update(changed_threads_qs, 50): thread.synchronize() thread.save() for forum in Forum.objects.filter(id__in=recount_forums): forum.synchronize() forum.save() else: is_completed = True return { 'deleted_count': deleted_posts, 'is_completed': is_completed }
def delete_user_threads(sender, **kwargs): recount_forums = set() recount_threads = set() for thread in batch_delete(sender.thread_set.all(), 50): recount_forums.add(thread.forum_id) with transaction.atomic(): thread.delete() for post in batch_delete(sender.post_set.all(), 50): recount_forums.add(post.forum_id) recount_threads.add(post.thread_id) with transaction.atomic(): post.delete() if recount_threads: changed_threads_qs = Thread.objects.filter(id__in=recount_threads) for thread in batch_update(changed_threads_qs, 50): thread.synchronize() thread.save() if recount_forums: for forum in Forum.objects.filter(id__in=recount_forums): forum.synchronize() forum.save()
def sync_threads(self, posts_to_sync): message = "Rebuilding %s posts...\n" self.stdout.write(message % posts_to_sync) message = "\n\nRebuild %s posts" synchronized_count = 0 show_progress(self, synchronized_count, posts_to_sync) start_time = time.time() queryset = Post.objects.select_related('thread').filter(is_event=False) for post in batch_update(queryset): if post.id == post.thread.first_post_id: post.set_search_document(post.thread.title) else: post.set_search_document() post.save(update_fields=['search_document']) post.update_search_vector() post.save(update_fields=['search_vector']) synchronized_count += 1 show_progress(self, synchronized_count, posts_to_sync, start_time) self.stdout.write(message % synchronized_count)
def sync_attachments(self, queryset, attachments_to_sync): message = "Clearing %s attachments...\n" self.stdout.write(message % attachments_to_sync) message = "\n\nCleared %s attachments" synchronized_count = 0 show_progress(self, synchronized_count, attachments_to_sync) start_time = time.time() for attachment in batch_update(queryset): attachment.delete() synchronized_count += 1 show_progress(self, synchronized_count, attachments_to_sync, start_time) self.stdout.write(message % synchronized_count)
def sync_threads(self, threads_to_sync): message = "Synchronizing %s threads...\n" self.stdout.write(message % threads_to_sync) message = "\n\nSynchronized %s threads" synchronized_count = 0 show_progress(self, synchronized_count, threads_to_sync) start_time = time.time() for thread in batch_update(Thread.objects.all()): thread.synchronize() thread.save() synchronized_count += 1 show_progress(self, synchronized_count, threads_to_sync, start_time) self.stdout.write(message % synchronized_count)
def handle(self, *args, **options): self.stdout.write("Building moves index...") counter = 1 self.start_timer() for moved_id in batch_update(MovedId.objects): counter += 1 if moved_id.model not in MAPPINGS: continue OldIdRedirect.objects.create( model=MAPPINGS[moved_id.model], old_id=moved_id.old_id, new_id=moved_id.new_id, ) summary = "Indexed %s items in %s" % (counter, self.stop_timer()) self.stdout.write(self.style.SUCCESS(summary))
def sync_users(self, users_to_sync): message = 'Synchronizing %s users...\n' self.stdout.write(message % users_to_sync) message = '\n\nSynchronized %s users' synchronized_count = 0 show_progress(self, synchronized_count, users_to_sync) start_time = time.time() for user in batch_update(get_user_model().objects.all()): user.threads = user.thread_set.count() user.posts = user.post_set.count() user.followers = user.followed_by.count() user.following = user.follows.count() user.save() synchronized_count += 1 show_progress(self, synchronized_count, users_to_sync, start_time) self.stdout.write(message % synchronized_count)
def handle(self, *args, **options): threads_to_sync = Thread.objects.count() message = 'Synchronizing %s threads...\n' self.stdout.write(message % threads_to_sync) message = '\n\nSynchronized %s threads' synchronized_count = 0 show_progress(self, synchronized_count, threads_to_sync) start_time = time.time() for thread in batch_update(Thread.objects.all()): thread.synchronize() thread.save() synchronized_count += 1 show_progress( self, synchronized_count, threads_to_sync, start_time) self.stdout.write(message % synchronized_count)
def handle(self, *args, **options): threads_to_sync = Thread.objects.count() message = 'Synchronizing %s threads...\n' self.stdout.write(message % threads_to_sync) message = '\n\nSynchronized %s threads' synchronized_count = 0 show_progress(self, synchronized_count, threads_to_sync) start_time = time.time() for thread in batch_update(Thread.objects.all()): thread.synchronize() thread.save() synchronized_count += 1 show_progress(self, synchronized_count, threads_to_sync, start_time) self.stdout.write(message % synchronized_count)
def sync_users(self, users_to_sync): categories = Category.objects.root_category().get_descendants() message = "Synchronizing %s users...\n" self.stdout.write(message % users_to_sync) synchronized_count = 0 show_progress(self, synchronized_count, users_to_sync) start_time = time.time() for user in batch_update(UserModel.objects.all()): user.threads = user.thread_set.filter(category__in=categories, is_hidden=False, is_unapproved=False).count() user.posts = user.post_set.filter(category__in=categories, is_event=False, is_unapproved=False).count() user.followers = user.followed_by.count() user.following = user.follows.count() user.save() synchronized_count += 1 show_progress(self, synchronized_count, users_to_sync, start_time) self.stdout.write("\n\nSynchronized %s users" % synchronized_count)
def remove_unparticipated_private_threads(sender, **kwargs): threads_qs = kwargs['instance'].private_thread_set.all() for thread in batch_update(threads_qs, 50): if thread.participants.count() == 1: with transaction.atomic(): thread.delete()