diff --git a/server/routes.py b/server/routes.py index 8058ef4..f589100 100644 --- a/server/routes.py +++ b/server/routes.py @@ -96,35 +96,44 @@ def devices(): @app.route("/calendar", methods=['GET', 'POST', 'DELETE']) -@login_required def calendar(): + if not current_user.is_authenticated: + return flask.render_template('login.html') + calendars = backend.calendarsFromDb(current_user) form = CalendarForm() if request.method == 'POST': - if form.validate_on_submit(): - # try to add the submitted ical as a calendar - print(form.iCalURL.data, flush=True) - - + if request.form.get("submit") == "Remove": + calendar = db.session.query(Calendar).filter(Calendar.calendar_id==request.form.get("calendar")).first() + db.session.delete(calendar) + db.session.commit() + + elif form.validate_on_submit(): ical.icalToCalendarDb(form.iCalURL.data, form.calName.data, current_user) # otherwise it is a javascript POST else: - calId = request.json.get('calendar_id') - color = request.json.get('color', None) - toggle = request.json.get('toggle', None) - + try: + calId = request.json.get('calendar_id') + color = request.json.get('color', None) + toggle = request.json.get('toggle', None) + except: + return flask.render_template('calendar.html', calendars=calendars, form=form) + if color != None: current_user.updateCalendar(calId, color=color) if toggle != None: current_user.updateCalendar(calId, toggle=toggle) # toggle specific calendar of user - calId = request.json.get('calendar_id') - color = request.json.get('color', None) - toggle = request.json.get('toggle', None) - + try: + calId = request.json.get('calendar_id') + color = request.json.get('color', None) + toggle = request.json.get('toggle', None) + except: + return flask.render_template('calendar.html', calendars=calendars, form=form) + if color != None: current_user.updateCalendar(calId, color=color) if toggle != None: @@ -133,13 +142,6 @@ def calendar(): return 'OK' - elif request.method == 'DELETE': - # do nothing - return 'NONE' - else: - # POST Error 405 - print("405") - return flask.render_template('calendar.html', calendars=calendars, form=form) # POST diff --git a/server/template/calendar.html b/server/template/calendar.html index d30ebfa..b9c91dc 100644 --- a/server/template/calendar.html +++ b/server/template/calendar.html @@ -9,6 +9,13 @@ {% for item in calendars %}