Cache status interact buttons

This commit is contained in:
Mouse Reeve 2022-01-05 14:54:51 -08:00
parent 0da0091237
commit 7df99afdc7
3 changed files with 46 additions and 29 deletions

View file

@ -82,6 +82,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
if not self.reply_parent:
self.thread_id = self.id
super().save(broadcast=False, update_fields=["thread_id"])
def delete(self, *args, **kwargs): # pylint: disable=unused-argument

View file

@ -1,6 +1,7 @@
{% extends 'components/card.html' %}
{% load i18n %}
{% load utilities %}
{% load cache %}
{% block card-header %}
<div
@ -30,38 +31,41 @@
{# nothing here #}
{% elif request.user.is_authenticated %}
<div class="card-footer-item">
{% 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" %}
</div>
<div class="card-footer-item">
{% include 'snippets/boost_button.html' with status=status %}
</div>
<div class="card-footer-item">
{% include 'snippets/fav_button.html' with status=status %}
</div>
{% if not moderation_mode %}
<div class="card-footer-item">
{% include 'snippets/status/status_options.html' with class="is-small is-light is-transparent" right=True %}
</div>
{% endif %}
{% cache 259200 interact request.user.id status.id %}
<div class="card-footer-item">
{% 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" %}
</div>
<div class="card-footer-item">
{% include 'snippets/boost_button.html' with status=status %}
</div>
<div class="card-footer-item">
{% include 'snippets/fav_button.html' with status=status %}
</div>
{% if not moderation_mode %}
<div class="card-footer-item">
{% include 'snippets/status/status_options.html' with class="is-small is-light is-transparent" right=True %}
</div>
{% endif %}
{% endcache %}
{% else %}
<div class="card-footer-item">
<a href="{% url 'login' %}">
<span class="icon icon-comment is-small" title="{% trans 'Reply' %}">
<span class="is-sr-only">{% trans "Reply" %}</span>
</span>
<span class="icon icon-boost is-small ml-4" title="{% trans 'Boost status' %}">
<span class="is-sr-only">{% trans "Boost status" %}</span>
</span>
<div class="card-footer-item">
<a href="{% url 'login' %}">
<span class="icon icon-comment is-small" title="{% trans 'Reply' %}">
<span class="is-sr-only">{% trans "Reply" %}</span>
</span>
<span class="icon icon-heart is-small ml-4" title="{% trans 'Like status' %}">
<span class="is-sr-only">{% trans "Like status" %}</span>
</span>
</a>
</div>
<span class="icon icon-boost is-small ml-4" title="{% trans 'Boost status' %}">
<span class="is-sr-only">{% trans "Boost status" %}</span>
</span>
<span class="icon icon-heart is-small ml-4" title="{% trans 'Like status' %}">
<span class="is-sr-only">{% trans "Like status" %}</span>
</span>
</a>
</div>
{% endif %}
{% endblock %}

View file

@ -1,6 +1,8 @@
""" boosts and favs """
from django.db import IntegrityError
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
@ -17,6 +19,7 @@ class Favorite(View):
def post(self, request, status_id):
"""create a like"""
clear_cache(request.user.id, status_id)
status = models.Status.objects.get(id=status_id)
try:
models.Favorite.objects.create(status=status, user=request.user)
@ -43,6 +46,7 @@ class Unfavorite(View):
return HttpResponseNotFound()
favorite.delete()
clear_cache(request.user.id, status_id)
if is_api_request(request):
return HttpResponse()
return redirect(request.headers.get("Referer", "/"))
@ -70,6 +74,7 @@ class Boost(View):
privacy=status.privacy,
user=request.user,
)
clear_cache(request.user.id, status_id)
if is_api_request(request):
return HttpResponse()
return redirect(request.headers.get("Referer", "/"))
@ -87,6 +92,13 @@ class Unboost(View):
).first()
boost.delete()
clear_cache(request.user.id, status_id)
if is_api_request(request):
return HttpResponse()
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)