itplanning/ITPlanning/api/appointement.py
2021-06-21 22:34:17 +02:00

76 lines
2.0 KiB
Python

from flask import request, jsonify
from pony.orm import ObjectNotFound
from datetime import datetime, timedelta
from ITPlanning.db import Appointment, Planning, Service, Ticket, Customer
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
def search_appointment(by=None, id=None):
rv = []
if by == None and id is not None:
rv.append(Appointment[id].to_dict())
else:
query = Appointment.select()
if by is not None:
query.filter(lambda a: getattr(a, by).id == id)
query = datefiltering(query)
for f in query:
rv.append(f.to_dict())
if len(rv) == 0:
raise NotFound("Appointment")
return jsonify(rv)
@api.route("/appointment")
@api.route("/appointment/")
@api.route("/appointment/<id>")
def get_appointment(id=None):
return search_appointment(id=id)
@api.route("/appointment/planning/<id>")
def get_appointment_by_planning(id=None):
return search_appointment("planning", id)
@api.route("/appointment/service/<id>")
def get_appointment_by_service(id):
return search_appointment("service", id)
@api.route("/appointment/customer/<id>")
def get_appointment_by_customer(id):
return search_appointment("customer", id)
@api.route("/appointment/ticket/<id>")
def get_appointment_by_ticket(id):
return search_appointment("ticket", id)
@api.route("/appointment", methods=["POST"])
def new_appointment():
raise NotImplementedError
@api.route("/appointment", methods=["PUT"])
def update_appointment():
raise NotImplementedError
@api.route("/appointment", methods=["DELETE"])
def delete_appointment():
raise NotImplementedError