log.py 2.56 KB
Newer Older
1 2 3 4 5 6 7
import gluon.contrib.simplejson

if request.env.http_origin:
    response.headers['Access-Control-Allow-Origin'] = request.env.http_origin  
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    response.headers['Access-Control-Max-Age'] = 86400
    
8 9
def anon_names(data):
    for row in data:
10
        row.name = vidjil_utils.anon_ids(row.id)
11 12 13 14 15 16 17 18 19 20 21 22
    return data

def get_data_list(table):
    data = db(auth.vidjil_accessible_query(PermissionEnum.read.value, table)).select()
    log.debug(data)

    if table == 'patient':
        data = anon_names(data)
    data_list = [(row.id, row.name) for row in data]
    data_list.sort(key=lambda tup: tup[1])
    return data_list

23
def index():
24
    user_log = db.user_log
25
    data_list = []
26 27 28
    groups = []
    table_name = 'all'
    id_value = 0
29 30

    auth.load_permissions(PermissionEnum.anon.value, 'patient')
31
    if auth.is_admin():
32
        query = (user_log.id > 0)
33 34 35 36 37 38
    else:
	user_groups = auth.get_user_groups()
	parent_groups = auth.get_user_group_parents()
	group_list = [g.id for g in user_groups]
	parent_list = [g.id for g in parent_groups]
	groups = list(set(group_list + parent_list))
39 40
        query = ((user_log.table_name == db.auth_permission.table_name) &
            (user_log.record_id == db.auth_permission.record_id) &
41
	    (db.auth_permission.name == PermissionEnum.access.value) &
42 43 44 45 46 47
	    (db.auth_permission.group_id.belongs(groups)))

    if 'table' in request.vars and request.vars['table'] != 'all':
        table_name = request.vars['table']
        table = db[table_name]
        query &= user_log.table_name == table_name
48
        data_list = get_data_list(table_name)
49 50 51 52

    if 'id' in request.vars and request.vars['id'] != 0:
        id_value = request.vars['id']
        query &= user_log.record_id == request.vars['id']
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    query &= (db.auth_user.id == user_log.user_id)
    query = db(query).select(user_log.ALL, db.auth_user.first_name, db.auth_user.last_name, db.patient.first_name, db.patient.last_name, db.run.name,
            left = [
                db.patient.on((db.patient.id == db.user_log.record_id) & (db.user_log.table_name == 'patient')),
                db.run.on((db.run.id == db.user_log.record_id) & (db.user_log.table_name == 'run'))
            ],
            orderby=~db.user_log.created)
    for row in query:
        if row.patient.first_name is not None:
            row.names = vidjil_utils.anon_ids(row.user_log.record_id)
        else:
            row.names = row.run.name
    return dict(query=query,
                data_list=data_list,
68 69
                stable=table_name,
                sid=id_value)