Create Author mixin
This commit is contained in:
+2
-11
@@ -4,6 +4,7 @@ import pathlib
|
|||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
from django.http import Http404, HttpRequest
|
from django.http import Http404, HttpRequest
|
||||||
|
from generic.mixins import AuthorMixin
|
||||||
from rad.settings import REMOTE_URL
|
from rad.settings import REMOTE_URL
|
||||||
from django.db.models.fields.files import ImageField
|
from django.db.models.fields.files import ImageField
|
||||||
from django.db.models.fields.related import ForeignKey
|
from django.db.models.fields.related import ForeignKey
|
||||||
@@ -273,7 +274,7 @@ class Structure(SynMixin, models.Model):
|
|||||||
|
|
||||||
|
|
||||||
@reversion.register
|
@reversion.register
|
||||||
class Case(models.Model):
|
class Case(models.Model, AuthorMixin):
|
||||||
# class SubspecialtyChoices(models.TextChoices):
|
# class SubspecialtyChoices(models.TextChoices):
|
||||||
# BREAST = "BR", _("Breast")
|
# BREAST = "BR", _("Breast")
|
||||||
# CARDIAC = "CA", _("Cardiac")
|
# CARDIAC = "CA", _("Cardiac")
|
||||||
@@ -369,16 +370,6 @@ class Case(models.Model):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse("atlas:case_detail", kwargs={"pk": self.pk})
|
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):
|
def get_link(self):
|
||||||
# return f"{self.pk}: {self.title}"
|
# return f"{self.pk}: {self.title}"
|
||||||
return format_html("<a href='{}'>{}</a>", self.get_absolute_url(), str(self))
|
return format_html("<a href='{}'>{}</a>", self.get_absolute_url(), str(self))
|
||||||
|
|||||||
@@ -1,6 +1,30 @@
|
|||||||
from django.contrib.auth.mixins import UserPassesTestMixin
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
class SuperuserRequiredMixin(UserPassesTestMixin):
|
class SuperuserRequiredMixin(UserPassesTestMixin):
|
||||||
def test_func(self):
|
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
|
||||||
|
|||||||
+7
-31
@@ -27,6 +27,7 @@ from django.contrib.auth.models import User
|
|||||||
|
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
from generic.mixins import AuthorMixin
|
||||||
from helpers.images import get_image_hash, pretty_print_dicom
|
from helpers.images import get_image_hash, pretty_print_dicom
|
||||||
from rad.settings import REMOTE_URL
|
from rad.settings import REMOTE_URL
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
@@ -37,6 +38,7 @@ from easy_thumbnails.exceptions import InvalidImageFormatError
|
|||||||
|
|
||||||
import pydicom
|
import pydicom
|
||||||
import dicognito.anonymizer
|
import dicognito.anonymizer
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
|
||||||
def findMiddle(input_list):
|
def findMiddle(input_list):
|
||||||
@@ -47,6 +49,8 @@ def findMiddle(input_list):
|
|||||||
return input_list[int(middle)]
|
return input_list[int(middle)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Modality(models.Model):
|
class Modality(models.Model):
|
||||||
modality = models.CharField(max_length=200, unique=True)
|
modality = models.CharField(max_length=200, unique=True)
|
||||||
short_code = models.CharField(max_length=5, unique=True, null=True)
|
short_code = models.CharField(max_length=5, unique=True, null=True)
|
||||||
@@ -152,7 +156,7 @@ class Site(models.Model):
|
|||||||
return self.short_code
|
return self.short_code
|
||||||
|
|
||||||
|
|
||||||
class QuestionBase(models.Model):
|
class QuestionBase(models.Model, AuthorMixin):
|
||||||
authors_only = models.BooleanField(
|
authors_only = models.BooleanField(
|
||||||
help_text="If true only question authors will be able to view.",
|
help_text="If true only question authors will be able to view.",
|
||||||
default=False,
|
default=False,
|
||||||
@@ -181,11 +185,6 @@ class QuestionBase(models.Model):
|
|||||||
"""If this makes sense in the question/answer context override"""
|
"""If this makes sense in the question/answer context override"""
|
||||||
return None
|
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):
|
class SeriesImageBase(models.Model):
|
||||||
"""
|
"""
|
||||||
@@ -519,7 +518,7 @@ class SeriesBase(models.Model):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ExamOrCollectionGenericBase(models.Model):
|
class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
|
||||||
"""Holds functions that relate to both case and other exams
|
"""Holds functions that relate to both case and other exams
|
||||||
|
|
||||||
e.g. user management
|
e.g. user management
|
||||||
@@ -556,18 +555,6 @@ class ExamOrCollectionGenericBase(models.Model):
|
|||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
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):
|
def check_user_can_edit(self, user: User):
|
||||||
if user.is_superuser:
|
if user.is_superuser:
|
||||||
return True
|
return True
|
||||||
@@ -1518,7 +1505,7 @@ def create_user_profile(sender, instance, created, **kwargs):
|
|||||||
def save_user_profile(sender, instance, **kwargs):
|
def save_user_profile(sender, instance, **kwargs):
|
||||||
instance.userprofile.save()
|
instance.userprofile.save()
|
||||||
|
|
||||||
class ExamCollection(models.Model):
|
class ExamCollection(models.Model, AuthorMixin):
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
|
|
||||||
date = models.DateField(blank=True,null=True)
|
date = models.DateField(blank=True,null=True)
|
||||||
@@ -1532,14 +1519,3 @@ class ExamCollection(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name} [{self.date}]"
|
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
|
|
||||||
@@ -136,16 +136,6 @@ class Long(QuestionBase):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse("longs:question_detail", kwargs={"pk": self.pk})
|
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):
|
def __str__(self):
|
||||||
return "{} / {}".format(self.pk, self.description)
|
return "{} / {}".format(self.pk, self.description)
|
||||||
examinations = [series.get_examination() for series in self.series.all()]
|
examinations = [series.get_examination() for series in self.series.all()]
|
||||||
|
|||||||
@@ -229,11 +229,6 @@ class Rapid(QuestionBase):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse("rapids:question_detail", kwargs={"pk": self.pk})
|
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):
|
def get_regions(self):
|
||||||
"""Returns a comma seperated text list of regions"""
|
"""Returns a comma seperated text list of regions"""
|
||||||
regions = ", ".join([i.name for i in self.region.all()])
|
regions = ", ".join([i.name for i in self.region.all()])
|
||||||
|
|||||||
Reference in New Issue
Block a user