Esempio n. 1
0
def _found_slots(start_date, end_date, professional_id):
    start_date = start_date + datetime.timedelta(
        minutes=(30 - start_date.minute % 30) % 30)
    slots = []
    half_hour = datetime.timedelta(minutes=30)
    professional = Professional.find_by_id(professional_id)
    appointments = Appointment.find_by_professional(professional)
    while start_date + half_hour <= end_date:
        occupied = False
        for appointment in appointments:
            schedule_start_date = dateparser.parse(appointment.start_date)
            schedule_end_date = dateparser.parse(appointment.end_date)
            if not (start_date <= schedule_start_date
                    and start_date + half_hour <= schedule_start_date
                    or start_date >= schedule_end_date
                    and start_date + half_hour >= schedule_end_date):
                occupied = True
                break
        if not occupied:
            slots.append({
                'id':
                hashlib.md5(str(start_date) + professional_id).hexdigest(),
                'start_date':
                start_date.isoformat(),
            })
        start_date += half_hour
    return slots
Esempio n. 2
0
def create_appointment():
    if not request.form:
        abort(400)
    try:
        params = request.form
        professional = Professional.find_by_id(params['professional_id'])
        appointments = Appointment.find_by_professional(professional)
        start_date = dateparser.parse(params['start_date'])
        end_date = dateparser.parse(params['end_date'])
        for appointment in appointments:
            schedule_start_date = dateparser.parse(appointment.start_date)
            schedule_end_date = dateparser.parse(appointment.end_date)
            if not (start_date <= schedule_start_date
                    and end_date <= schedule_start_date
                    or start_date >= schedule_end_date
                    and end_date >= schedule_end_date):
                abort(404)

        appointment = Appointment(
            professional=professional,
            patient=Patient.find_by_id(params['patient_id']),
            start_date=params['start_date'],
            end_date=params['end_date'],
        )

        appointment.save()
    except:
        abort(500)
    return jsonify(appointment.to_dict())
Esempio n. 3
0
def get_appointment_professional_id(professional_id):
    try:
        professional = Professional.find_by_id(professional_id)
        appointments = [
            appointment.to_dict()
            for appointment in Appointment.find_by_professional(professional)
        ]
    except:
        abort(500)
    return json.dumps(appointments)
Esempio n. 4
0
def get_slots(professional_id):
    try:
        professional = Professional.find_by_id(professional_id)
        appointments = Appointment.find_by_professional(professional)
        start_date = dateparser.parse(request.args['start_date'])
        end_date = dateparser.parse(request.args['end_date'])
        slots = _found_slots(start_date, end_date, professional_id)
        for slot in slots:
            print slot["start_date"]
    except:
        abort(500)
    return json.dumps(slots)