Fixes cache of status interact

The CSRF token was being cached which caused submits to fail
This commit is contained in:
Mouse Reeve 2022-01-06 11:07:22 -08:00
parent 32ac4111aa
commit e416ef05e8
4 changed files with 26 additions and 22 deletions

View file

@ -1,7 +1,6 @@
{% extends 'components/card.html' %} {% extends 'components/card.html' %}
{% load i18n %} {% load i18n %}
{% load utilities %} {% load utilities %}
{% load cache %}
{% block card-header %} {% block card-header %}
<div <div
@ -31,7 +30,6 @@
{# nothing here #} {# nothing here #}
{% elif request.user.is_authenticated %} {% elif request.user.is_authenticated %}
{% cache 259200 interact request.user.id status.id %}
<div class="card-footer-item"> <div class="card-footer-item">
{% trans "Reply" as button_text %} {% trans "Reply" as button_text %}
{% include 'snippets/toggle/toggle_button.html' with controls_text="show_comment" controls_uid=status.id text=button_text icon_with_text="comment" class="is-small is-light is-transparent toggle-button" focus="id_content_reply" %} {% include 'snippets/toggle/toggle_button.html' with controls_text="show_comment" controls_uid=status.id text=button_text icon_with_text="comment" class="is-small is-light is-transparent toggle-button" focus="id_content_reply" %}
@ -47,7 +45,6 @@
{% include 'snippets/status/status_options.html' with class="is-small is-light is-transparent" right=True %} {% include 'snippets/status/status_options.html' with class="is-small is-light is-transparent" right=True %}
</div> </div>
{% endif %} {% endif %}
{% endcache %}
{% else %} {% else %}

View file

@ -20,17 +20,21 @@
</li> </li>
{% if status.status_type != 'GeneratedNote' and status.status_type != 'Rating' %} {% if status.status_type != 'GeneratedNote' and status.status_type != 'Rating' %}
<li role="menuitem" class="dropdown-item p-0"> <li role="menuitem" class="dropdown-item p-0">
<a href="{% url 'edit-status' status.id %}" class="button is-radiusless is-fullwidth is-small" type="submit"> <span class="control">
{% trans "Edit" %} <a href="{% url 'edit-status' status.id %}" class="button is-radiusless is-fullwidth is-small" type="submit">
</a> {% trans "Edit" %}
</a>
</span>
</li> </li>
{% endif %} {% endif %}
{% else %} {% else %}
{# things you can do to other people's statuses #} {# things you can do to other people's statuses #}
<li role="menuitem" class="dropdown-item p-0"> <li role="menuitem" class="dropdown-item p-0">
<a href="{% url 'direct-messages-user' status.user|username %}" class="button is-small is-white is-radiusless is-fullwidth"> <span class="control">
{% trans "Send direct message" %} <a href="{% url 'direct-messages-user' status.user|username %}" class="button is-small is-white is-radiusless is-fullwidth">
</a> {% trans "Send direct message" %}
</a>
</span>
</li> </li>
<li role="menuitem" class="dropdown-item p-0"> <li role="menuitem" class="dropdown-item p-0">
{% include 'snippets/report_button.html' with user=status.user status=status %} {% include 'snippets/report_button.html' with user=status.user status=status %}

View file

@ -1,5 +1,7 @@
""" template filters for status interaction buttons """ """ template filters for status interaction buttons """
from django import template from django import template
from django.core.cache import cache
from bookwyrm import models from bookwyrm import models
@ -9,13 +11,21 @@ register = template.Library()
@register.filter(name="liked") @register.filter(name="liked")
def get_user_liked(user, status): def get_user_liked(user, status):
"""did the given user fav a status?""" """did the given user fav a status?"""
return models.Favorite.objects.filter(user=user, status=status).exists() return cache.get_or_set(
f"fav-{user.id}-{status.id}",
models.Favorite.objects.filter(user=user, status=status).exists(),
259200,
)
@register.filter(name="boosted") @register.filter(name="boosted")
def get_user_boosted(user, status): def get_user_boosted(user, status):
"""did the given user fav a status?""" """did the given user fav a status?"""
return status.boosters.filter(user=user).exists() return cache.get_or_set(
f"boost-{user.id}-{status.id}",
status.boosters.filter(user=user).exists(),
259200,
)
@register.filter(name="saved") @register.filter(name="saved")

View file

@ -1,7 +1,6 @@
""" boosts and favs """ """ boosts and favs """
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.cache import cache from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import IntegrityError from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import redirect from django.shortcuts import redirect
@ -19,7 +18,7 @@ class Favorite(View):
def post(self, request, status_id): def post(self, request, status_id):
"""create a like""" """create a like"""
clear_cache(request.user.id, status_id) cache.delete(f"fav-{request.user.id}-{status_id}")
status = models.Status.objects.get(id=status_id) status = models.Status.objects.get(id=status_id)
try: try:
models.Favorite.objects.create(status=status, user=request.user) models.Favorite.objects.create(status=status, user=request.user)
@ -38,6 +37,7 @@ class Unfavorite(View):
def post(self, request, status_id): def post(self, request, status_id):
"""unlike a status""" """unlike a status"""
cache.delete(f"fav-{request.user.id}-{status_id}")
status = models.Status.objects.get(id=status_id) status = models.Status.objects.get(id=status_id)
try: try:
favorite = models.Favorite.objects.get(status=status, user=request.user) favorite = models.Favorite.objects.get(status=status, user=request.user)
@ -46,7 +46,6 @@ class Unfavorite(View):
return HttpResponseNotFound() return HttpResponseNotFound()
favorite.delete() favorite.delete()
clear_cache(request.user.id, status_id)
if is_api_request(request): if is_api_request(request):
return HttpResponse() return HttpResponse()
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
@ -58,6 +57,7 @@ class Boost(View):
def post(self, request, status_id): def post(self, request, status_id):
"""boost a status""" """boost a status"""
cache.delete(f"boost-{request.user.id}-{status_id}")
status = models.Status.objects.get(id=status_id) status = models.Status.objects.get(id=status_id)
# is it boostable? # is it boostable?
if not status.boostable: if not status.boostable:
@ -74,7 +74,6 @@ class Boost(View):
privacy=status.privacy, privacy=status.privacy,
user=request.user, user=request.user,
) )
clear_cache(request.user.id, status_id)
if is_api_request(request): if is_api_request(request):
return HttpResponse() return HttpResponse()
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
@ -86,19 +85,13 @@ class Unboost(View):
def post(self, request, status_id): def post(self, request, status_id):
"""boost a status""" """boost a status"""
cache.delete(f"boost-{request.user.id}-{status_id}")
status = models.Status.objects.get(id=status_id) status = models.Status.objects.get(id=status_id)
boost = models.Boost.objects.filter( boost = models.Boost.objects.filter(
boosted_status=status, user=request.user boosted_status=status, user=request.user
).first() ).first()
boost.delete() boost.delete()
clear_cache(request.user.id, status_id)
if is_api_request(request): if is_api_request(request):
return HttpResponse() return HttpResponse()
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
def clear_cache(user_id, status_id):
"""clear template cache"""
cache_key = make_template_fragment_key("interact", [user_id, status_id])
cache.delete(cache_key)