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-13 19:04:19 +00:00
|
|
|
raise NotImplementedError
|
2021-05-30 09:06:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@api.route("/appointment", methods=["PUT"])
|
|
|
|
def update_appointment():
|
2021-06-13 19:04:19 +00:00
|
|
|
raise NotImplementedError
|
2021-05-30 09:06:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@api.route("/appointment", methods=["DELETE"])
|
|
|
|
def delete_appointment():
|
2021-06-13 19:04:19 +00:00
|
|
|
raise NotImplementedError
|