Пример #1
0
    def test_lang_list_long_ordering(self):
        """messages - preserving language order"""
        lang_list_long = messages.language_list_long()

        # Preliminary test: same number of languages in both lists
        self.assertEqual(len(lang_list_long), len(cfg['CFG_SITE_LANGS']))

        for lang, cfg_lang in zip(lang_list_long, cfg['CFG_SITE_LANGS']):
            self.assertEqual(lang[0], cfg_lang)
Пример #2
0
 def get_locale_value():
     """Returns all the available languages"""
     sm_locale_code = '''<select name="sm_locale">'''
     sm_locale_code += '''<option value='en'>English (default)</option>'''
     langs = language_list_long(True)
     for lang in langs:
         if lang[0] != 'en': # we already added English as default
             sm_locale_code += '''<option value='%(lang_short)s'>%(lang_long)s</option>''' \
                               % {'lang_short': lang[0], 'lang_long': lang[1]}
     sm_locale_code += '''</select>'''
     return sm_locale_code
Пример #3
0
 def get_locale_value():
     """Returns all the available languages"""
     sm_locale_code = '''<select name="sm_locale">'''
     sm_locale_code += '''<option value='en'>English (default)</option>'''
     langs = language_list_long(True)
     for lang in langs:
         if lang[0] != 'en':  # we already added English as default
             sm_locale_code += '''<option value='%(lang_short)s'>%(lang_long)s</option>''' \
                               % {'lang_short': lang[0], 'lang_long': lang[1]}
     sm_locale_code += '''</select>'''
     return sm_locale_code
Пример #4
0
    def test_lang_list_long_ordering(self):
        """messages - preserving language order"""
        lang_list_long = messages.language_list_long()

        # Preliminary test: same number of languages in both lists
        self.assertEqual(len(lang_list_long),
                         len(cfg['CFG_SITE_LANGS']))


        for lang, cfg_lang in zip(lang_list_long,
                                  cfg['CFG_SITE_LANGS']):
            self.assertEqual(lang[0],
                             cfg_lang)
Пример #5
0
def manage_collection(name):
    """Manage collection."""
    collection = Collection.query.filter(
        Collection.name == name).first_or_404()
    form = CollectionForm(request.form, obj=collection)

    # gets the collections translations
    translations = dict((x.ln, x.value) for x in collection.collection_names)

    # Creating the translations form
    TranslationsFormFilled = TranslationsForm(language_list_long(),
                                              translations)
    translation_form = TranslationsFormFilled(request.form)

    return render_template('search/admin_collection.html',
                           collection=collection, form=form,
                           translation_form=translation_form)
Пример #6
0
def manage_collection(name):
    """Manage collection."""
    collection = Collection.query.filter(
        Collection.name == name).first_or_404()
    form = CollectionForm(request.form, obj=collection)

    # gets the collections translations
    translations = dict((x.ln, x.value) for x in collection.collection_names)

    # Creating the translations form
    TranslationsFormFilled = TranslationsForm(language_list_long(),
                                              translations)
    translation_form = TranslationsFormFilled(request.form)

    return render_template('search/admin_collection.html',
                           collection=collection, form=form,
                           translation_form=translation_form)
Пример #7
0
def manage_collection(name):
    collection = Collection.query.filter(Collection.name == name).first_or_404()
    form = CollectionForm(request.form, obj=collection)

    # gets the collections translations
    translations = dict((x.ln, x.value) for x in collection.collection_names)

    # Creating the translations form
    TranslationsFormFilled = TranslationsForm(language_list_long(), translations)
    translation_form = TranslationsFormFilled(request.form)
    # for x in  collection.collection_names:
    #    translation_form[x.ln](default = x.value)

    # translation_form.populate_obj(translations)

    return render_template(
        "search/admin_collection.html", collection=collection, form=form, translation_form=translation_form
    )
Пример #8
0
    def tmpl_language_selection_box(self, req, language=CFG_SITE_LANG):
        """Take URLARGS and LANGUAGE and return textual language
           selection box for the given page.

           Parameters:

          - 'req' - The mod_python request object

          - 'language' *string* - The selected language

        """

        # load the right message language
        _ = gettext_set_language(language)

        # Work on a copy in order not to bork the arguments of the caller
        argd = {}
        if req and req.args:
            argd.update(cgi.parse_qs(req.args))

        parts = []

        for (lang, lang_namelong) in language_list_long():
            if lang == language:
                parts.append('<span class="langinfo">%s</span>' %
                             lang_namelong)
            else:
                # Update the 'ln' argument in the initial request
                argd['ln'] = lang
                if req and req.uri:
                    args = urllib.quote(
                        req.uri, '/:?') + make_canonical_urlargd(argd, {})
                else:
                    args = ""
                parts.append(
                    create_html_link(args, {}, lang_namelong,
                                     {'class': "langinfo"}))
        if len(parts) > 1:
            return _("This site is also available in the following languages:") + \
                 "<br />" + ' &nbsp;'.join(parts)
        else:
            ## There is only one (or zero?) languages configured,
            ## so there so need to display language alternatives.
            return ""
Пример #9
0
    def tmpl_language_selection_box(self, req, language=CFG_SITE_LANG):
        """Take URLARGS and LANGUAGE and return textual language
           selection box for the given page.

           Parameters:

          - 'req' - The mod_python request object

          - 'language' *string* - The selected language

        """

        # load the right message language
        _ = gettext_set_language(language)

        # Work on a copy in order not to bork the arguments of the caller
        argd = {}
        if req and req.args:
            argd.update(cgi.parse_qs(req.args))

        parts = []

        for (lang, lang_namelong) in language_list_long():
            if lang == language:
                parts.append('<span class="langinfo">%s</span>' % lang_namelong)
            else:
                # Update the 'ln' argument in the initial request
                argd['ln'] = lang
                if req and req.uri:
                    args = urllib.quote(req.uri, '/:?') + make_canonical_urlargd(argd, {})
                else:
                    args = ""
                parts.append(create_html_link(args,
                                              {}, lang_namelong,
                                              {'class': "langinfo"}))
        if len(parts) > 1:
            return _("This site is also available in the following languages:") + \
                 "<br />" + ' &nbsp;'.join(parts)
        else:
            ## There is only one (or zero?) languages configured,
            ## so there so need to display language alternatives.
            return ""
Пример #10
0
def update_translations(id):
    """Update translations if the value is altered or not void."""
    collection = Collection.query.filter(Collection.id == id).first_or_404()

    for (lang, lang_long) in language_list_long():

        collection_name = Collectionname.query.filter(
            db.and_(Collectionname.id_collection == id, Collectionname.ln == lang, Collectionname.type == "ln")
        ).first()

        if collection_name:
            if collection_name.value != request.form.get(lang):
                collection_name.value = request.form.get(lang)
                db.session.commit()
        else:
            if request.form.get(lang) != "":
                collection_name = Collectionname(collection, lang, "ln", request.form.get(lang))
                db.session.add(collection_name)
                db.session.commit()

    flash(_("Collection was updated on n languages:"), "info")
    return redirect(url_for(".manage_collection", name=collection.name))
Пример #11
0
def update_translations(id):
    """Update translations if the value is altered or not void."""
    collection = Collection.query.filter(Collection.id == id).first_or_404()

    for (lang, lang_long) in language_list_long():
        collection_name = Collectionname.query.filter(
            db.and_(Collectionname.id_collection == id,
                    Collectionname.ln == lang,
                    Collectionname.type == 'ln')).first()

        if collection_name:
            if collection_name.value != request.form.get(lang):
                collection_name.value = request.form.get(lang)
                db.session.commit()
        else:
            if request.form.get(lang) != '':
                collection_name = Collectionname(collection, lang, 'ln',
                                                 request.form.get(lang))
                db.session.add(collection_name)
                db.session.commit()

    flash(_('Collection was updated on n languages:'), "info")
    return redirect(url_for('.manage_collection', name=collection.name))
Пример #12
0
class ArticleForm(WebDepositForm):
    """Article form."""

    #
    # Fields
    #
    doi = fields.TextField(
        label=_("Digital Object Identifier"),
        placeholder=_("e.g. 10.1234/foo.bar..."),
        widget_classes="form-control",
        icon='fa fa-barcode fa-fw',
        validators=[
            doi_syntax_validator,
        ],
        filters=[
            strip_string,
            strip_prefixes("doi:", "http://dx.doi.org/"),
        ],
        processors=[
            missing_doi_warning,
        ],
    )

    publication_date = fields.Date(
        label=_('Publication date'),
        icon='fa fa-calendar fa-fw',
        description=_('Required. Format: YYYY-MM-DD.'),
        default=date.today(),
        validators=[validators.DataRequired()],
        widget=date_widget,
        widget_classes='input-sm',
        export_key='imprint.date',
    )

    title = fields.TextField(
        label=_('Title'),
        export_key='title.title',
        icon='fa fa-book fa-fw',
        widget_classes="form-control",
        validators=[validators.DataRequired()],
    )

    authors = fields.DynamicFieldList(
        fields.FormField(
            AuthorInlineForm,
            widget=ExtendedListWidget(
                item_widget=ItemWidget(),
                html_tag='div',
            ),
        ),
        label=_('Authors'),
        add_label=_('Add another author'),
        icon='fa fa-user fa-fw',
        min_entries=1,
        widget_classes='',
        export_key='authors',
        validators=[
            validators.DataRequired(),
            list_length(
                min_num=1,
                element_filter=filter_empty_helper(),
            )
        ],
    )

    abstract = fields.TextAreaField(
        label=_("Description"),
        description=_('Required.'),
        default='',
        icon='fa fa-pencil fa-fw',
        validators=[
            validators.DataRequired(),
        ],
        widget=CKEditorWidget(
            toolbar=[
                ['PasteText', 'PasteFromWord'],
                [
                    'Bold',
                    'Italic',
                    'Strike',
                    '-',
                    'Subscript',
                    'Superscript',
                ],
                ['NumberedList', 'BulletedList'],
                ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'RemoveFormat'],
                ['SpecialChar', 'ScientificChar'],
                ['Source'],
                ['Maximize'],
            ],
            disableNativeSpellChecker=False,
            extraPlugins='scientificchar',
            removePlugins='elementspath',
        ),
        filters=[
            sanitize_html(),
            strip_string,
        ],
        export_key='abstract.summary',
    )

    journal_title = fields.TextField(
        label=_("Journal title"),
        description=_("Optional."),
        validators=[
            required_if(
                'journal_volume',
                [
                    lambda x: bool(x.strip()),
                ],  # non-empty
                message=_("Journal title is required if you specify either "
                          "volume, issue or pages.")),
            required_if(
                'journal_issue',
                [
                    lambda x: bool(x.strip()),
                ],  # non-empty
                message=_("Journal title is required if you specify either "
                          "volume, issue or pages.")),
            required_if(
                'journal_pages',
                [
                    lambda x: bool(x.strip()),
                ],  # non-empty
                message=_("Journal title is required if you specify either "
                          "volume, issue or pages.")),
        ],
        export_key='journal_info.title',
        widget_classes='form-control',
    )

    journal_volume = fields.TextField(
        label=_("Volume"),
        description=_("Optional."),
        export_key='journal_info.volume',
        widget_classes='form-control',
    )

    journal_issue = fields.TextField(
        label=_("Issue"),
        description=_("Optional."),
        export_key='journal_info.issue',
        widget_classes='form-control',
    )

    journal_pages = fields.TextField(
        label=_("Pages"),
        description=_("Optional."),
        export_key='journal_info.pagination',
        widget_classes='form-control',
    )

    language = fields.SelectField(
        choices=LocalProxy(
            lambda: language_list_long(enabled_langs_only=False)),
        default='english',
        icon='fa fa-globe fa-fw',
        widget_classes='form-control',
    )

    keywords = fields.DynamicFieldList(
        fields.TextField(
            widget_classes='form-control',
            autocomplete=keywords_autocomplete,
            widget=ColumnInput(class_="col-xs-10"),
        ),
        label=_('Keywords'),
        add_label=_('Add another keyword'),
        icon='fa fa-tags fa-fw',
        widget_classes='',
        min_entries=1,
    )

    notes = fields.TextAreaField(
        label=_("Notes"),
        description=_('Optional.'),
        default='',
        validators=[validators.optional()],
        filters=[
            strip_string,
        ],
        widget_classes='form-control',
        icon='fa fa-pencil fa-fw',
        export_key='comment',
    )

    plupload_file = fields.FileUploadField(label="",
                                           widget=plupload_widget,
                                           export_key=False)

    #
    # Form configuration
    #
    _title = _('New article')
    _subtitle = _('Instructions: (i) Press "Save" to save your upload for '
                  'editing later, as many times you like. (ii) Upload or '
                  'remove  extra files in the bottom of the form. (iii) When '
                  'ready, press "Submit" to finalize your upload.')

    groups = [
        ('Basic Information', [
            'doi',
            'publication_date',
            'title',
            'authors',
            'abstract',
        ], {
            'indication': 'required',
        }),
        ('Journal',
         ['journal_title', 'journal_volume', 'journal_issue',
          'journal_pages'], {
              'indication': 'required'
          }),
        ('Additional information', ['language', 'keywords', 'notes'], {
            'indication': 'optional',
        })
    ]

    field_sizes = {
        'plupload_file': 'col-md-12',
    }
Пример #13
0
    >                  #closing <lang> start tag
    (?P<langs>.*?)     #anything but the next group (greedy)
    (</lang\s*>)       #end tag
    """,
    re.IGNORECASE | re.DOTALL | re.VERBOSE,
)

# Regular expression for finding <en>...</en> tag (particular case of
# pattern_lang)
pattern_CFG_SITE_LANG = re.compile(
    r"<(" + CFG_SITE_LANG + r")\s*>(.*?)(</" + CFG_SITE_LANG + r"\s*>)", re.IGNORECASE | re.DOTALL
)

# Builds regular expression for finding each known language in <lang> tags
ln_pattern_text = r"<(?P<lang>"
ln_pattern_text += r"|".join([lang[0] for lang in language_list_long(enabled_langs_only=False)])
ln_pattern_text += r')\s*(revision="[^"]"\s*)?>(?P<translation>.*?)</\1>'
ln_pattern = re.compile(ln_pattern_text, re.IGNORECASE | re.DOTALL)

defined_tags = {
    "<CFG_SITE_NAME>": CFG_SITE_NAME,
    "<CFG_SITE_SUPPORT_EMAIL>": CFG_SITE_SUPPORT_EMAIL,
    "<CFG_SITE_ADMIN_EMAIL>": CFG_SITE_ADMIN_EMAIL,
    "<CFG_SITE_URL>": CFG_SITE_URL,
    "<CFG_SITE_SECURE_URL>": CFG_SITE_SECURE_URL,
    "<CFG_SITE_RECORD>": CFG_SITE_RECORD,
    "<CFG_VERSION>": CFG_VERSION,
    "<CFG_SITE_NAME_INTL>": CFG_SITE_NAME_INTL,
}

Пример #14
0
def get_month(date, ln=CFG_SITE_LANG, default=""):
    """
    Returns the year from a textual date retrieved from a record

    The returned value is the 3 letters short month name in language 'ln'
    If year cannot be found, returns 'default'

    @param date: the textual date to retrieve the year from
    @param default: a default value to return if year not fount
    """
    import re
    from invenio.utils.date import get_i18n_month_name
    from invenio.base.i18n import language_list_long

    #Look for textual month like "Jan" or "sep" or "November" or "novem"
    #Limit to CFG_SITE_LANG as language first (most probable date)
    #Look for short months. Also matches for long months
    short_months = [
        get_i18n_month_name(month).lower() for month in range(1, 13)
    ]  # ["jan","feb","mar",...]
    short_months_pattern = re.compile(r'(' + r'|'.join(short_months) + r')',
                                      re.IGNORECASE)  # (jan|feb|mar|...)
    result = short_months_pattern.search(date)
    if result is not None:
        try:
            month_nb = short_months.index(result.group().lower()) + 1
            return get_i18n_month_name(month_nb, "short", ln)
        except:
            pass

    #Look for month specified as number in the form 2004/03/08 or 17 02 2004
    #(always take second group of 2 or 1 digits separated by spaces or - etc.)
    month_pattern = re.compile(
        r'\d([\s]|[-/.,])+(?P<month>(\d){1,2})([\s]|[-/.,])')
    result = month_pattern.search(date)
    if result is not None:
        try:
            month_nb = int(result.group("month"))
            return get_i18n_month_name(month_nb, "short", ln)
        except:
            pass

    #Look for textual month like "Jan" or "sep" or "November" or "novem"
    #Look for the month in each language

    #Retrieve ['en', 'fr', 'de', ...]
    language_list_short = [x[0] for x in language_list_long()]
    for lang in language_list_short:  #For each language
        #Look for short months. Also matches for long months
        short_months = [
            get_i18n_month_name(month, "short", lang).lower()
            for month in range(1, 13)
        ]  # ["jan","feb","mar",...]
        short_months_pattern = re.compile(r'(' + r'|'.join(short_months) +
                                          r')',
                                          re.IGNORECASE)  # (jan|feb|mar|...)
        result = short_months_pattern.search(date)
        if result is not None:
            try:
                month_nb = short_months.index(result.group().lower()) + 1
                return get_i18n_month_name(month_nb, "short", ln)
            except:
                pass

    return default
Пример #15
0
                           # treated as HTML (and therefore no escaping of
                           # HTML tags should occur.
                           # Appears in some field values.

# Regular expression for finding <lang>...</lang> tag in format templates
pattern_lang = re.compile(r'''
    <lang              #<lang tag (no matter case)
    \s*                #any number of white spaces
    >                  #closing <lang> start tag
    (?P<langs>.*?)     #anything but the next group (greedy)
    (</lang\s*>)       #end tag
    ''', re.IGNORECASE | re.DOTALL | re.VERBOSE)

# Builds regular expression for finding each known language in <lang> tags
ln_pattern_text = r"<("
for lang in language_list_long(enabled_langs_only=False):
    ln_pattern_text += lang[0] +r"|"

ln_pattern_text = ln_pattern_text.rstrip(r"|")
ln_pattern_text += r")>(.*?)</\1>"

ln_pattern = re.compile(ln_pattern_text, re.IGNORECASE | re.DOTALL)

# Regular expression for finding text to be translated
TRANSLATION_PATTERN = re.compile(r'_\((?P<word>.*?)\)_',
                                 re.IGNORECASE | re.DOTALL | re.VERBOSE)

# Regular expression for finding <name> tag in format templates
pattern_format_template_name = re.compile(r'''
    <name              #<name tag (no matter case)
    \s*                #any number of white spaces
Пример #16
0
def get_month(date, ln=CFG_SITE_LANG, default=""):
    """
    Returns the year from a textual date retrieved from a record

    The returned value is the 3 letters short month name in language 'ln'
    If year cannot be found, returns 'default'

    @param date: the textual date to retrieve the year from
    @param default: a default value to return if year not fount
    """
    import re
    from invenio.utils.date import get_i18n_month_name
    from invenio.base.i18n import language_list_long

    #Look for textual month like "Jan" or "sep" or "November" or "novem"
    #Limit to CFG_SITE_LANG as language first (most probable date)
    #Look for short months. Also matches for long months
    short_months = [get_i18n_month_name(month).lower()
                    for month in range(1, 13)] # ["jan","feb","mar",...]
    short_months_pattern = re.compile(r'('+r'|'.join(short_months)+r')',
                                      re.IGNORECASE) # (jan|feb|mar|...)
    result = short_months_pattern.search(date)
    if result is not None:
        try:
            month_nb = short_months.index(result.group().lower()) + 1
            return get_i18n_month_name(month_nb, "short", ln)
        except:
            pass

    #Look for month specified as number in the form 2004/03/08 or 17 02 2004
    #(always take second group of 2 or 1 digits separated by spaces or - etc.)
    month_pattern = re.compile(r'\d([\s]|[-/.,])+(?P<month>(\d){1,2})([\s]|[-/.,])')
    result = month_pattern.search(date)
    if result is not None:
        try:
            month_nb = int(result.group("month"))
            return get_i18n_month_name(month_nb, "short", ln)
        except:
            pass

    #Look for textual month like "Jan" or "sep" or "November" or "novem"
    #Look for the month in each language

    #Retrieve ['en', 'fr', 'de', ...]
    language_list_short = [x[0]
                           for x in language_list_long()]
    for lang in language_list_short: #For each language
        #Look for short months. Also matches for long months
        short_months = [get_i18n_month_name(month, "short", lang).lower()
                        for month in range(1, 13)] # ["jan","feb","mar",...]
        short_months_pattern = re.compile(r'('+r'|'.join(short_months)+r')',
                                          re.IGNORECASE) # (jan|feb|mar|...)
        result = short_months_pattern.search(date)
        if result is not None:
            try:
                month_nb = short_months.index(result.group().lower()) + 1
                return get_i18n_month_name(month_nb, "short", ln)
            except:
                pass

    return default
Пример #17
0
def get_languages():
    languages = []
    for (lang, lang_namelong) in language_list_long():
        languages.append((lang, lang_namelong))
    languages.sort()
    return languages
Пример #18
0
def get_languages():
    languages = []
    for (lang, lang_namelong) in language_list_long():
        languages.append((lang, lang_namelong))
    languages.sort()
    return languages