from flask import request, jsonify from pony.orm import ObjectNotFound from datetime import datetime from ITPlanning.db import Appointment 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 @api.route("/appointment") @api.route("/appointment/") @api.route("/appointment/") def get_appointment(id=None): rv = None if id: rv = Appointment[id] else: query = Appointment.select() query = datefiltering(query) for f in query: rv += query if rv is None: raise NotFound("Appointment") return jsonify(rv) @api.route("/appointment/planning/") def get_appointment_by_planning(id): rv = None if id is None: raise GenericError("enter an ID") query = Appointment.select(lambda a: a.planning.id == id) query = datefiltering(query) for f in query: rv += f if rv is None: raise NotFound("Appointment") return jsonify(rv) @api.route("/appointment/service/") def get_appointment_by_service(id): rv = None if id is None: raise GenericError("enter an ID") query = Appointment.select(lambda a: a.service.id == id) query = datefiltering(query) for f in query: rv += f if rv is None: raise NotFound("Appointment") return jsonify(rv) @api.route("/appointment/customer/") def get_appointment_by_customer(id): rv = None if id is None: raise GenericError("enter an ID") query = Appointment.select(lambda a: a.customer.id == id) query = datefiltering(query) for f in query: rv += f if rv is None: raise NotFound("Appointment") return jsonify(rv) @api.route("/appointment/ticket/") def get_appointment_by_ticket(id): rv = None if id is None: raise GenericError("enter an ID") query = Appointment.select(lambda a: a.ticket.id == id) query = datefiltering(query) for f in query: rv += f if rv is None: raise NotFound("Appointment") return jsonify(rv) @api.route("/appointment", methods=["POST"]) def new_appointment(): pass @api.route("/appointment", methods=["PUT"]) def update_appointment(): pass @api.route("/appointment", methods=["DELETE"]) def delete_appointment(): pass