Commit f918bdb3 authored by Mikaël Salson's avatar Mikaël Salson Committed by Vidjil Team

controller_utils: returning an error is highly used in controller, make it simple.

parent f5e3453e
...@@ -3,6 +3,7 @@ import gluon.contrib.simplejson ...@@ -3,6 +3,7 @@ import gluon.contrib.simplejson
import defs import defs
import vidjil_utils import vidjil_utils
import os import os
import controller_utils
if request.env.http_origin: if request.env.http_origin:
response.headers['Access-Control-Allow-Origin'] = 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-Allow-Credentials'] = 'true'
...@@ -11,13 +12,9 @@ if request.env.http_origin: ...@@ -11,13 +12,9 @@ if request.env.http_origin:
def add(): def add():
if not auth.can_modify_patient(request.vars['id'], auth.user_id): if not auth.can_modify_patient(request.vars['id'], auth.user_id):
res = {"success" : "false", "message" : "you need admin permission on this patient to add files"} return error_message("you need admin permission on this patient to add files")
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
elif not auth.can_upload_file(request.vars['id']): elif not auth.can_upload_file(request.vars['id']):
res = {"success" : "false", "message" : "you don't have right to upload files"} return error_message("you don't have right to upload files")
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else: else:
query = db((db.sequence_file.patient_id==request.vars['id'])).select() query = db((db.sequence_file.patient_id==request.vars['id'])).select()
if len(query) != 0 : if len(query) != 0 :
...@@ -48,9 +45,7 @@ def add_form(): ...@@ -48,9 +45,7 @@ def add_form():
query = db((db.sequence_file.patient_id==request.vars['patient_id'])).select() query = db((db.sequence_file.patient_id==request.vars['patient_id'])).select()
for row in query : for row in query :
if row.filename == request.vars['filename'] : if row.filename == request.vars['filename'] :
res = {"message": "this sequence file already exists for this patient"} return error_message("this sequence file already exists for this patient")
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
id = db.sequence_file.insert(sampling_date=request.vars['sampling_date'], id = db.sequence_file.insert(sampling_date=request.vars['sampling_date'],
info=request.vars['file_info'], info=request.vars['file_info'],
...@@ -71,9 +66,7 @@ def add_form(): ...@@ -71,9 +66,7 @@ def add_form():
return gluon.contrib.simplejson.dumps(res, separators=(',',':')) return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else : else :
res = {"success" : "false", "message" : error} return error_message(error)
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
def edit(): def edit():
...@@ -83,9 +76,7 @@ def edit(): ...@@ -83,9 +76,7 @@ def edit():
# res = {"success" : "false", "message" : "you don't have right to upload files"} # res = {"success" : "false", "message" : "you don't have right to upload files"}
# return gluon.contrib.simplejson.dumps(res, separators=(',',':')) # return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else: else:
res = {"success" : "false", "message" : "you need admin permission to edit files"} return error_message("you need admin permission to edit files")
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
...@@ -161,10 +152,7 @@ def confirm(): ...@@ -161,10 +152,7 @@ def confirm():
if auth.can_modify_patient(request.vars['patient_id']): if auth.can_modify_patient(request.vars['patient_id']):
return dict(message=T('confirm sequence file deletion')) return dict(message=T('confirm sequence file deletion'))
else: else:
res = {"success" : "false", "message" : "you need admin permission to delete this file"} return error_message("you need admin permission to delete this file")
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
def delete(): def delete():
import shutil, os.path import shutil, os.path
...@@ -181,10 +169,7 @@ def delete(): ...@@ -181,10 +169,7 @@ def delete():
log.info(res) log.info(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':')) return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else: else:
res = {"success" : "false", "message" : "you need admin permission to delete this file"} return error_message("you need admin permission to delete this file")
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
def sequencer_list(): def sequencer_list():
sequencer_list = [] sequencer_list = []
......
from gluon import current
import gluon.contrib.simplejson
def error_message(msg):
res = {"success" : "false", "message" : msg}
current.log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment