Esempio n. 1
0
File: terms.py Progetto: julen/tzos
def edit(id):

    if g.user.is_corrector:
        user_restriction = "true()"
    else:
        user_restriction = "term:is_public($tig)"

    qs = """
    import module namespace term = "http://tzos.net/term" at "term.xqm";

    let $tig := collection($collection)/martif/text/body/termEntry/langSet/tig[@id="{0}"]
    let $unreviewed :=
        if (term:owner($tig) = "{1}") then
            true()
        else
            false()
    where term:owner($tig) = "{1}" or {2}
    return term:values($tig, $unreviewed)
    """.format(unicode(id).encode('utf-8'),
               getattr(g.user, 'username', u'').encode('utf-8'),
               user_restriction)

    term = dbxml.session.raw_query(qs).as_callback(Term.parse).first_or_404()

    try:
        term.lock()
    except Term.TermLocked:
        return render_template('terms/locked.html')

    # Be aware these checks have to be done from highest to lowest permissions
    if g.user.is_moderator:
        form_cls = EditTermFormMod
    elif g.user.is_corrector:
        form_cls = EditTermFormCor
    else:
        form_cls = EditTermForm

    form = _gen_term_form(form_cls, obj=term)

    #
    # Adapt working_status for reviewing cases
    #
    review = request.args.get('mode', u'') == u'review'

    if g.user.is_moderator and review:
        form.working_status.data = u"workingElement"
    elif g.user.is_corrector and review:
        form.working_status.data = True
    elif not g.user.is_moderator and g.user.is_corrector:
        form.working_status.data = term.is_public

    if form.validate_on_submit():

        new_term = Term()
        form.populate_obj(new_term)
        new_term._id = id

        objects = new_term.check_collision(term_id=id)

        if objects:
            flash(_(u'Collision detected! The renamed term you entered '
                    'already exists in the database.'), 'warning')

            return render_template('terms/edit.html', form=form, term=term)

        if new_term.update():
            _register_transaction(id)

            term.unlock()

            flash(_(u"Term ‘%(term)s’ has been edited.", term=new_term.term),
                    "success")

            return redirect(url_for("terms.detail", id=id))
        else:
            flash(_(u"Failed to edit term."), "error")

    elif request.method == 'POST' and not form.validate():
        flash(_(u"Failed to edit term. Please review the data you "
                 "entered is correct."), "error")

    return render_template('terms/edit.html', form=form, term=term)