Commits

Stefan Seelmann committed eaa9f70

Save URLField and EmailField to GAE native data types Link and Email.

  • Participants
  • Parent commits 49bb2e1

Comments (0)

Files changed (3)

                         'None')
                 else:
                     value = value.id()
-            elif db_type == 'text':
+            elif db_type in ('text', 'gae_email', 'gae_link') :
                 if value.name() is None:
                     raise DatabaseError('Wrong type for Key. Expected string, found'
                         'None')
 
         if db_type == 'gae_key':
             return value
+        elif db_type == 'gae_link':
+            value = Link(value)
+        elif db_type == 'gae_email':
+            value = Email(value)
         elif db_type == 'longtext':
             # long text fields cannot be indexed on GAE so use GAE's database
             # type Text
         self.internal_type = internal_type
 
     def __mod__(self, field):
+        # Hack to save URLField and EmailField to the native
+        # GAE data types Link and Email.
+        # Overriding the data_types dictionary has no effect because the
+        # get_internal_type() is not overridden in URLField and EmailField
+        # and returns "CharField". Hence the Field.db_type() method returns
+        # the mapping of the CharField. 
+        field_type = field['model']._meta.get_field(field['name'])
+        field_type_class_name = field_type.__class__.__name__
+        if field_type_class_name == 'URLField':
+            return 'gae_link'
+        elif field_type_class_name == 'EmailField':
+            return 'gae_email'
+        
         indexes = get_indexes().get(field['model'], {})
         if field['name'] in indexes.get('indexed', ()):
             return 'text'

tests/field_db_conversion.py

         for name, gae_db_type in [('long_text', Text),
                 ('indexed_text', unicode), ('xml', Text),
                 ('text', unicode), ('ip_address', unicode), ('slug', unicode),
-                ('email', unicode),('comma_seperated_integer', unicode),
-                ('url', unicode), ('time', datetime.datetime),
+                ('email', Email),('comma_seperated_integer', unicode),
+                ('url', Link), ('time', datetime.datetime),
                 ('datetime', datetime.datetime), ('date', datetime.datetime),
                 ('floating_point', float), ('boolean', bool),
                 ('null_boolean', bool), ('integer', (int, long)),