66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
from django.contrib.auth import login
|
|
from django.contrib.auth.forms import UserCreationForm
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.contrib.auth.views import LoginView, LogoutView
|
|
from django.urls import reverse_lazy
|
|
from django.views.generic import CreateView, DeleteView, ListView, TemplateView, UpdateView
|
|
|
|
from .forms import StudentForm
|
|
from .models import Student
|
|
|
|
|
|
class DashboardView(LoginRequiredMixin, ListView):
|
|
model = Student
|
|
template_name = 'students/dashboard.html'
|
|
context_object_name = 'students'
|
|
|
|
def get_queryset(self):
|
|
return Student.objects.all()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['total_students'] = Student.objects.count()
|
|
context['average_age'] = round(sum(student.age for student in context['students']) / context['total_students'], 1) if context['total_students'] else 0
|
|
context['grade_count'] = Student.objects.values('grade').distinct().count()
|
|
return context
|
|
|
|
|
|
class ManagerLoginView(LoginView):
|
|
template_name = 'registration/login.html'
|
|
redirect_authenticated_user = True
|
|
|
|
|
|
class ManagerSignupView(CreateView):
|
|
form_class = UserCreationForm
|
|
template_name = 'registration/signup.html'
|
|
success_url = reverse_lazy('dashboard')
|
|
|
|
def form_valid(self, form):
|
|
response = super().form_valid(form)
|
|
login(self.request, self.object)
|
|
return response
|
|
|
|
|
|
class ManagerLogoutView(LogoutView):
|
|
next_page = reverse_lazy('login')
|
|
|
|
|
|
class StudentCreateView(LoginRequiredMixin, CreateView):
|
|
model = Student
|
|
form_class = StudentForm
|
|
template_name = 'students/student_form.html'
|
|
success_url = reverse_lazy('dashboard')
|
|
|
|
|
|
class StudentUpdateView(LoginRequiredMixin, UpdateView):
|
|
model = Student
|
|
form_class = StudentForm
|
|
template_name = 'students/student_form.html'
|
|
success_url = reverse_lazy('dashboard')
|
|
|
|
|
|
class StudentDeleteView(LoginRequiredMixin, DeleteView):
|
|
model = Student
|
|
template_name = 'students/student_confirm_delete.html'
|
|
success_url = reverse_lazy('dashboard')
|