From 564146d31744051359660dac9771c15178044be0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 26 Dec 2024 12:52:06 -0500 Subject: [PATCH] fix language for tasks and top10 --- journal/management/commands/top10.py | 26 ++++++++++++++++---------- users/models/task.py | 3 +++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/journal/management/commands/top10.py b/journal/management/commands/top10.py index 73951cb2..2a07fc4d 100644 --- a/journal/management/commands/top10.py +++ b/journal/management/commands/top10.py @@ -1,8 +1,11 @@ +from auditlog.context import set_actor from django.core.management.base import BaseCommand from django.db.models import Count, F, Q +from django.utils.translation import gettext_lazy as t from catalog.models import * from journal.models import * +from users.middlewares import activate_language_for_user from users.models import * from users.models import APIdentity @@ -19,6 +22,8 @@ def add_arguments(self, parser): def handle(self, year: int, top: int, save: str, **options): # type: ignore collector = APIdentity.objects.get(username=save, local=True) if save else None + if collector: + activate_language_for_user(collector.user) types = [ [Edition], [Movie], @@ -31,7 +36,7 @@ def handle(self, year: int, top: int, save: str, **options): # type: ignore mapping = item_content_types() for typ in types: cids = [mapping[t] for t in typ] - title = f"{year}年标记最多的{typ[0].category.label}" + title = f"{year}年标记最多的{t(typ[0].category.label)}" print(title) top10 = list( ShelfMember.objects.filter( @@ -44,15 +49,16 @@ def handle(self, year: int, top: int, save: str, **options): # type: ignore items = [(Item.objects.get(pk=i["item"]), i["c"]) for i in top10] _ = [print(c, i.display_title, i.absolute_url) for i, c in items] if collector: - print(f"Saving to {collector}") - c, _ = Collection.objects.get_or_create( - owner=collector, - title=title, - brief="*根据用户标记数统计*", - defaults={"visibility": 2}, - ) - for i in items: - c.append_item(i) + with set_actor(collector.user): + print(f"Saving to {collector}") + c, _ = Collection.objects.get_or_create( + owner=collector, + title=title, + brief="*根据用户标记数统计*", + defaults={"visibility": 2}, + ) + for i, cat in items: + c.append_item(i) # top10 = list( # Comment.objects.filter( diff --git a/users/models/task.py b/users/models/task.py index b3aed406..0fb2a2a0 100644 --- a/users/models/task.py +++ b/users/models/task.py @@ -6,6 +6,8 @@ from typedmodels.models import TypedModel from user_messages import api as msg +from users.middlewares import activate_language_for_user + from .user import User @@ -61,6 +63,7 @@ def _run(cls, task_id: int): return task.state = cls.States.started task.save() + activate_language_for_user(task.user) with set_actor(task.user): try: task.run()