Commits

igoryan  committed 1a6ec8d

Added Field layout element

  • Participants
  • Parent commits c656afa

Comments (0)

Files changed (1)

File thelayouts/layouts_base.py

 
 __all__ = [
     "CannotResolveElementException", "LayoutElement", "CallableElement",
-    "Script", "Container", "Column", "Layout", "Row",
+    "Script", "Container", "Column", "Layout", "Row", "Field",
 ]
 
 
         self._is_present = attrs.pop("is_present", True)
         self.element_attrs = attrs.pop("element_attrs", {})
         self.help = attrs.pop('help', None)
+        self.label_text = attrs.pop('label_text', u"")
         self.attrs = attrs
         # Before elements come LayoutElements they are stored in 
         # self.unresolved_child_elements
         """Get title for this layout element."""
         return self.title
 
+    def get_label_text(self):
+        return self.label_text
+
     def iterate_child_elements(self):
         """Iterate child elements yielding LayoutElement instances."""
         for element in self.resolved_child_elements:
             literal(u"<div style='clear: both;'></div>"),
             super(Row, self).render_container_end(),
         ))
+
+
+class Field(LayoutElement):
+    
+    def __init__(
+        self, field_name, parent_layout_element=None, hint_text=None, 
+        warning_text=None, **attrs
+    ):
+        assert isinstance(field_name, basestring), type(field_name)
+        super(Field, self).__init__(**attrs)
+        self.name = field_name
+        #self.short_name = field_name
+        self.hint_text = hint_text
+        self.parent_layout_element = proxy(parent_layout_element) \
+            if parent_layout_element is not None else None
+        self.warning_text = warning_text
+
+    def get_title(self):
+        return self.label.text
+
+    def get_help(self):
+        return self.form.helps[self.field_name]
+
+    @property
+    def id(self):
+        return getattr(self.form, self.name).id
+
+    @property
+    def form_field(self):
+        return getattr(self.form, self.name)
+
+    def get_flags(self):
+        return self.form_field.flags
+
+    def get_parent_layout_element_label_text(self):
+        if not self.parent_layout_element:
+            return u""
+        return self.parent_layout_element.get_label_text()
+
+    @property
+    def label(self):
+        has_parent_label = self.get_parent_layout_element_label_text() is not None
+        return Label(
+            getattr(self.form, self.name).label.text if not has_parent_label \
+                else self.parent_layout_element.label_text, 
+            self.id, 
+            required=self.get_flags().required,
+        )
+
+    @property
+    def errors(self):
+        return getattr(self.form, self.name).errors
+
+    def get_hint_text(self):
+        return (
+            self.hint_text or 
+            getattr(self.form, 'hints', {}).get(self.name, u'')
+        )
+
+    @property
+    def inline_hint_text(self):
+        return getattr(self.form, 'inline_hints', {}).get(self.name, u'')
+
+    def get_help(self):
+        return self.help or getattr(self.form, 'helps', {}).get(self.name, u'')
+
+
+    def render(self, **kwargs):
+        if not self.is_present():
+            return ""
+        attrs = getattr(self.form, 'widgets_attrs', {}).get(self.name, {}).copy()
+        attrs.update(self.attrs)
+        attrs.update(kwargs)
+        return self.form_field(**attrs)
+
+    def render_errors(self):
+        return ErrorList(self.id, self.errors).render()
+
+    # def is_boolean_field(self):
+    #     from wtforms import fields
+    #     return isinstance(self.form_field, fields.BooleanField)