config.py 6.75 KB
Newer Older
Marc Duez's avatar
Marc Duez committed
1
# coding: utf8
Marc Duez's avatar
Marc Duez committed
2
3
4
5
6
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
Marc Duez's avatar
Marc Duez committed
7
8

def index():
Marc Duez's avatar
Marc Duez committed
9
10
11
    if not auth.user : 
        res = {"redirect" : "default/user/login"}
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
12
13
    

Marc Duez's avatar
Marc Duez committed
14
15
16
    query = db((auth.accessible_query('read', db.config) | auth.accessible_query('admin', db.config) ) ).select() 

    return dict(message=T('Configs'),
17
18
               query=query,
               isAdmin = auth.has_membership("admin"))
Marc Duez's avatar
Marc Duez committed
19

20

Marc Duez's avatar
Marc Duez committed
21
def add(): 
Mathieu Giraud's avatar
Mathieu Giraud committed
22
    return dict(message=T('Add config'))
Marc Duez's avatar
Marc Duez committed
23

24

Marc Duez's avatar
Marc Duez committed
25
26
#TODO check data
def add_form(): 
27
28
    error =""

29
    required_fields = ['config_name', 'config_command', 'config_fuse_command', 'config_program']
30
31
32
    for field in required_fields:
        if request.vars[field] == "" :
            error += field+" needed, "
33
34
35
36
37
38

    if error=="" :
        
        db.config.insert(name=request.vars['config_name'],
                        info=request.vars['config_info'],
                        command=request.vars['config_command'],
39
40
                        fuse_command=request.vars['config_fuse_command'],
                        program=request.vars['config_program']
41
42
                        )

Marc Duez's avatar
Marc Duez committed
43
        res = {"redirect": "config/index",
44
               "message": "config '%s' added" % request.vars['config_name']}
45
46
47
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
        
    else :
Marc Duez's avatar
Marc Duez committed
48
        res = {"success" : "false", "message" : error}
49
50
51
52
53
54
55
56
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))


def edit(): 
    return dict(message=T('edit config'))


def edit_form(): 
Marc Duez's avatar
Marc Duez committed
57
    import shutil, os.path
Marc Duez's avatar
Marc Duez committed
58
    
59
60
    error =""

Marc Duez's avatar
Marc Duez committed
61
    required_fields = ['id', 'config_name', 'config_command', 'config_fuse_command', 'config_program']
62
63
64
    for field in required_fields:
        if request.vars[field] == "" :
            error += field+" needed, "
65
66
67
68

    if error=="" :

        db.config[request.vars["id"]] = dict(name=request.vars['config_name'],
69
70
71
72
73
                                             info=request.vars['config_info'],
                                             command=request.vars['config_command'],
                                             fuse_command=request.vars['config_fuse_command'],
                                             program=request.vars['config_program']
                                             )
74

Marc Duez's avatar
Marc Duez committed
75
        res = {"redirect": "config/index",
76
               "message": "config '%s' saved" % request.vars['config_name']}
Marc Duez's avatar
Marc Duez committed
77

78
        log.info(res)
79
80
81
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

    else :
Marc Duez's avatar
Marc Duez committed
82
        res = {"success" : "false", "message" : error}
83
        log.error(res)
84
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
85
86
87
88
89

def confirm():
    return dict(message=T('confirm config deletion'))

def delete():
Marc Duez's avatar
Marc Duez committed
90
    import shutil, os.path
91
    
92
93
    #delete results_file using this config
    db(db.results_file.config_id==request.vars["id"]).delete()
94
95
96
97
    
    #delete config
    db(db.config.id==request.vars["id"]).delete() 
    
Marc Duez's avatar
Marc Duez committed
98
99
    res = {"redirect": "config/index",
           "message": "config deleted"}
100
    return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
101
102
103
104
105


def permission(): 
    if (auth.has_permission('admin', 'patient', request.vars["id"]) ):
        
106
107
108
109
110
111
112
113
114
115
116
        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 
117
118
119
120
        
        for row in query :
            row.owner = row.role
            if row.owner[:5] == "user_" :
121
                id = usermap[row.owner]
122
123
124
125
126
127
128
129
130
131
132
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
                row.owner = db.auth_user[id].first_name + " " + db.auth_user[id].last_name 

            row.admin = False
            if db(   (db.auth_permission.name == "admin")
                  & (db.auth_permission.record_id == request.vars["id"])
                  & (db.auth_permission.group_id == row.id)
                  & (db.auth_permission.table_name == db.config)
              ).count() > 0 :
                row.admin = True
                
            row.read = False
            if db(   (db.auth_permission.name == "read")
                  & (db.auth_permission.record_id == request.vars["id"])
                  & (db.auth_permission.group_id == row.id)
                  & (db.auth_permission.table_name == db.config)
              ).count() > 0 :
                row.read = True
        
        return dict(query = query)
    else :
        res = {"message": ACCESS_DENIED}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
    
#TODO refactor with patient/change_permission
def change_permission():
    if (auth.has_permission('admin', 'config', request.vars["config_id"]) ):
        error = ""
        if request.vars["group_id"] == "" :
            error += "missing group_id, "
        if request.vars["config_id"] == "" :
            error += "missing patient_id, "
        if request.vars["permission"] == "" :
            error += "missing permission, "

        if error=="":
            if db(   (db.auth_permission.name == request.vars["permission"])
                      & (db.auth_permission.record_id == request.vars["config_id"])
                      & (db.auth_permission.group_id == request.vars["group_id"])
                      & (db.auth_permission.table_name == db.config)
                  ).count() > 0 :
                auth.del_permission(request.vars["group_id"], request.vars["permission"], db.config, request.vars["config_id"])
                res = {"message" : "access '%s' deleted to '%s'" % (request.vars["permission"], db.auth_group[request.vars["group_id"]].role)}
            else :
                auth.add_permission(request.vars["group_id"], request.vars["permission"], db.config, request.vars["config_id"])
                res = {"message" : "access '%s' granted to '%s'" % (request.vars["permission"], db.auth_group[request.vars["group_id"]].role)}
            
            log.info(res)
            return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
        else :
            res = {"message": "incomplete request : "+error }
            log.error(res)
            return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
    else :
        res = {"message": ACCESS_DENIED}
        log.error(res)
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))