Skip to content

Commit

Permalink
fix import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email authored and alphatownsman committed Nov 28, 2023
1 parent 0411550 commit e31a2f3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
8 changes: 6 additions & 2 deletions journal/exporters/doufen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
23 changes: 15 additions & 8 deletions journal/importers/douban.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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
4 changes: 2 additions & 2 deletions journal/importers/goodreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions journal/importers/opml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions users/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion users/models/preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions users/templates/users/data.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
value=""
placeholder="例如 https://www.goodreads.com/user/show/12345-janedoe"
required>
<input type="submit" value="{% trans '导入' %}" id="uploadBtn" />
<input type="submit" value="{% trans '导入' %}" />
</div>
<ul>
<li>
Expand Down Expand Up @@ -128,7 +128,7 @@
<br>
选择OPML文件
<input type="file" name="file" id="excel" required accept=".opml,.xml">
<input type="submit" value="{% trans '导入' %}" id="uploadBtn" />
<input type="submit" value="{% trans '导入' %}" />
</div>
</form>
</details>
Expand All @@ -140,14 +140,8 @@
method="post"
enctype="multipart/form-data">
{% csrf_token %}
{% if export_status.marks_pending %}
<input type="submit"
value="{% trans '正在导出标记、短评和评论' %}"
id="uploadBtn"
disabled />
{% else %}
<input type="submit" value="{% trans '导出标记、短评和评论' %}" id="uploadBtn" />
{% endif %}
<input type="submit"
{% if export_status.marks_pending %} value="{% trans '正在导出标记、短评和评论' %}" onclick="return confirm('短期重复导出可能有无法预期的效果,确定现在导出吗?')" {% else %} value="{% trans '导出标记、短评和评论' %}" {% endif %} />
{% if export_status.marks_file %}
<a href="{% url 'users:export_marks' %}" download>下载 {{ export_status.marks_date }} 的导出</a>
{% endif %}
Expand All @@ -159,7 +153,7 @@
<summary>{% trans '重置所有标记和短评可见性' %}</summary>
<form action="{% url 'users:reset_visibility' %}" method="post">
{% csrf_token %}
<input type="submit" value="{% trans '重置' %}" id="uploadBtn" />
<input type="submit" value="{% trans '重置' %}" />
<div>
<input type="radio" name="visibility" id="visPublic" value="0" checked>
<label for="visPublic">{% trans '公开' %}</label>
Expand Down

0 comments on commit e31a2f3

Please sign in to comment.