from functools import cached_property from django.contrib.auth.models import User from django.db import models from lib.core.db.models.mixins import DateAware class SharedPermissionBase(DateAware, models.Model): class Meta: abstract = True user = models.ForeignKey(User, on_delete=models.CASCADE) view = models.BooleanField(default=True) change = models.BooleanField(default=False) delete = models.BooleanField(default=False) add = models.BooleanField(default=False) # item = models.IntegerField(default=NotImplementedError('Must be defined in inheriting class')) @cached_property def perms(self): perms = filter(None, [ 'read' if self.view else False, 'change' if self.change else False, 'delete' if self.delete else False, 'add' if self.add else False, ]) return list(perms) def __str__(self): return f'{self.user.username} ({", ".join(self.perms)})'