From 9e06cb45ca8fe698be8801fbd50e0a79ae21c748 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 5 Feb 2024 09:34:47 +0000 Subject: [PATCH] Create Author mixin --- atlas/models.py | 13 ++----------- generic/mixins.py | 26 +++++++++++++++++++++++++- generic/models.py | 40 ++++++++-------------------------------- longs/models.py | 10 ---------- rapids/models.py | 5 ----- 5 files changed, 35 insertions(+), 59 deletions(-) diff --git a/atlas/models.py b/atlas/models.py index d77d3413..a67bb3d9 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -4,6 +4,7 @@ import pathlib from typing import Tuple from django.http import Http404, HttpRequest +from generic.mixins import AuthorMixin from rad.settings import REMOTE_URL from django.db.models.fields.files import ImageField from django.db.models.fields.related import ForeignKey @@ -273,7 +274,7 @@ class Structure(SynMixin, models.Model): @reversion.register -class Case(models.Model): +class Case(models.Model, AuthorMixin): # class SubspecialtyChoices(models.TextChoices): # BREAST = "BR", _("Breast") # CARDIAC = "CA", _("Cardiac") @@ -369,16 +370,6 @@ class Case(models.Model): def get_absolute_url(self): return reverse("atlas:case_detail", kwargs={"pk": self.pk}) - def get_authors(self): - """Returns a comma seperated text list of authors""" - authors = ", ".join([i.username for i in self.author.all()]) - return authors - - 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_link(self): # return f"{self.pk}: {self.title}" return format_html("{}", self.get_absolute_url(), str(self)) diff --git a/generic/mixins.py b/generic/mixins.py index 875aa998..d0d8f4d5 100644 --- a/generic/mixins.py +++ b/generic/mixins.py @@ -1,6 +1,30 @@ from django.contrib.auth.mixins import UserPassesTestMixin +from django.contrib.auth.models import User +from django.db import models class SuperuserRequiredMixin(UserPassesTestMixin): def test_func(self): - return self.request.user.is_superuser \ No newline at end of file + return self.request.user.is_superuser + +class AuthorMixin(): + """Mixin class for models that have authors + + requires an author many to many field to be defined on the derived class + + """ + author: models.ManyToManyField[User] # Is this correct typing? + + def get_author_objects(self) -> list[User]: + """Returns list of author objects""" + authors = [i for i in self.author.all()] + return authors + + def get_authors(self): + """Returns a comma separated text list of authors as usernames. + + """ + authors = ", ".join([i.username for i in self.author.all()]) + if not authors: + return "None" + return authors diff --git a/generic/models.py b/generic/models.py index 2c20f382..45ac6d75 100644 --- a/generic/models.py +++ b/generic/models.py @@ -27,6 +27,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 helpers.images import get_image_hash, pretty_print_dicom from rad.settings import REMOTE_URL from django.utils.html import format_html @@ -37,6 +38,7 @@ from easy_thumbnails.exceptions import InvalidImageFormatError import pydicom import dicognito.anonymizer +from django.contrib.auth.models import User def findMiddle(input_list): @@ -47,6 +49,8 @@ def findMiddle(input_list): return input_list[int(middle)] + + class Modality(models.Model): modality = models.CharField(max_length=200, unique=True) short_code = models.CharField(max_length=5, unique=True, null=True) @@ -152,7 +156,7 @@ class Site(models.Model): return self.short_code -class QuestionBase(models.Model): +class QuestionBase(models.Model, AuthorMixin): authors_only = models.BooleanField( help_text="If true only question authors will be able to view.", default=False, @@ -181,11 +185,6 @@ class QuestionBase(models.Model): """If this makes sense in the question/answer context override""" return None - def get_author_objects(self): - """Returns a comma seperated text list of authors""" - authors = [i for i in self.author.all()] - return authors - class SeriesImageBase(models.Model): """ @@ -519,7 +518,7 @@ class SeriesBase(models.Model): pass -class ExamOrCollectionGenericBase(models.Model): +class ExamOrCollectionGenericBase(models.Model, AuthorMixin): """Holds functions that relate to both case and other exams e.g. user management @@ -556,18 +555,6 @@ class ExamOrCollectionGenericBase(models.Model): class Meta: abstract = True - 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 - - def get_author_objects(self): - """Returns a comma seperated text list of authors""" - authors = [i for i in self.author.all()] - return authors - def check_user_can_edit(self, user: User): if user.is_superuser: return True @@ -1518,7 +1505,7 @@ def create_user_profile(sender, instance, created, **kwargs): def save_user_profile(sender, instance, **kwargs): instance.userprofile.save() -class ExamCollection(models.Model): +class ExamCollection(models.Model, AuthorMixin): name = models.CharField(max_length=255) date = models.DateField(blank=True,null=True) @@ -1531,15 +1518,4 @@ class ExamCollection(models.Model): 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 + \ No newline at end of file diff --git a/longs/models.py b/longs/models.py index 6cecd68f..2bd56383 100644 --- a/longs/models.py +++ b/longs/models.py @@ -136,16 +136,6 @@ class Long(QuestionBase): def get_absolute_url(self): return reverse("longs:question_detail", kwargs={"pk": self.pk}) - def get_authors(self): - """Returns a comma seperated text list of authors""" - authors = ", ".join([i.username for i in self.author.all()]) - return authors - - def get_author_objects(self): - """Returns a comma seperated text list of authors""" - authors = [i for i in self.author.all()] - return authors - def __str__(self): return "{} / {}".format(self.pk, self.description) examinations = [series.get_examination() for series in self.series.all()] diff --git a/rapids/models.py b/rapids/models.py index 738a56ce..81ecf677 100644 --- a/rapids/models.py +++ b/rapids/models.py @@ -229,11 +229,6 @@ class Rapid(QuestionBase): def get_absolute_url(self): return reverse("rapids:question_detail", kwargs={"pk": self.pk}) - def get_authors(self): - """Returns a comma seperated text list of authors""" - authors = ", ".join([i.username for i in self.author.all()]) - return authors - def get_regions(self): """Returns a comma seperated text list of regions""" regions = ", ".join([i.name for i in self.region.all()])