config.py 3.21 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():
9 10 11 12 13
   if not auth.user : 
    res = {"redirect" : "default/user/login"}
    return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
    

14
   query = db((auth.accessible_query('read', db.config) | auth.accessible_query('admin', db.config) ) ).select() 
15
    
16
   return dict(message=T('config_list'),
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 22 23
def add(): 
    return dict(message=T('add config'))

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 44
        res = {"redirect": "config/index",
               "message": "config added"}
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 =""

61
    required_fields = ['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 76
        res = {"redirect": "config/index",
               "message": "config saved"}
77
        log.info(res)
78 79 80
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

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

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

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