def prepare_template_vars(self, name_prefix, data): '''Prepare data for the template. The markup for the object will be generated here, i.e before the actual template is called to render(). ''' tpl_vars = FieldWidget.prepare_template_vars(self, name_prefix, data) field, value = self.field, self.value qname = tpl_vars.get('qname') # If value is None, pass an empty (but structured) object created # from the default factory if value is None: factory = Object.Factory(self.field.schema) value = factory() # Build the template context for the object's widget: some variables # must be moved from current field context to the object's context data1 = {} for k in ['title', 'description', 'required', 'readonly']: v = tpl_vars.pop(k, None) if v: data1[k] = v # Generate markup for the object, feed to the current template context qa = self.context.requested_action markup = markup_for_object(qa, value, errors=self.errors, name_prefix=qname, data=data1) tpl_vars['content'] = { 'markup': markup } return tpl_vars
def _test_markup_for_object(self, fixture_name, action, data={}): '''Render an object widget''' obj = getattr(fixtures, fixture_name) markup = markup_for_object(action, obj, name_prefix=fixture_name, data=data) log1.info('Generated %s markup for object %r:\n%s' %(action, obj, markup)) assert markup pq = pyquery.PyQuery(unicode(markup)) assert pq assert pq.is_('div')
def _test_markup_for_object(self, fixture_name, action, data={}): '''Render an object widget''' obj = getattr(fixtures, fixture_name) markup = markup_for_object(action, obj, name_prefix=fixture_name, data=data) log1.info('Generated %s markup for object %r:\n%s' % (action, obj, markup)) assert markup pq = pyquery.PyQuery(unicode(markup)) assert pq assert pq.is_('div')
def show_foo(self, id='foo1'): '''Grab a Foo fixture and show it with the requested format ''' obj = getattr(fixtures, id) assert isinstance(obj, types.FooMetadata) read_action = 'read' f = request.params.get('f') if f: read_action = 'read:%s' %(f) c.markup = markup_for_object( str(read_action), obj, name_prefix='a.foo1', data={ 'title': u'Foo %s' % (id) }) return render('tests/page.html')
def show_foo(self, id='foo1'): '''Grab a Foo fixture and show it with the requested format ''' obj = getattr(fixtures, id) assert isinstance(obj, types.FooMetadata) read_action = 'read' f = request.params.get('f') if f: read_action = 'read:%s' % (f) c.markup = markup_for_object(str(read_action), obj, name_prefix='a.foo1', data={'title': u'Foo %s' % (id)}) return render('tests/page.html')
def edit_foo(self, id='foo1'): '''Grab a Foo fixture and present an edit form ''' obj = getattr(fixtures, id) assert isinstance(obj, types.FooMetadata) errors = obj.validate(dictize_errors=True) # Examine POSTed data if request.method == 'POST': # Parse request, filter-out empty values d = dict(filter(lambda t: t[1], request.params.items())) # Create a factory for this factory = Object.Factory(schemata.IFooMetadata, opts={ 'unserialize-keys': True, 'unserialize-values': True, }) obj = factory(d, is_flat=True) errors = obj.validate() if not errors: # Output a JSON dump of a valid object response.headers['Content-Type'] = 'application/json' out = {'status': 'success', 'obj': obj.to_dict()} return to_json(out) else: # Prepare error dict for display errors = obj.dictize_errors(errors) #response.headers['Content-Type'] = 'application/json' #out = { 'status': 'failure', 'errors': errors, 'obj': obj.to_dict() } #return to_json(out) # Display form c.form_class = 'form-horizontal' c.form_errors = errors c.form_markup = markup_for_object('edit:datasetform', obj, errors=errors, name_prefix='', data={'title': u'Foo %s' % (id)}) return render('tests/form.html')
def edit_foo(self, id='foo1'): '''Grab a Foo fixture and present an edit form ''' obj = getattr(fixtures, id) assert isinstance(obj, types.FooMetadata) errors = obj.validate(dictize_errors=True) # Examine POSTed data if request.method == 'POST': # Parse request, filter-out empty values d = dict(filter(lambda t: t[1], request.params.items())) # Create a factory for this factory = Object.Factory(schemata.IFooMetadata, opts={ 'unserialize-keys': True, 'unserialize-values': True, }) obj = factory(d, is_flat=True) errors = obj.validate() if not errors: # Output a JSON dump of a valid object response.headers['Content-Type'] = 'application/json' out = { 'status': 'success', 'obj': obj.to_dict() } return to_json(out) else: # Prepare error dict for display errors = obj.dictize_errors(errors) #response.headers['Content-Type'] = 'application/json' #out = { 'status': 'failure', 'errors': errors, 'obj': obj.to_dict() } #return to_json(out) # Display form c.form_class = 'form-horizontal' c.form_errors = errors c.form_markup = markup_for_object('edit:datasetform', obj, errors = errors, name_prefix = '', data = { 'title': u'Foo %s' % (id) } ) return render('tests/form.html')
def get_objects_markup(self): markup = '' c.form_sections = [] # 1. A Point object obj = fixtures.pt1 assert isinstance(obj, types.Point) data = { 'required': False, 'classes': [], 'input_classes': ['input-small'], 'title': u'Point A', } c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>Point</code></h3>'), 'body': \ markup_for_object('edit:baz', obj, name_prefix='pt1', data=data) + \ toolkit.literal('<hr/>') + \ markup_for_object('read:boz', obj, name_prefix='pt1', data={'title': u'Point B'}) }) # 2.1 A TemporalExtent object obj = fixtures.dt1 assert isinstance(obj, types.TemporalExtent) c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>TemporalExtent</code></h3>'), 'body': markup_for_object('edit:faz.baz', obj, name_prefix='dt1', data={'title': u'Extent A'}) + toolkit.literal('<hr/>') + markup_for_object( 'read', obj, name_prefix='dt1', data={'title': u'Extent B'}) }) # 2.2 A TemporalExtent object (with errors) obj = types.TemporalExtent(start=datetime.date(2014, 1, 1), end=datetime.date(2013, 1, 1)) errs = obj.validate() errs = obj.dictize_errors(errs) assert isinstance(obj, types.TemporalExtent) c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>TemporalExtent</code></h3>'), 'body': \ markup_for_object('edit:faz.baz', obj, errors=errs, name_prefix='dt1', data={'title': u'Extent A'}) + \ toolkit.literal('<hr/>') + \ markup_for_object('read', obj, errors=errs, name_prefix='dt1', data={ 'title': u'Extent B' }) }) # 3. A PostalAddress object obj = types.PostalAddress(address=u'22 Acacia Avenue', postalcode=u'12345') c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>PostalAddress</code></h3>'), 'body': \ markup_for_object('edit:comfortable', obj, name_prefix='contact_info', data={'title': u'Address A'}) + \ toolkit.literal('<hr/>') + \ markup_for_object('read', obj, name_prefix='contact_info', data={'title': u'Address B'}) }) # Render c.form_class = 'form-horizontal' return render('tests/accordion-form.html')
def get_objects_markup(self): markup = '' c.form_sections = [] # 1. A Point object obj = fixtures.pt1 assert isinstance(obj, types.Point) data = { 'required': False, 'classes': [], 'input_classes': [ 'input-small' ], 'title': u'Point A', } c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>Point</code></h3>'), 'body': \ markup_for_object('edit:baz', obj, name_prefix='pt1', data=data) + \ toolkit.literal('<hr/>') + \ markup_for_object('read:boz', obj, name_prefix='pt1', data={'title': u'Point B'}) }) # 2.1 A TemporalExtent object obj = fixtures.dt1 assert isinstance(obj, types.TemporalExtent) c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>TemporalExtent</code></h3>'), 'body': markup_for_object('edit:faz.baz', obj, name_prefix='dt1', data={'title': u'Extent A'}) + toolkit.literal('<hr/>') + markup_for_object('read', obj, name_prefix='dt1', data={ 'title': u'Extent B' }) }) # 2.2 A TemporalExtent object (with errors) obj = types.TemporalExtent( start=datetime.date(2014, 1, 1), end=datetime.date(2013, 1, 1)) errs = obj.validate() errs = obj.dictize_errors(errs) assert isinstance(obj, types.TemporalExtent) c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>TemporalExtent</code></h3>'), 'body': \ markup_for_object('edit:faz.baz', obj, errors=errs, name_prefix='dt1', data={'title': u'Extent A'}) + \ toolkit.literal('<hr/>') + \ markup_for_object('read', obj, errors=errs, name_prefix='dt1', data={ 'title': u'Extent B' }) }) # 3. A PostalAddress object obj = types.PostalAddress(address=u'22 Acacia Avenue', postalcode=u'12345') c.form_sections.append({ 'heading': toolkit.literal('<h3>Object <code>PostalAddress</code></h3>'), 'body': \ markup_for_object('edit:comfortable', obj, name_prefix='contact_info', data={'title': u'Address A'}) + \ toolkit.literal('<hr/>') + \ markup_for_object('read', obj, name_prefix='contact_info', data={'title': u'Address B'}) }) # Render c.form_class = 'form-horizontal' return render('tests/accordion-form.html')
def _format(self, obj, opts): qualifier = opts.get('q') qual_action = 'read:%s' % (qualifier) if qualifier else 'read' return markup_for_object(qual_action, obj, name_prefix='')