moviewyrm/bookwyrm/views/interaction.py

84 lines
2.5 KiB
Python
Raw Permalink Normal View History

2021-03-08 16:49:10 +00:00
""" boosts and favs """
2021-01-13 16:10:50 +00:00
from django.db import IntegrityError
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import models
# pylint: disable= no-self-use
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-13 16:10:50 +00:00
class Favorite(View):
2021-03-08 16:49:10 +00:00
""" like a status """
2021-01-13 16:10:50 +00:00
def post(self, request, status_id):
2021-03-08 16:49:10 +00:00
""" create a like """
2021-01-13 16:10:50 +00:00
status = models.Status.objects.get(id=status_id)
try:
2021-03-08 16:49:10 +00:00
models.Favorite.objects.create(status=status, user=request.user)
2021-01-13 16:10:50 +00:00
except IntegrityError:
# you already fav'ed that
return HttpResponseBadRequest()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 16:10:50 +00:00
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-13 16:10:50 +00:00
class Unfavorite(View):
2021-03-08 16:49:10 +00:00
""" take back a fav """
2021-01-13 16:10:50 +00:00
def post(self, request, status_id):
2021-03-08 16:49:10 +00:00
""" unlike a status """
2021-01-13 16:10:50 +00:00
status = models.Status.objects.get(id=status_id)
try:
2021-03-08 16:49:10 +00:00
favorite = models.Favorite.objects.get(status=status, user=request.user)
2021-01-13 16:10:50 +00:00
except models.Favorite.DoesNotExist:
# can't find that status, idk
return HttpResponseNotFound()
favorite.delete()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 16:10:50 +00:00
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-13 16:10:50 +00:00
class Boost(View):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
def post(self, request, status_id):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
status = models.Status.objects.get(id=status_id)
# is it boostable?
if not status.boostable:
return HttpResponseBadRequest()
if models.Boost.objects.filter(
2021-03-08 16:49:10 +00:00
boosted_status=status, user=request.user
).exists():
2021-01-13 16:10:50 +00:00
# you already boosted that.
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 16:10:50 +00:00
2021-02-04 21:21:55 +00:00
models.Boost.objects.create(
2021-01-13 16:10:50 +00:00
boosted_status=status,
privacy=status.privacy,
user=request.user,
)
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 16:10:50 +00:00
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-13 16:10:50 +00:00
class Unboost(View):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
def post(self, request, status_id):
2021-03-08 16:49:10 +00:00
""" boost a status """
2021-01-13 16:10:50 +00:00
status = models.Status.objects.get(id=status_id)
boost = models.Boost.objects.filter(
boosted_status=status, user=request.user
).first()
boost.delete()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))