pre_process.py 7.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# coding: utf8
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

    
ACCESS_DENIED = "access denied"

def index():
    if not auth.user : 
        res = {"redirect" : "default/user/login"}
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

HERBERT Ryan's avatar
HERBERT Ryan committed
16
    query = db((auth.vidjil_accessible_query(PermissionEnum.read_pre_process.value, db.pre_process) | auth.vidjil_accessible_query(PermissionEnum.admin_pre_process.value, db.pre_process) ) ).select(orderby=~db.pre_process.name)
17 18 19
    log.info("view pre process list", extra={'user_id': auth.user.id,
                'record_id': None,
                'table_name': "pre_process"})
20 21 22 23 24 25

    return dict(message=T('Pre-process list'),
               query=query,
               isAdmin = auth.is_admin())


26 27 28 29
def add():
    if auth.can_create_pre_process():
        return dict(message=T('Add pre-process'))
    return error_message(ACCESS_DENIED)
30 31


32 33 34
def add_form():
    if not auth.can_create_pre_process():
        return error_message(ACCESS_DENIED)
35 36
    error =""

37
    required_fields = ['pre_process_name', 'pre_process_command']
38 39 40 41 42 43
    for field in required_fields:
        if request.vars[field] == "" :
            error += field+" needed, "

    if error=="" :
        
44
        pre_proc_id = db.pre_process.insert(name=request.vars['pre_process_name'],
45 46
                        info=request.vars['pre_process_info'],
                        command=request.vars['pre_process_command']
47 48
                        )

HERBERT Ryan's avatar
HERBERT Ryan committed
49
	auth.add_permission(auth.user_group(), PermissionEnum.read_pre_process.value, db.pre_process, pre_proc_id)
50

51 52
        res = {"redirect": "pre_process/index",
               "message": "pre_process '%s' added" % request.vars['pre_process_name']}
53
        log.admin(res)
54 55 56
        log.info(res, extra={'user_id': auth.user.id,
                'record_id': pre_proc_id,
                'table_name': "pre_process"})
57 58 59 60 61 62 63 64 65
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
        
    else :
        res = {"success" : "false", "message" : error}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))


def edit(): 
66 67 68
    if auth.can_modify_pre_process(request.vars['id']):
        return dict(message=T('edit config'))
    return error_message(ACCESS_DENIED)
69 70 71


def edit_form(): 
72 73
    if not auth.can_modify_pre_process(request.vars['id']):
        return error_message(ACCESS_DENIED)
74 75
    error =""

76
    required_fields = ['pre_process_name', 'pre_process_command']
77 78 79 80 81 82
    for field in required_fields:
        if request.vars[field] == "" :
            error += field+" needed, "

    if error=="" :

83 84 85 86
        db.pre_process[request.vars["id"]] = dict(name=request.vars['pre_process_name'],
                        info=request.vars['pre_process_info'],
                        command=request.vars['pre_process_command']
                        )
87

88 89
        res = {"redirect": "pre_process/index",
               "message": "pre_process '%s' updated" % request.vars['pre_process_name']}
90 91

        log.admin(res)
92 93 94
        log.info(res, extra={'user_id': auth.user.id,
                'record_id': request.vars["id"],
                'table_name': "pre_process"})
95 96 97 98 99 100 101 102
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

    else :
        res = {"success" : "false", "message" : error}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

def confirm():
103 104 105
    if auth.can_modify_pre_process(request.vars['id']):
        return dict(message=T('confirm pre_process deletion'))
    return error_message(ACCESS_DENIES)
106 107 108

def delete():
    
109 110
    if not auth.can_modify_pre_process(request.vars['id']):
        return error_message(ACCESS_DENIED)
111 112
    #delete pre_process
    db(db.pre_process.id==request.vars["id"]).delete() 
113
    
114 115
    res = {"redirect": "pre_process/index",
           "message": "pre_process '%s' deleted" % request.vars["id"]}
116
    log.admin(res)
117 118 119
    log.info(res, extra={'user_id': auth.user.id,
            'record_id': request.vars["id"],
            'table_name': "pre_process"})
120
    return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
marc's avatar
marc committed
121 122 123 124 125

## need ["sequence_file_id"]
## need ["sample_set_id"]
def info():
    if (auth.can_modify_sample_set(request.vars["sample_set_id"])):
126 127 128
        log.info("view pre process info", extra={'user_id': auth.user.id,
                'record_id': request.vars["sample_set_id"],
                'table_name': "sample_set_id"})
marc's avatar
marc committed
129 130 131 132
        return dict(message=T('result info'))
    else :
        res = {"message": "acces denied"}
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

def permission():
    if (not auth.can_modify_pre_process(request.vars["id"]) ):
        res = {"message": ACCESS_DENIED}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

    query = db( (db.auth_group.role != 'admin') ).select()

    query2 = db( (db.auth_group.role != 'admin') &
                (db.auth_membership.group_id == db.auth_group.id) &
                (db.auth_membership.user_id == db.auth_user.id)    
              ).select()

    usermap = {}
    for row in query2 : 
        if row.auth_group.role[:5] == "user_" :
            usermap[row.auth_group.role] = row.auth_user.id 

    for row in query :
        row.owner = row.role
        if row.owner[:5] == "user_" :
            id = usermap[row.owner]
            row.owner = db.auth_user[id].first_name + " " + db.auth_user[id].last_name

        permissions = db(
                (db.auth_permission.group_id == row.id) &
                (db.auth_permission.record_id == 0) &
                (db.auth_permission.table_name == 'sample_set')).select()
        row.perms = ', '.join(map(lambda x: x.name, permissions))

        row.parent_access = ', '.join(str(value) for value in auth.get_access_groups(db.pre_process, request.vars['id'], group=row.id))
        row.read =  auth.get_group_access('pre_process', request.vars['id'], row.id)

    return dict(query = query)

#TODO refactor with patient/change_permission
def change_permission():
    if (not auth.can_modify_pre_process(request.vars["pre_process_id"]) ):
        res = {"message": ACCESS_DENIED}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

    error = ""
    if request.vars["group_id"] == "" :
        error += "missing group_id, "
    if request.vars["pre_process_id"] == "" :
        error += "missing pre_process_id, "

    if error=="":
        if auth.get_group_access(db.pre_process, int(request.vars["pre_process_id"]), int(request.vars["group_id"])):
184
            auth.del_permission(request.vars["group_id"], PermissionEnum.access.value, db.pre_process, request.vars["pre_process_id"])
185 186 187 188 189 190 191 192
            res = {"message" : "c%s: access '%s' deleted to '%s'" % (request.vars["pre_process_id"],
                                                                     PermissionEnum.access.value, db.auth_group[request.vars["group_id"]].role)}
        else :
            auth.add_permission(request.vars["group_id"], PermissionEnum.access.value, db.pre_process, request.vars["pre_process_id"])
            res = {"message" : "c%s: access '%s' granted to '%s'" % (request.vars["pre_process_id"],
                                                                     PermissionEnum.access.value, db.auth_group[request.vars["group_id"]].role)}

        log.admin(res)
193 194 195
        log.info(res, extra={'user_id': auth.user.id,
                'record_id': request.vars["pre_process_id"],
                'table_name': "pre_process"})
196 197 198 199 200
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
    else :
        res = {"message": "incomplete request : "+error }
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))