def post(self): checkUser = UsersModel.find_by_username(get_jwt_identity()) if not checkUser: return {'message': 'Login Required'}, 400 new_access_token = create_access_token(identity=get_jwt_identity(), fresh=True) return {'access_token': new_access_token}, 200
def post(self): _user_login_parser = reqparse.RequestParser() _user_login_parser.add_argument('username', type=str, required=True, help='Username is required') _user_login_parser.add_argument('password', type=str, required=True, help='Password is required') _user_login_parser.add_argument('role', type=str, required=True, choices=['Doctor', 'Patient']) data = _user_login_parser.parse_args() checkUser = UsersModel.find_by_username_role( str(data['username']).lower().strip(), str(data['role']).title().strip()) if not checkUser: return {'message': 'Invalid Details'}, 400 if checkUser and safe_str_cmp(checkUser.password, data['password']): access_token = create_access_token(identity=checkUser.username, fresh=True) refresh_token = create_refresh_token(checkUser.username) return { 'access_token': access_token, 'refresh_token': refresh_token }, 200 return {'message': 'Invalid credentials'}, 400
class User(AbstractModelObject): attributes = { 'id': None, 'email': None, 'password': None, 'created_on': None, 'updated_on': None } protected_attributes = ['password'] required_attributes = ['email', 'password'] _model = UsersModel() def save(self): if self.email: existing_user = User() existing_user.load_by_email(self.email) if existing_user.id: if self.id and self.id == existing_user.id: super().save() else: raise EmailExists('email already exists') super().save() def load_by_email(self, email): users = self._model.get_all_by_index('email', email) if len(users) == 1: self.deserialize(users[0])
def create_user(tipo,nombre)-> UsersModel: user = UsersModel( tipo_usuario= tipo, nombre_usuario= nombre ) db.session.add(user) db.session.commit() return user
def get(self): customer_details = UsersModel.find_by_username(get_jwt_identity()) if not customer_details: return {'username': '******', 'role': 'None'}, 401 current_user_customer = get_jwt_identity() current_user_role = customer_details.role return { 'username': current_user_customer, 'role': current_user_role }, 200
def post(self): data = _user_parser.parse_args() if UsersModel.find_by_username(str(data['username']).lower().strip()): return {'message': 'Username already exists, try another'}, 400 if UsersModel.find_by_email_address( str(data['email_address']).lower().strip()): return { 'message': 'Email Address already registered, try another' }, 400 regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' if not re.search(regex, str(data['email_address']).lower().strip()): return {'message': 'Email id seems invalid, please check it!'}, 400 if len(str(data['password'])) > 16: return {'message': 'Password length exceeds'}, 400 if len(str(data['password'])) < 6: return { 'message': 'Password must be six or more than six characters' }, 400 if data['password'] != data['confirm_password']: return { 'message': 'Password and confirm password not matching' }, 400 try: new_user = UsersModel( str(data['name']).title().strip(), str(data['username']).lower().strip(), str(data['email_address']).lower().strip(), data['password'], data['role']) new_user.save_to_db() access_token = create_access_token(identity=str( data['username']).lower().strip(), fresh=True) refresh_token = create_refresh_token( str(data['username']).lower().strip()) return { 'access_token': access_token, 'refresh_token': refresh_token }, 200 except Exception as e: print(f'Error while saving new user {e}') return {'message': 'Something went wrong'}, 500
def post(self): fetchUserDetail = GetCurrentUserDetails.get(self) user_verify = fetchUserDetail[0] if not user_verify['role'] == 'Patient': return {'message': 'Only patient can create new appointments'}, 401 print(user_verify['username']) data = _appointment_parser.parse_args() checkDoctor = UsersModel.find_by_username_role( str(data['sent_to']).lower().strip(), 'Doctor') if not checkDoctor: return {'message': 'Requested Doctor not found'}, 400 try: userDate = datetime.datetime.strptime( str(data['date']).strip(), "%Y-%m-%d") if userDate.date() < datetime.date.today(): return { 'message': 'Appointment date must be greater than today\'s date' }, 400 except ValueError as e: return {'message': 'Date should be in format YYYY-MM-DD'}, 400 try: time.strptime(str(data['time']).strip(), "%H:%M") except ValueError as e: return {'message': 'Time should be in format HH:MM'}, 400 todayDateTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') userDateTime = f'{userDate.date()} {str(data["time"]).strip()}' if userDateTime <= todayDateTime: return { 'message': 'Appointment date and time must be greater than today\'s date and current time' }, 400 checkOldRequest = AppointmentModel.find_by_patient_date_notAccepted_doctor( str(user_verify['username']).lower().strip(), str(data['sent_to']).lower().strip()) users = list(map(lambda x: x.appointment_json(), checkOldRequest)) for user in users: if datetime.datetime.strptime( str(user['appointmentDate']).strip(), "%Y-%m-%d").date() > datetime.date.today(): return { 'message': 'You already sent appointment to the doctor' }, 400 try: new_appointment = AppointmentModel( str(user_verify['username']).lower().strip(), str(data['date']).strip(), str(data['time']).strip(), str(data['message']).capitalize().lower(), str(data['sent_to']).lower().strip()) new_appointment.save_to_db() return { 'message': f'Your appointment sent to doctor {checkDoctor.name}' }, 200 except Exception as e: print(f'Exception while creating new appointment {e}') return {'message': 'Something went wrong'}, 400