diff --git a/journal/exporters/doufen.py b/journal/exporters/doufen.py index d859a611..c4017adb 100644 --- a/journal/exporters/doufen.py +++ b/journal/exporters/doufen.py @@ -33,7 +33,7 @@ def export_marks_task(user): user.preference.export_status["marks_pending"] = True user.preference.save(update_fields=["export_status"]) filename = GenerateDateUUIDMediaFilePath( - None, "f.xlsx", settings.MEDIA_ROOT + settings.EXPORT_FILE_PATH_ROOT + None, "f.xlsx", settings.MEDIA_ROOT + "/" + settings.EXPORT_FILE_PATH_ROOT ) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) @@ -266,7 +266,11 @@ def export_marks_task(user): ]: ws = wb.create_sheet(title=label) q = q_item_in_category(category) - reviews = Review.objects.filter(owner=user).filter(q).order_by("created_time") + reviews = ( + Review.objects.filter(owner=user.identity) + .filter(q) + .order_by("created_time") + ) ws.append(review_heading) for review in reviews: title = review.title diff --git a/journal/importers/douban.py b/journal/importers/douban.py index 2ce456d0..9c7a9d60 100644 --- a/journal/importers/douban.py +++ b/journal/importers/douban.py @@ -31,7 +31,7 @@ def _fetch_remote_image(url): f = GenerateDateUUIDMediaFilePath( None, f"x.{ext}", settings.MARKDOWNX_MEDIA_PATH ) - file = settings.MEDIA_ROOT + f + file = settings.MEDIA_ROOT + "/" + f local_url = settings.MEDIA_URL + f os.makedirs(os.path.dirname(file), exist_ok=True) with open(file, "wb") as binary_file: @@ -93,8 +93,12 @@ def import_from_file(self, uploaded_file): uploaded_file, read_only=True, data_only=True, keep_links=False ) wb.close() - file = settings.MEDIA_ROOT + GenerateDateUUIDMediaFilePath( - None, "x.xlsx", settings.SYNC_FILE_PATH_ROOT + file = ( + settings.MEDIA_ROOT + + "/" + + GenerateDateUUIDMediaFilePath( + None, "x.xlsx", settings.SYNC_FILE_PATH_ROOT + ) ) os.makedirs(os.path.dirname(file), exist_ok=True) with open(file, "wb") as destination: @@ -106,7 +110,8 @@ def import_from_file(self, uploaded_file): django_rq.get_queue("import").enqueue( self.import_from_file_task, job_id=jid ) - except Exception: + except Exception as e: + logger.error(e) return False return True @@ -248,7 +253,7 @@ def import_mark(self, url, shelf_type, comment, rating_grade, tags, time): if not item: logger.warning(f"{self.user} | match/fetch {url} failed") return - mark = Mark(self.user, item) + mark = Mark(self.user.identity, item) if self.mode == 0 and ( mark.shelf_type == shelf_type or mark.shelf_type == ShelfType.COMPLETE @@ -264,7 +269,7 @@ def import_mark(self, url, shelf_type, comment, rating_grade, tags, time): ) print("+", end="", flush=True) if tags: - TagManager.tag_item(item, self.user, tags) + TagManager.tag_item(item, self.user.identity, tags) return 1 def import_review_sheet(self, worksheet, sheet_name): @@ -353,7 +358,7 @@ def import_review(self, entity_title, rating, title, review_url, content, time): return if ( self.mode == 1 - and Review.objects.filter(owner=self.user, item=item).exists() + and Review.objects.filter(owner=self.user.identity, item=item).exists() ): return 2 content = re.sub( @@ -374,5 +379,7 @@ def import_review(self, entity_title, rating, title, review_url, content, time): "body": content, "visibility": self.visibility, } - Review.objects.update_or_create(owner=self.user, item=item, defaults=params) + Review.objects.update_or_create( + owner=self.user.identity, item=item, defaults=params + ) return 1 diff --git a/journal/importers/goodreads.py b/journal/importers/goodreads.py index 98fc7fcf..74a352b5 100644 --- a/journal/importers/goodreads.py +++ b/journal/importers/goodreads.py @@ -59,7 +59,7 @@ def import_from_url_task(cls, url, user): + "\n\nImported from [Goodreads](" + url + ")", - owner=user, + owner=user.identity, ) for book in shelf["books"]: collection.append_item(book["book"], note=book["review"]) @@ -77,7 +77,7 @@ def import_from_url_task(cls, url, user): shelf_url = shelves.get(shelf_type) shelf = cls.parse_shelf(shelf_url, user) for book in shelf["books"]: - mark = Mark(user, book["book"]) + mark = Mark(user.identity, book["book"]) if ( ( mark.shelf_type == shelf_type diff --git a/journal/importers/opml.py b/journal/importers/opml.py index fbc09689..a2bf393e 100644 --- a/journal/importers/opml.py +++ b/journal/importers/opml.py @@ -33,7 +33,7 @@ def import_from_file_task(self, feeds): with set_actor(self.user): if self.mode == 1: collection = Collection.objects.create( - owner=self.user, title=f"{self.user.display_name}的播客订阅列表" + owner=self.user.identity, title=f"{self.user.display_name}的播客订阅列表" ) for feed in feeds: logger.info(f"{self.user} import {feed.url}") @@ -43,7 +43,7 @@ def import_from_file_task(self, feeds): continue item = res.item if self.mode == 0: - mark = Mark(self.user, item) + mark = Mark(self.user.identity, item) if mark.shelfmember: logger.info(f"{self.user} marked, skip {feed.url}") skip += 1 diff --git a/users/data.py b/users/data.py index c6bcdd3e..f94640f8 100644 --- a/users/data.py +++ b/users/data.py @@ -86,10 +86,9 @@ def export_reviews(request): @login_required def export_marks(request): if request.method == "POST": - if not request.user.preference.export_status.get("marks_pending"): - django_rq.get_queue("export").enqueue(export_marks_task, request.user) - request.user.preference.export_status["marks_pending"] = True - request.user.preference.save() + django_rq.get_queue("export").enqueue(export_marks_task, request.user) + request.user.preference.export_status["marks_pending"] = True + request.user.preference.save() messages.add_message(request, messages.INFO, _("导出已开始。")) return redirect(reverse("users:data")) else: diff --git a/users/models/preference.py b/users/models/preference.py index 691d0220..3b9e2c86 100644 --- a/users/models/preference.py +++ b/users/models/preference.py @@ -17,7 +17,6 @@ from django.utils.translation import gettext_lazy as _ from loguru import logger -from common.utils import GenerateDateUUIDMediaFilePath from management.models import Announcement from mastodon.api import * from takahe.utils import Takahe diff --git a/users/templates/users/data.html b/users/templates/users/data.html index 1c6d8e53..7c95d798 100644 --- a/users/templates/users/data.html +++ b/users/templates/users/data.html @@ -88,7 +88,7 @@ value="" placeholder="例如 https://www.goodreads.com/user/show/12345-janedoe" required> - +