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 doctor_all(): try: query = db.session \ .query(models.Doctor, models.User, models.TypeDoctor) \ .join(models.User) \ .join(models.TypeDoctor) \ .group_by(models.Doctor.user_id) \ .all() except Exception: return jsonify({ 'error_code': '1', 'error_msg': 'Doctors does not exist.' }) doctors = [] for doctor, user, type_doctor in query: doctor_schema = models.DoctorSchema() doctor_json = doctor_schema.dump(doctor).data user_schema = models.UserSchema(only=('id', 'last_name', 'first_name', 'mid_name', 'photo_path', 'sex')) user_json = user_schema.dump(user).data type_doctor_schema = models.TypeDoctorSchema() type_doctor_json = type_doctor_schema.dump(type_doctor).data subquery = db.session \ .query(models.Doctor, models.Hospital) \ .join(models.Hospital) \ .filter(models.Doctor.user_id == user.id) \ .all() hosp_list = [] for doc, hosp in subquery: hospital_schema = models.HospitalSchema() hospital_json = hospital_schema.dump(hosp).data hosp_list.append(hospital_json) doctors.append({ 'doctor': doctor_json, 'user': user_json, 'hospitals': hosp_list, 'type_doctor': type_doctor_json }) return jsonify({'count': len(query), 'doctors': doctors})
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 doctor_get_id(id): try: hospital_id = int(id) except Exception: return jsonify({ 'error_code': '1', 'error_msg': 'Value [id] is not valid, value should be a number.' }) try: doctor, user, hospital, type_doctor = db.session \ .query(models.Doctor, models.User, models.Hospital, models.TypeDoctor) \ .join(models.User) \ .join(models.Hospital) \ .join(models.TypeDoctor) \ .filter(models.User.id == id) \ .first() except Exception: return jsonify({ 'error_code': '2', 'error_msg': 'Doctor does not exist.' }) doctor_schema = models.DoctorSchema() doctor_json = doctor_schema.dump(doctor).data user_schema = models.UserSchema(only=('id', '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 return jsonify({ 'doctor': doctor_json, 'user': user_json, 'hospital': hospital_json, 'type_doctor': type_doctor_json })
def doctor_in_hospital(id): try: hospital_id = int(id) except Exception: return jsonify({ 'error_code': '1', 'error_msg': 'Value [id] is not valid, value should be a number.' }) try: query = db.session \ .query(models.Doctor, models.User, models.Hospital, models.TypeDoctor) \ .join(models.User) \ .join(models.Hospital) \ .join(models.TypeDoctor) \ .filter(models.Hospital.id == id) \ .all() except Exception: return jsonify({ 'error_code': '2', 'error_msg': 'Doctors does not exist.' }) doctors = [] for doctor, user, hospital, type_doctor in query: doctor_schema = models.DoctorSchema() doctor_json = doctor_schema.dump(doctor).data user_schema = models.UserSchema(only=('id', 'last_name', 'first_name', 'mid_name', 'photo_path', 'sex')) user_json = user_schema.dump(user).data type_doctor_schema = models.TypeDoctorSchema() type_doctor_json = type_doctor_schema.dump(type_doctor).data doctors.append({ 'doctor': doctor_json, 'user': user_json, 'type_doctor': type_doctor_json }) return jsonify({'count': len(query), 'doctors': doctors})
def create_user(user_info): username = user_info.get('username') email = user_info.get('email') user_type = user_info.get('user_type') password = user_info.get('password') # will add hashing new_user = m.User( username=username, email=email, user_type=user_type) # Create an instance of the User class new_user.set_password(password) user_schema = m.UserSchema() user_data, user_err = user_schema.load(user_info) if user_err: print(user_err) return None else: m.db.session.add(new_user) # Adds new User record to database m.db.session.commit() return new_user
def get_users(): users_schema = models.UserSchema(many=True) users = models.User.query.all() return users_schema.dump(users).data
def get_user_by_id(_id): user_schema = models.UserSchema() user = models.User.query.filter(models.User.id == _id).one() return user_schema.dump(user).data
def get_user_by_email(email): user_schema = models.UserSchema() user = models.User.query.filter(models.User.email == email).one() return user_schema.dump(user).data
def get_user_by_username(user_name): user_schema = models.UserSchema() user = models.User.query.filter(models.User.username == user_name).one() return user_schema.dump(user).data