itplanning/ITPlanning/api/appointement.py

112 lines
3.3 KiB
Python
Raw Permalink Normal View History

2021-05-30 09:06:28 +00:00
from flask import request, jsonify
from pony.orm import ObjectNotFound
2021-06-21 20:34:17 +00:00
from datetime import datetime, timedelta
from ITPlanning.db import Appointment, Planning, Service, Ticket, Customer
2021-05-30 09:06:28 +00:00
from ITPlanning.api.exception import NotFound, GenericError
from . import api
def datefiltering(query):
before, after = map(request.values.get, ("before", "after"))
if before:
before = datetime.strptime(before, "%Y-%m-%dT%H%M")
query = query.filter(lambda a: a.start_datetime < before)
if after:
after = datetime.strptime(after, "%Y-%m-%dT%H%M")
query = query.filter(lambda a: a.start_datetime > after)
return query
2021-06-21 20:34:17 +00:00
def search_appointment(by=None, id=None):
rv = []
if by == None and id is not None:
rv.append(Appointment[id].to_dict())
2021-05-30 09:06:28 +00:00
else:
query = Appointment.select()
2021-06-21 20:34:17 +00:00
if by is not None:
query.filter(lambda a: getattr(a, by).id == id)
2021-05-30 09:06:28 +00:00
query = datefiltering(query)
for f in query:
2021-06-21 20:34:17 +00:00
rv.append(f.to_dict())
if len(rv) == 0:
2021-05-30 09:06:28 +00:00
raise NotFound("Appointment")
return jsonify(rv)
2021-06-21 20:34:17 +00:00
@api.route("/appointment")
@api.route("/appointment/")
@api.route("/appointment/<id>")
def get_appointment(id=None):
return search_appointment(id=id)
2021-05-30 09:06:28 +00:00
@api.route("/appointment/planning/<id>")
2021-06-21 20:34:17 +00:00
def get_appointment_by_planning(id=None):
return search_appointment("planning", id)
2021-05-30 09:06:28 +00:00
@api.route("/appointment/service/<id>")
def get_appointment_by_service(id):
2021-06-21 20:34:17 +00:00
return search_appointment("service", id)
2021-05-30 09:06:28 +00:00
@api.route("/appointment/customer/<id>")
def get_appointment_by_customer(id):
2021-06-21 20:34:17 +00:00
return search_appointment("customer", id)
2021-05-30 09:06:28 +00:00
@api.route("/appointment/ticket/<id>")
def get_appointment_by_ticket(id):
2021-06-21 20:34:17 +00:00
return search_appointment("ticket", id)
2021-05-30 09:06:28 +00:00
@api.route("/appointment", methods=["POST"])
def new_appointment():
2021-06-21 20:34:59 +00:00
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
2021-05-30 09:06:28 +00:00
@api.route("/appointment", methods=["PUT"])
def update_appointment():
raise NotImplementedError
2021-05-30 09:06:28 +00:00
@api.route("/appointment", methods=["DELETE"])
def delete_appointment():
raise NotImplementedError