From 5f98a0a7bf21ca35298642b4d97ec8695b9fc7e7 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 21 Jun 2021 22:34:59 +0200 Subject: [PATCH] implement new appointement method --- ITPlanning/api/appointement.py | 38 +++++++++++++++++++++++++++++++++- tests/api/test_appointment.py | 23 +++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/ITPlanning/api/appointement.py b/ITPlanning/api/appointement.py index b3f41be..2fc09ca 100644 --- a/ITPlanning/api/appointement.py +++ b/ITPlanning/api/appointement.py @@ -62,7 +62,43 @@ def get_appointment_by_ticket(id): @api.route("/appointment", methods=["POST"]) def new_appointment(): - raise NotImplementedError + book_datetime = datetime.now() + start_datetime = request.values.get("start_datetime") + service = request.values.get("service") + planning = request.values.get("planning") + ticket = request.values.get("ticket") + customer = request.values.get("customer") + is_unavaillable = request.values.get("is_unavaillable") + notes = request.values.get("notes") + if start_datetime is None or service is None or planning is None: + return GenericError("missing parameter") + if is_unavaillable is None: + is_unavaillable = False + service = Service[service] + planning = Planning[planning] + start_datetime = datetime.strptime(start_datetime, "%Y-%m-%dT%H%M") + end_datetime = start_datetime + timedelta(minutes=service.service_category.duration) + if customer: + customer = Customer[customer] + + if ticket: + ticket = Ticket[ticket] + if notes is None: + notes = "" + newappointment = Appointment( + book_datetime=book_datetime, + start_datetime=start_datetime, + end_datetime=end_datetime, + is_unavaillable=is_unavaillable, + service=service, + planning=planning, + ticket=ticket, + customer=customer, + notes="", + ) + rv = jsonify(newappointment.to_dict()) + rv.status_code = 201 + return rv @api.route("/appointment", methods=["PUT"]) diff --git a/tests/api/test_appointment.py b/tests/api/test_appointment.py index 2d7d218..1070be0 100644 --- a/tests/api/test_appointment.py +++ b/tests/api/test_appointment.py @@ -1,5 +1,9 @@ -from ..testBase import APITestBase +from datetime import datetime, timedelta from pony.orm import db_session +from ITPlanning.db import Appointment +from json import loads +from ..testBase import APITestBase +from ..assets.filldb import filldatabase class AppointmentTestCase(APITestBase): @@ -63,3 +67,20 @@ class AppointmentTestCase(APITestBase): req, data = self.make_request("appointment/customer/1", 200) req, data = self.make_request("appointment/ticket/1", 200) + def test_post_appointment(self): + with db_session: + service = filldatabase.create_service() + customer = filldatabase.create_Customer() + planning = filldatabase.create_planning() + planning.services.add(service) + req = self.make_request( + "appointment", + 201, + { + "start_datetime": datetime.now().strftime("%Y-%m-%dT%H%M"), + "planning": planning.id, + "service": service.id, + }, + "post", + ) + print(req)