Follow button snippet

This commit is contained in:
Mouse Reeve 2020-02-22 12:57:13 -08:00
parent 5b13959850
commit 808ca0bd60
4 changed files with 25 additions and 32 deletions

View file

@ -0,0 +1,13 @@
{% if not request.user in user.followers.all %}
<form action="/follow/" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}"></input>
<input type="submit" value="Follow"></input>
</form>
{% else %}
<form action="/unfollow/" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}"></input>
<input type="submit" value="Unfollow"></input>
</form>
{% endif %}

View file

@ -11,19 +11,7 @@
<blockquote>{{ user.summary | safe }}</blockquote> <blockquote>{{ user.summary | safe }}</blockquote>
{% endif %} {% endif %}
{% if not is_self %} {% if not is_self %}
{% if not request.user in user.followers.all %} {% include 'snippets/follow_button.html' with user=user %}
<form action="/follow/" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}"></input>
<input type="submit" value="Follow"></input>
</form>
{% else %}
<form action="/unfollow/" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}"></input>
<input type="submit" value="Unfollow"></input>
</form>
{% endif %}
{% endif %} {% endif %}
{% if is_self %} {% if is_self %}
@ -38,17 +26,7 @@
<div> <div>
<a href="{{ follower.actor }}">{{ follower.username }}</a> <a href="{{ follower.actor }}">{{ follower.username }}</a>
{% if not request.user in follower.followers.all %} {% if not request.user in follower.followers.all %}
<form action="/follow/" method="post"> {% include 'snippets/follow_button.html' with user=user %}
{% csrf_token %}
<input type="hidden" name="user" value="{{ follower.username }}"></input>
<input type="submit" value="Follow"></input>
</form>
{% else %}
<form action="/unfollow/" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ follower.username }}"></input>
<input type="submit" value="Unfollow"></input>
</form>
{% endif %} {% endif %}
</div> </div>
{% endfor %} {% endfor %}

View file

@ -4,10 +4,7 @@
{% for result in results %} {% for result in results %}
<div> <div>
<h2>{{ result.username }}</h2> <h2>{{ result.username }}</h2>
<form action="/follow/{{ result.username }}" method="post"> {% include 'snippets/follow_button.html' with user=result %}
{% csrf_token %}
<input type="submit" value="Follow"></input>
</form>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>

View file

@ -2,7 +2,7 @@
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.db.models import Avg, Q from django.db.models import Avg, Q
from django.http import HttpResponseNotFound from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import redirect from django.shortcuts import redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
import re import re
@ -365,8 +365,10 @@ def favorite(request, status_id):
def follow(request): def follow(request):
''' follow another user, here or abroad ''' ''' follow another user, here or abroad '''
username = request.POST['user'] username = request.POST['user']
# should this be an actor rather than an id? idk try:
to_follow = models.User.objects.get(username=username) to_follow = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseBadRequest()
outgoing.handle_outgoing_follow(request.user, to_follow) outgoing.handle_outgoing_follow(request.user, to_follow)
user_slug = to_follow.localname if to_follow.localname \ user_slug = to_follow.localname if to_follow.localname \
@ -378,7 +380,10 @@ def follow(request):
def unfollow(request): def unfollow(request):
''' unfollow a user ''' ''' unfollow a user '''
username = request.POST['user'] username = request.POST['user']
to_unfollow = models.User.objects.get(username=username) try:
to_unfollow = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseBadRequest()
outgoing.handle_outgoing_unfollow(request.user, to_unfollow) outgoing.handle_outgoing_unfollow(request.user, to_unfollow)
user_slug = to_unfollow.localname if to_unfollow.localname \ user_slug = to_unfollow.localname if to_unfollow.localname \