From 19deae3d5018cdcb13a5f054f99aef9a1cdec049 Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Sat, 6 Feb 2021 10:17:04 -0700 Subject: [PATCH] Add pagination for periods --- attendance/models.py | 3 ++ .../attendance_overview_instructor.html | 11 +++++-- .../attendance_overview_student.html | 3 ++ attendance/urls.py | 1 + attendance/views.py | 29 +++++++++++++++++-- static/scss/_instructors.scss | 9 ++++++ 6 files changed, 51 insertions(+), 5 deletions(-) diff --git a/attendance/models.py b/attendance/models.py index 838c69d..4ea25c5 100644 --- a/attendance/models.py +++ b/attendance/models.py @@ -29,6 +29,9 @@ class Period(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) + class Meta: + ordering = ['-clocked_in'] + @property def p_duration(self): return round((self.clocked_out - self.clocked_in).seconds / 3600, 2) diff --git a/attendance/templates/attendance/attendance_overview_instructor.html b/attendance/templates/attendance/attendance_overview_instructor.html index a6b266a..75d2799 100644 --- a/attendance/templates/attendance/attendance_overview_instructor.html +++ b/attendance/templates/attendance/attendance_overview_instructor.html @@ -41,12 +41,13 @@

Attendance log

- Generate Reports + Generate Reports → + @@ -58,9 +59,10 @@ - + + {% if period.clocked_out %} - + {% else %} @@ -73,6 +75,9 @@ {% endfor %}
Student StationDate Clocked in Clocked out Duration
{{ period.student }} {{ period.station_number }}{{ period.clocked_in }}{{ period.clocked_in|date:"M d, y" }}{{ period.clocked_in|time:"P" }}{{ period.clocked_out }}{{ period.clocked_out|time:"P" }} {{ period.clocked_in|timesince:period.clocked_out }}Current sesson: {{ period.clocked_in|timesince }}
+

+ See more → +

diff --git a/attendance/templates/attendance/attendance_overview_student.html b/attendance/templates/attendance/attendance_overview_student.html index 367d1d8..22c9ac4 100644 --- a/attendance/templates/attendance/attendance_overview_student.html +++ b/attendance/templates/attendance/attendance_overview_student.html @@ -22,5 +22,8 @@

Attendance log

Total hours for the month: {{ period_total.total|timedelta_format:2 }} (Does not include current session.)

{% include 'attendance/_student_periods.html' %} +

+ See previous → +

diff --git a/attendance/urls.py b/attendance/urls.py index 3f170ce..5f99900 100644 --- a/attendance/urls.py +++ b/attendance/urls.py @@ -10,6 +10,7 @@ from . import views urlpatterns = [ path('', views.AttendanceOverview.as_view(), name='attendance-overview'), path('update/', views.AttendanceUpdateView.as_view(), name='attendance-update'), + path('periods/', views.PeriodListView.as_view(), name='period-list'), path('periods/new/', views.PeriodCreateView.as_view(), name='period-create'), path('periods//', include([ path('', views.PeriodDetailView.as_view(), name='period-detail'), diff --git a/attendance/views.py b/attendance/views.py index d047572..470ced2 100644 --- a/attendance/views.py +++ b/attendance/views.py @@ -6,6 +6,7 @@ from django.views.generic.base import TemplateView from django.views.generic.edit import FormView, CreateView, UpdateView, DeleteView from django.views.generic.detail import DetailView from django.views.generic.list import ListView +from django.core.paginator import Paginator from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.utils import timezone @@ -31,10 +32,16 @@ class AttendanceOverview(LoginRequiredMixin, TemplateView): if hasattr(self.request.user, 'instructor'): instructor = self.request.user.instructor context['student_list'] = Student.objects.filter( - department=instructor + department=instructor.department ).annotate(total_hours=Sum('period__duration')) - context['period_list'] = Period.objects.order_by('-clocked_in') + context['period_list'] = Period.objects.filter( + student__department=instructor.department + ).filter( + clocked_in__year=timezone.now().year + ).filter( + clocked_in__month=timezone.now().month + ) elif hasattr(self.request.user, 'student'): student = self.request.user.student @@ -90,6 +97,24 @@ class AttendanceUpdateView(LoginRequiredMixin, FormView): # PERIODS +class PeriodListView(LoginRequiredMixin, ListView): + model = Period + paginate_by = 25 + template_name = 'attendance/period_list.html' + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + if hasattr(self.request.user, 'student'): + period_list = Period.objects.filter( + student = self.request.user.student + ) + paginator = Paginator(period_list, 25) + page_number = self.request.GET.get('page') + page_obj = paginator.get_page(page_number) + + context['page_obj'] = page_obj + return context + class PeriodCreateView(LoginRequiredMixin, CreateView): model = Period form_class = PeriodForm diff --git a/static/scss/_instructors.scss b/static/scss/_instructors.scss index aa1cb04..09306f3 100644 --- a/static/scss/_instructors.scss +++ b/static/scss/_instructors.scss @@ -11,4 +11,13 @@ margin: 0 1rem; } + + &__add_period { + margin-bottom: 1rem; + } + + &__generate_reports { + margin-bottom: 1rem; + + } }