feat: add unsubscribe functionality
This commit is contained in:
parent
3816290eb7
commit
b993621773
@ -13,13 +13,13 @@
|
|||||||
<i class="fas fa-search"></i> {% trans 'Suchen' %}
|
<i class="fas fa-search"></i> {% trans 'Suchen' %}
|
||||||
</button>
|
</button>
|
||||||
{% if searched %}
|
{% if searched %}
|
||||||
{% if not user_is_subscribed_to_search %}
|
{% if subscribed_search %}
|
||||||
<button class="btn" type="submit" name="subscribe_to_search">
|
<button class="btn" type="submit" value="{{ subscribed_search.pk }}" name="unsubscribe_to_search">
|
||||||
<i class="fas fa-bell"></i> {% trans 'Suche abonnieren' %}
|
<i class="fas fa-bell-slash"></i> {% trans 'Suche nicht mehr abonnieren' %}
|
||||||
</button>
|
</button>
|
||||||
{% else %}
|
{% else %}
|
||||||
<button class="btn" type="submit" name="unsubscribe_to_search">
|
<button class="btn" type="submit" name="subscribe_to_search">
|
||||||
<i class="fas fa-bell-slash"></i> {% trans 'Suche nicht mehr abonnieren' %}
|
<i class="fas fa-bell"></i> {% trans 'Suche abonnieren' %}
|
||||||
</button>
|
</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -114,12 +114,18 @@ class Search:
|
|||||||
sex=self.sex,
|
sex=self.sex,
|
||||||
max_distance=self.max_distance)
|
max_distance=self.max_distance)
|
||||||
|
|
||||||
|
def get_subscription_or_none(self, user):
|
||||||
|
user_subscriptions = SearchSubscription.objects.filter(owner=user)
|
||||||
|
for subscription in user_subscriptions:
|
||||||
|
if self == subscription:
|
||||||
|
return subscription
|
||||||
|
|
||||||
def is_subscribed(self, user):
|
def is_subscribed(self, user):
|
||||||
"""
|
"""
|
||||||
Returns true if a user is already subscribed to a search with these parameters
|
Returns true if a user is already subscribed to a search with these parameters
|
||||||
"""
|
"""
|
||||||
user_subscriptions = SearchSubscription.objects.filter(owner=user)
|
subscription = self.get_subscription_or_none()
|
||||||
for subscription in user_subscriptions:
|
if subscription is None:
|
||||||
if self == subscription:
|
|
||||||
return True
|
|
||||||
return False
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
@ -183,11 +183,21 @@ def search(request):
|
|||||||
if not request.user.is_authenticated:
|
if not request.user.is_authenticated:
|
||||||
return redirect(f"{settings.LOGIN_URL}?next={request.path}")
|
return redirect(f"{settings.LOGIN_URL}?next={request.path}")
|
||||||
search.subscribe(request.user)
|
search.subscribe(request.user)
|
||||||
|
if "unsubscribe_to_search" in request.POST:
|
||||||
|
if not request.user.is_authenticated:
|
||||||
|
return redirect(f"{settings.LOGIN_URL}?next={request.path}")
|
||||||
|
search_subscription = SearchSubscription.objects.get(pk=request.POST["unsubscribe_to_search"])
|
||||||
|
if search_subscription.owner == request.user:
|
||||||
|
search_subscription.delete()
|
||||||
|
else:
|
||||||
|
raise PermissionDenied
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
context = {"adoption_notices": search.get_adoption_notices(),
|
context = {"adoption_notices": search.get_adoption_notices(),
|
||||||
"search_form": search.search_form,
|
"search_form": search.search_form,
|
||||||
"place_not_found": search.place_not_found,
|
"place_not_found": search.place_not_found,
|
||||||
"user_is_subscribed_to_search": search.is_subscribed(request.user),
|
"subscribed_search": search.get_subscription_or_none(request.user),
|
||||||
"searched": searched}
|
"searched": searched}
|
||||||
return render(request, 'fellchensammlung/search.html', context=context)
|
return render(request, 'fellchensammlung/search.html', context=context)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user