moviewyrm/bookwyrm/views/follow.py

102 lines
2.9 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-13 21:36:01 +00:00
from django.contrib.auth.decorators import login_required
2021-02-17 19:28:54 +00:00
from django.db import IntegrityError
2021-01-13 21:36:01 +00:00
from django.http import HttpResponseBadRequest
from django.shortcuts import redirect
from django.views.decorators.http import require_POST
from bookwyrm import models
from .helpers import get_user_from_username
2021-03-08 16:49:10 +00:00
2021-01-13 21:36:01 +00:00
@login_required
@require_POST
def follow(request):
2021-04-26 16:15:42 +00:00
"""follow another user, here or abroad"""
2021-03-08 16:49:10 +00:00
username = request.POST["user"]
2021-01-13 21:36:01 +00:00
try:
2021-02-23 20:41:37 +00:00
to_follow = get_user_from_username(request.user, username)
2021-01-13 21:36:01 +00:00
except models.User.DoesNotExist:
return HttpResponseBadRequest()
2021-02-17 19:28:54 +00:00
try:
models.UserFollowRequest.objects.create(
user_subject=request.user,
user_object=to_follow,
)
except IntegrityError:
pass
2021-02-13 00:16:22 +00:00
2021-01-13 21:36:01 +00:00
return redirect(to_follow.local_path)
@login_required
@require_POST
def unfollow(request):
2021-04-26 16:15:42 +00:00
"""unfollow a user"""
2021-03-08 16:49:10 +00:00
username = request.POST["user"]
2021-01-13 21:36:01 +00:00
try:
2021-02-23 20:41:37 +00:00
to_unfollow = get_user_from_username(request.user, username)
2021-01-13 21:36:01 +00:00
except models.User.DoesNotExist:
return HttpResponseBadRequest()
2021-03-13 22:55:09 +00:00
try:
models.UserFollows.objects.get(
user_subject=request.user, user_object=to_unfollow
).delete()
except models.UserFollows.DoesNotExist:
pass
try:
models.UserFollowRequest.objects.get(
user_subject=request.user, user_object=to_unfollow
).delete()
except models.UserFollowRequest.DoesNotExist:
pass
# this is handled with ajax so it shouldn't really matter
return redirect(request.headers.get("Referer", "/"))
2021-01-13 21:36:01 +00:00
@login_required
@require_POST
def accept_follow_request(request):
2021-04-26 16:15:42 +00:00
"""a user accepts a follow request"""
2021-03-08 16:49:10 +00:00
username = request.POST["user"]
2021-01-13 21:36:01 +00:00
try:
2021-02-23 20:41:37 +00:00
requester = get_user_from_username(request.user, username)
2021-01-13 21:36:01 +00:00
except models.User.DoesNotExist:
return HttpResponseBadRequest()
try:
follow_request = models.UserFollowRequest.objects.get(
2021-03-08 16:49:10 +00:00
user_subject=requester, user_object=request.user
2021-01-13 21:36:01 +00:00
)
except models.UserFollowRequest.DoesNotExist:
# Request already dealt with.
return redirect(request.user.local_path)
follow_request.accept()
2021-01-13 21:36:01 +00:00
return redirect(request.user.local_path)
@login_required
@require_POST
def delete_follow_request(request):
2021-04-26 16:15:42 +00:00
"""a user rejects a follow request"""
2021-03-08 16:49:10 +00:00
username = request.POST["user"]
2021-01-13 21:36:01 +00:00
try:
2021-02-23 20:41:37 +00:00
requester = get_user_from_username(request.user, username)
2021-01-13 21:36:01 +00:00
except models.User.DoesNotExist:
return HttpResponseBadRequest()
try:
follow_request = models.UserFollowRequest.objects.get(
2021-03-08 16:49:10 +00:00
user_subject=requester, user_object=request.user
2021-01-13 21:36:01 +00:00
)
except models.UserFollowRequest.DoesNotExist:
return HttpResponseBadRequest()
follow_request.delete()
2021-03-08 16:49:10 +00:00
return redirect("/user/%s" % request.user.localname)