Create Author mixin

This commit is contained in:
Ross
2024-02-05 09:34:47 +00:00
parent 1e16386e1e
commit 9e06cb45ca
5 changed files with 35 additions and 59 deletions
+2 -11
View File
@@ -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("<a href='{}'>{}</a>", self.get_absolute_url(), str(self))
+25 -1
View File
@@ -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
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
+8 -32
View File
@@ -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
-10
View File
@@ -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()]
-5
View File
@@ -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()])