From 744413b2fcbc67051bed7dc762b7564153b59f78 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 23 Dec 2022 00:08:42 -0500 Subject: [PATCH] edit mark --- catalog/common/models.py | 6 +- catalog/templates/album.html | 2 +- catalog/templates/item_base.html | 154 ++++++++--------------- catalog/views.py | 64 +--------- journal/models.py | 37 +++++- journal/templates/add_to_collection.html | 45 +++++++ journal/templates/mark.html | 128 +++++++++++++++++++ journal/urls.py | 2 + journal/views.py | 59 +++++++++ social/models.py | 1 - social/templates/activity/mark_item.html | 13 +- social/templates/feed_data.html | 13 ++ 12 files changed, 350 insertions(+), 174 deletions(-) create mode 100644 journal/templates/add_to_collection.html create mode 100644 journal/templates/mark.html diff --git a/catalog/common/models.py b/catalog/common/models.py index aa2775cf..35e52931 100644 --- a/catalog/common/models.py +++ b/catalog/common/models.py @@ -10,7 +10,7 @@ from simple_history.models import HistoricalRecords import uuid from .utils import DEFAULT_ITEM_COVER, item_cover_path from .mixins import SoftDeleteMixin -# from django.conf import settings +from django.conf import settings class SiteName(models.TextChoices): @@ -229,6 +229,10 @@ class Item(SoftDeleteMixin, PolymorphicModel): def url(self): return f'/{self.url_path}/{self.uuid}/' + @property + def absolute_url(self): + return settings.APP_WEBSITE + self.url + @property def api_url(self): return '/api' + self.url diff --git a/catalog/templates/album.html b/catalog/templates/album.html index ad764336..f57d2aa2 100644 --- a/catalog/templates/album.html +++ b/catalog/templates/album.html @@ -96,7 +96,7 @@ {% endif %} - {% if item.last_editor and item.last_editor.preference.show_last_edit or user.is_staff %} + {% if item.last_editor and item.last_editor.preference.show_last_edit %}
{% trans '最近编辑者:' %}{{ item.last_editor | default:"" }}
{% endif %} diff --git a/catalog/templates/item_base.html b/catalog/templates/item_base.html index 08b795c7..f03cf59c 100644 --- a/catalog/templates/item_base.html +++ b/catalog/templates/item_base.html @@ -33,7 +33,8 @@ {% include "partial/_common_libs.html" with jquery=1 %} - + + @@ -68,7 +69,7 @@
{% if item.rating %} - + {{ item.rating }} ({{ item.rating_count }}人评分) {% else %} @@ -166,13 +167,13 @@
{% trans '我' %}{% trans mark.shelf_label %} {% if mark.rating %} - + {% endif %} {% if mark.visibility > 0 %} {% endif %} - {% trans '修改' %} + {% trans '修改' %}
{% csrf_token %} {% trans '删除' %} @@ -180,7 +181,13 @@
-
{{ mark.created_time }}
+
+ {% if mark.metadata.shared_link %} + {{ mark.created_time }} + {% else %} + {{ mark.created_time }} + {% endif %} +
{% if mark.text %}

{{ mark.text }}

@@ -245,23 +252,25 @@ {% endblock %} {% block sidebar_collection %} - {% if collection_list %}
{% trans '相关收藏单' %}
+ {% if collection_list %} {% for c in collection_list %}

{{ c.title }}

{% endfor %} + {% endif %} + {% if request.user.is_authenticated %}
- +
+ {% endif %}
- {% endif %} {% endblock %}
@@ -269,97 +278,44 @@
{% include "partial/_footer.html" %} + + // expand hidden long text + $(".entity-desc__unfold-button a").on('click', function() { + $(this).parent().siblings(".entity-desc__content").removeClass('entity-desc__content--folded'); + $(this).parent(".entity-desc__unfold-button").remove(); + }); + - - diff --git a/catalog/views.py b/catalog/views.py index e16d23bd..0813685c 100644 --- a/catalog/views.py +++ b/catalog/views.py @@ -11,9 +11,6 @@ from django.core.paginator import Paginator from mastodon import mastodon_request_included from mastodon.models import MastodonApplication from mastodon.api import share_mark, share_review -from common.utils import PageLinksGenerator -from common.views import PAGE_LINK_NUMBER, jump_or_scrape, go_relogin -from common.models import SourceSiteEnum from .models import * # from .forms import * # from .forms import BookMarkStatusTranslator @@ -22,6 +19,7 @@ from collection.models import CollectionItem from common.scraper import get_scraper_by_url, get_normalized_url from django.utils.baseconv import base62 from journal.models import Mark +from journal.models import query_visible _logger = logging.getLogger(__name__) @@ -48,65 +46,9 @@ def retrieve(request, item_path, item_uid): mark_form = None if request.user.is_authenticated: mark = Mark(request.user, item) + _logger.info(mark.rating) review = mark.review - - # # retreive tags - # book_tag_list = book.book_tags.values('content').annotate( - # tag_frequency=Count('content')).order_by('-tag_frequency')[:TAG_NUMBER] - - # # retrieve user mark and initialize mark form - # try: - # if request.user.is_authenticated: - # mark = BookMark.objects.get(owner=request.user, book=book) - # except ObjectDoesNotExist: - # mark = None - # if mark: - # mark_tags = mark.bookmark_tags.all() - # mark.get_status_display = BookMarkStatusTranslator(mark.status) - # mark_form = BookMarkForm(instance=mark, initial={ - # 'tags': mark_tags - # }) - # else: - # mark_form = BookMarkForm(initial={ - # 'book': book, - # 'visibility': request.user.get_preference().default_visibility if request.user.is_authenticated else 0, - # 'tags': mark_tags - # }) - - # # retrieve user review - # try: - # if request.user.is_authenticated: - # review = BookReview.objects.get(owner=request.user, book=book) - # except ObjectDoesNotExist: - # review = None - - # # retrieve other related reviews and marks - # if request.user.is_anonymous: - # # hide all marks and reviews for anonymous user - # mark_list = None - # review_list = None - # mark_list_more = None - # review_list_more = None - # else: - # mark_list = BookMark.get_available_for_identicals(book, request.user) - # review_list = BookReview.get_available_for_identicals(book, request.user) - # mark_list_more = True if len(mark_list) > MARK_NUMBER else False - # mark_list = mark_list[:MARK_NUMBER] - # for m in mark_list: - # m.get_status_display = BookMarkStatusTranslator(m.status) - # review_list_more = True if len( - # review_list) > REVIEW_NUMBER else False - # review_list = review_list[:REVIEW_NUMBER] - # all_collections = CollectionItem.objects.filter(book=book).annotate(num_marks=Count('collection__collection_marks')).order_by('-num_marks')[:20] - # collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, all_collections)) - - # def strip_html_tags(text): - # import re - # regex = re.compile('<.*?>') - # return re.sub(regex, '', text) - - # for r in review_list: - # r.content = strip_html_tags(r.content) + collection_list = item.collections.all().filter(query_visible(request.user)).annotate(like_counts=Count('likes')).order_by('-like_counts') return render(request, item.class_name + '.html', { 'item': item, diff --git a/journal/models.py b/journal/models.py index 38b3cf0c..ba6fe732 100644 --- a/journal/models.py +++ b/journal/models.py @@ -19,6 +19,11 @@ import math import uuid from catalog.common.utils import DEFAULT_ITEM_COVER, item_cover_path from django.utils.baseconv import base62 +from django.db.models import Q + + +def query_visible(user): + return Q(visibility=0) | Q(owner_id__in=user.following, visibility__lt=2) | Q(owner_id=user.id) class Piece(PolymorphicModel, UserOwnedObjectMixin): @@ -276,14 +281,16 @@ ShelfTypeNames = [ [ItemCategory.Game, ShelfType.WISHLIST, _('想玩')], [ItemCategory.Game, ShelfType.PROGRESS, _('在玩')], [ItemCategory.Game, ShelfType.COMPLETE, _('玩过')], - [ItemCategory.Collection, ShelfType.WISHLIST, _('关注')], - # TODO add more combinations ] class ShelfMember(ListMember): parent = models.ForeignKey('Shelf', related_name='members', on_delete=models.CASCADE) + @cached_property + def mark(self): + return Mark(self.owner, self.item) + class Shelf(List): class Meta: @@ -487,6 +494,11 @@ class TagManager: tag = Tag.objects.filter(owner=user, title=title).first() tag.remove_item(item) + @staticmethod + def get_item_tags_by_user(item, user): + current_titles = [m.parent.title for m in TagMember.objects.filter(owner=user, item=item)] + return current_titles + @staticmethod def add_tag_by_user(item, tag_title, user, default_visibility=0): title = Tag.cleanup_title(tag_title) @@ -535,6 +547,10 @@ class Mark: def id(self): return self.shelfmember.id if self.shelfmember else None + @property + def shelf(self): + return self.shelfmember.parent if self.shelfmember else None + @property def shelf_type(self): return self.shelfmember.parent.shelf_type if self.shelfmember else None @@ -575,7 +591,8 @@ class Mark: def review(self): return Review.objects.filter(owner=self.owner, item=self.item).first() - def update(self, shelf_type, comment_text, rating_grade, visibility, metadata=None, created_time=None): + def update(self, shelf_type, comment_text, rating_grade, visibility, metadata=None, created_time=None, share_to_mastodon=False): + share = share_to_mastodon and shelf_type is not None and (shelf_type != self.shelf_type or comment_text != self.text or rating_grade != self.rating) if shelf_type != self.shelf_type or visibility != self.visibility: self.shelfmember = self.owner.shelf_manager.move_item(self.item, shelf_type, visibility=visibility, metadata=metadata) if self.shelfmember and created_time: @@ -586,3 +603,17 @@ class Mark: if rating_grade != self.rating or visibility != self.visibility: Rating.rate_item_by_user(self.item, self.owner, rating_grade, visibility) self.rating = rating_grade + if share: + # this is a bit hacky but let's keep it until move to implement ActivityPub, + # by then, we'll just change this to boost + from mastodon.api import share_mark + self.shared_link = self.shelfmember.metadata.get('shared_link') if self.shelfmember.metadata else None + self.translated_status = self.shelf_label + self.save = lambda **args: None + if not share_mark(self): + raise ValueError("sharing failed") + if not self.shelfmember.metadata: + self.shelfmember.metadata = {} + if self.shelfmember.metadata.get('shared_link') != self.shared_link: + self.shelfmember.metadata['shared_link'] = self.shared_link + self.shelfmember.save() diff --git a/journal/templates/add_to_collection.html b/journal/templates/add_to_collection.html new file mode 100644 index 00000000..363f96aa --- /dev/null +++ b/journal/templates/add_to_collection.html @@ -0,0 +1,45 @@ +{% load static %} +{% load i18n %} +{% load l10n %} +{% load humanize %} +{% load admin_url %} +{% load mastodon %} +{% load oauth_token %} +{% load truncate %} +{% load highlight %} +{% load thumb %} + + diff --git a/journal/templates/mark.html b/journal/templates/mark.html new file mode 100644 index 00000000..997cefba --- /dev/null +++ b/journal/templates/mark.html @@ -0,0 +1,128 @@ +{% load static %} +{% load i18n %} +{% load l10n %} +{% load humanize %} +{% load admin_url %} +{% load mastodon %} +{% load oauth_token %} +{% load truncate %} +{% load highlight %} +{% load thumb %} + +

- {% if activity.review %} + {% if activity.action_object.review %} {{ activity.review.title }} {% endif %} - {% if activity.mark %} - {% if activity.mark.rating %} - - {% endif %} - - {% if activity.mark.text %} -

{{ activity.mark.text }}

+ {% if activity.action_object.mark.rating %} + {% endif %} + {% if activity.action_object.mark.text %} +

{{ activity.action_object.mark.text }}

{% endif %}

\ No newline at end of file diff --git a/social/templates/feed_data.html b/social/templates/feed_data.html index 61310301..3d326494 100644 --- a/social/templates/feed_data.html +++ b/social/templates/feed_data.html @@ -78,3 +78,16 @@ hx-swap="outerHTML"> {% empty %}
{% trans '目前没有更多内容了' %}
{% endfor %} + \ No newline at end of file