moviewyrm/bookwyrm/views/block.py

44 lines
1.4 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" views for actions you can take in the application """
2021-01-25 22:03:18 +00:00
from django.contrib.auth.decorators import login_required
2021-01-26 21:00:36 +00:00
from django.http import HttpResponseNotFound
2021-01-25 22:03:18 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
2021-01-25 22:03:18 +00:00
from django.utils.decorators import method_decorator
from django.views import View
2021-01-26 21:00:36 +00:00
from django.views.decorators.http import require_POST
2021-01-25 22:03:18 +00:00
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-25 22:03:18 +00:00
class Block(View):
2021-04-26 16:15:42 +00:00
"""blocking users"""
2021-03-08 16:49:10 +00:00
2021-01-25 22:03:18 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""list of blocked users?"""
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "preferences/blocks.html")
2021-01-25 22:03:18 +00:00
def post(self, request, user_id):
2021-04-26 16:15:42 +00:00
"""block a user"""
2021-01-25 22:03:18 +00:00
to_block = get_object_or_404(models.User, id=user_id)
2021-02-04 21:21:55 +00:00
models.UserBlocks.objects.create(
2021-03-08 16:49:10 +00:00
user_subject=request.user, user_object=to_block
)
2021-09-27 17:17:16 +00:00
return redirect("prefs-block")
2021-01-26 21:00:36 +00:00
@require_POST
@login_required
def unblock(request, user_id):
2021-04-26 16:15:42 +00:00
"""undo a block"""
2021-01-26 21:00:36 +00:00
to_unblock = get_object_or_404(models.User, id=user_id)
try:
block = models.UserBlocks.objects.get(
user_subject=request.user,
user_object=to_unblock,
)
except models.UserBlocks.DoesNotExist:
return HttpResponseNotFound()
block.delete()
2021-09-27 17:17:16 +00:00
return redirect("prefs-block")