Events can be deleted

fixes #5
This commit is contained in:
boyska 2019-02-09 21:31:26 +01:00
parent 08b18e02bd
commit cd45d67c62
3 changed files with 50 additions and 1 deletions

View file

@ -8,7 +8,7 @@ from datetime import datetime, timedelta, time
from collections import defaultdict
from flask import current_app, Blueprint, render_template, jsonify, abort, \
request, redirect, url_for
request, redirect, url_for, flash
from larigira.entrypoints_utils import get_avail_entrypoints
from larigira.audiogen import get_audiogenerator
@ -22,6 +22,14 @@ db = Blueprint('db', __name__,
template_folder='templates')
def request_wants_json():
best = request.accept_mimetypes \
.best_match(['application/json', 'text/html'])
return best == 'application/json' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['text/html']
def get_model():
return current_app.larigira.controller.monitor.model
@ -100,6 +108,7 @@ def edit_time(alarmid):
form=form,
kind=kind,
mode='edit',
alarmid=alarmid,
)
@ -189,3 +198,18 @@ def change_actions(alarmid):
new_fields={'actions': [int(a) for a in
new_actions]})
return jsonify(dict(updated=alarmid, ret=ret))
@db.route('/api/alarm/<int:alarmid>/delete', methods=['POST'])
def delete_alarm(alarmid):
model = current_app.larigira.controller.monitor.model
try:
alarm = model.get_alarm_by_id(int(alarmid))
print(alarm['nick'])
model.delete_alarm(alarmid)
except KeyError:
abort(404)
if request_wants_json():
return jsonify(dict(deleted=alarmid))
flash('Evento %d `%s` cancellato' % (alarmid, alarm['nick']) )
return redirect(url_for('db.events_list'))

View file

@ -36,5 +36,17 @@ webshim.polyfill('forms-ext');
<div class="container-fluid">
{{wtf.quick_form(form)}}
</div>
{% if mode == 'edit' %}
<div class="container-fluid">
<div class="alert alert-danger">
<form method="POST"
action="{{ url_for ('db.delete_alarm', alarmid=alarmid) }}" >
<button class="btn btn-danger">Cancella</button>
</form>
</div>
</div>
{% endif %}
{% endblock content %}
{# vim: set ts=2 sw=2 noet: #}

View file

@ -46,6 +46,19 @@ $(function() {
</div>{#container-fluid#}
</nav>
{% block messages %}
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert alert-info">
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
{% endblock messages %}
{% endblock %}
{% macro dict_table(obj, exclude_list=[]) %}