validation of URL fields
[plstackapi.git] / planetstack / core / models / plcorebase.py
index 782737c..3bceb08 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,22 @@ 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")
+            if field.__class__.__name__=="URLField":
+                l.append("url")
+            validators[field.name] = l
+        return validators
+
 class PlCoreBase(models.Model): # , DiffModelMixIn):
     objects = PlCoreBaseManager()
     deleted_objects = PlCoreBaseDeletionManager()
@@ -117,6 +134,23 @@ 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")
+            if field.__class__.__name__=="URLField":
+                l.append("url")
+            validators[field.name] = l
+        return validators
+
     # ---- end copy stuff from DiffModelMixin ----
 
     # default values for created and updated are only there to keep evolution
@@ -203,7 +237,9 @@ class PlCoreBase(models.Model): # , DiffModelMixIn):
 
     @classmethod
     def is_ephemeral(cls):
-       return False
+       return cls in ephemeral_models
+
+