start taking more advantage of class based views

This commit is contained in:
Ross
2024-03-11 11:08:35 +00:00
parent 78e4ed871e
commit 0cb38f5c31
27 changed files with 244 additions and 104 deletions
+24 -1
View File
@@ -8,6 +8,29 @@ class SuperuserRequiredMixin(UserPassesTestMixin):
def test_func(self):
return self.request.user.is_superuser
class CheckCanEditMixin():
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
print(self.object)
print("1234", self.object.check_user_can_edit(self.request.user))
context["can_edit"] = self.object.check_user_can_edit(self.request.user)
return context
class QuestionMixin():
def get_app_name(self):
raise NotImplementedError("You must implement get_app_name in the derived class")
def get_base_template(self):
return f"{self.get_app_name()}/base.html"
def get_link_headers(self):
return f"{self.get_app_name()}/question_link_header.html"
class AuthorMixin():
"""Mixin class for models that have authors
@@ -15,7 +38,7 @@ class AuthorMixin():
"""
author = models.ManyToManyField(User) # Corrected typing
def get_author_objects(self) -> List[User]: # Updated type hint
"""Returns list of author objects"""
authors = [i for i in self.author.all()]
+11 -2
View File
@@ -26,7 +26,7 @@ from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from generic.mixins import AuthorMixin
from generic.mixins import AuthorMixin, QuestionMixin
from helpers.images import get_image_hash, pretty_print_dicom
from rad.settings import REMOTE_URL
from django.utils.html import format_html
@@ -154,7 +154,7 @@ class Site(models.Model):
return self.short_code
class QuestionBase(models.Model, AuthorMixin):
class QuestionBase(models.Model, AuthorMixin, QuestionMixin):
authors_only = models.BooleanField(
help_text="If true only question authors will be able to view.",
default=False,
@@ -580,6 +580,15 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
class Meta:
abstract = True
def get_app_name(self):
raise NotImplementedError("You must implement get_app_name in the derived class")
def get_base_template(self):
return f"{self.get_app_name()}/exams.html"
def get_link_headers(self):
return "generic/exam_link_headers.html"
def check_user_can_edit(self, user: User):
if user.is_superuser:
return True
@@ -0,0 +1,10 @@
<br/>
{% if can_edit %}
<a href="{% url exam.get_app_name|add:':exam_update' exam.id %}" title="Edit the Exam">Edit</a>
\ <a href="{% url exam.get_app_name|add:':exam_delete' exam.id %}" title="Delete the Exam">Delete</a>
\ <a href="{% url exam.get_app_name|add:':exam_clone' exam.id %}" title="Clone the Exam">Clone</a>
\ <a href="{% url exam.get_app_name|add:':exam_authors' exam.id %}" title="Edit Exam Authors">Authors</a>
{% endif %}
{% if request.user.is_superuser %}
\ <a href="{% url 'admin:'|add:exam.get_app_name|add:'_exam_change' exam.id %}" title="Edit the Exam using the admin interface">Admin Edit</a>
{% endif %}
@@ -1,12 +1,4 @@
{% if can_edit %}
<a href="{% url app_name|add:':exam_update' exam.id %}" title="Edit the Exam">Edit</a>
\ <a href="{% url app_name|add:':exam_delete' exam.id %}" title="Delete the Exam">Delete</a>
\ <a href="{% url app_name|add:':exam_clone' exam.id %}" title="Clone the Exam">Clone</a>
\ <a href="{% url app_name|add:':exam_authors' exam.id %}" title="Edit Exam Authors">Authors</a>
{% endif %}
{% if request.user.is_superuser %}
\ <a href="{% url 'admin:'|add:app_name|add:'_exam_change' exam.id %}" title="Edit the Exam using the admin interface">Admin Edit</a>
{% endif %}
{% include "generic/exam_link_headers.html" %}
<h1>Exam: {{ exam.name }}</h1>
{% include 'exam_notes.html' %}
@@ -15,7 +7,7 @@
<div class="alert alert-warning" role="alert">
Exam JSON may be out of date.
<a href="{% url app_name|add:':exam_json_recreate' pk=exam.pk %}">Click here to force refresh</a>
<a href="{% url exam.get_app_name|add:':exam_json_recreate' pk=exam.pk %}">Click here to force refresh</a>
</div>
{% endif %}
@@ -44,15 +36,15 @@ Exam mode: {{ exam.exam_mode }}<br />
Open access: {{ exam.open_access }}<br />
<div class="parent-help" title="Click to enable / disable the exam">
Exam active: <input type="checkbox" id="exam-active-switch" {% if exam.active %}checked{% endif %} data-posturl="{% url app_name|add:':exam_toggle_active' pk=exam.pk %}"> <span class="help-text">[When checked the exam will be available to take in the test system]</span>
Exam active: <input type="checkbox" id="exam-active-switch" {% if exam.active %}checked{% endif %} data-posturl="{% url exam.get_app_name|add:':exam_toggle_active' pk=exam.pk %}"> <span class="help-text">[When checked the exam will be available to take in the test system]</span>
</div>
{% if exam.exam_mode %}
<div class="parent-help" title="Click to enable / disable the exam results">
Publish results: <input type="checkbox" id="exam-publish-results-switch" data-posturl="{% url app_name|add:':exam_toggle_results_published' pk=exam.pk %}"
Publish results: <input type="checkbox" id="exam-publish-results-switch" data-posturl="{% url exam.get_app_name|add:':exam_toggle_results_published' pk=exam.pk %}"
{% if exam.publish_results %}checked{% endif %}> <span class="help-text">[When checked the exam results will be available to users on this site]</span>
</div>
{% if app_name == "anatomy" or app_name == "rapids" or app_name == "longs" %}
<p><a href="{% url app_name|add:':mark_overview' pk=exam.pk %}"><button>Mark exam</button></a></p>
{% if exam.get_app_name == "anatomy" or exam.get_app_name == "rapids" or exam.get_app_name == "longs" %}
<p><a href="{% url exam.get_app_name|add:':mark_overview' pk=exam.pk %}"><button>Mark exam</button></a></p>
{% endif %}
{% endif %}