def _test_markup_for_field(self, fixture_name, k, action, data={}):
     '''Render a field widget'''
     
     qa = QualAction.from_string(action)
     x = getattr(fixtures, fixture_name)
     f = x.get_field(k)
     
     errs = x.validate(dictize_errors=True)
     
     markup = markup_for_field(str(qa), f,
         errors=errs, name_prefix=fixture_name, data=data)
     log1.info('Generated %s markup for %r:\n%s' % (qa, f, markup))
     assert markup
     canonicalized_markup = to_c14n_markup(markup, pretty=1)
     log1.info('Generated %s markup for %r (tidy-ed):\n%s' % (
         qa, f, to_tidy_markup(markup)))
     
     pq = pyquery.PyQuery(unicode(markup))
     assert pq
     assert pq.is_('.field-qname-%s\\.%s' %(fixture_name, k))
     assert pq.is_('.field-%s-widget' %(qa.action))
     
     if qa.action == 'edit':
         e = pq.find('input') or pq.find('textarea') or pq.find('select')
         if not e:
             # This can only happen on empty container fields that provide a 
             # javascript template for editing
             assert (
                 isinstance(f, (DictField, ListField))
                 and pq.find('script[type="x-template-mustache"]'))
         else:
             # This is a normal input and should have a proper id and name
             assert e.attr('name').startswith('%s.%s' %(fixture_name, k))
             assert e.attr('id').startswith('input-%s.%s' %(fixture_name, k))
 def _format(self, value, opts):
     field = self.field
     if not field.context:
         field = field.bind(FieldContext(key=field.__name__, value=value))
     qualifier = opts.get('q')
     qual_action = 'read:%s' % (qualifier) if qualifier else 'read'
     return markup_for_field(qual_action, field, name_prefix='')
예제 #3
0
 def render_item_template():
     yf = field.value_type.bind(FieldContext(key='{{key}}', value=None))
     yd = self.get_item_template_vars(key=None)
     return {
         'variables': ['key', 'title'],
         'markup': to_c14n_markup(
             markup_for_field(qa, yf, name_prefix=qname, data=yd),
             with_comments=False)
     }
예제 #4
0
 def render_item(i, y):
     yf = field.value_type.bind(FieldContext(key=i, value=y))
     ye = error_dict.get(i) if error_dict else None
     yd = self.get_item_template_vars(index=i)
     return {
         'index': i,
         'markup': markup_for_field(
             qa, yf, errors=ye, name_prefix=qname, data=yd)
     }
예제 #5
0
    def get_fields_markup(self):
        if request.method == 'POST':
            d = dict(request.params.items())
            response.headers['Content-Type'] = 'application/json' 
            return json.dumps(d)

        x = fixtures.foo1
        S = x.get_schema()
        test_fields = {
            'url': { 'title': u'Website URL' },
            'rating': { 'title': u'Foo Rating' },
            'grade': { 'title': u'Foo Grade' },
            'contacts': { 'title': u'Contacts', },
            'title': {
                'required': True,
                'classes': [ 'control-medium' ],
                'title': u'Title',
                'description': u'Blah blah',
                'placeholder': u'Enter a title',
                'attrs': { 'data-foo': 'baz', 'data-boo': 'faz', 'autocomplete': 'off' }
            },
            'temporal_extent': { 'title': u'Temporal Extent', },
            'reviewed': { 'title': u'Reviewed', },
            'description': { 'description': u'Add a detailed description', },
            'thematic_category': {},
            'tags': {},
            'created': { 'title': u'Created At', 'placeholder': datetime.datetime.now() },
            'wakeup_time': { 'title': u'Wakeup At',},
            'password': {},
        }
        c.form_sections = []
        for k, data in test_fields.items():
            f = x.get_field(k)
            c.form_sections.append({
                'heading': toolkit.literal('<h3>Field <code>%s</code></h3>' %(k)),
                'body': \
                    markup_for_field('edit', f, name_prefix='foo1', data=data) + \
                    toolkit.literal('<hr/>') + \
                    markup_for_field('read:bar', f, name_prefix='foo1', data=data)
            })
        #raise Exception('Break')
        c.form_class = 'form-horizontal' # 'form-horizontal'
        return render('tests/accordion-form.html')
예제 #6
0
 def render_item(k, y):
     yf = field.value_type.bind(FieldContext(key=k, value=y))
     term = terms.get(k)
     ye = error_dict.get(k) if error_dict else None
     yd = self.get_item_template_vars(key=k, term=term)
     return {
         'key': k,
         'key_term': term,
         'markup': markup_for_field(
             qa, yf, errors=ye, name_prefix=qname, data=yd),
     }
예제 #7
0
 def render_field(k, qf):
     f = obj.get_field(k)
     if not qf:
         qf = f.queryTaggedValue('widget-qualifier')
     if not qf:
         qf = self.context.provided_action.qualifier
     qa = QualAction(self.action, qualifier=qf)
     ef = error_dict.get(k) if error_dict else None
     df = field_data.get(k, {})
     mf = markup_for_field(qa, f, 
         errors=ef, name_prefix=name_prefix, data=df)
     return dict(field=f, markup=mf)
예제 #8
0
    def show_metadata(self, id):
        '''Show dataset's metadata formatted as a table'''

        from ckanext.publicamundi.lib.metadata import formatter_for_field
        from ckanext.publicamundi.lib.metadata import fields, bound_field

        context = {'model': model, 'session': model.Session}
        try:
            pkg_dict = toolkit.get_action('package_show')(context, {'id': id})
        except toolkit.ObjectNotFound as ex:
            abort(404)

        dtype = pkg_dict['dataset_type']
        obj = pkg_dict[dtype]

        q = str(toolkit.request.params.get('q', ''))
        title_field = bound_field(fields.TextField(), 'title',
                                  pkg_dict['title'])
        data = {
            'title':
            u'%s: Metadata' % (pkg_dict['title']),
            'max_depth':
            1,
            'extras': [{
                'key': 'language',
                'title': _('Source Language'),
                'value': Language(pkg_dict['language']).name,
            }, {
                'key':
                'title',
                'title':
                _('Title'),
                'value':
                markup_for_field('read:dd%s' % (('.' + q) if q else ''),
                                 title_field,
                                 name_prefix='',
                                 data={'translatable': True})
            }],
        }

        qa = 'read:table.%s' % (q) if q else 'read:table'
        c.markup = markup_for(qa, obj, name_prefix=dtype, data=data)

        return render('tests/page.html')
예제 #9
0
 def get_field_markup(self, id):
     x = getattr(fixtures, id)
    
     if request.method == 'POST':
         response.headers['Content-Type'] = 'application/json' 
         out = { tuple(k.split('.')): v for k, v in request.params.items() } 
         out = unflatten(out)
         return to_json(out)
     else:
         params = dict(request.params)
         k = params.pop('field', 'title')
         action = params.pop('action', 'edit')
         prefix = params.pop('prefix', id)
         
         field = x.get_field(k)
         field_markup = markup_for_field(str(action),
             field, name_prefix=str(prefix), errors=None, data=params)
         
         return render('tests/field.html', extra_vars={ 
             'field_markup': field_markup })
예제 #10
0
    def show_metadata(self, id):
        '''Show dataset's metadata formatted as a table'''
        
        from ckanext.publicamundi.lib.metadata import formatter_for_field
        from ckanext.publicamundi.lib.metadata import fields, bound_field
        
        context = { 'model': model, 'session': model.Session }
        try:
            pkg_dict = toolkit.get_action('package_show')(context, { 'id': id })
        except toolkit.ObjectNotFound as ex:  
            abort(404)
        
        dtype = pkg_dict['dataset_type']
        obj = pkg_dict[dtype]

        q = str(toolkit.request.params.get('q', ''))
        title_field = bound_field(fields.TextField(), 'title', pkg_dict['title'])
        data = {
            'title': u'%s: Metadata' % (pkg_dict['title']),
            'max_depth': 1,
            'extras': [
                {
                    'key': 'language',
                    'title': _('Source Language'), 
                    'value': Language(pkg_dict['language']).name, 
                },
                {
                    'key': 'title',
                    'title': _('Title'),
                    'value': markup_for_field('read:dd%s' % (('.' + q) if q else ''),
                        title_field, name_prefix='', data={'translatable': True})
                }
            ],
        }
        
        qa = 'read:table.%s' %(q) if q else 'read:table' 
        c.markup = markup_for(qa, obj, name_prefix=dtype, data=data)

        return render('tests/page.html')
예제 #11
0
    def get_field_markup(self, id):
        x = getattr(fixtures, id)

        if request.method == 'POST':
            response.headers['Content-Type'] = 'application/json'
            out = {tuple(k.split('.')): v for k, v in request.params.items()}
            out = unflatten(out)
            return to_json(out)
        else:
            params = dict(request.params)
            k = params.pop('field', 'title')
            action = params.pop('action', 'edit')
            prefix = params.pop('prefix', id)

            field = x.get_field(k)
            field_markup = markup_for_field(str(action),
                                            field,
                                            name_prefix=str(prefix),
                                            errors=None,
                                            data=params)

            return render('tests/field.html',
                          extra_vars={'field_markup': field_markup})
예제 #12
0
    def _test_markup_for_field(self, fixture_name, k, action, data={}):
        '''Render a field widget'''

        qa = QualAction.from_string(action)
        x = getattr(fixtures, fixture_name)
        f = x.get_field(k)

        errs = x.validate(dictize_errors=True)

        markup = markup_for_field(str(qa),
                                  f,
                                  errors=errs,
                                  name_prefix=fixture_name,
                                  data=data)
        log1.info('Generated %s markup for %r:\n%s' % (qa, f, markup))
        assert markup
        canonicalized_markup = to_c14n_markup(markup, pretty=1)
        log1.info('Generated %s markup for %r (tidy-ed):\n%s' %
                  (qa, f, to_tidy_markup(markup)))

        pq = pyquery.PyQuery(unicode(markup))
        assert pq
        assert pq.is_('.field-qname-%s\\.%s' % (fixture_name, k))
        assert pq.is_('.field-%s-widget' % (qa.action))

        if qa.action == 'edit':
            e = pq.find('input') or pq.find('textarea') or pq.find('select')
            if not e:
                # This can only happen on empty container fields that provide a
                # javascript template for editing
                assert (isinstance(f, (DictField, ListField))
                        and pq.find('script[type="x-template-mustache"]'))
            else:
                # This is a normal input and should have a proper id and name
                assert e.attr('name').startswith('%s.%s' % (fixture_name, k))
                assert e.attr('id').startswith('input-%s.%s' %
                                               (fixture_name, k))
예제 #13
0
    def get_fields_markup(self):
        if request.method == 'POST':
            d = dict(request.params.items())
            response.headers['Content-Type'] = 'application/json'
            return json.dumps(d)

        x = fixtures.foo1
        S = x.get_schema()
        test_fields = {
            'url': {
                'title': u'Website URL'
            },
            'rating': {
                'title': u'Foo Rating'
            },
            'grade': {
                'title': u'Foo Grade'
            },
            'contacts': {
                'title': u'Contacts',
            },
            'title': {
                'required': True,
                'classes': ['control-medium'],
                'title': u'Title',
                'description': u'Blah blah',
                'placeholder': u'Enter a title',
                'attrs': {
                    'data-foo': 'baz',
                    'data-boo': 'faz',
                    'autocomplete': 'off'
                }
            },
            'temporal_extent': {
                'title': u'Temporal Extent',
            },
            'reviewed': {
                'title': u'Reviewed',
            },
            'description': {
                'description': u'Add a detailed description',
            },
            'thematic_category': {},
            'tags': {},
            'created': {
                'title': u'Created At',
                'placeholder': datetime.datetime.now()
            },
            'wakeup_time': {
                'title': u'Wakeup At',
            },
            'password': {},
        }
        c.form_sections = []
        for k, data in test_fields.items():
            f = x.get_field(k)
            c.form_sections.append({
                'heading': toolkit.literal('<h3>Field <code>%s</code></h3>' %(k)),
                'body': \
                    markup_for_field('edit', f, name_prefix='foo1', data=data) + \
                    toolkit.literal('<hr/>') + \
                    markup_for_field('read:bar', f, name_prefix='foo1', data=data)
            })
        #raise Exception('Break')
        c.form_class = 'form-horizontal'  # 'form-horizontal'
        return render('tests/accordion-form.html')