2022-11-09 13:56:50 -05:00
|
|
|
from django.shortcuts import reverse, redirect, render, get_object_or_404
|
|
|
|
from django.http import HttpResponseBadRequest, HttpResponse
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib import auth
|
|
|
|
from django.contrib.auth import authenticate
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from django.db.models import Count
|
|
|
|
from .models import User, Report, Preference
|
|
|
|
from .forms import ReportForm
|
|
|
|
from mastodon.api import *
|
|
|
|
from mastodon import mastodon_request_included
|
|
|
|
from common.config import *
|
|
|
|
from common.models import MarkStatusEnum
|
|
|
|
from common.utils import PageLinksGenerator
|
|
|
|
from management.models import Announcement
|
|
|
|
from books.models import *
|
|
|
|
from movies.models import *
|
|
|
|
from music.models import *
|
|
|
|
from games.models import *
|
|
|
|
from books.forms import BookMarkStatusTranslator
|
|
|
|
from movies.forms import MovieMarkStatusTranslator
|
|
|
|
from music.forms import MusicMarkStatusTranslator
|
|
|
|
from games.forms import GameMarkStatusTranslator
|
|
|
|
from mastodon.models import MastodonApplication
|
|
|
|
from django.conf import settings
|
|
|
|
from urllib.parse import quote
|
|
|
|
from openpyxl import Workbook
|
|
|
|
from common.utils import GenerateDateUUIDMediaFilePath
|
|
|
|
from datetime import datetime
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def refresh_mastodon_data_task(user, token=None):
|
|
|
|
if token:
|
|
|
|
user.mastodon_token = token
|
|
|
|
if user.refresh_mastodon_data():
|
|
|
|
user.save()
|
|
|
|
print(f"{user} mastodon data refreshed")
|
|
|
|
else:
|
|
|
|
print(f"{user} mastodon data refresh failed")
|
|
|
|
|
|
|
|
|
|
|
|
def export_marks_task(user):
|
2023-01-01 01:07:32 -05:00
|
|
|
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
|
|
|
|
)
|
2022-11-09 13:56:50 -05:00
|
|
|
if not os.path.exists(os.path.dirname(filename)):
|
|
|
|
os.makedirs(os.path.dirname(filename))
|
2023-01-01 01:07:32 -05:00
|
|
|
heading = ["标题", "简介", "豆瓣评分", "链接", "创建时间", "我的评分", "标签", "评论", "NeoDB链接", "其它ID"]
|
|
|
|
wb = (
|
|
|
|
Workbook()
|
|
|
|
) # adding write_only=True will speed up but corrupt the xlsx and won't be importable
|
|
|
|
for status, label in [("collect", "看过"), ("do", "在看"), ("wish", "想看")]:
|
2022-11-09 13:56:50 -05:00
|
|
|
ws = wb.create_sheet(title=label)
|
2023-01-01 01:07:32 -05:00
|
|
|
marks = MovieMark.objects.filter(owner=user, status=status).order_by(
|
|
|
|
"-edited_time"
|
|
|
|
)
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(heading)
|
|
|
|
for mark in marks:
|
|
|
|
movie = mark.movie
|
|
|
|
title = movie.title
|
2023-01-01 01:07:32 -05:00
|
|
|
summary = (
|
|
|
|
str(movie.year)
|
|
|
|
+ " / "
|
|
|
|
+ ",".join(movie.area)
|
|
|
|
+ " / "
|
|
|
|
+ ",".join(map(lambda x: str(MovieGenreTranslator[x]), movie.genre))
|
|
|
|
+ " / "
|
|
|
|
+ ",".join(movie.director)
|
|
|
|
+ " / "
|
|
|
|
+ ",".join(movie.actor)
|
|
|
|
)
|
|
|
|
tags = ",".join(list(map(lambda m: m.content, mark.tags)))
|
2022-11-09 13:56:50 -05:00
|
|
|
world_rating = (movie.rating / 2) if movie.rating else None
|
2023-01-01 01:07:32 -05:00
|
|
|
timestamp = mark.edited_time.strftime("%Y-%m-%d %H:%M:%S")
|
2022-11-09 13:56:50 -05:00
|
|
|
my_rating = (mark.rating / 2) if mark.rating else None
|
|
|
|
text = mark.text
|
|
|
|
source_url = movie.source_url
|
|
|
|
url = settings.APP_WEBSITE + movie.get_absolute_url()
|
2023-01-01 01:07:32 -05:00
|
|
|
line = [
|
|
|
|
title,
|
|
|
|
summary,
|
|
|
|
world_rating,
|
|
|
|
source_url,
|
|
|
|
timestamp,
|
|
|
|
my_rating,
|
|
|
|
tags,
|
|
|
|
text,
|
|
|
|
url,
|
|
|
|
movie.imdb_code,
|
|
|
|
]
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(line)
|
|
|
|
|
2023-01-01 01:07:32 -05:00
|
|
|
for status, label in [("collect", "听过"), ("do", "在听"), ("wish", "想听")]:
|
2022-11-09 13:56:50 -05:00
|
|
|
ws = wb.create_sheet(title=label)
|
2023-01-01 01:07:32 -05:00
|
|
|
marks = AlbumMark.objects.filter(owner=user, status=status).order_by(
|
|
|
|
"-edited_time"
|
|
|
|
)
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(heading)
|
|
|
|
for mark in marks:
|
|
|
|
album = mark.album
|
|
|
|
title = album.title
|
2023-01-01 01:07:32 -05:00
|
|
|
summary = (
|
|
|
|
",".join(album.artist)
|
|
|
|
+ " / "
|
|
|
|
+ (album.release_date.strftime("%Y") if album.release_date else "")
|
|
|
|
)
|
|
|
|
tags = ",".join(list(map(lambda m: m.content, mark.tags)))
|
2022-11-09 13:56:50 -05:00
|
|
|
world_rating = (album.rating / 2) if album.rating else None
|
2023-01-01 01:07:32 -05:00
|
|
|
timestamp = mark.edited_time.strftime("%Y-%m-%d %H:%M:%S")
|
2022-11-09 13:56:50 -05:00
|
|
|
my_rating = (mark.rating / 2) if mark.rating else None
|
|
|
|
text = mark.text
|
|
|
|
source_url = album.source_url
|
|
|
|
url = settings.APP_WEBSITE + album.get_absolute_url()
|
2023-01-01 01:07:32 -05:00
|
|
|
line = [
|
|
|
|
title,
|
|
|
|
summary,
|
|
|
|
world_rating,
|
|
|
|
source_url,
|
|
|
|
timestamp,
|
|
|
|
my_rating,
|
|
|
|
tags,
|
|
|
|
text,
|
|
|
|
url,
|
|
|
|
"",
|
|
|
|
]
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(line)
|
|
|
|
|
2023-01-01 01:07:32 -05:00
|
|
|
for status, label in [("collect", "读过"), ("do", "在读"), ("wish", "想读")]:
|
2022-11-09 13:56:50 -05:00
|
|
|
ws = wb.create_sheet(title=label)
|
2023-01-01 01:07:32 -05:00
|
|
|
marks = BookMark.objects.filter(owner=user, status=status).order_by(
|
|
|
|
"-edited_time"
|
|
|
|
)
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(heading)
|
|
|
|
for mark in marks:
|
|
|
|
book = mark.book
|
|
|
|
title = book.title
|
2023-01-01 01:07:32 -05:00
|
|
|
summary = (
|
|
|
|
",".join(book.author)
|
|
|
|
+ " / "
|
|
|
|
+ str(book.pub_year)
|
|
|
|
+ " / "
|
|
|
|
+ book.pub_house
|
|
|
|
)
|
|
|
|
tags = ",".join(list(map(lambda m: m.content, mark.tags)))
|
2022-11-09 13:56:50 -05:00
|
|
|
world_rating = (book.rating / 2) if book.rating else None
|
2023-01-01 01:07:32 -05:00
|
|
|
timestamp = mark.edited_time.strftime("%Y-%m-%d %H:%M:%S")
|
2022-11-09 13:56:50 -05:00
|
|
|
my_rating = (mark.rating / 2) if mark.rating else None
|
|
|
|
text = mark.text
|
|
|
|
source_url = book.source_url
|
|
|
|
url = settings.APP_WEBSITE + book.get_absolute_url()
|
2023-01-01 01:07:32 -05:00
|
|
|
line = [
|
|
|
|
title,
|
|
|
|
summary,
|
|
|
|
world_rating,
|
|
|
|
source_url,
|
|
|
|
timestamp,
|
|
|
|
my_rating,
|
|
|
|
tags,
|
|
|
|
text,
|
|
|
|
url,
|
|
|
|
book.isbn,
|
|
|
|
]
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(line)
|
|
|
|
|
2023-01-01 01:07:32 -05:00
|
|
|
for status, label in [("collect", "玩过"), ("do", "在玩"), ("wish", "想玩")]:
|
2022-11-09 13:56:50 -05:00
|
|
|
ws = wb.create_sheet(title=label)
|
2023-01-01 01:07:32 -05:00
|
|
|
marks = GameMark.objects.filter(owner=user, status=status).order_by(
|
|
|
|
"-edited_time"
|
|
|
|
)
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(heading)
|
|
|
|
for mark in marks:
|
|
|
|
game = mark.game
|
|
|
|
title = game.title
|
2023-01-01 01:07:32 -05:00
|
|
|
summary = (
|
|
|
|
",".join(game.genre)
|
|
|
|
+ " / "
|
|
|
|
+ ",".join(game.platform)
|
|
|
|
+ " / "
|
|
|
|
+ (game.release_date.strftime("%Y-%m-%d") if game.release_date else "")
|
|
|
|
)
|
|
|
|
tags = ",".join(list(map(lambda m: m.content, mark.tags)))
|
2022-11-09 13:56:50 -05:00
|
|
|
world_rating = (game.rating / 2) if game.rating else None
|
2023-01-01 01:07:32 -05:00
|
|
|
timestamp = mark.edited_time.strftime("%Y-%m-%d %H:%M:%S")
|
2022-11-09 13:56:50 -05:00
|
|
|
my_rating = (mark.rating / 2) if mark.rating else None
|
|
|
|
text = mark.text
|
|
|
|
source_url = game.source_url
|
|
|
|
url = settings.APP_WEBSITE + game.get_absolute_url()
|
2023-01-01 01:07:32 -05:00
|
|
|
line = [
|
|
|
|
title,
|
|
|
|
summary,
|
|
|
|
world_rating,
|
|
|
|
source_url,
|
|
|
|
timestamp,
|
|
|
|
my_rating,
|
|
|
|
tags,
|
|
|
|
text,
|
|
|
|
url,
|
|
|
|
"",
|
|
|
|
]
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(line)
|
|
|
|
|
2023-01-01 01:07:32 -05:00
|
|
|
review_heading = [
|
|
|
|
"标题",
|
|
|
|
"评论对象",
|
|
|
|
"链接",
|
|
|
|
"创建时间",
|
|
|
|
"我的评分",
|
|
|
|
"类型",
|
|
|
|
"内容",
|
|
|
|
"评论对象原始链接",
|
|
|
|
"评论对象NeoDB链接",
|
|
|
|
]
|
|
|
|
for ReviewModel, label in [
|
|
|
|
(MovieReview, "影评"),
|
|
|
|
(BookReview, "书评"),
|
|
|
|
(AlbumReview, "乐评"),
|
|
|
|
(GameReview, "游戏评论"),
|
|
|
|
]:
|
2022-11-09 13:56:50 -05:00
|
|
|
ws = wb.create_sheet(title=label)
|
|
|
|
reviews = ReviewModel.objects.filter(owner=user).order_by("-edited_time")
|
|
|
|
ws.append(review_heading)
|
|
|
|
for review in reviews:
|
|
|
|
title = review.title
|
|
|
|
target = "《" + review.item.title + "》"
|
|
|
|
url = review.url
|
2023-01-01 01:07:32 -05:00
|
|
|
timestamp = review.edited_time.strftime("%Y-%m-%d %H:%M:%S")
|
2022-11-09 13:56:50 -05:00
|
|
|
my_rating = None # (mark.rating / 2) if mark.rating else None
|
|
|
|
content = review.content
|
|
|
|
target_source_url = review.item.source_url
|
2022-11-26 20:36:44 +01:00
|
|
|
target_url = review.item.absolute_url
|
2023-01-01 01:07:32 -05:00
|
|
|
line = [
|
|
|
|
title,
|
|
|
|
target,
|
|
|
|
url,
|
|
|
|
timestamp,
|
|
|
|
my_rating,
|
|
|
|
label,
|
|
|
|
content,
|
|
|
|
target_source_url,
|
|
|
|
target_url,
|
|
|
|
]
|
2022-11-09 13:56:50 -05:00
|
|
|
ws.append(line)
|
|
|
|
|
|
|
|
wb.save(filename=filename)
|
2023-01-01 01:07:32 -05:00
|
|
|
user.preference.export_status["marks_pending"] = False
|
|
|
|
user.preference.export_status["marks_file"] = filename
|
|
|
|
user.preference.export_status["marks_date"] = datetime.now().strftime(
|
|
|
|
"%Y-%m-%d %H:%M"
|
|
|
|
)
|
|
|
|
user.preference.save(update_fields=["export_status"])
|