diff --git a/backend/flaschengeist_events/event_controller.py b/backend/flaschengeist_events/event_controller.py index 9b988de..b627b90 100644 --- a/backend/flaschengeist_events/event_controller.py +++ b/backend/flaschengeist_events/event_controller.py @@ -16,6 +16,7 @@ from flaschengeist.plugins.scheduler import scheduled from . import EventPlugin from .models import EventType, Event, Invitation, Job, JobType, Service + # STUB def _(x): return x @@ -43,8 +44,8 @@ def clear_services(user): db.session.commit() -def update(): - db.session.commit() +# def update(): +# db.session.commit() def get_event_types(): @@ -234,10 +235,10 @@ def create_event(event_type, start, end=None, jobs=[], is_template=None, name=No def get_job(job_id, event_id=None) -> Job: - query = Job.query.filter(Job.id == job_id) + query = db.select(Job).where(Job.id == job_id) if event_id is not None: - query = query.filter(Job.event_id_ == event_id) - job = query.one_or_none() + query = query.where(Job.event_id_ == event_id) + job = db.session.execute(query).scalar_one_or_none() if job is None: raise NotFound return job diff --git a/backend/flaschengeist_events/routes.py b/backend/flaschengeist_events/routes.py index 4508049..6787528 100644 --- a/backend/flaschengeist_events/routes.py +++ b/backend/flaschengeist_events/routes.py @@ -7,12 +7,14 @@ from flaschengeist.controller import userController from flaschengeist.utils.decorators import login_required from flaschengeist.utils.datetime import from_iso_format from flaschengeist.utils.HTTP import get_filter_args, no_content +from flaschengeist import logger from . import event_controller, permissions, EventPlugin blueprint = Blueprint("events", __name__) + def dict_get(self, key, default=None, type=None): """Same as .get from MultiDict""" try: @@ -206,18 +208,36 @@ def _add_job(event, data): end = dict_get(data, "end", None, type=from_iso_format) required_services = data["required_services"] job_type = int(data["type"]) + + job_type = event_controller.get_job_type(job_type) + job_id = dict_get(data, "id", None, int) + if job_id: + job = next(job for job in event.jobs if job.id == job_id) + job.event = event + job.job_type = job_type + job.start = start + job.end = end + job.required_services = required_services + else: + event_controller.add_job( + event, + job_type, + required_services, + start, + end, + comment=dict_get(data, "comment", None, str), + ) except (KeyError, ValueError): raise BadRequest("Missing or invalid POST parameter") + except StopIteration: + raise BadRequest("Job not in event") - job_type = event_controller.get_job_type(job_type) - event_controller.add_job( - event, - job_type, - required_services, - start, - end, - comment=dict_get(data, "comment", None, str), - ) + +def _delete_jobs_from_event(event, data): + job_ids = [x["id"] for x in data if "id" in x] + for job in event.jobs: + if job.id not in job_ids: + event.jobs.remove(job) @blueprint.route("/events", methods=["POST"]) @@ -279,6 +299,7 @@ def modify_event(event_id, current_session): """ event = event_controller.get_event(event_id) data = request.get_json() + logger.debug("PUT data: %s", data) event.start = dict_get(data, "start", event.start, type=from_iso_format) event.end = dict_get(data, "end", event.end, type=from_iso_format) event.name = dict_get(data, "name", event.name, type=str) @@ -286,7 +307,10 @@ def modify_event(event_id, current_session): if "type" in data: event_type = event_controller.get_event_type(data["type"]) event.type = event_type - + if "jobs" in data: + _delete_jobs_from_event(event, data["jobs"]) + for job in data["jobs"]: + _add_job(event, job) event_controller.update() return jsonify(event) diff --git a/src/components/management/EditEvent.vue b/src/components/management/EditEvent.vue index 1276665..7ea0642 100644 --- a/src/components/management/EditEvent.vue +++ b/src/components/management/EditEvent.vue @@ -165,7 +165,6 @@ export default defineComponent({ ); function addJob() { - console.log('addJob', activeJob.value); if (!activeJob.value[active.value]) { event.value.jobs.push(emptyJob()); active.value = event.value.jobs.length - 1; @@ -207,9 +206,6 @@ export default defineComponent({ async function save(template = false) { event.value.is_template = template; try { - if (event.value?.id !== undefined) { - //fix - } await store.addEvent(event.value); if (props.modelValue === undefined && recurrent.value && !event.value.is_template) { diff --git a/src/store/index.ts b/src/store/index.ts index 8a69d83..de935c4 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -146,17 +146,16 @@ export const useEventStore = defineStore({ }, async addEvent(event: EditableEvent) { - console.log('addEvent', event); if (event?.id === undefined) { const { data } = await api.post('/events', event); if (data.is_template) this.templates.push(data); return data; } else { if (typeof event.type === 'object') event.type = event.type.id; - const { data } = await api.put( `/events/${event.id}`, - Object.assign(event, { jobs: undefined }) + // Object.assign(event, { jobs: undefined }) + event ); if (data.is_template) this.templates.push(data); return data;