# -*- coding: utf-8 -*-

from cmsplugin_text_ng import exceptions

_registry = {}

def register_type(type_name, model_class):
    from cmsplugin_text_ng.models import TextNGVariableBase
    if type_name in _registry:
        if _registry[type_name] == model_class:
            # already registered
            return
        else:
            raise exceptions.VariableTypeAlreadyRegistered(
                'The type "%s" is already registered by %s' % (type_name, _registry[type_name].__name__)
            )
    if not issubclass(model_class, TextNGVariableBase):
        raise exceptions.InvalidType('%s is not a subclass of TextNGVariableBase' % model_class.__name__)
    # TODO: validate that there is a "value" field that is nullable
    _registry[type_name] = model_class

def get_type(type_name):
    return _registry[type_name]

def get_type_list():
    return _registry.values()
@@ -16,16 +16,63 @@
 tf-8 -*-
+%0Afrom django.db.models import FieldDoesNotExist
 %0A%0Afrom c
@@ -720,71 +720,301 @@
     
-# TODO: validate that there is a %22value%22 field that
+try:%0A        field = model_class._meta.get_field_by_name('value')%5B0%5D%0A    except FieldDoesNotExist:%0A        raise exceptions.InvalidType('%25s does not define a %22value%22 field' %25 model_class.__name__)%0A    if not field.null:%0A        raise exceptions.InvalidType('%22value%22 field of %25s
  is 
+not 
 nullable
 %0A   
@@ -1009,16 +1009,41 @@
 nullable
+' %25 model_class.__name__)
 %0A    _re

validate that a variable type defines a nullable "value" field