shimatta-kenkyusho/shimatta_kenkyusho/parts/views/generic_views.py

122 lines
3.7 KiB
Python
Raw Normal View History

from django.shortcuts import render, redirect
from django.contrib.auth import logout, login
from django.contrib.auth.forms import AuthenticationForm as AuthForm
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from ..navbar import NavBar
from ..forms import QrSearchForm
class BaseTemplateMixin():
navbar_selected = ''
base_title = ''
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
base_context = {
'navbar': NavBar.get_navbar(self.navbar_selected, self.request.user),
'title': NavBar.get_brand()+' / '+ self.base_title,
'login_active': False,
}
context['base'] = base_context
return context
def post(self, request, *args, **kwargs):
data = request.POST
if 'qr_search' not in data:
super().post(request, *args, **kwargs)
print('QR',data['qr_search'])
f = QrSearchForm(data)
if f.is_valid():
return redirect(f.my_qr_validator.get_redirect_url(f.cleaned_data['qr_search']))
return self.get(request)
class ChangePasswordView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
template_name = 'parts/change-pw.html'
navbar_selected = 'Main'
base_title = 'Change Password'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = PasswordChangeForm(self.request.user)
return context
def post(self, request, *args, **kwargs):
if 'submit-change-pw' not in request.POST:
return super().post(request, *args, **kwargs)
form = PasswordChangeForm(request.user, data=request.POST)
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user)
return redirect('parts-main')
context = self.get_context_data(**kwargs)
if form.errors:
context['form'] = form
return self.render_to_response(context)
class MainView(BaseTemplateMixin, TemplateView):
template_name = 'parts/main.html'
navbar_selected = 'Main'
base_title = 'Main'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user'] = self.request.user
return context
def logout_view(request):
logout(request)
return redirect('parts-main')
def login_view(request):
base_context = {
'navbar': NavBar.get_navbar('Login', request.user),
'title': NavBar.get_brand()+' / '+'Login',
'login_active': True,
}
if request.user.is_authenticated:
next_param = request.GET.get('next')
if next_param is not None:
return redirect(next_param)
return redirect('parts-main')
if request.method == 'POST':
form = AuthForm(data=request.POST)
if form.is_valid():
valid_user = form.get_user()
login(request, valid_user)
next_param = request.GET.get('next')
if next_param is not None:
return redirect(next_param)
return redirect('parts-main')
else:
form = AuthForm()
context = {
'base': base_context,
'form': form,
}
return render(request, 'parts/login.html', context)
def health_check_view(_request) -> HttpResponse:
"""
Health checking view. Returns empty http response with HTTP status OK.
This will be used to check if the system is actually running correctly
"""
return HttpResponse(status=200)