feat: Allow (un)subscribing without commenting
This commit is contained in:
parent
a28b6ce4b3
commit
022bf577d4
@ -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"))
|
||||||
|
|
||||||
|
@ -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>
|
||||||
|
@ -75,37 +75,53 @@ 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:
|
||||||
comment_form = CommentForm(request.POST)
|
if action == "comment":
|
||||||
|
comment_form = CommentForm(request.POST)
|
||||||
|
|
||||||
if comment_form.is_valid():
|
if comment_form.is_valid():
|
||||||
comment_instance = comment_form.save(commit=False)
|
comment_instance = comment_form.save(commit=False)
|
||||||
comment_instance.adoption_notice_id = adoption_notice_id
|
comment_instance.adoption_notice_id = adoption_notice_id
|
||||||
comment_instance.user = request.user
|
comment_instance.user = request.user
|
||||||
comment_instance.save()
|
comment_instance.save()
|
||||||
|
|
||||||
# Auto-subscribe user to adoption notice
|
# Auto-subscribe user to adoption notice
|
||||||
subscription, created = Subscriptions.objects.get_or_create(adoption_notice=adoption_notice,
|
subscription, created = Subscriptions.objects.get_or_create(adoption_notice=adoption_notice,
|
||||||
owner=request.user)
|
owner=request.user)
|
||||||
subscription.save()
|
subscription.save()
|
||||||
|
|
||||||
# Notify users that a comment was added
|
# Notify users that a comment was added
|
||||||
for subscription in adoption_notice.get_subscriptions():
|
for subscription in adoption_notice.get_subscriptions():
|
||||||
# Create a notification but only if the user is not the one that posted the comment
|
# Create a notification but only if the user is not the one that posted the comment
|
||||||
if subscription.owner != request.user:
|
if subscription.owner != request.user:
|
||||||
notification = CommentNotification(user=subscription.owner,
|
notification = CommentNotification(user=subscription.owner,
|
||||||
title=f"{adoption_notice.name} - Neuer Kommentar",
|
title=f"{adoption_notice.name} - Neuer Kommentar",
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user