def _populate_professional(params): addrezz = params['address'] professional = Professional( username=params['username'], email=params['email'], first_name=params['first_name'], last_name=params['last_name'], birthdate=params['birthdate'], gender=params['gender'], specialties=[], rating=params['rating'], address=Address( street=addrezz['street'], number=addrezz.get('number'), complement=addrezz.get('complement'), neighborhood=addrezz['neighborhood'], city=addrezz['city'], state=addrezz['state'], country=addrezz['country'], zip_code=addrezz['zip_code'], ), ) professional.image_url = params.get('image_url') for specialty in params['specialties']: specialty_id = specialty['id'] professional.specialties.append(Specialty.find_by_id(specialty_id)) for insurance in params['insurances']: insurance_id = insurance['id'] professional.specialties.append(Insurance.find_by_id(insurance_id)) return professional
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
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())
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)
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)
def search_professionals(): professionals = Professional.get_all() return json.dumps(_filter_professionals(professionals, request.args))
def get_professional(professional_id): try: professional = Professional.find_by_id(professional_id) except: abort(500) return jsonify(professional.to_dict())
def get_professionals(): return json.dumps(Professional.get_all())