validators for xoslib
[plstackapi.git] / planetstack / core / models / plcorebase.py
index e483a79..dee8a87 100644 (file)
@@ -7,6 +7,7 @@ from django.forms.models import model_to_dict
 from django.utils import timezone
 from django.core.exceptions import PermissionDenied
 import model_policy
+from model_autodeletion import ephemeral_models
 
 try:
     # This is a no-op if observer_disabled is set to 1 in the config file
@@ -82,6 +83,20 @@ class DiffModelMixIn:
     def get_field_diff(self, field_name):
         return self.diff.get(field_name, None)
 
+    #classmethod
+    def getValidators(cls):
+        """ primarily for REST API, return a dictionary of field names mapped
+            to lists of the type of validations that need to be applied to
+            those fields.
+        """
+        validators = {}
+        for field in cls._meta.fields:
+            l = []
+            if field.blank==False:
+                l.append("notBlank")
+            validators[field.name] = l
+        return validators
+
 class PlCoreBase(models.Model): # , DiffModelMixIn):
     objects = PlCoreBaseManager()
     deleted_objects = PlCoreBaseDeletionManager()
@@ -117,6 +132,21 @@ class PlCoreBase(models.Model): # , DiffModelMixIn):
 
     def get_field_diff(self, field_name):
         return self.diff.get(field_name, None)
+
+    #classmethod
+    def getValidators(cls):
+        """ primarily for REST API, return a dictionary of field names mapped
+            to lists of the type of validations that need to be applied to
+            those fields.
+        """
+        validators = {}
+        for field in cls._meta.fields:
+            l = []
+            if field.blank==False:
+                l.append("notBlank")
+            validators[field.name] = l
+        return validators
+
     # ---- end copy stuff from DiffModelMixin ----
 
     # default values for created and updated are only there to keep evolution
@@ -201,6 +231,12 @@ class PlCoreBase(models.Model): # , DiffModelMixIn):
         # filtering of visible objects by user.
         return cls.objects.all()
 
+    @classmethod
+    def is_ephemeral(cls):
+       return cls in ephemeral_models
+
+
+