bookwyrm/fedireads/views.py

40 lines
1.2 KiB
Python
Raw Normal View History

2020-01-25 23:25:19 +00:00
''' application views/pages '''
2020-01-25 06:32:41 +00:00
from django.contrib.auth.decorators import login_required
2020-01-25 23:25:19 +00:00
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.views.decorators.csrf import csrf_exempt
from fedireads.models import Shelf
2020-01-25 06:32:41 +00:00
@login_required
2020-01-25 23:25:19 +00:00
def home(request):
''' user feed '''
shelves = Shelf.objects.filter(user=request.user.id)
data = {
'user': request.user,
'shelves': shelves,
}
return TemplateResponse(request, 'feed.html', data)
2020-01-25 06:32:41 +00:00
2020-01-25 23:25:19 +00:00
@csrf_exempt
def user_login(request):
''' authentication '''
# send user to the login page
if request.method == 'GET':
return TemplateResponse(request, 'login.html')
2020-01-25 06:32:41 +00:00
2020-01-25 23:25:19 +00:00
# authenticate user
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect(request.GET.get('next', '/'))
return TemplateResponse(request, 'login.html')
@csrf_exempt
@login_required
def user_logout(request):
logout(request)
return redirect('/')