From 72cb163a0043f1812d611b3cb819fe6d1068aec8 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 22 Dec 2021 01:05:10 +0100 Subject: [PATCH] fix(backend): Fix Job model and using controller functions type should be the type id not the object --- .../flaschengeist_events/event_controller.py | 9 ++-- backend/flaschengeist_events/models.py | 5 +- backend/flaschengeist_events/routes.py | 46 +++++++++---------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/backend/flaschengeist_events/event_controller.py b/backend/flaschengeist_events/event_controller.py index 89d81a5..c835688 100644 --- a/backend/flaschengeist_events/event_controller.py +++ b/backend/flaschengeist_events/event_controller.py @@ -99,7 +99,6 @@ def get_job_types(): def get_job_type(type_id): job_type = JobType.query.get(type_id) - print(job_type) if not job_type: raise NotFound return job_type @@ -250,9 +249,9 @@ def get_jobs(user, start=None, end=None, limit=None, offset=None, descending=Non if end is not None: query = query.filter(end >= Job.start) if descending is not None: - query = query.order_by(Job.start.desc(), Job.type_id_) + query = query.order_by(Job.start.desc(), Job.type) else: - query = query.order_by(Job.start, Job.type_id_) + query = query.order_by(Job.start, Job.type) count = query.count() if limit is not None: query = query.limit(limit) @@ -264,7 +263,7 @@ def get_jobs(user, start=None, end=None, limit=None, offset=None, descending=Non def add_job(event, job_type, required_services, start, end=None, comment=None): job = Job( required_services=required_services, - type=job_type, + type_=job_type, start=start, end=end, comment=comment, @@ -386,7 +385,7 @@ def respond_invitation(invite: Invitation, accepted=True): ) -@scheduled(id='dev.flaschengeist.events.assign_backups', minutes=30) +@scheduled(id="dev.flaschengeist.events.assign_backups", minutes=30) def assign_backups(): now = datetime.now(tz=timezone.utc) # now + backup_time + next cron tick diff --git a/backend/flaschengeist_events/models.py b/backend/flaschengeist_events/models.py index 97c06ca..47dc293 100644 --- a/backend/flaschengeist_events/models.py +++ b/backend/flaschengeist_events/models.py @@ -62,17 +62,17 @@ class Job(db.Model, ModelSerializeMixin): id: int = db.Column(Serial, primary_key=True) start: datetime = db.Column(UtcDateTime, nullable=False) end: Optional[datetime] = db.Column(UtcDateTime) - type: Union[JobType, int] = db.relationship("JobType") comment: Optional[str] = db.Column(db.String(256)) + type: int = db.Column("type_id", Serial, db.ForeignKey(f"{_table_prefix_}job_type.id"), nullable=False) locked: bool = db.Column(db.Boolean(), default=False, nullable=False) services: list[Service] = db.relationship( "Service", back_populates="job_", cascade="save-update, merge, delete, delete-orphan" ) required_services: float = db.Column(db.Numeric(precision=4, scale=2, asdecimal=False), nullable=False) + type_: JobType = db.relationship("JobType") event_ = db.relationship("Event", back_populates="jobs") event_id_ = db.Column("event_id", Serial, db.ForeignKey(f"{_table_prefix_}event.id"), nullable=False) - type_id_ = db.Column("type_id", Serial, db.ForeignKey(f"{_table_prefix_}job_type.id"), nullable=False) invitations_ = db.relationship("Invitation", cascade="all,delete,delete-orphan", back_populates="job_") @@ -112,6 +112,7 @@ class Invitation(db.Model, ModelSerializeMixin): __tablename__ = _table_prefix_ + "invitation" id: int = db.Column(Serial, primary_key=True) + time: datetime = db.Column(UtcDateTime, nullable=False, default=UtcDateTime.current_utc) job_id: int = db.Column(Serial, db.ForeignKey(_table_prefix_ + "job.id"), nullable=False) # Dummy properties for API export invitee_id: str = None # User who was invited to take over diff --git a/backend/flaschengeist_events/routes.py b/backend/flaschengeist_events/routes.py index ab427f6..b9c674f 100644 --- a/backend/flaschengeist_events/routes.py +++ b/backend/flaschengeist_events/routes.py @@ -203,9 +203,7 @@ def _add_job(event, data): start = from_iso_format(data["start"]) end = dict_get(data, "end", None, type=from_iso_format) required_services = data["required_services"] - job_type = data["type"] - if isinstance(job_type, dict): - job_type = data["type"]["id"] + job_type = int(data["type"]) except (KeyError, ValueError): raise BadRequest("Missing or invalid POST parameter") @@ -239,29 +237,27 @@ def create_event(current_session): try: start = from_iso_format(data["start"]) end = dict_get(data, "end", None, type=from_iso_format) - data_type = data["type"] - if isinstance(data_type, dict): - data_type = data["type"]["id"] - event_type = event_controller.get_event_type(data_type) + event_type = event_controller.get_event_type(int(data["type"])) + + event = event_controller.create_event( + start=start, + end=end, + name=dict_get(data, "name", None, type=str), + is_template=dict_get(data, "is_template", None, type=bool), + event_type=event_type, + description=dict_get(data, "description", None, type=str), + ) + if "jobs" in data: + for job in data["jobs"]: + _add_job(event, job) + + return jsonify(event) + except KeyError: raise BadRequest("Missing POST parameter") except (NotFound, ValueError): raise BadRequest("Invalid parameter") - event = event_controller.create_event( - start=start, - end=end, - name=dict_get(data, "name", None), - is_template=dict_get(data, "is_template", None), - event_type=event_type, - description=dict_get(data, "description", None), - ) - if "jobs" in data: - for job in data["jobs"]: - _add_job(event, job) - - return jsonify(event) - @EventPlugin.blueprint.route("/events/", methods=["PUT"]) @login_required(permission=permissions.EDIT) @@ -378,8 +374,8 @@ def update_job(event_id, job_id, current_session: Session): job = event_controller.get_job(job_id, event_id) try: - if "type" in data: - job.type = event_controller.get_job_type(data["type"]) + if "type" in data or "type_id" in data: + job.type_ = event_controller.get_job_type(data.get("type", None) or data["type_id"]) job.start = from_iso_format(data.get("start", job.start)) job.end = from_iso_format(data.get("end", job.end)) job.comment = str(data.get("comment", job.comment)) @@ -492,7 +488,9 @@ def invite(current_session: Session): raise BadRequest return jsonify( [ - event_controller.invite(job, invitee, current_session.user_, userController.get_user(transferee) if transferee else None) + event_controller.invite( + job, invitee, current_session.user_, userController.get_user(transferee) if transferee else None + ) for invitee in [userController.get_user(uid) for uid in data["invitees"]] ] )