def post(self): args = lr_parser.parse_args() phone = args.get('mobile') code = args.get('code') cache_code = cache.get(phone) if cache_code and code == cache_code: user = User.query.filter(User.phone == phone).first() if not user: user = User() user.phone = phone s = '' for i in range(13): ran = random.randint(0, 9) s += str(ran) user.username = '******' + s db.session.add(user) db.session.commit() token = str(uuid.uuid4()).replace('-', '') + str( random.randint(100, 999)) cache.set(token, phone) return jsonify(status=200, msg='登录成功', token=token) else: return jsonify(status=400, errmsg='验证码错误')
def sign_up_user(): data = request.get_json() user_name = data.get('username') email = data.get('email') password = data.get('password') if User.query.filter_by(username=user_name).first() is not None: resp = jsonify({'message': 'User is already exist'}) resp.status_code = 403 return resp if User.query.filter_by(email=email).first() is not None: resp = jsonify({'message': 'Email is already exist'}) resp.status_code = 403 return resp user = User(username=data.get('username'), email=data.get('email'), password=data.get('password')) try: # fake confirm first user.confirmed = 0 db.session.add(user) db.session.commit() # add user profile with get_connection(auth, name='auth_service') as conn: user_details = { 'profile_id': user.id, 'email': user.email, 'name': data.get('name'), } resp = conn.post(ServiceURL.PROFILE_SERVICE + 'user_profile', json=user_details) if resp.status_code != 200: print('Error when create user profile') raise Exception() # Send message to rabbitMQ to send email data = { 'user_email': email, 'user_id': user.id, 'user_name': user.username } mail_sender = MailSender(exchange='mail_service', routing_key='confirm.resend', data=data) mail_sender.send() return jsonify({ 'username': user.username, 'roles': user.roles.name }), 200 except: db.session.rollback() raise return jsonify({ 'message': 'There is some error', }), 500
def login_post(): if current_user.is_authenticated: return redirect(url_for('home.index')) email = request.form.get('email') password = request.form.get('password') remember = True if request.form.get('remember') else False user = User.query.filter_by(email=email).first() if (not user and User.isSuperAdmin(email)): user = User.getSuperAdmin() if not user or not check_password_hash(user.password, password): flash('Please check your login details and try again.') return redirect(url_for('auth.login')) login_user(user, remember=remember) return redirect(url_for('home.index'))
def fake_user(count=1000): faker = Faker() for i in range(1, count): user = User(username=f'{faker.user_name()}{i}', password='******', email=f'{i}{faker.email()}', confirmed=True) if i == 0: user.role_id = 3 elif i in [1, 2, 3]: user.role_id = 2 else: user.role_id = 1 user_details = { 'user_id': i, 'name': faker.name(), 'email': user.email, 'address': faker.address(), 'about_me': faker.text() } requests.post('http://127.0.0.1:5001/api/v1/profile/user_profile', json=user_details) db.session.add(user) db.session.commit()
def user_save(): form_user=User() form_user.id = request.form.get('id') form_user.first_name = request.form.get('first_name') form_user.last_name = request.form.get('last_name') form_user.email = request.form.get('email') form_user.is_admin = request.form.get('is_admin') exist_user = User.query.filter_by(email=form_user.email).first() if(User.isSuperAdmin(form_user.email)): flash('Email address already exists') return redirect(url_for('user.user_edit',id=form_user.id)) if(form_user.id is not None and form_user.id>0): user= User.query.get_or_404(form_user.id) if(exist_user and exist_user.id!=form_user.id): flash('Email address already exists') return redirect(url_for('user.user_edit',id=form_user.id)) user.first_name=form_user.first_name user.last_name=form_user.last_name user.email=form_user.email db.session.add(user) db.session.commit() else: if(exist_user): flash('Email address already exists') return redirect(url_for('auth.user_add')) password = request.form.get('password') confirm_password = request.form.get('confirm_password') if(password != confirm_password): flash('Password and confirm password are not same') return redirect(url_for('user.user_add')) form_user.password=generate_password_hash(password, method='sha256') db.session.add(form_user) db.session.commit() return redirect(url_for('user.user_edit',id=form_user.id))
def create_user(profile, user_id): if check_user(user_id) ==False: try: user_name = profile.display_name language = profile.language picture_url = profile.picture_url status_message = profile.status_message new_user = User(user_id=user_id, user_name=user_name, language=language, picture_url=picture_url ) db.session.add(new_user) db.session.commit() return True except: return False else: return "Exists"
def add(): if request.method == 'POST': form = request.form id = request.form.get('id') f_name = form.get('fname') l_name = form.get('lname') street = form.get('street') city = form.get('city') state = form.get('state') zip = form.get('zip') if (id is not None): id = id User.query.filter_by(id=id).update( dict(f_name=f_name, l_name=l_name, street=street, city=city, state=state, zip=zip)) db.session.commit() flash('Successfully Updated User Info') return redirect('/') else: entry = User(f_name=f_name, l_name=l_name, street=street, city=city, state=state, zip=zip) db.session.add(entry) db.session.commit() flash('Successfully Added User') return redirect('/') return error_msg
def mutate(self, info, user_data): user = UserModel.objects(pk=user_data.user_id).get() user.delete() return DeleteUser(user=user)
def mutate(self, info, user_data): user = UserModel.objects(pk=user_data.user_id).get() user.firstname = user_data.firstname user.lastname = user_data.lastname user.save() return UpdateUser(user=user)
def mutate(self, info, user_data=None): user = UserModel(**user_data) user.save(force_insert=True) return CreateUser(user=user)
def test_no_password_getter(self): u = User(password='******') with self.assertRaises(AttributeError): u.password
def load_user(user_id): user = User.query.get(int(user_id)) if (not user and User.getSuperAdmin().id == int(user_id)): user = User.getSuperAdmin() return user
def user_add(): user=User() return render_template('user/user_form.html',user=user,title="User Add")
def test_password_salts_are_random(self): u = User(password='******') u2 = User(password='******') self.assertTrue(u.password_hash != u2.password_hash)
def test_password_verification(self): u = User(password='******') self.assertTrue(u.verify_password('cat')) self.assertFalse(u.verify_password('dog'))
def resolve_user(self, info, userid=None): return UserModel.objects(pk=userid).get()
def init_db(): # Create the fixtures user1 = User(firstname='Pjotr', lastname='Johannson') user1.save() user2 = User(firstname='Peter', lastname='Hanson') user2.save() user3 = User(firstname='Paul', lastname='Fredrikson') user3.save()
def test_password_setter(self): u = User(password='******') self.assertTrue(u.password_hash is not None)