def scheduling_add(): name = request.form.get("name") description = request.form.get("description") interval_starts = request.form.getlist("interval-start[]") interval_values = request.form.getlist("interval-value[]") interval_units = request.form.getlist("interval-unit[]") checks = request.form.getlist("checks[]") if not name: return error_response("You must supply a name for this schedule") s = Schedule(name=name, description=description) session.add(s) session.flush() for check_id in checks: sc = ScheduleCheck(check_id=check_id, schedule_id=s.id) session.add(sc) for interval in zip(interval_starts, interval_values, interval_units): try: start_timestamp = datetime.strptime(interval[0], "%Y-%m-%d %H:%M:%S") except ValueError: return error_response("Start timestamp for interval was not " "understood") si = ScheduleInterval(schedule_id=s.id, start_timestamp=start_timestamp) try: si.set_interval(int(interval[1]), interval[2]) except ValueError: return error_response("Interval must be an integer") session.add(si) session.commit() return jsonify(success=True, message="Schedule has been added successfully")
def scheduling_edit(): schedule_id = request.form.get("schedule-id") name = request.form.get("name") description = request.form.get("description") interval_starts = request.form.getlist("interval-start[]") interval_values = request.form.getlist("interval-value[]") interval_units = request.form.getlist("interval-unit[]") checks = request.form.getlist("checks[]") if not name: return error_response("You must supply a name for this schedule") s = Schedule.query.get(schedule_id) if not s: abort(404) s.name = name s.description = description ScheduleCheck.query.filter(ScheduleCheck.schedule_id == s.id).delete() for check_id in checks: sc = ScheduleCheck(check_id=check_id, schedule_id=s.id) session.add(sc) ScheduleInterval.query.filter( ScheduleInterval.schedule_id == s.id).delete() for interval in zip(interval_starts, interval_values, interval_units): try: start_timestamp = datetime.strptime(interval[0], "%Y-%m-%d %H:%M:%S") except ValueError: session.rollback() return error_response("Start timestamp for interval was not " "understood") si = ScheduleInterval(schedule_id=s.id, start_timestamp=start_timestamp) try: si.set_interval(int(interval[1]), interval[2]) except ValueError: session.rollback() return error_response("Interval must be an integer") session.add(si) session.commit() return jsonify(success=True, message="Schedule has been saved successfully")
def scheduling_edit(): schedule_id = request.form.get("schedule-id") name = request.form.get("name") description = request.form.get("description") interval_starts = request.form.getlist("interval-start[]") interval_values = request.form.getlist("interval-value[]") interval_units = request.form.getlist("interval-unit[]") checks = request.form.getlist("checks[]") if not name: return error_response("You must supply a name for this schedule") s = Schedule.query.get(schedule_id) if not s: abort(404) s.name = name s.description = description ScheduleCheck.query.filter(ScheduleCheck.schedule_id == s.id).delete() for check_id in checks: sc = ScheduleCheck(check_id=check_id, schedule_id=s.id) session.add(sc) ScheduleInterval.query.filter(ScheduleInterval.schedule_id == s.id).delete() for interval in zip(interval_starts, interval_values, interval_units): try: start_timestamp = datetime.strptime(interval[0], "%Y-%m-%d %H:%M:%S") except ValueError: session.rollback() return error_response("Start timestamp for interval was not " "understood") si = ScheduleInterval(schedule_id=s.id, start_timestamp=start_timestamp) try: si.set_interval(int(interval[1]), interval[2]) except ValueError: session.rollback() return error_response("Interval must be an integer") session.add(si) session.commit() return jsonify(success=True, message="Schedule has been saved successfully")