feat: Allow (un)subscribing without commenting

This commit is contained in:
moanos [he/him] 2024-09-29 23:35:54 +02:00
parent a28b6ce4b3
commit 022bf577d4
3 changed files with 55 additions and 22 deletions

View File

@ -142,6 +142,7 @@ class CommentForm(forms.ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.helper = FormHelper() self.helper = FormHelper()
self.helper.form_action = "comment"
self.helper.form_class = 'form-comments' self.helper.form_class = 'form-comments'
self.helper.add_input(Submit('submit', _('Kommentieren'), css_class="btn2")) self.helper.add_input(Submit('submit', _('Kommentieren'), css_class="btn2"))

View File

@ -4,7 +4,23 @@
{% block content %} {% block content %}
<div class="detail-adoption-notice-header"> <div class="detail-adoption-notice-header">
<h1 class="detail-adoption-notice-header">{{ adoption_notice.name }}</h1> <h1 class="detail-adoption-notice-header">{{ adoption_notice.name }}
{% if not is_subscribed %}
<form class="notification-card-mark-read" method="post">
{% csrf_token %}
<input type="hidden" name="action" value="subscribe">
<input type="hidden" name="adoption_notice_id" value="{{ adoption_notice.pk }}">
<button class="btn2" type="submit" id="submit"><i class="fa-solid fa-bell"></i></button>
</form>
{% else %}
<form class="notification-card-mark-read" method="post">
{% csrf_token %}
<input type="hidden" name="action" value="unsubscribe">
<input type="hidden" name="adoption_notice_id" value="{{ adoption_notice.pk }}">
<button class="btn2" type="submit" id="submit"><i class="fa-solid fa-bell-slash"></i></button>
</form>
{% endif %}
</h1>
{% if has_edit_permission %} {% if has_edit_permission %}
<a class="btn2" <a class="btn2"
href="{% url 'adoption-notice-add-photo' adoption_notice_id=adoption_notice.pk %}">{% translate 'Foto hinzufügen' %}</a> href="{% url 'adoption-notice-add-photo' adoption_notice_id=adoption_notice.pk %}">{% translate 'Foto hinzufügen' %}</a>

View File

@ -75,9 +75,17 @@ def change_language(request):
def adoption_notice_detail(request, adoption_notice_id): def adoption_notice_detail(request, adoption_notice_id):
adoption_notice = AdoptionNotice.objects.get(id=adoption_notice_id) adoption_notice = AdoptionNotice.objects.get(id=adoption_notice_id)
if request.user.is_authenticated:
try:
subscription = Subscriptions.objects.get(owner=request.user, adoption_notice=adoption_notice)
is_subscribed = True
except Subscriptions.DoesNotExist:
is_subscribed = False
has_edit_permission = user_is_owner_or_trust_level(request.user, adoption_notice) has_edit_permission = user_is_owner_or_trust_level(request.user, adoption_notice)
if request.method == 'POST': if request.method == 'POST':
action = request.POST.get("action")
if request.user.is_authenticated: if request.user.is_authenticated:
if action == "comment":
comment_form = CommentForm(request.POST) comment_form = CommentForm(request.POST)
if comment_form.is_valid(): if comment_form.is_valid():
@ -100,12 +108,20 @@ def adoption_notice_detail(request, adoption_notice_id):
text=f"{request.user}: {comment_instance.text}", text=f"{request.user}: {comment_instance.text}",
comment=comment_instance) comment=comment_instance)
notification.save() notification.save()
else:
comment_form = CommentForm(instance=adoption_notice)
if action == "subscribe":
Subscriptions.objects.create(owner=request.user, adoption_notice=adoption_notice)
is_subscribed = True
if action == "unsubscribe":
subscription.delete()
is_subscribed = False
else: else:
raise PermissionDenied raise PermissionDenied
else: else:
comment_form = CommentForm(instance=adoption_notice) comment_form = CommentForm(instance=adoption_notice)
context = {"adoption_notice": adoption_notice, "comment_form": comment_form, "user": request.user, context = {"adoption_notice": adoption_notice,"comment_form": comment_form, "user": request.user,
"has_edit_permission": has_edit_permission} "has_edit_permission": has_edit_permission, "is_subscribed": is_subscribed}
return render(request, 'fellchensammlung/details/detail_adoption_notice.html', context=context) return render(request, 'fellchensammlung/details/detail_adoption_notice.html', context=context)