def record_add(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): raise Exception('Access token is not valid.') user = auxiliary_metods.get_user(token) except Exception: return jsonify({'error_code': '1', 'error_msg': 'Access token is not valid.'}) try: doctor_id = int(request.form['doctor_id']) except Exception: return jsonify( {'error_code': '2', 'error_msg': 'Value [doctor_id] is not valid, value should be a number.'}) if doctor_id == user.id: return jsonify( {'error_code': '3', 'error_msg': 'It is impossible to enroll in the reception.'}) check_doctor = db.session.query(models.Doctor).filter(models.Doctor.id == doctor_id).first() if check_doctor == None: return jsonify( {'error_code': '4', 'error_msg': 'Value [doctor_id] is not found.'}) try: date_create = datetime.now() date_record = request.form['date_record'] date_record = datetime.strptime(date_record, "%d-%m-%Y %H:%M") except Exception: return jsonify( {'error_code': '5', 'error_msg': 'Value [date_record] is not valid.'}) min_interval = (datetime.now() + timedelta(days=1)) max_interval = (datetime.now() + timedelta(days=30)) if not date_record > min_interval and date_record < max_interval: return jsonify( {'error_code': '6', 'error_msg': 'You can not enroll in the reception now.'}) check_time_record = db.session.query(models.Record) \ .filter(models.Record.date_record == date_record) \ .filter(models.Record.doctor_id == doctor_id) \ .first() if not check_time_record == None: return jsonify( {'error_code': '7', 'error_msg': 'This time is already busy.'}) try: record = models.Record(doctor_id=doctor_id, pacient_id=user.id, date_record=date_record, date_create=date_create) db.session.add(record) db.session.commit() return jsonify({'msg': 'Record successfully added.'}) except Exception as e: print(e) db.session.rollback() return jsonify({'error_code': '8', 'error_msg': 'Error connect to database.'}) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def account_getInfo(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): raise NameError('Access token is not valid.') except: return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) user_schema = models.UserSchema() output = user_schema.dump(user).data if os.path.exists(os.path.join('dist', output['photo_path'])) == False: output['photo_path'] = 'assets\\img\\user.jpg' return jsonify({'account': output}) return jsonify({ 'error_code': '0', 'error_msg': 'User authorization failed: post parameters [access_token] not found.' })
def record_del(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): raise('Access token is not valid.') user = auxiliary_metods.get_user(token) except Exception: return jsonify({'error_code': '1', 'error_msg': 'Access token is not valid.'}) if user == None: return jsonify({'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.'}) try: record_id = int(request.form['record_id']) except Exception: return jsonify( {'error_code': '3', 'error_msg': 'Value [record_id] is not valid, value should be a number.'}) try: db.session.query(models.Record).filter(models.Record.id == record_id).delete() db.session.commit() return jsonify({'Good': 'Deleted record.' , 'record_id': record_id}) except: db.session.rollback() return jsonify({'error_code': '4', 'error_msg': 'Try again later.'}) return jsonify({'records'})
def medcard_info_get(): if request.method == 'POST': token = request.form['access_token'] if not validate.hash(token): return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) result_query = db.session.query(models.MedcardInfo) \ .filter(models.MedcardInfo.user_id == user.id) \ .first() if result_query == None: try: medcard_info = models.MedcardInfo(user_id=user.id, blood_type='0', main_diagnosis='-', allergic_history='-', drug_intolerance='-', physical_injury='-') db.session.add(medcard_info) db.session.commit() medcard_info_schema = models.MedcardInfoSchema() output = medcard_info_schema.dump(medcard_info).data return jsonify({'MedcardInfo': output}) except Exception: return jsonify({ 'error_code': '3', 'error_msg': 'Try again later.' }) medcard_info_schema = models.MedcardInfoSchema() output = medcard_info_schema.dump(result_query).data return jsonify({'MedcardInfo': output}) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def medcard_vaccination_del(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) except: return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) try: id = int(request.form['id']) except: return jsonify({ 'error_code': '2', 'error_msg': 'Value [id] is not valid.' }) try: db.session.query(models.MedcardVaccination).filter( models.MedcardVaccination.id == id).delete() db.session.commit() return jsonify({'Good': 'Deleted vaccination.', 'id': id}) except: db.session.rollback() return jsonify({ 'error_code': '3', 'error_msg': 'Try again later.' }) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def record_get(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): return jsonify({'error_code': '1', 'error_msg': 'Access token is not valid.'}) user = auxiliary_metods.get_user(token) except Exception: return jsonify({'error_code': '1', 'error_msg': 'Access token is not valid.'}) if user == None: return jsonify({'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.'}) query = db.session.query(models.Record, models.Doctor, models.User, models.Hospital, models.TypeDoctor) \ .join(models.Doctor) \ .join(models.User) \ .join(models.Hospital) \ .join(models.TypeDoctor) \ .filter(models.Record.pacient_id == user.id) \ .all() list_records = [] for record, doctor, user, hospital, type_doctor in query: records_schema = models.RecordSchema() record_json = records_schema.dump(record).data doctor_schema = models.DoctorSchema() doctor_json = doctor_schema.dump(doctor).data user_schema = models.UserSchema( only=('last_name', 'first_name', 'mid_name', 'photo_path', 'sex')) user_json = user_schema.dump(user).data hospital_schema = models.HospitalSchema() hospital_json = hospital_schema.dump(hospital).data type_doctor_schema = models.TypeDoctorSchema() type_doctor_json = type_doctor_schema.dump(type_doctor).data list_records.append({'record': record_json, 'doctor': doctor_json, 'user': user_json, 'hospital': hospital_json, 'type_doctor': type_doctor_json}) return jsonify({'records': list_records})
def medcard_complaints_add(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): raise Exception('Access token is not valid.') except: return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def medcard_info_set(): params = {} if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) except: return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) try: params['blood_type'] = request.form['blood_type'] if not validate.blood_type(str(params['blood_type']).replace(' ')): return jsonify({ 'error_code': '3', 'error_msg': 'Value [blood_type] is not valid.' }) except: pass try: params['main_diagnosis'] = request.form['main_diagnosis'] if not validate.description(params['main_diagnosis']): return jsonify({ 'error_code': '4', 'error_msg': 'Value [main_diagnosis] is not valid.' }) except: pass try: params['allergic_history'] = request.form['allergic_history'] if not validate.description(params['allergic_history']): return jsonify({ 'error_code': '5', 'error_msg': 'Value [allergic_history] is not valid.' }) except: pass try: params['drug_intolerance'] = request.form['drug_intolerance'] if not validate.description(params['drug_intolerance']): return jsonify({ 'error_code': '6', 'error_msg': 'Value [drug_intolerance] is not valid.' }) except: pass try: params['physical_injury'] = request.form['physical_injury'] if not validate.description(params['physical_injury']): return jsonify({ 'error_code': '7', 'error_msg': 'Value [physical_injury] is not valid.' }) except: pass if len(params) == 0: return jsonify({ 'error_code': '8', 'error_msg': 'Specify at least one parameter.' }) result_query = db.session.query(models.MedcardInfo) \ .filter(models.MedcardInfo.user_id == user.id) \ .first() if result_query == None: try: medcard_info = models.MedcardInfo(user_id=user.id, blood_type='0', main_diagnosis='-', allergic_history='-', drug_intolerance='-', physical_injury='-') db.session.add(medcard_info) db.session.commit() except Exception: db.session.rollback() return jsonify({ 'error_code': '9', 'error_msg': 'Try again later.' }) try: print(params) db.session.query(models.MedcardInfo) \ .filter(models.MedcardInfo.user_id == user.id).update(params) db.session.commit() return jsonify({'Set': 'True'}) except Exception: db.session.rollback() return jsonify({ 'error_code': '9', 'error_msg': 'Try again later.' }) return jsonify({'MedcardInfo': output}) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def medcard_vaccination_add(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) except: return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) try: type_vaccination_id = int(request.form['type_vaccination_id']) result = db.session.query(models.TypeVaccination.id).filter( models.TypeVaccination.id == type_vaccination_id).first() if result == None: raise Exception() except: return jsonify({ 'error_code': '3', 'error_msg': 'Value [type_vaccination_id] is not valid.' }) try: reaction = int(request.form['reaction']) if not (reaction > 0 and reaction < 100): raise Exception() except: return jsonify({ 'error_code': '4', 'error_msg': 'Value [reaction] is not valid.' }) try: date_vaccination = request.form['date_vaccination'] date_vaccination = datetime.strptime(date_vaccination, "%d-%m-%Y %H:%M") if not date_vaccination < datetime.now(): raise Exception() except Exception as ex: print(ex) return jsonify({ 'error_code': '5', 'error_msg': 'Value [date_vaccination] is not valid.' }) try: medcard_vaccination = models.MedcardVaccination( user_id=user.id, type_vaccination_id=type_vaccination_id, reaction=reaction, date_vaccination=date_vaccination) db.session.add(medcard_vaccination) db.session.commit() id = medcard_vaccination.id return jsonify({'Good': 'Add vaccination.', 'id': id}) except Exception as ex: print(ex) db.session.rollback() return jsonify({ 'error_code': '6', 'error_msg': 'Try again later.' }) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def medcard_type_vaccination_add(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) except: return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) try: name = request.form['name'] if not validate.description(name): return jsonify({ 'error_code': '3', 'error_msg': 'Value [name] is not valid.' }) except: return jsonify({ 'error_code': '3', 'error_msg': 'Value [name] is not valid.' }) try: description = request.form['description'] if not validate.description(description): return jsonify({ 'error_code': '4', 'error_msg': 'Value [description] is not valid.' }) except: return jsonify({ 'error_code': '4', 'error_msg': 'Value [description] is not valid.' }) try: type_vaccination = models.TypeVaccination(name=name, description=description) db.session.add(type_vaccination) db.session.commit() return jsonify({'Good': 'Add type vaccination.'}) except: db.session.rollback() return jsonify({ 'error_code': '5', 'error_msg': 'Try again later.' }) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def account_set_photo(): MAX_FILE_SIZE = 1024 * 1024 + 1 if request.method == 'POST': try: token = request.form['access_token'] print(token) if not validate.hash(token): raise NameError('Access token is not valid.') except: return jsonify({ 'error_code': '1', 'error_msg': 'Access token is not valid.' }) user = auxiliary_metods.get_user(token) if user == None: return jsonify({ 'error_code': '2', 'error_msg': 'User authorization failed: no access token passed.' }) try: file = request.files['file'] if file and validate.file_type(file.filename): id = auxiliary_metods.id_generator() filename = file.filename.split('.')[1] pub_name = os.path.join('assets', 'photo', id + '.' + filename) file.save( os.path.join('dist', 'assets', 'photo', id + '.' + filename)) try: if user.photo_path.find('user.jpg') == -1: os.remove(os.path.join('dist', user.photo_path)) except: print('Error delete last photo user') try: db.session.query(models.User) \ .filter(models.User.id == user.id) \ .update({'photo_path': pub_name}) db.session.commit() except Exception: db.session.rollback() return jsonify({ 'error_code': '3', 'error_msg': 'Error connect to database.' }) return jsonify({'photo': pub_name}) else: return jsonify({ 'error_code': '3', 'error_msg': 'Error file type.' }) except Exception as ex: print(ex) return jsonify({ 'error_code': '0', 'error_msg': 'Use post query parameters.' })