add time/audio is XHR-friendly
This commit is contained in:
parent
4d492def58
commit
b0fd381fc7
1 changed files with 28 additions and 11 deletions
|
@ -126,15 +126,27 @@ def edit_time(alarmid):
|
||||||
"add_time_kind.html", form=form, kind=kind, mode="edit", alarmid=alarmid
|
"add_time_kind.html", form=form, kind=kind, mode="edit", alarmid=alarmid
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def is_xhr():
|
||||||
|
return request.headers.get('x-requested-with') is not None
|
||||||
|
|
||||||
@db.route("/add/time/<kind>", methods=["GET", "POST"])
|
@db.route("/add/time/<kind>", methods=["GET", "POST"])
|
||||||
def addtime_kind(kind):
|
def addtime_kind(kind):
|
||||||
Form, receiver = tuple(forms.get_timeform(kind))
|
Form, receiver = tuple(forms.get_timeform(kind))
|
||||||
form = Form()
|
form = Form(csrf_enabled=(not is_xhr()))
|
||||||
if request.method == "POST" and form.validate():
|
if request.method == "POST":
|
||||||
|
if form.validate():
|
||||||
data = receiver(form)
|
data = receiver(form)
|
||||||
eid = get_model().add_alarm(data)
|
eid = get_model().add_alarm(data)
|
||||||
|
if not is_xhr():
|
||||||
return redirect(url_for("db.edit_event", alarmid=eid))
|
return redirect(url_for("db.edit_event", alarmid=eid))
|
||||||
|
else:
|
||||||
|
resp = jsonify(alarmid=eid)
|
||||||
|
resp.status_code = 201
|
||||||
|
return resp
|
||||||
|
elif is_xhr():
|
||||||
|
resp = jsonify(errors=form.errors)
|
||||||
|
resp.status_code = 400
|
||||||
|
return resp
|
||||||
|
|
||||||
return render_template("add_time_kind.html", form=form, kind=kind, mode="add")
|
return render_template("add_time_kind.html", form=form, kind=kind, mode="add")
|
||||||
|
|
||||||
|
@ -153,12 +165,17 @@ def addaudio():
|
||||||
@db.route("/add/audio/<kind>", methods=["GET", "POST"])
|
@db.route("/add/audio/<kind>", methods=["GET", "POST"])
|
||||||
def addaudio_kind(kind):
|
def addaudio_kind(kind):
|
||||||
Form, receiver = tuple(forms.get_audioform(kind))
|
Form, receiver = tuple(forms.get_audioform(kind))
|
||||||
form = Form()
|
form = Form(csrf_enabled=(not is_xhr()))
|
||||||
if request.method == "POST" and form.validate():
|
if request.method == "POST":
|
||||||
|
if form.validate():
|
||||||
data = receiver(form)
|
data = receiver(form)
|
||||||
model = current_app.larigira.controller.monitor.model
|
model = current_app.larigira.controller.monitor.model
|
||||||
eid = model.add_action(data)
|
eid = model.add_action(data)
|
||||||
return jsonify(dict(inserted=eid, data=data))
|
return jsonify(dict(inserted=eid, data=data))
|
||||||
|
else:
|
||||||
|
resp = jsonify(errors=form.errors)
|
||||||
|
resp.status_code = 400
|
||||||
|
return resp
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
"add_audio_kind.html", form=form, kind=kind, suggestions=get_suggestions()
|
"add_audio_kind.html", form=form, kind=kind, suggestions=get_suggestions()
|
||||||
|
|
Loading…
Reference in a new issue