From 3875a8238143348aec0bb594359b2cdf2e82c301 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 24 Dec 2021 11:54:12 -0800 Subject: [PATCH] wip --- collection/forms.py | 27 +++-- collection/models.py | 5 +- collection/templates/create_update.html | 90 +++++++++++++++++ collection/templates/detail.html | 114 +++++++++++++++++++++ collection/views.py | 126 ++++++------------------ 5 files changed, 258 insertions(+), 104 deletions(-) create mode 100644 collection/templates/create_update.html create mode 100644 collection/templates/detail.html diff --git a/collection/forms.py b/collection/forms.py index 57bad4f6..0aa317d2 100644 --- a/collection/forms.py +++ b/collection/forms.py @@ -2,28 +2,39 @@ from django import forms from django.contrib.postgres.forms import SimpleArrayField from django.utils.translation import gettext_lazy as _ from .models import Collection -from common.models import MarkStatusEnum from common.forms import * class CollectionForm(forms.ModelForm): # id = forms.IntegerField(required=False, widget=forms.HiddenInput()) - name = forms.CharField(label=_("标题")) + title = forms.CharField(label=_("标题")) description = MarkdownxFormField(label=_("正文 (Markdown)")) + share_to_mastodon = forms.BooleanField( + label=_("分享到联邦网络"), initial=True, required=False) + rating = forms.IntegerField( + label=_("评分"), validators=[RatingValidator()], widget=forms.HiddenInput(), required=False) + visibility = forms.TypedChoiceField( + label=_("可见性"), + initial=0, + coerce=int, + choices=VISIBILITY_CHOICES, + widget=forms.RadioSelect + ) class Meta: model = Collection fields = [ - 'name', + 'title', + 'visibility', 'description', 'cover', ] widgets = { - 'name': forms.TextInput(attrs={'placeholder': _("收藏单名称")}), - 'developer': forms.TextInput(attrs={'placeholder': _("多个开发商使用英文逗号分隔")}), - 'publisher': forms.TextInput(attrs={'placeholder': _("多个发行商使用英文逗号分隔")}), - 'genre': forms.TextInput(attrs={'placeholder': _("多个类型使用英文逗号分隔")}), - 'platform': forms.TextInput(attrs={'placeholder': _("多个平台使用英文逗号分隔")}), + # 'name': forms.TextInput(attrs={'placeholder': _("收藏单名称")}), + # 'developer': forms.TextInput(attrs={'placeholder': _("多个开发商使用英文逗号分隔")}), + # 'publisher': forms.TextInput(attrs={'placeholder': _("多个发行商使用英文逗号分隔")}), + # 'genre': forms.TextInput(attrs={'placeholder': _("多个类型使用英文逗号分隔")}), + # 'platform': forms.TextInput(attrs={'placeholder': _("多个平台使用英文逗号分隔")}), 'cover': PreviewImageInput(), } diff --git a/collection/models.py b/collection/models.py index 4617db10..a1a89a61 100644 --- a/collection/models.py +++ b/collection/models.py @@ -5,8 +5,9 @@ from books.models import Book from music.models import Song, Album from games.models import Game from markdownx.models import MarkdownxField -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from django.conf import settings +from common.utils import ChoicesDictGenerator, GenerateDateUUIDMediaFilePath def collection_cover_path(instance, filename): @@ -14,7 +15,7 @@ def collection_cover_path(instance, filename): class Collection(UserOwnedEntity): - name = models.CharField(max_length=200) + title = models.CharField(max_length=200) description = MarkdownxField() cover = models.ImageField(_("封面"), upload_to=collection_cover_path, default=settings.DEFAULT_COLLECTION_IMAGE, blank=True) diff --git a/collection/templates/create_update.html b/collection/templates/create_update.html new file mode 100644 index 00000000..d2d69cb3 --- /dev/null +++ b/collection/templates/create_update.html @@ -0,0 +1,90 @@ +{% load static %} +{% load i18n %} +{% load admin_url %} +{% load mastodon %} +{% load oauth_token %} +{% load truncate %} + + + + + + + {{ site_name }} - {{ title }} + + + + + +
+
+ {% include "partial/_navbar.html" %} + +
+
+
+
+ {% csrf_token %} + {{ form.media }} + {% for field in form %} + + {% if field.name == 'release_date' %} + {{ field.label_tag }} + + {% else %} + {% if field.name != 'id' %} + {{ field.label_tag }} + {% endif %} + {{ field }} + {% endif %} + + {% endfor %} + + +
+
+ +
+
+ {% include "partial/_footer.html" %} +
+ + + {% comment %} + + + + + {% endcomment %} + + + + + + \ No newline at end of file diff --git a/collection/templates/detail.html b/collection/templates/detail.html new file mode 100644 index 00000000..f20c9d43 --- /dev/null +++ b/collection/templates/detail.html @@ -0,0 +1,114 @@ +{% load static %} +{% load i18n %} +{% load l10n %} +{% load humanize %} +{% load admin_url %} +{% load mastodon %} +{% load oauth_token %} +{% load truncate %} +{% load thumb %} + + + + + + + + + + + + {{ site_name }} {% trans '收藏單' %} - {{ collection.title }} + + + + + + + + +
+
+ {% include "partial/_navbar.html" %} + +
+
+
+
+
+
+ {{ collection.title }} +
+ {% if collection.visibility > 0 %} + + + + {% endif %} +
+
+ + {{ collection.owner.mastodon_username }} + + + {{ collection.edited_time }} + +
+
+ {% if request.user == collection.owner %} + {% trans '编辑' %} + {% trans '删除' %} + {% endif %} +
+
+ +
+ {{ form.description }} +
+ {{ form.media }} +
+
+ +
+
+
+
+
+ +
+ + +
+
+
+
+
+
+ {% include "partial/_footer.html" %} +
+ + + {% comment %} + + + + + {% endcomment %} + + + + + + diff --git a/collection/views.py b/collection/views.py index 89742b49..3dfc9729 100644 --- a/collection/views.py +++ b/collection/views.py @@ -41,14 +41,14 @@ TAG_NUMBER = 10 @login_required def create(request): if request.method == 'GET': - form = GameForm() + form = CollectionForm() return render( request, - 'games/create_update.html', + 'create_update.html', { 'form': form, - 'title': _('添加游戏'), - 'submit_url': reverse("games:create"), + 'title': _('添加收藏单'), + 'submit_url': reverse("collection:create"), # provided for frontend js 'this_site_enum_value': SourceSiteEnum.IN_SITE.value, } @@ -56,28 +56,25 @@ def create(request): elif request.method == 'POST': if request.user.is_authenticated: # only local user can alter public data - form = GameForm(request.POST, request.FILES) + form = CollectionForm(request.POST, request.FILES) + form.instance.owner = request.user if form.is_valid(): form.instance.last_editor = request.user try: with transaction.atomic(): form.save() - if form.instance.source_site == SourceSiteEnum.IN_SITE.value: - real_url = form.instance.get_absolute_url() - form.instance.source_url = real_url - form.instance.save() except IntegrityError as e: logger.error(e.__str__()) return HttpResponseServerError("integrity error") - return redirect(reverse("games:retrieve", args=[form.instance.id])) + return redirect(reverse("collection:retrieve", args=[form.instance.id])) else: return render( request, - 'games/create_update.html', + 'create_update.html', { 'form': form, - 'title': _('添加游戏'), - 'submit_url': reverse("games:create"), + 'title': _('添加收藏單'), + 'submit_url': reverse("collection:create"), # provided for frontend js 'this_site_enum_value': SourceSiteEnum.IN_SITE.value, } @@ -90,25 +87,25 @@ def create(request): @login_required def update(request, id): + page_title = _("修改游戏") + collection = get_object_or_404(Collection, pk=id) + if not collection.is_visible_to(request.user): + raise PermissionDenied() if request.method == 'GET': - game = get_object_or_404(Game, pk=id) - form = GameForm(instance=game) - page_title = _('修改游戏') + form = CollectionForm(instance=collection) return render( request, - 'games/create_update.html', + 'create_update.html', { 'form': form, 'title': page_title, - 'submit_url': reverse("games:update", args=[game.id]), + 'submit_url': reverse("collection:update", args=[collection.id]), # provided for frontend js 'this_site_enum_value': SourceSiteEnum.IN_SITE.value, } ) elif request.method == 'POST': - game = get_object_or_404(Game, pk=id) - form = GameForm(request.POST, request.FILES, instance=game) - page_title = _("修改游戏") + form = CollectionForm(request.POST, request.FILES, instance=collection) if form.is_valid(): form.instance.last_editor = request.user form.instance.edited_time = timezone.now() @@ -125,11 +122,11 @@ def update(request, id): else: return render( request, - 'games/create_update.html', + 'create_update.html', { 'form': form, 'title': page_title, - 'submit_url': reverse("games:update", args=[game.id]), + 'submit_url': reverse("collection:update", args=[collection.id]), # provided for frontend js 'this_site_enum_value': SourceSiteEnum.IN_SITE.value, } @@ -144,82 +141,23 @@ def update(request, id): # @login_required def retrieve(request, id): if request.method == 'GET': - game = get_object_or_404(Game, pk=id) - mark = None - mark_tags = None - review = None + collection = get_object_or_404(Collection, pk=id) + if not collection.is_visible_to(request.user): + raise PermissionDenied() + form = CollectionForm(instance=collection) - # retreive tags - game_tag_list = game.game_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 = GameMark.objects.get(owner=request.user, game=game) - except ObjectDoesNotExist: - mark = None - if mark: - mark_tags = mark.gamemark_tags.all() - mark.get_status_display = GameMarkStatusTranslator(mark.status) - mark_form = GameMarkForm(instance=mark, initial={ - 'tags': mark_tags - }) - else: - mark_form = GameMarkForm(initial={ - 'game': game, - 'tags': mark_tags - }) - - # retrieve user review - try: - if request.user.is_authenticated: - review = GameReview.objects.get( - owner=request.user, game=game) - 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 = GameMark.get_available(game, request.user) - review_list = GameReview.get_available(game, 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 = GameMarkStatusTranslator(m.status) - review_list_more = True if len( - review_list) > REVIEW_NUMBER else False - review_list = review_list[:REVIEW_NUMBER] - - # 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) + followers = [] + if request.user.is_authenticated: + followers = [] return render( request, - 'games/detail.html', + 'detail.html', { - 'game': game, - 'mark': mark, - 'review': review, - 'status_enum': MarkStatusEnum, - 'mark_form': mark_form, - 'mark_list': mark_list, - 'mark_list_more': mark_list_more, - 'review_list': review_list, - 'review_list_more': review_list_more, - 'game_tag_list': game_tag_list, - 'mark_tags': mark_tags, + 'collection': collection, + 'form': form, + 'followers': followers, + } ) else: