Example #1
0
	def _create_field(self, attribute, parent_class, doinlines = True, prefix = None):
		"""
		Creates a HTML form field for the given attribute.
		Always returns a List.
		
		@param attribute: the attribute for which a FormField shall be created
		@param parent_class: class of the parent-Resource of the attribute
		@param doinlines:  do inlines? NOT doin' lines ...
		@param prefix: prefix
		@return: a list of FormField+ or []
		@raise NotImplementedError: if the attribute type is not supported yet.
		"""
		name = attribute.name
		label = attribute.label
		if prefix:
			name = prefix + "." + name
		# check the type
		if isinstance(attribute.type, type):
			# PASSWORD
			if issubclass(attribute.type, Password) or (parent_class is Ptm and attribute.name in ("provider", "owner")):
				return []
			# ENUM
			if attribute.enum:
				return [ SelectField(name, choices = [ (x, x) for x in attribute.enum ], label = label ) ]
			# STRING
			if issubclass(attribute.type, basestring):
				if name == "email":
					return [ EmailField(name, **self._get_field_args(attribute, parent_class)) ]
				return [ StringField(name, **self._get_field_args(attribute, parent_class)) ]
			# BOOLEAN
			if issubclass(attribute.type, bool):
				return [ BooleanField(name, label = attribute.label) ]
			# INT, LONG
			if issubclass(attribute.type, (int, long)):
				return [ NumberField(name, **self._get_field_args(attribute, parent_class)) ]
			# ENTITY
			if issubclass(attribute.type, Entity):
				if attribute.wrapper_for:
					a_type = attribute.type
					f_dict = a_type.get_field_dict()
					if isinstance(attribute.wrapper_for, (tuple, list, set, frozenset)):
						return [ self._create_field(f_dict[a], a_type, prefix = name)  for a in attribute.wrapper_for ]
					return self._create_field(f_dict[attribute.wrapper_for], a_type, prefix = name)
				if attribute.shared:
					return [ SelectField(name, choices = None, object_factory = self.object_factory, entity_class = attribute.type, **self._get_field_args(attribute, parent_class)) ]
				if attribute.display_inline and doinlines:
					return [ self._make_complex_field(attribute) ]
				return []
		else:
			#return StringField(name)
			return []
		# probably wont happen because of the else				
		raise NotImplementedError(attribute.type)
Example #2
0
	def _make_complex_field(self, attribute):
		fields = []
		assert issubclass(attribute.type, Entity)
		for a in attribute.type.get_fields():
			f = self._create_field(a, attribute.type, doinlines = False)
			if f is not None:
				fields.append(f)
		return ComplexField(attribute.name, fields = fields, hidden_fields = [ NumberField("id", type = "hidden") ], entity_class = attribute.type, object_factory = self.object_factory)