publications.py 1.36 KB
Newer Older
1 2 3 4
""" Publications Controllers

"""
import json
LE GAC Renaud's avatar
LE GAC Renaud committed
5

6 7 8
from plugin_dbui import CALLBACK_ERRORS, UNDEF_ID

SQL_UNLOCK = "UPDATE publications SET id_status=%i WHERE id=%s"
LE GAC Renaud's avatar
LE GAC Renaud committed
9

10 11 12

def read():
    """Read a publication record identified by its id.
13
    Design to load a record within a basic Ext.form.Panel.
LE GAC Renaud's avatar
LE GAC Renaud committed
14

15
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
16 17 18
    recid = request.vars.id
    row = db.publications[recid]

19
    return json.dumps(dict(success=True, data=row.as_dict()))
20 21


22 23 24 25 26 27 28 29 30
def unlock_status():
    """Unlock the status OK bypassing the protection.
    The publication identified by its id.

    """
    # build a raw query to bypass protection (callback)
    db.executesql(SQL_UNLOCK % (UNDEF_ID, request.vars.id))


31 32 33
def update():
    """Update a publication record identified by its id.
    Design to update record from a basic Ext.form.Panel.
LE GAC Renaud's avatar
LE GAC Renaud committed
34 35


36
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
37
    recid = request.vars.id
38
    del request.vars.id
39 40

    table = db.publications
LE GAC Renaud's avatar
LE GAC Renaud committed
41 42

    # update the record
43
    try:
LE GAC Renaud's avatar
LE GAC Renaud committed
44
        rep = db(table.id == recid).update(**request.vars)
45 46 47 48 49 50 51

    # operation can be rejected by the database
    except Exception as dbe:
        return json.dumps(dict(success=False, msg=dbe.message))

    # operation can be reject by callbacks table._before_update
    if not rep:
LE GAC Renaud's avatar
LE GAC Renaud committed
52
        if CALLBACK_ERRORS in table:
53
            return json.dumps(dict(success=False, msg=table._callback_errors))
LE GAC Renaud's avatar
LE GAC Renaud committed
54 55

    return json.dumps(dict(success=True, msg="Record %s modified" % id))