from django import forms


class SchemaField(forms.CharField):
    """
    Custom field to accept a comma and line delimited
    text input of a schema definition
    """
    def parse_schema_entries(self, text):
        lines = [line for line in text.split('\n')]
        schema = {}

        for line in lines:
            parts = line.split(',')
            if len(parts) >= 2:
                key, value = parts[0].strip(), parts[1].strip()
                if key and value:
                    if key in schema:
                        msg = "Schema keys must be unique, more than one %s defined" % key
                        raise forms.ValidationError(msg)
                    schema[key] = value

        return schema

    def to_python(self, value):
        if value:
            value = value.strip()
        if not value:
            return {}
        return self.parse_schema_entries(value)

@@ -160,24 +160,382 @@
 ion%0A    %22%22%22%0A
+%0A    def validate_key_value_pairs(self, key, value, schema):%0A        if not(key and value):%0A            msg = %22Must provide both a column name and a column type%22%0A            raise forms.ValidationError(msg)%0A        if key in schema:%0A            msg = %22Schema keys must be unique, more than one %25s defined%22 %25 key%0A            raise forms.ValidationError(msg)%0A%0A
     def pars
@@ -816,216 +816,58 @@
     
-if key and value:%0A                    if key in schema:%0A                        msg = %22Schema keys must be unique, more than one %25s defined%22 %25 key%0A                        raise forms.ValidationError(msg)%0A    
+self.validate_key_value_pairs(key, value, schema)%0A
     
@@ -898,17 +898,16 @@
 = value%0A
-%0A
         

Raising an error if duplicate column names are provided.