Attention une mise à jour du serveur va être effectuée le vendredi 16 avril entre 12h et 12h30. Cette mise à jour va générer une interruption du service de quelques minutes.

Commit c30fd2c2 authored by Ryan Herbert's avatar Ryan Herbert

refactor patient add/edit into a single controller and form

Having a single controller and view, is better for clean code practices,
since changes to the form/controller won't require two forms and
controllers to be changed.

So when the changes for the multi-patient form come in, we'll have less
to do and a cleaner code-bade to work from.

I also took the opportunity to build the from with divs rather than a
table since we're going to need a more flexible layout when the time
comes. Also less DOM nodes.
parent 32fd3aca
...@@ -2272,6 +2272,26 @@ label.highlight_label { ...@@ -2272,6 +2272,26 @@ label.highlight_label {
box-shadow: none; box-shadow: none;
} }
fieldset {
//display: inline-block;
//float: left;
}
.file_fieldset {
clear: right;
width: -moz-available;
}
form label {
display: inline-block;
width: 100px;
text-align: right;
}
.clear {
clear: both;
}
/* atwho css */ /* atwho css */
.atwho-view { .atwho-view {
......
...@@ -12,16 +12,27 @@ if request.env.http_origin: ...@@ -12,16 +12,27 @@ if request.env.http_origin:
ACCESS_DENIED = "access denied" ACCESS_DENIED = "access denied"
## return form to create new patient ## return form to create new patient
def add(): def form():
if (auth.can_create_patient()): # edit patient
creation_group_tuple = get_default_creation_group(auth) if("id" in request.vars and auth.can_modify_patient(request.vars["id"])):
groups = [get_set_group(defs.SET_TYPE_PATIENT, request.vars["id"])]
message = 'edit patient'
max_group = None
#new patient
elif (auth.can_create_patient()):
creation_group_tuple = get_default_creation_group(auth)
groups = creation_group_tuple[0] groups = creation_group_tuple[0]
max_group = creation_group_tuple[1] max_group = creation_group_tuple[1]
return dict(message=T('add patient'), groups=groups, master_group=max_group) message = 'add patient'
else : else :
res = {"message": ACCESS_DENIED} res = {"message": ACCESS_DENIED}
log.error(res) log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':')) return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
return dict(message=T(message),
groups=groups,
master_group=max_group,
patient=db.patient[request.vars["id"]])
...@@ -29,124 +40,87 @@ def add(): ...@@ -29,124 +40,87 @@ def add():
## need ["first_name", "last_name", "birth_date", "info"] ## need ["first_name", "last_name", "birth_date", "info"]
## redirect to patient list if success ## redirect to patient list if success
## return a flash error message if fail ## return a flash error message if fail
def add_form(): def submit():
if (auth.can_create_patient()): error = ""
if request.vars["first_name"] == "" :
error = "" error += "first name needed, "
if request.vars["first_name"] == "" : if request.vars["last_name"] == "" :
error += "first name needed, " error += "last name needed, "
if request.vars["last_name"] == "" : if request.vars["birth"] != "" :
error += "last name needed, " try:
if request.vars["birth"] != "" : datetime.datetime.strptime(""+request.vars['birth'], '%Y-%m-%d')
try: except ValueError:
datetime.datetime.strptime(""+request.vars['birth'], '%Y-%m-%d') error += "date (wrong format)"
except ValueError:
error += "date (wrong format)" if error != "":
res = {"success" : "false",
if error=="" : "message" : error}
id_sample_set = db.sample_set.insert(sample_type=defs.SET_TYPE_PATIENT)
id = db.patient.insert(first_name=request.vars["first_name"],
last_name=request.vars["last_name"],
sample_set_id=id_sample_set,
birth=request.vars["birth"],
info=request.vars["info"],
id_label=request.vars["id_label"],
creator=auth.user_id)
user_group = int(request.vars["patient_group"])
admin_group = db(db.auth_group.role=='admin').select().first().id
register_tags(db, defs.SET_TYPE_PATIENT, id, request.vars["info"], user_group)
#patient creator automaticaly has all rights
auth.add_permission(user_group, PermissionEnum.access.value, db.patient, id)
patient_name = request.vars["first_name"] + ' ' + request.vars["last_name"]
res = {"redirect": "sample_set/index",
"args" : { "id" : id_sample_set },
"message": "(%s) patient %s added" % (id_sample_set, patient_name) }
log.info(res, extra={'user_id': auth.user.id, 'record_id': id, 'table_name': 'patient'})
if (id % 100) == 0:
mail.send(to=defs.ADMIN_EMAILS,
subject="[Vidjil] %d" % id,
message="The %dth patient has just been created." % id)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else :
res = {"success" : "false",
"message" : error}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else :
res = {"message": ACCESS_DENIED}
log.error(res) log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':')) return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
p = dict(first_name=request.vars["first_name"],
last_name=request.vars["last_name"],
birth=request.vars["birth"],
info=request.vars["info"],
id_label=request.vars["id_label"]
)
register = False
reset = False
## return edit form # edit patient
def edit(): log.debug("id present: " + "id" in request.vars)
if (auth.can_modify_patient(request.vars["id"]) ): log.debug("id: " + request.vars["id"] + request.vars['id'] == "" + request.vars['id'] is None)
group_id = get_set_group(defs.SET_TYPE_PATIENT, request.vars["id"]) if (request.vars['id'] != "" and auth.can_modify_patient(request.vars["id"])):
return dict(message=T('edit patient'), group_id=group_id) patient = db.patient[request.vars["id"]]
else : db.patient[request.vars["id"]] = p
res = {"message": ACCESS_DENIED}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
if (patient.info != request.vars["info"]):
group_id = get_set_group(defs.SET_TYPE_PATIENT, request.vars["id"])
register = True
reset = True
res = {"redirect": "back",
"message": "%s %s (%s): patient edited" % (request.vars["first_name"], request.vars["last_name"], request.vars["id"])}
id = request.vars["id"]
## check edit form # add patient
## need ["first_name", "last_name", "birth_date", "info"] elif (auth.can_create_patient()):
## redirect to patient list if success
## return a flash error message if fail id_sample_set = db.sample_set.insert(sample_type=defs.SET_TYPE_PATIENT)
def edit_form():
if (auth.can_modify_patient(request.vars["id"]) ): p['creator'] = auth.user_id
error = "" p['sample_set_id'] = id_sample_set
if request.vars["first_name"] == "" : id = db.patient.insert(**p)
error += "first name needed, "
if request.vars["last_name"] == "" : user_group = int(request.vars["patient_group"])
error += "last name needed, " admin_group = db(db.auth_group.role=='admin').select().first().id
if request.vars["birth"] != "" :
try: group_id = user_group
datetime.datetime.strptime(""+request.vars['birth'], '%Y-%m-%d') register = True
except ValueError:
error += "date (wrong format)" #patient creator automaticaly has all rights
if request.vars["id"] == "" : auth.add_permission(user_group, PermissionEnum.access.value, db.patient, id)
error += "patient id needed, "
patient_name = request.vars["first_name"] + ' ' + request.vars["last_name"]
if error=="" :
patient = db.patient[request.vars["id"]] res = {"redirect": "sample_set/index",
db.patient[request.vars["id"]] = dict(first_name=request.vars["first_name"], "args" : { "id" : id_sample_set },
last_name=request.vars["last_name"], "message": "(%s) patient %s added" % (id_sample_set, patient_name) }
birth=request.vars["birth"],
info=request.vars["info"], if (id % 100) == 0:
id_label=request.vars["id_label"] mail.send(to=defs.ADMIN_EMAILS,
) subject="[Vidjil] %d" % id,
message="The %dth patient has just been created." % id)
if (patient.info != request.vars["info"]):
group_id = get_set_group(defs.SET_TYPE_PATIENT, request.vars["id"])
register_tags(db, defs.SET_TYPE_PATIENT, request.vars["id"], request.vars["info"], group_id, reset=True)
res = {"redirect": "back",
"message": "%s %s (%s): patient edited" % (request.vars["first_name"], request.vars["last_name"], request.vars["id"])}
log.info(res, extra={'user_id': auth.user.id, 'record_id': request.vars['id'], 'table_name': 'patient'})
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else :
res = {"success" : "false", "message" : error}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else : else :
res = {"message": ACCESS_DENIED} res = {"message": ACCESS_DENIED}
log.error(res) log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
if register:
register_tags(db, defs.SET_TYPE_PATIENT, request.vars["id"], request.vars["info"], group_id, reset=reset)
log.info(res, extra={'user_id': auth.user.id, 'record_id': id, 'table_name': 'patient'})
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
def download(): def download():
return response.download(request, db) return response.download(request, db)
......
...@@ -446,7 +446,7 @@ def edit(): ...@@ -446,7 +446,7 @@ def edit():
if sample_type == defs.SET_TYPE_PATIENT: if sample_type == defs.SET_TYPE_PATIENT:
patient = db((db.patient.sample_set_id == request.vars["id"])).select()[0] patient = db((db.patient.sample_set_id == request.vars["id"])).select()[0]
redirect(URL('patient', 'edit', vars=dict(id=patient.id)), headers=response.headers) redirect(URL('patient', 'form', vars=dict(id=patient.id)), headers=response.headers)
elif sample_type == defs.SET_TYPE_RUN: elif sample_type == defs.SET_TYPE_RUN:
run = db((db.run.sample_set_id == request.vars["id"])).select()[0] run = db((db.run.sample_set_id == request.vars["id"])).select()[0]
......
...@@ -38,7 +38,7 @@ class Patient(SampleSet): ...@@ -38,7 +38,7 @@ class Patient(SampleSet):
) )
def get_add_route(self): def get_add_route(self):
return 'patient/add' return 'patient/form'
def get_data(self, sample_set_id): def get_data(self, sample_set_id):
return db(db.patient.sample_set_id == sample_set_id).select()[0] return db(db.patient.sample_set_id == sample_set_id).select()[0]
......
{{extend 'db_layout.html'}}
<form id="data_form" action="DB_ADDRESS/patient/add_form" enctype="multipart/form-data" method="post">
<table>
<tbody>
<tr>
<td><label for="patient_id_label" id="patient_id_label__label">Patient ID: </label></td>
<td><input class="date" id="patient_id_label" name="id_label" type="text" value=""><span></span></td>
<td></td>
</tr>
<tr {{ if len(groups) <= 1: }} class="hiddenCheckBox" {{pass}}>
<td><label for="patient_group" id="patient_group__label">Owner Group: </label></td>
<td>
{{= SELECT(*[OPTION(g['name'], _value=g['id']) for g in groups], _id="group_select", _name="patient_group", _value=master_group, value=master_group) }}
</td>
<td></td>
</tr>
<tr>
<td ><label for="patient_first_name" id="patient_first_name__label">First Name: </label></td>
<td ><input class="string" id="patient_first_name" name="first_name" type="text" value=""><span>*</span></td>
<td ></td>
</tr>
<tr>
<td><label for="patient_last_name" id="patient_last_name__label">Last Name: </label></td>
<td><input class="string" id="patient_last_name" name="last_name" type="text" value=""><span>*</span></td>
<td></td>
</tr>
<tr>
<td><label for="patient_birth" id="patient_birth__label">Birth: </label></td>
<td><input class="date" id="patient_birth" name="birth" type="text" value=""><span>(format: yyyy-mm-dd)</span></td>
<td></td>
</tr>
<tr>
<td><label for="patient_info" id="patient_info__label">Info: </label></td>
<td><textarea onfocus="$(this).data('group-ids', [$('#group_select option:selected').val()]); new VidjilAutoComplete().setupAtWho(this);" data-needs-atwho="true" class="text" cols="40" id="patient_info" name="info" rows="10"></textarea></td>
<td></td>
</tr>
<tr>
<td></td>
<td ><input type="submit" value="save" class="btn"></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<div>
(* required fields)
</div>
{{extend 'db_layout.html'}}
{{
info = db.patient[request.vars["id"]]
}}
<form id="data_form" action="DB_ADDRESS/patient/edit_form" enctype="multipart/form-data" method="post">
<input type="HIDDEN" id="patient_id" name="id" value="{{=request.vars["id"]}}">
<table>
<tbody>
<tr>
<td><label for="patient_id_label" id="patient_id_label__label">Patient ID:</label></td>
<td><input class="date" id="patient_id_label" name="id_label" type="text" value="{{=info.id_label}}"><span></span></td>
<td></td>
</tr>
<tr>
<td > <label for="patient_first_name" id="patient_first_name__label">First Name: </label> </td>
<td > <input class="string" id="patient_first_name" name="first_name" type="text" value="{{=info.first_name}}"><span>*</span></td>
<td > </td>
</tr>
<tr>
<td> <label for="patient_last_name" id="patient_last_name__label">Last Name: </label> </td>
<td> <input class="string" id="patient_last_name" name="last_name" type="text" value="{{=info.last_name}}"><span>*</span></td>
<td> </td>
</tr>
<tr>
<td> <label for="patient_birth" id="patient_birth__label">Birth: </label></td>
<td> <input class="date" id="patient_birth" name="birth" type="text" value="{{if info.birth:}}{{=info.birth}}{{pass}}"><span>(yyyy-mm-dd)</span></td>
<td> </td>
</tr>
<tr>
<td> <label for="patient_info" id="patient_info__label">Info: </label> </td>
<td> <textarea onfocus="new VidjilAutoComplete().setupAtWho(this);" data-needs-atwho="true" data-group-ids="[{{=group_id}}]" class="text" cols="40" id="patient_info" name="info" rows="10">{{=info.info}}</textarea> </td>
<td></td>
</tr>
<tr>
<td> </td>
<td> <input type="submit" value="update patient" class="btn"> </td>
<td> </td>
</tr>
</tbody>
</table>
</form>
<div>
(* required fields)
</div>
{{extend 'db_layout.html'}}
<form id="data_form" action="DB_ADDRESS/patient/submit" enctype="multipart/form-data" method="post">
<input type="hidden" id="id" name="id" value="{{if patient is not None:}}{{=patient.id}}{{pass}}">
<div {{ if len(groups) <= 1: }} class="hiddenCheckBox" {{pass}}>
<label for="group_select" id="patient_group__label">Owner Group: </label>
{{= SELECT(*[OPTION(g['name'], _value=g['id']) for g in groups], _id="group_select", _name="patient_group", _value=master_group, value=master_group) }}
</div>
<div id="fieldset_container">
<fieldset name="patient0">
<legend>Patient 1</legend>
<div>
<label for="patient_id_label_0" id="patient_id_label__label_0">Patient ID: </label>
<input class="date" id="patient_id_label_0" name="id_label" type="text"
value="{{if patient is not None and patient.id_label is not None:}}{{=patient.id_label}}{{pass}}"><span></span>
</div>
<div>
<label for="patient_first_name_0" id="patient_first_name__label_0">First Name: </label>
<input class="string" id="patient_first_name_0" name="first_name" type="text"
value="{{if patient is not None:}}{{=patient.first_name}}{{pass}}"><span>*</span>
</div>
<div>
<label for="patient_last_name_0" id="patient_last_name__label_0">Last Name: </label>
<input class="string" id="patient_last_name_0" name="last_name" type="text"
value="{{if patient is not None:}}{{=patient.last_name}}{{pass}}"><span>*</span>
</div>
<div>
<label for="patient_birth_0" id="patient_birth__label_0">Birth: </label>
<input class="date" id="patient_birth_0" name="birth" type="text"
value="{{if patient is not None and patient.birth is not None:}}{{=patient.birth}}{{pass}}" placeholder="yyyy-mm-dd">
</div>
<div>
<label for="patient_info_0" id="patient_info__label_0">Info: </label>
<textarea
onfocus="$(this).data('keys', [$('#group_select option:selected').val()]);
new VidjilAutoComplete().setupTags(this);"
data-needs-atwho="true"
class="text"
cols="40"
id="patient_info_0"
name="info"
rows="10">{{if patient is not None and patient.info is not None:}}{{=patient.info}}{{pass}}</textarea>
</div>
</fieldset>
</div>
<input type="submit" value="save" class="btn"></td>
</form>
<div>
(* required fields)
</div>
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