Esempio n. 1
0
    def _show(self):
        schemas = [
            ("Dataset", scheming_dataset_schemas()),
            ("Group", scheming_group_schemas()),
            ("Organization", scheming_organization_schemas()),
        ]

        for n, s in schemas:
            print n, "schemas:"
            if s is None:
                print "    plugin not loaded or schema not specified\n"
                continue
            if not s:
                print "    no schemas"
            for typ in sorted(s):
                print " * " + json.dumps(typ)
                field_names = ('dataset_fields', 'fields', 'resource_fields')

                for field_name in field_names:
                    if s[typ].get(field_name):
                        if field_name == 'resource_fields':
                            print " * " + json.dumps("resource")
                        for field in s[typ][field_name]:
                            print "   - " + json.dumps(field['field_name']),
                            print scheming_language_text(field.get('label'))
            print
Esempio n. 2
0
    def metadata_download(self, package_id):
        context = {
            'model': model,
            'session': model.Session,
            'user': p.toolkit.c.user
        }

        data_dict = {
            'id': package_id,
        }
        try:
            result = get_action('package_show')(context, data_dict)
        except (ObjectNotFound, NotAuthorized):
            abort(404, _('Package not found'))

        dataset_fields = helpers.scheming_get_dataset_schema(
            "dataset")['dataset_fields']
        if hasattr(response, u'headers'):
            response.headers['Content-Type'] = 'text/csv'
            response.headers['Content-disposition'] = \
            'attachment; filename="{name}-metadata.csv"'.format(name=package_id)

        f = StringIO.StringIO()
        wr = csv.writer(f, encoding='utf-8')

        header = ['Field', 'Value']
        wr.writerow(header)

        for field in dataset_fields:
            if field['field_name'] == 'tag_string':
                value = self.get_package_tags(result.get('tags'))
                wr.writerow(
                    [helpers.scheming_language_text(field['label']), value])
            elif field['field_name'] == 'owner_org':
                org_alias = str(
                    config.get('ckan.organization_alias', 'Organization'))
                wr.writerow([org_alias, result['organization']['title']])
            elif field['field_name'] == 'groups':
                group_alias = str(config.get('ckan.group_alias',
                                             'Group')) + 's'
                value = self.get_package_groups(result.get('groups'))
                wr.writerow([group_alias, value])
            elif helpers.scheming_field_choices(field):
                value = helpers.scheming_choices_label(
                    helpers.scheming_field_choices(field),
                    result.get(field['field_name']))
                wr.writerow(
                    [helpers.scheming_language_text(field['label']), value])
            else:
                wr.writerow([
                    helpers.scheming_language_text(field['label']),
                    result.get(field['field_name'])
                ])

        return f.getvalue()
Esempio n. 3
0
def fluent_form_label(field, lang):
    """
    Return a label for the input field for the given language
    If the field has a fluent_form_label defined the label will
    be taken from there.  If a matching label can't be found
    this helper will return the language code in uppercase and
    the standard label.
    """
    form_label = field.get('fluent_form_label', {})

    if lang in form_label:
        return scheming_language_text(form_label, lang)

    return scheming_language_text(field['label'], lang)
Esempio n. 4
0
def fluent_form_label(field, lang):
    """
    Return a label for the input field for the given language

    If the field has a fluent_form_label defined the label will
    be taken from there.  If a matching label can't be found
    this helper will return the language code in uppercase and
    the standard label.
    """
    form_label = field.get('fluent_form_label', {})

    if lang in form_label:
        return scheming_language_text(form_label[lang])

    return lang.upper() + ' ' + scheming_language_text(field['label'])
Esempio n. 5
0
 def test_no_user_lang(self, lang):
     lang.side_effect = Exception()
     assert_equals('hello',
                   scheming_language_text({
                       'en': 'hello',
                       'aa': 'aaaa'
                   }))
Esempio n. 6
0
 def test_matching_language(self):
     assert_equals(
         'hello',
         scheming_language_text({
             'en': 'hello',
             'aa': 'aaaa'
         },
                                prefer_lang='en'))
Esempio n. 7
0
 def test_first_when_no_matching_language(self):
     assert_equals(
         'hello',
         scheming_language_text({
             'aa': 'hello',
             'bb': 'no'
         },
                                prefer_lang='en'))
Esempio n. 8
0
    def validator(key, data, errors, context):
        """
        Return a value for a core field using a multilingual dict.
        """
        data[key] = fluent_text_output(data[key])

        k = key[-1]
        new_key = key[:-1] + (k[:-len(LANG_SUFFIX)],)

        if new_key in data:
            data[new_key] = scheming_language_text(data[key], config.get('ckan.locale_default', 'en'))
Esempio n. 9
0
    def validator(key, data, errors, context):
        """
        Return a value for a core field using a multilingual dict.
        """
        data[key] = fluent_text_output(data[key])

        k = key[-1]
        new_key = key[:-1] + (k[:-len(LANG_SUFFIX)],)

        if new_key in data:
            data[new_key] = scheming_language_text(data[key], config.get('ckan.locale_default', 'en'))
Esempio n. 10
0
    def before_view(self, pkg_dict):
        """
        Ensure that (if available) the correct language strings
        are used for core CKAN fields.
        """
        fields_to_fluent = (
            u'title',
            u'notes'
        )

        for field in fields_to_fluent:
            if field in pkg_dict and isinstance(pkg_dict[field], dict):
                pkg_dict[field] = scheming_language_text(pkg_dict[field])

        return pkg_dict
Esempio n. 11
0
    def before_view(self, pkg_dict):
        """
        Ensure that (if available) the correct language strings
        are used for core CKAN fields.
        """
        fields_to_fluent = (
            u'title',
            u'notes'
        )

        for field in fields_to_fluent:
            if field in pkg_dict and isinstance(pkg_dict[field], dict):
                pkg_dict[field] = scheming_language_text(pkg_dict[field])

        return pkg_dict
Esempio n. 12
0
 def test_only_one_language(self):
     assert_equals('hello', scheming_language_text(
         {'zh': 'hello'},
         prefer_lang='en'))
Esempio n. 13
0
 def test_pass_through_gettext(self):
     assert_equals('hello1', scheming_language_text(
         'hello', _gettext=lambda x: x + '1'))
Esempio n. 14
0
 def test_decodes_utf8(self):
     assert_equals(u'\xa1Hola!', scheming_language_text('\xc2\xa1Hola!'))
Esempio n. 15
0
 def test_pass_through_gettext(self, _):
     _.side_effect = lambda x: x + "1"
     assert "hello1" == scheming_language_text("hello")
Esempio n. 16
0
 def test_decodes_utf8(self):
     assert_equals(u'\xa1Hola!', scheming_language_text('\xc2\xa1Hola!'))
Esempio n. 17
0
 def test_pass_through_gettext(self, gettext):
     gettext.side_effect = lambda x: x + '1'
     assert_equals('hello1', scheming_language_text('hello'))
 def test_pass_through_gettext(self):
     assert_equals(
         'hello1',
         scheming_language_text('hello', _gettext=lambda x: x + '1'))
Esempio n. 19
0
 def test_matching_language(self):
     assert "hello" == scheming_language_text({
         "en": "hello",
         "aa": "aaaa"
     },
                                              prefer_lang="en")
Esempio n. 20
0
 def test_first_when_no_matching_language(self):
     assert "hello" == scheming_language_text({
         "aa": "hello",
         "bb": "no"
     },
                                              prefer_lang="en")
Esempio n. 21
0
 def test_decodes_utf8(self):
     assert u"\xa1Hola!" == scheming_language_text(six.b("\xc2\xa1Hola!"))
Esempio n. 22
0
 def test_no_user_lang(self, lang):
     lang.side_effect = TypeError()
     assert_equals('hello', scheming_language_text(
         {'en': 'hello', 'aa': 'aaaa'}))
Esempio n. 23
0
 def test_pass_through_gettext(self, _):
     _.side_effect = lambda x: x + '1'
     assert_equals('hello1', scheming_language_text('hello'))
Esempio n. 24
0
 def test_matching_language(self):
     assert_equals('hello', scheming_language_text(
         {'en': 'hello', 'aa': 'aaaa'},
         prefer_lang='en'))
Esempio n. 25
0
 def test_first_when_no_matching_language(self):
     assert_equals('hello', scheming_language_text(
         {'aa': 'hello', 'bb': 'no'},
         prefer_lang='en'))
Esempio n. 26
0
 def test_only_one_language(self):
     assert_equals(
         'hello', scheming_language_text({'zh': 'hello'}, prefer_lang='en'))
Esempio n. 27
0
 def test_only_one_language(self):
     assert "hello" == scheming_language_text({"zh": "hello"},
                                              prefer_lang="en")
Esempio n. 28
0
 def test_no_user_lang(self, lang):
     lang.side_effect = TypeError()
     assert "hello" == scheming_language_text({"en": "hello", "aa": "aaaa"})