From cfb90be686f426d966861ea00433d662871c5c13 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 29 Jan 2024 10:52:02 +0000 Subject: [PATCH] add some tests and views --- generic/admin.py | 4 ++- .../migrations/0012_examcollection_author.py | 20 +++++++++++ generic/models.py | 20 +++++++++++ generic/templates/generic/base.html | 3 +- .../generic/examcollection_base.html | 9 +++++ .../generic/examcollection_detail.html | 15 ++++++++ .../generic/examcollection_list.html | 20 +++++++++++ generic/tests.py | 34 ++++++++++++++++++- generic/urls.py | 10 ++++++ generic/views.py | 6 ++++ rapids/tests/test_urls.py | 7 ++-- 11 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 generic/migrations/0012_examcollection_author.py create mode 100644 generic/templates/generic/examcollection_base.html create mode 100755 generic/templates/generic/examcollection_detail.html create mode 100755 generic/templates/generic/examcollection_list.html diff --git a/generic/admin.py b/generic/admin.py index b444062b..d1f81ddf 100644 --- a/generic/admin.py +++ b/generic/admin.py @@ -17,6 +17,7 @@ from .models import ( UserGrades, UserProfile, Supervisor, + ExamCollection, Site ) @@ -37,4 +38,5 @@ admin.site.register(CidUserGroup) admin.site.register(UserProfile) admin.site.register(UserGrades) admin.site.register(Supervisor) -admin.site.register(Modality) \ No newline at end of file +admin.site.register(Modality) +admin.site.register(ExamCollection) \ No newline at end of file diff --git a/generic/migrations/0012_examcollection_author.py b/generic/migrations/0012_examcollection_author.py new file mode 100644 index 00000000..d30fafc9 --- /dev/null +++ b/generic/migrations/0012_examcollection_author.py @@ -0,0 +1,20 @@ +# Generated by Django 4.1.4 on 2024-01-29 10:11 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('generic', '0011_examcollection'), + ] + + operations = [ + migrations.AddField( + model_name='examcollection', + name='author', + field=models.ManyToManyField(blank=True, help_text='Author/Manager(s) of the exam collection', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/generic/models.py b/generic/models.py index 268320ba..2c20f382 100644 --- a/generic/models.py +++ b/generic/models.py @@ -1523,3 +1523,23 @@ class ExamCollection(models.Model): date = models.DateField(blank=True,null=True) + author = models.ManyToManyField( + settings.AUTH_USER_MODEL, + blank=True, + help_text="Author/Manager(s) of the exam collection", + ) + + def __str__(self): + return f"{self.name} [{self.date}]" + + def get_author_objects(self): + """Returns a comma seperated text list of authors""" + authors = [i for i in self.author.all()] + return authors + + def get_authors(self): + """Returns a comma seperated text list of authors""" + authors = ", ".join([i.username for i in self.author.all()]) + if not authors: + return "None" + return authors \ No newline at end of file diff --git a/generic/templates/generic/base.html b/generic/templates/generic/base.html index 5fb8a198..559b5139 100755 --- a/generic/templates/generic/base.html +++ b/generic/templates/generic/base.html @@ -22,7 +22,8 @@ Cid Groups / User Groups / Manage Users / - Manage Supervisors + Manage Supervisors / + Collections {% endif %} {% endblock %} diff --git a/generic/templates/generic/examcollection_base.html b/generic/templates/generic/examcollection_base.html new file mode 100644 index 00000000..ee20e988 --- /dev/null +++ b/generic/templates/generic/examcollection_base.html @@ -0,0 +1,9 @@ +{% extends 'generic/base.html' %} + + +{% block navigation %} + {{block.super}} +
Test + {% comment %} Collections / {% endcomment %} + +{% endblock %} \ No newline at end of file diff --git a/generic/templates/generic/examcollection_detail.html b/generic/templates/generic/examcollection_detail.html new file mode 100755 index 00000000..512ea833 --- /dev/null +++ b/generic/templates/generic/examcollection_detail.html @@ -0,0 +1,15 @@ +{% extends 'generic/examcollection_base.html' %} + +{% block content %} + +

{{ object.name }} [{{object.date}}]

+ +{{object.anatomy_exams}} + + + +{% endblock %} + +{% block js %} + +{% endblock %} diff --git a/generic/templates/generic/examcollection_list.html b/generic/templates/generic/examcollection_list.html new file mode 100755 index 00000000..7c6553d8 --- /dev/null +++ b/generic/templates/generic/examcollection_list.html @@ -0,0 +1,20 @@ +{% extends 'generic/examcollection_base.html' %} + +{% block content %} + +

Exam Collections

+ + + + +{% endblock %} + +{% block js %} + +{% endblock %} diff --git a/generic/tests.py b/generic/tests.py index 7ce503c2..c6f1a752 100644 --- a/generic/tests.py +++ b/generic/tests.py @@ -1,3 +1,35 @@ from django.test import TestCase -# Create your tests here. +import pytest +from django.urls import reverse + +from bs4 import BeautifulSoup + +from generic.models import ExamCollection + +import datetime + +class TestExamCollections: + def test_list_url(self, db, client): + # Test with no content + response = client.get(reverse("generic:exam_collection_list")) + assert response.status_code == 200 + soup = BeautifulSoup(response.content, "html.parser") + assert "No exam collections yet" in soup.text + + # No content yet so this should fail + response = client.get(reverse("generic:exam_collection_detail", args=( 1, ))) + assert response.status_code == 404 + + collection = ExamCollection.objects.create(name="test collection", date=datetime.date.today()) + + response = client.get(reverse("generic:exam_collection_list")) + assert response.status_code == 200 + soup = BeautifulSoup(response.content, "html.parser") + assert collection.name in soup.find("li", class_="collection").text + + + response = client.get(reverse("generic:exam_collection_detail", args=( collection.pk, ))) + assert response.status_code == 200 + soup = BeautifulSoup(response.content, "html.parser") + assert collection.name in soup.find("h1").text \ No newline at end of file diff --git a/generic/urls.py b/generic/urls.py index d0b915f0..d37e0189 100755 --- a/generic/urls.py +++ b/generic/urls.py @@ -162,6 +162,16 @@ urlpatterns = [ views.SupervisorDelete.as_view(), name="supervisor_delete", ), + path( + "exam_collection/", + views.ExamCollectionList.as_view(), + name="exam_collection_list", + ), + path( + "exam_collection//", + views.ExamCollectionDetail.as_view(), + name="exam_collection_detail", + ), ] diff --git a/generic/views.py b/generic/views.py index 93efd295..5350fefe 100644 --- a/generic/views.py +++ b/generic/views.py @@ -73,6 +73,7 @@ from .models import ( CidUser, CidUserGroup, ExamBase, + ExamCollection, ExamUserStatus, Examination, QuestionNote, @@ -3255,6 +3256,11 @@ class SupervisorCreate(CidManagerRequiredMixin, CreateView): class SupervisorList(CidManagerRequiredMixin, ListView): model = Supervisor +class ExamCollectionList(ListView): + model = ExamCollection + +class ExamCollectionDetail(DetailView, AuthorRequiredMixin): + model = ExamCollection class ExaminationView(SuperuserRequiredMixin, SingleTableMixin, FilterView): model = Examination diff --git a/rapids/tests/test_urls.py b/rapids/tests/test_urls.py index 137f41f1..277e282b 100644 --- a/rapids/tests/test_urls.py +++ b/rapids/tests/test_urls.py @@ -1,7 +1,5 @@ import json import pytest - -# from django.contrib.auth.models import User from django.urls import reverse from rich.pretty import pprint @@ -361,4 +359,7 @@ def test_cookies(db, client): # Check our 2 cookies are displayed (we would have to add them for this to work) #assert "csrftoken" in [i.text for i in soup.find_all("th")] - #assert "sessionid" in [i.text for i in soup.find_all("th")] \ No newline at end of file + #assert "sessionid" in [i.text for i in soup.find_all("th")] + + +