示例#1
0
def request_handler(request,
                    journal_id,
                    redirect_route="admin.journal_page",
                    template="admin/journal.html",
                    locked_template="admin/journal_locked.html",
                    activate_deactivate=False,
                    group_editable=False,
                    editors=None,
                    editorial_available=False):
    # check our permissions
    if not current_user.has_role("edit_journal"):
        abort(401)
    j = get_journal(journal_id)
    if j is None:
        abort(404)

    # attempt to get a lock on the object
    try:
        lockinfo = lock.lock("journal", journal_id, current_user.id)
    except lock.Locked as l:
        return render_template(locked_template,
                               journal=j,
                               lock=l.lock,
                               edit_journal_page=True)

    current_info = models.ObjectDict(JournalFormXWalk.obj2form(j))
    form = JournalForm(request.form, current_info)

    current_country = xwalk.get_country_code(j.bibjson().country)

    if current_country not in country_options_two_char_code_index:
        # couldn't find it, better warn the user to look for it
        # themselves
        country_help_text = "This journal's country has been recorded as \"{country}\". Please select it in the Country menu.".format(
            country=current_country)
    else:
        country_help_text = ''

    form.country.description = '<span class="red">' + country_help_text + '</span>'

    # add the contents of a few fields to their descriptions since select2 autocomplete
    # would otherwise obscure the full values
    if form.publisher.data:
        if not form.publisher.description:
            form.publisher.description = 'Full contents: ' + form.publisher.data
        else:
            form.publisher.description += '<br><br>Full contents: ' + form.publisher.data

    if form.society_institution.data:
        if not form.society_institution.description:
            form.society_institution.description = 'Full contents: ' + form.society_institution.data
        else:
            form.society_institution.description += '<br><br>Full contents: ' + form.society_institution.data

    if form.platform.data:
        if not form.platform.description:
            form.platform.description = 'Full contents: ' + form.platform.data
        else:
            form.platform.description += '<br><br>Full contents: ' + form.platform.data

    first_field_with_error = ''

    if editors is not None:
        form.editor.choices = [("", "Choose an editor")
                               ] + [(editor, editor) for editor in editors]
    else:
        if j.editor is not None:
            form.editor.choices = [(j.editor, j.editor)]
        else:
            form.editor.choices = [("", "")]

    if request.method == 'POST':
        if form.make_all_fields_optional.data:
            valid = True
        else:
            valid = form.validate()
        if valid:
            # even though you can only edit journals right now, keeping the same
            # method as editing suggestions (i.e. creating a new object
            # and editing its properties)

            email_editor = False
            if group_editable:
                email_editor = JournalFormXWalk.is_new_editor_group(form, j)

            email_associate = False
            if editorial_available:
                email_associate = JournalFormXWalk.is_new_editor(form, j)

            # do the core crosswalk
            journal = JournalFormXWalk.form2obj(form, existing_journal=j)

            # some of the properties (id, in_doaj, etc.) have to be carried over
            # otherwise they implicitly end up getting changed to their defaults
            # when a journal gets edited (e.g. it always gets taken out of DOAJ)
            # if we don't copy over the in_doaj attribute to the new journal object
            journal['id'] = j['id']
            created_date = j.created_date if j.created_date else datetime.now(
            ).strftime("%Y-%m-%dT%H:%M:%SZ")
            journal.set_created(created_date)
            journal.bibjson().active = j.is_in_doaj()
            journal.set_in_doaj(j.is_in_doaj())
            if ((journal.owner is None or journal.owner == "") and
                (j.owner is not None)) or not current_user.has_role("admin"):
                journal.set_owner(j.owner)

            if not group_editable or not editorial_available:
                journal.set_editor_group(j.editor_group)

            if not editorial_available:
                journal.set_editor(j.editor)

            # FIXME: probably should check that the editor is in the editor_group and remove if not

            journal.save()
            flash('Journal updated.', 'success')

            # only actually send the email when we've successfully processed the form
            if email_editor:
                send_editor_group_email(journal)

            if email_associate:
                send_editor_email(journal)

            return redirect(
                url_for(redirect_route, journal_id=journal_id, _anchor='done'))
            # meaningless anchor to replace #first_problem used on the form
            # anchors persist between 3xx redirects to the same resource
        else:
            for field in form:  # in order of definition of fields, so the order of rendering should be (manually) kept the same as the order of definition for this to work
                if field.errors:
                    first_field_with_error = field.short_name
                    break

    return render_template(
        template,
        form=form,
        first_field_with_error=first_field_with_error,
        q_numbers=xrange(1, 10000).__iter__(
        ),  # a generator for the purpose of displaying numbered questions
        other_val=other_val,
        digital_archiving_policy_specific_library_value=
        digital_archiving_policy_specific_library_value,
        edit_journal_page=True,
        admin_page=True,
        journal=j,
        subjectstr=subjects2str(j.bibjson().subjects()),
        lcc_jstree=json.dumps(lcc_jstree),
        activate_deactivate=activate_deactivate,
        group_editable=group_editable,
        editorial_available=editorial_available,
        lock=lockinfo)
示例#2
0
文件: journal.py 项目: jbarnsby/doaj
def request_handler(request, journal_id, redirect_route="admin.journal_page", template="admin/journal.html", locked_template="admin/journal_locked.html",
                    activate_deactivate=False, group_editable=False, editors=None, editorial_available=False):
    # check our permissions
    if not current_user.has_role("edit_journal"):
        abort(401)
    j = get_journal(journal_id)
    if j is None:
        abort(404)

    # attempt to get a lock on the object
    try:
        lockinfo = lock.lock("journal", journal_id, current_user.id)
    except lock.Locked as l:
        return render_template(locked_template, journal=j, lock=l.lock, edit_journal_page=True)

    current_info = models.ObjectDict(JournalFormXWalk.obj2form(j))
    form = JournalForm(request.form, current_info)

    current_country = xwalk.get_country_code(j.bibjson().country)

    if current_country not in country_options_two_char_code_index:
        # couldn't find it, better warn the user to look for it
        # themselves
        country_help_text = "This journal's country has been recorded as \"{country}\". Please select it in the Country menu.".format(country=current_country)
    else:
        country_help_text = ''

    form.country.description = '<span class="red">' + country_help_text + '</span>'

    # add the contents of a few fields to their descriptions since select2 autocomplete
    # would otherwise obscure the full values
    if form.publisher.data:
        if not form.publisher.description:
            form.publisher.description = 'Full contents: ' + form.publisher.data
        else:
            form.publisher.description += '<br><br>Full contents: ' + form.publisher.data

    if form.society_institution.data:
        if not form.society_institution.description:
            form.society_institution.description = 'Full contents: ' + form.society_institution.data
        else:
            form.society_institution.description += '<br><br>Full contents: ' + form.society_institution.data

    if form.platform.data:
        if not form.platform.description:
            form.platform.description = 'Full contents: ' + form.platform.data
        else:
            form.platform.description += '<br><br>Full contents: ' + form.platform.data

    first_field_with_error = ''

    if editors is not None:
        form.editor.choices = [("", "Choose an editor")] + [(editor, editor) for editor in editors]
    else:
        if j.editor is not None:
            form.editor.choices = [(j.editor, j.editor)]
        else:
            form.editor.choices = [("", "")]

    if request.method == 'POST':
        if form.make_all_fields_optional.data:
            valid = True
        else:
            valid = form.validate()
        if valid:
            # even though you can only edit journals right now, keeping the same
            # method as editing suggestions (i.e. creating a new object
            # and editing its properties)

            email_editor = False
            if group_editable:
                email_editor = JournalFormXWalk.is_new_editor_group(form, j)

            email_associate = False
            if editorial_available:
                email_associate = JournalFormXWalk.is_new_editor(form, j)

            # do the core crosswalk
            journal = JournalFormXWalk.form2obj(form, existing_journal=j)

            # some of the properties (id, in_doaj, etc.) have to be carried over
            # otherwise they implicitly end up getting changed to their defaults
            # when a journal gets edited (e.g. it always gets taken out of DOAJ)
            # if we don't copy over the in_doaj attribute to the new journal object
            journal['id'] = j['id']
            created_date = j.created_date if j.created_date else datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
            journal.set_created(created_date)
            journal.bibjson().active = j.is_in_doaj()
            journal.set_in_doaj(j.is_in_doaj())
            if ((journal.owner is None or journal.owner == "") and (j.owner is not None)) or not current_user.has_role("admin"):
                journal.set_owner(j.owner)

            if not group_editable or not editorial_available:
                journal.set_editor_group(j.editor_group)

            if not editorial_available:
                journal.set_editor(j.editor)

            # FIXME: probably should check that the editor is in the editor_group and remove if not

            journal.save()
            flash('Journal updated.', 'success')

            # only actually send the email when we've successfully processed the form
            if email_editor:
                send_editor_group_email(journal)

            if email_associate:
                send_editor_email(journal)

            return redirect(url_for(redirect_route, journal_id=journal_id, _anchor='done'))
                # meaningless anchor to replace #first_problem used on the form
                # anchors persist between 3xx redirects to the same resource
        else:
            for field in form:  # in order of definition of fields, so the order of rendering should be (manually) kept the same as the order of definition for this to work
                if field.errors:
                    first_field_with_error = field.short_name
                    break

    return render_template(
            template,
            form=form,
            first_field_with_error=first_field_with_error,
            q_numbers=xrange(1, 10000).__iter__(),  # a generator for the purpose of displaying numbered questions
            other_val=other_val,
            digital_archiving_policy_specific_library_value=digital_archiving_policy_specific_library_value,
            edit_journal_page=True,
            admin_page=True,
            journal=j,
            subjectstr=subjects2str(j.bibjson().subjects()),
            lcc_jstree=json.dumps(lcc_jstree),
            activate_deactivate=activate_deactivate,
            group_editable=group_editable,
            editorial_available=editorial_available,
            lock=lockinfo
    )
示例#3
0
def request_handler(request, suggestion_id, redirect_route="admin.suggestion_page", template="admin/suggestion.html", locked_template="admin/suggestion_locked.html",
                    editors=None, group_editable=False, editorial_available=False, status_options="admin"):
    if not current_user.has_role("edit_suggestion"):
        abort(401)
    s = models.Suggestion.pull(suggestion_id)
    if s is None:
        abort(404)

    # attempt to get a lock on the object
    try:
        lockinfo = lock.lock("suggestion", suggestion_id, current_user.id)
    except lock.Locked as l:
        return render_template(locked_template, suggestion=s, lock=l.lock, edit_suggestion_page=True)

    current_info = models.ObjectDict(SuggestionFormXWalk.obj2form(s))
    form = EditSuggestionForm(request.form, current_info)

    if status_options == "admin":
        form.application_status.choices = forms.application_status_choices_admin
    else:
        form.application_status.choices = forms.application_status_choices_editor

    process_the_form = True
    if request.method == 'POST' and s.application_status == 'accepted':
        flash('You cannot edit applications which have been accepted into DOAJ.', 'error')
        process_the_form = False

    if form.application_status.data == "accepted" and form.make_all_fields_optional.data:
        flash("You cannot accept an application into the DOAJ without fully validating it", "error")
        process_the_form = False

    # add the contents of a few fields to their descriptions since select2 autocomplete
    # would otherwise obscure the full values
    if form.publisher.data:
        if not form.publisher.description:
            form.publisher.description = 'Full contents: ' + form.publisher.data
        else:
            form.publisher.description += '<br><br>Full contents: ' + form.publisher.data

    if form.society_institution.data:
        if not form.society_institution.description:
            form.society_institution.description = 'Full contents: ' + form.society_institution.data
        else:
            form.society_institution.description += '<br><br>Full contents: ' + form.society_institution.data

    if form.platform.data:
        if not form.platform.description:
            form.platform.description = 'Full contents: ' + form.platform.data
        else:
            form.platform.description += '<br><br>Full contents: ' + form.platform.data

    if editors is not None:
        form.editor.choices = [("", "Choose an editor")] + [(editor, editor) for editor in editors]
    else:
        if s.editor is not None:
            form.editor.choices = [(s.editor, s.editor)]
        else:
            form.editor.choices = [("", "")]

    return suggestion_form(form, request, template,
                           existing_suggestion=s,
                           suggestion=s,
                           process_the_form=process_the_form,
                           admin_page=True,
                           subjectstr=subjects2str(s.bibjson().subjects()),
                           lcc_jstree=json.dumps(lcc_jstree),
                           group_editable=group_editable,
                           editorial_available=editorial_available,
                           redirect_route=redirect_route,
                           lock=lockinfo
    )