moviewyrm/bookwyrm/views/block.py

42 lines
1.4 KiB
Python
Raw Normal View History

2021-01-25 22:03:18 +00:00
''' views for actions you can take in the application '''
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
@method_decorator(login_required, name='dispatch')
class Block(View):
''' blocking users '''
def get(self, request):
''' list of blocked users? '''
return TemplateResponse(request, 'preferences/blocks.html')
2021-01-25 22:03:18 +00:00
def post(self, request, user_id):
''' block a user '''
to_block = get_object_or_404(models.User, id=user_id)
2021-02-04 21:21:55 +00:00
models.UserBlocks.objects.create(
2021-01-25 22:03:18 +00:00
user_subject=request.user, user_object=to_block)
2021-01-29 17:28:00 +00:00
return redirect('/preferences/block')
2021-01-26 21:00:36 +00:00
@require_POST
@login_required
def unblock(request, user_id):
''' undo a block '''
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-01-29 17:28:00 +00:00
return redirect('/preferences/block')