Create Author mixin
This commit is contained in:
+25
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user