def __new__(cls, name, bases, attrs): # get all valid existing Fields and sort them fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # get all Fields from base classes for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields # add the fields as "our" base fields attrs['base_fields'] = SortedDict(fields) # Meta class available? if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \ issubclass(attrs['Meta'].document, BaseDocument): doc_fields = SortedDict() formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \ MongoFormFieldGenerator)() # walk through the document fields for field_name, field in iter_valid_fields(attrs['Meta']): # add field and override clean method to respect mongoengine-validator doc_fields[field_name] = formfield_generator.generate(field_name, field) if doc_fields[field_name] is not None: doc_fields[field_name].clean = mongoengine_validate_wrapper( doc_fields[field_name].clean, field._validate) # write the new document fields to base_fields doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields # sorting with respect to Meta.fields (like django forms) def get_indexes(x, y): meta_field_list = getattr(attrs['Meta'], 'fields') try: return meta_field_list.index(x[0]), meta_field_list.index(y[0]) except ValueError: return None, None if 'Meta' in attrs and hasattr(attrs['Meta'], 'fields'): flds = attrs['base_fields'].items() flds.sort(lambda x, y: cmp(*get_indexes(x,y))) attrs['base_fields']=SortedDict(flds) # maybe we need the Meta class later attrs['_meta'] = attrs.get('Meta', object()) return super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs)
def __new__(cls, name, bases, attrs): # get all valid existing Fields and sort them fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # get all Fields from base classes for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields # add the fields as "our" base fields # discards fields specified for embeddeddocuments attrs['base_fields'] = SortedDict([f for f in fields if '__' not in f[0]]) # Meta class available? if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \ issubclass(attrs['Meta'].document, BaseDocument): labels = getattr(attrs['Meta'], 'labels', {}) doc_fields = SortedDict() meta_fields = list(getattr(attrs['Meta'], 'fields', [])) formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \ MongoFormFieldGenerator)(meta_fields, overriden_fields=fields, exclude=getattr(attrs['Meta'], 'exclude', ())) overriden_fields = dict(fields) # walk through the document fields for field_name, field in iter_valid_fields(attrs['Meta']): if field_name == '_cls': continue # add field and override clean method to respect mongoengine-validator if field_name in overriden_fields: doc_fields[field_name] = overriden_fields[field_name] else: doc_fields[field_name] = formfield_generator.generate(field_name, field) doc_fields[field_name].clean = mongoengine_validate_wrapper( doc_fields[field_name].clean, field._validate, field.required) if labels.get(field_name): doc_fields[field_name].label = labels[field_name] # write the new document fields to base_fields doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields # maybe we need the Meta class later attrs['_meta'] = attrs.get('Meta', object()) return super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs)
def __new__(cls, name, bases, attrs): # get all valid existing Fields and sort them fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # get all Fields from base classes for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields # add the fields as "our" base fields attrs['base_fields'] = SortedDict(fields) # Meta class available? if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \ issubclass(attrs['Meta'].document, BaseDocument): doc_fields = SortedDict() formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \ MongoFormFieldGenerator)() dict_fields = dict(fields) # walk through the document fields for field_name, field in iter_valid_fields(attrs['Meta']): # add field and override clean method to respect mongoengine-validator if field_name in dict_fields: doc_fields[field_name] = dict_fields[field_name] else: doc_fields[field_name] = formfield_generator.generate(field_name, field) doc_fields[field_name].clean = mongoengine_validate_wrapper( doc_fields[field_name].clean, field._validate) # write the new document fields to base_fields doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields # maybe we need the Meta class later attrs['_meta'] = attrs.get('Meta', object()) return super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs)