dashboard/accounts/views.py

64 lines
1.9 KiB
Python
Raw Normal View History

2022-06-10 11:52:54 +00:00
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_protect
from django.urls import reverse
GREETINGS = {
"greeting": "Welcome to Hostea - Free Forge Ecosystem for Free Developers",
"features": [
"Fully managed",
"100% Free Software",
"Fully Self-Hostable",
"Observable and reliable",
"Federation when available",
"Radically transparent",
"Horizontal community",
"Run Hostea and become a service provider!",
],
}
LOGIN_CONTENT = {
"login_name": "Username or Email",
"action": "Login",
"password": "Password",
"forgot_password": "Forgot password?",
"register_prompt": "New to Hostea?",
"register_action_link_text": "Create an account",
"greetings": GREETINGS,
}
@csrf_protect
def login_view(request):
if request.method == "POST":
user = authenticate(request, username=request.POST["username"], password=request.POST["password"])
if user is not None:
login(request, user)
print("user logged in")
if "next" in request.POST:
next_url = request.POST["next"]
if next_url:
return redirect(next_url)
return redirect(reverse('accounts.protected'))
else:
return HttpResponse("Login required")
ctx = LOGIN_CONTENT
if "next" in request.GET:
ctx["next"] = request.GET["next"]
return render(request, "accounts/auth/login.html", ctx)
@login_required
def protected_view(request):
return render(request, "accounts/protected.html")
@login_required
def logout_view(request):
logout(request)
return redirect(reverse('accounts.login'))
def public_view(request):
return render(request, "accounts/public.html")