From 06fb1ef1908b8dfdcbb09821f53ff422fa182877 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Mon, 26 Aug 2019 21:10:31 +0200
Subject: [PATCH] Add first part of week view for groups. Advances #8.

---
 biscuit/apps/alsijil/models.py                |  2 +-
 .../alsijil/templates/alsijil/group_week.html | 76 +++++++++++++++++++
 biscuit/apps/alsijil/urls.py                  |  3 +
 biscuit/apps/alsijil/views.py                 | 41 +++++++++-
 4 files changed, 120 insertions(+), 2 deletions(-)
 create mode 100644 biscuit/apps/alsijil/templates/alsijil/group_week.html

diff --git a/biscuit/apps/alsijil/models.py b/biscuit/apps/alsijil/models.py
index 5e9094d75..7f992790b 100644
--- a/biscuit/apps/alsijil/models.py
+++ b/biscuit/apps/alsijil/models.py
@@ -22,7 +22,7 @@ class PersonalNote(SchoolRelated):
 
 class LessonDocumentation(SchoolRelated):
     week = models.IntegerField()
-    lesson_period = models.ForeignKey('chronos.LessonPeriod', models.CASCADE)
+    lesson_period = models.ForeignKey('chronos.LessonPeriod', models.CASCADE, related_name='documentations')
 
     topic = models.CharField(verbose_name=_('Lesson topic'), max_length=200, blank=True)
     homework = models.CharField(verbose_name=_('Homework'), max_length=200, blank=True)
diff --git a/biscuit/apps/alsijil/templates/alsijil/group_week.html b/biscuit/apps/alsijil/templates/alsijil/group_week.html
new file mode 100644
index 000000000..eebeaa114
--- /dev/null
+++ b/biscuit/apps/alsijil/templates/alsijil/group_week.html
@@ -0,0 +1,76 @@
+{# -*- engine:django -*- #}
+{% extends "core/base.html" %}
+{% load bootstrap4 font_awesome i18n %}
+
+{% block page_title %}
+ {% blocktrans %}Current week{% endblocktrans %}
+{% endblock %}
+
+{% block content %}
+ {% if group %}
+  <div class="row">
+   <div class="col-md-7">
+    <div class="card">
+     <div class="card-header bg-light text-dark">
+      {% blocktrans %}Week{% endblocktrans %}
+      {{ week }} ({{ week|week_start }} - {{ week|week_end }})
+      ,
+      {{ group.name }}
+     </div>
+     <div class="card-body">
+      {% regroup lesson_periods by period.weekday_display as periods_by_day %}
+      {% for weekday, periods in periods_by_day %}
+       <table class="table table-striped table-bordered table-hover table-responsive-xl">
+        <thead>
+         <tr>
+          <th>{% blocktrans %}Period{% endblocktrans %}</th>
+          <th>{% blocktrans %}Subject{% endblocktrans %}</th>
+          <th>{% blocktrans %}Teachers{% endblocktrans %}</th>
+          <th>{% blocktrans %}Topic{% endblocktrans %}</th>
+         </tr>
+        </thead>
+        <tbody>
+         {% for period in periods %}
+          <tr>
+           <td>{{ period.period.period }}</td>
+           <td>{{ lesson.subject.name }}</td>
+           <td>{{ lesson.teachers|join:', ' }}</td>
+           <td>
+            {% if period.documentations__count %}
+             {% fa 'fa-check' %}
+            {% endif}
+           </td>
+          </tr>
+         {% endfor %}
+        </tbody>
+       </table>
+      {% endfor %}
+     </div>
+    </div>
+   </div>
+   <div class="col-md-5">
+    <div class="card">
+     <div class="card-header bg-light text-dark">
+      {% blocktrans %}Personal notes{% endblocktrans %}
+     </div>
+     <div class="card-body">
+      Not implemented yet.
+     </div>
+    </div>
+   </div>
+  </div>
+ {% else %}
+  <div class="card text-white bg-danger">
+   <div class="card-header">
+    {% blocktrans %}No group selected{% endblocktrans %}
+   </div>
+   <div class="card-body">
+    <p>
+     {% blocktrans %}
+      You must select a group to see the week summary.
+     {% endblocktrans %}
+    </p>
+   </div>
+  </div>
+ {% endif %}
+{% endblock %}
diff --git a/biscuit/apps/alsijil/urls.py b/biscuit/apps/alsijil/urls.py
index 28c8688df..a414c1587 100644
--- a/biscuit/apps/alsijil/urls.py
+++ b/biscuit/apps/alsijil/urls.py
@@ -7,4 +7,7 @@ urlpatterns = [
     path('lesson', views.lesson, name='lesson'),
     path('lesson/<int:week>/<int:period_id>', views.lesson,
          name='lesson_by_week_and_period'),
+    path('group/week', views.group_week, name='group_week'),
+    path('group/week/<int:week>', views.group_week,
+         name='group_week_by_week'),
 ]
diff --git a/biscuit/apps/alsijil/views.py b/biscuit/apps/alsijil/views.py
index 4c6870ba2..a38d2fc1b 100644
--- a/biscuit/apps/alsijil/views.py
+++ b/biscuit/apps/alsijil/views.py
@@ -1,11 +1,12 @@
 from typing import Optional
 
 from django.contrib.auth.decorators import login_required
+from django.db.models import Count, Q
 from django.http import HttpRequest, HttpResponse
 from django.shortcuts import render
 from django.utils.translation import ugettext as _
 
-from biscuit.apps.chronos.models import LessonPeriod
+from biscuit.apps.chronos.models import Lesson, LessonPeriod
 from biscuit.apps.chronos.util import current_lesson_periods, current_week
 
 from .forms import LessonDocumentationForm, PersonalNoteFormSet
@@ -58,3 +59,41 @@ def lesson(request: HttpRequest, week: Optional[int] = None, period_id: Optional
         context['personal_note_formset'] = personal_note_formset
 
     return render(request, 'alsijil/lesson.html', context)
+
+
+@login_required
+def group_week(request: HttpRequest, week: Optional[int] = None) -> HttpResponse:
+    context = {}
+
+    wanted_week = week or current_week()
+    week_start = week_days(wanted_week)[0]
+    week_end = week_days(wanted_week)[-1]
+
+    if request.GET,get('group_id', None):
+        # Use requested group
+        group = Group.objects.get(pk=request.GET['group_id'])
+    elif hasattr(request, 'user') and hasattr(request.user, 'person'):
+        # Try to select group from owned groups of user
+        group = request.user.person.owner_of.first()
+    else:
+        group = None
+
+    lesson_periods = LessonPeriod.objects.none()
+
+    if group:
+        for lesson in group.lessons.filter(date_start__lte=week_start, date_end__gte=week_end):
+            qs = lesson.periods
+
+            # Get number of filled-in documentations
+            qs.annotate(Count('documentations',
+                              filter=Q(documentations__week=wanted_week,
+                                       documentations__topic__regex=r'.+'))
+
+            # Combine the lesson periods of all lessons
+            lesson_periods = lesson.periods.union(lesson_periods)
+
+    context['week'] = wanted_week
+    context['group'] = group
+    context['lesson_periods'] = lesson_periods
+
+    return render(request, 'alsijil/group_week.html', context)
-- 
GitLab