fix adding, modify, remove jobs from event

This commit is contained in:
Tim Gröger 2023-04-26 11:28:16 +02:00
parent 9359db3881
commit b7741cfa37
4 changed files with 42 additions and 22 deletions

View File

@ -16,6 +16,7 @@ from flaschengeist.plugins.scheduler import scheduled
from . import EventPlugin from . import EventPlugin
from .models import EventType, Event, Invitation, Job, JobType, Service from .models import EventType, Event, Invitation, Job, JobType, Service
# STUB # STUB
def _(x): def _(x):
return x return x
@ -43,8 +44,8 @@ def clear_services(user):
db.session.commit() db.session.commit()
def update(): # def update():
db.session.commit() # db.session.commit()
def get_event_types(): 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: 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: if event_id is not None:
query = query.filter(Job.event_id_ == event_id) query = query.where(Job.event_id_ == event_id)
job = query.one_or_none() job = db.session.execute(query).scalar_one_or_none()
if job is None: if job is None:
raise NotFound raise NotFound
return job return job

View File

@ -7,12 +7,14 @@ from flaschengeist.controller import userController
from flaschengeist.utils.decorators import login_required from flaschengeist.utils.decorators import login_required
from flaschengeist.utils.datetime import from_iso_format from flaschengeist.utils.datetime import from_iso_format
from flaschengeist.utils.HTTP import get_filter_args, no_content from flaschengeist.utils.HTTP import get_filter_args, no_content
from flaschengeist import logger
from . import event_controller, permissions, EventPlugin from . import event_controller, permissions, EventPlugin
blueprint = Blueprint("events", __name__) blueprint = Blueprint("events", __name__)
def dict_get(self, key, default=None, type=None): def dict_get(self, key, default=None, type=None):
"""Same as .get from MultiDict""" """Same as .get from MultiDict"""
try: try:
@ -206,18 +208,36 @@ def _add_job(event, data):
end = dict_get(data, "end", None, type=from_iso_format) end = dict_get(data, "end", None, type=from_iso_format)
required_services = data["required_services"] required_services = data["required_services"]
job_type = int(data["type"]) 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): except (KeyError, ValueError):
raise BadRequest("Missing or invalid POST parameter") 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( def _delete_jobs_from_event(event, data):
event, job_ids = [x["id"] for x in data if "id" in x]
job_type, for job in event.jobs:
required_services, if job.id not in job_ids:
start, event.jobs.remove(job)
end,
comment=dict_get(data, "comment", None, str),
)
@blueprint.route("/events", methods=["POST"]) @blueprint.route("/events", methods=["POST"])
@ -279,6 +299,7 @@ def modify_event(event_id, current_session):
""" """
event = event_controller.get_event(event_id) event = event_controller.get_event(event_id)
data = request.get_json() data = request.get_json()
logger.debug("PUT data: %s", data)
event.start = dict_get(data, "start", event.start, type=from_iso_format) 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.end = dict_get(data, "end", event.end, type=from_iso_format)
event.name = dict_get(data, "name", event.name, type=str) 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: if "type" in data:
event_type = event_controller.get_event_type(data["type"]) event_type = event_controller.get_event_type(data["type"])
event.type = event_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() event_controller.update()
return jsonify(event) return jsonify(event)

View File

@ -165,7 +165,6 @@ export default defineComponent({
); );
function addJob() { function addJob() {
console.log('addJob', activeJob.value);
if (!activeJob.value[active.value]) { if (!activeJob.value[active.value]) {
event.value.jobs.push(emptyJob()); event.value.jobs.push(emptyJob());
active.value = event.value.jobs.length - 1; active.value = event.value.jobs.length - 1;
@ -207,9 +206,6 @@ export default defineComponent({
async function save(template = false) { async function save(template = false) {
event.value.is_template = template; event.value.is_template = template;
try { try {
if (event.value?.id !== undefined) {
//fix
}
await store.addEvent(event.value); await store.addEvent(event.value);
if (props.modelValue === undefined && recurrent.value && !event.value.is_template) { if (props.modelValue === undefined && recurrent.value && !event.value.is_template) {

View File

@ -146,17 +146,16 @@ export const useEventStore = defineStore({
}, },
async addEvent(event: EditableEvent) { async addEvent(event: EditableEvent) {
console.log('addEvent', event);
if (event?.id === undefined) { if (event?.id === undefined) {
const { data } = await api.post<FG.Event>('/events', event); const { data } = await api.post<FG.Event>('/events', event);
if (data.is_template) this.templates.push(data); if (data.is_template) this.templates.push(data);
return data; return data;
} else { } else {
if (typeof event.type === 'object') event.type = event.type.id; if (typeof event.type === 'object') event.type = event.type.id;
const { data } = await api.put<FG.Event>( const { data } = await api.put<FG.Event>(
`/events/${event.id}`, `/events/${event.id}`,
Object.assign(event, { jobs: undefined }) // Object.assign(event, { jobs: undefined })
event
); );
if (data.is_template) this.templates.push(data); if (data.is_template) this.templates.push(data);
return data; return data;