IdHub/idhub/views.py

98 lines
2.8 KiB
Python
Raw Normal View History

2023-09-29 16:06:17 +00:00
import logging
import datetime
import json
2023-09-29 16:06:17 +00:00
from django.conf import settings
from django.contrib import messages
2023-10-02 15:49:33 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
2023-09-29 16:06:17 +00:00
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect
from django.urls import reverse_lazy
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from django.views import View
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.edit import DeleteView, FormView
from django.views.generic.list import ListView
from django.contrib.auth.models import User
2023-10-03 03:47:00 +00:00
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
2023-10-03 03:47:00 +00:00
from django.urls import reverse
2023-09-28 09:01:14 +00:00
2023-10-03 03:47:00 +00:00
from .models import AppUser
from .forms import UserForm
2023-09-28 09:01:14 +00:00
from django.shortcuts import redirect, render
2023-10-03 03:47:00 +00:00
from django.contrib.auth.decorators import login_required
2023-09-29 16:06:17 +00:00
from .forms import LoginForm
logger = logging.getLogger(__name__)
2023-09-28 09:01:14 +00:00
def index(request):
return redirect("/user")
2023-10-03 03:47:00 +00:00
@login_required
2023-09-28 09:01:14 +00:00
def user(request):
2023-10-03 03:47:00 +00:00
current_user: AppUser = request.user.appuser
if request.method == "POST":
form = UserForm(request.POST)
if form.is_valid():
cdata = form.cleaned_data
current_user.django_user.first_name = cdata['first_name']
current_user.save()
current_user.django_user.save()
return HttpResponseRedirect(reverse("user"))
else:
return render(request, "idhub/user-details.html", {"form": form})
elif request.method == "GET":
form = UserForm.from_user(current_user)
return render(request, "idhub/user-details.html", {"form": form})
2023-10-03 04:01:54 +00:00
2023-09-29 16:06:17 +00:00
2023-10-02 15:49:33 +00:00
# class UserDashboardView(LoginRequiredMixin, TemplateView):
2023-09-29 16:06:17 +00:00
class UserDashboardView(TemplateView):
template_name = "idhub/user_dashboard.html"
2023-10-02 15:49:33 +00:00
title = _('Dashboard')
#login_url = "/login/"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': self.title,
})
return context
2023-09-29 16:06:17 +00:00
class LoginView(FormView):
template_name = 'auth/login.html'
form_class = LoginForm
2023-10-02 15:49:33 +00:00
success_url = reverse_lazy('idhub:user_dashboard')
2023-09-29 16:06:17 +00:00
extra_context = {
'title': _('Login'),
}
def form_valid(self, form):
2023-10-02 15:49:33 +00:00
return super().form_valid(form)
2023-09-29 16:06:17 +00:00
class LogoutView(RedirectView):
"""
Log out the user.
"""
permanent = False
pattern_name = 'login'
def get_redirect_url(self, *args, **kwargs):
"""
Logs out the user.
"""
return super().get_redirect_url(*args, **kwargs)
def post(self, request, *args, **kwargs):
"""Logout may be done via POST."""
return self.get(request, *args, **kwargs)