from django.db import models from django.contrib.auth.models import User from django.contrib.contenttypes.fields import GenericRelation from .pendingModeration import PendingModeration from shared import OBJ_MODERATION_PERMISSIONS from shared import DEFAULT_OBJ_MODERATION_LV from django.core.validators import MinValueValidator from django.core.exceptions import ValidationError oml = DEFAULT_OBJ_MODERATION_LV POSSIBLE_OBJ_MODER_LV = [OBJ_MODERATION_PERMISSIONS[key] for key in OBJ_MODERATION_PERMISSIONS] def validate_obj_model_lv(value): if value not in POSSIBLE_OBJ_MODER_LV: raise ValidationError('obj_moderation_level not recognized') class MyModel(models.Model): """ All models in the app deppend of this one. It contains the required attributes for managing eventual moderation data. All the logic behind moderation is done in myModelSerializer """ moderated_by = models.ForeignKey( User, null=True, on_delete=models.SET_NULL, related_name='+') moderated_on = models.DateTimeField(null=True) updated_on = models.DateTimeField(null=True) updated_by = models.ForeignKey( User, null=True, on_delete=models.SET_NULL, related_name='+') obj_moderation_level = models.SmallIntegerField( default=oml, validators=[MinValueValidator(0), validate_obj_model_lv]) pending_moderation = GenericRelation(PendingModeration) class Meta: abstract = True model_config = { "moderation_level": None, }