def add_test_users(): """ Method adds users to the database for testing """ _pword = "test" user = User(email='*****@*****.**', password=_pword) user2 = User(email='*****@*****.**', password=_pword) db.session.add(user) db.session.add(user2) db.session.commit()
def user_logout(user_id): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) app.logger.info("logout: %s" % user_id) user = User(user_id) ret = user.logout() ret_json = jsonify(ret) app.logger.info("logout %s:[%s]" % (user_id, ret_json)) return ret_json
def send_score(user_id): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) user = User(current_user.user_id) ret = user.show_user() ret_json = jsonify(ret) app.logger.info("send_score %s:[%s]" % (user_id, ret_json)) return ret_json
def test_add_user(self): """ Method checks that add user method actually adds a user to the database """ _pword = "test" user = User(email='*****@*****.**', password=_pword) check = user.add_user() self.assertTrue(check, "User should be added") self.assertTrue( user.id, "User doesnot contain id so he is not added to the db")
def test_no_repeated_users_added(self): """ Method checks that add user method actually adds a user to the database """ _pword = "test" user = User(email='*****@*****.**', password=_pword) check = user.add_user() self.assertFalse(check, "User should already exist") self.assertFalse( user.id, "User doesnot contain id so he is not added to the db")
def add_friend_confirm(): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) if request.json is None or 'friend' not in request.json: app.logger.error("missing something:friend key is lost") abort(400) friend = request.json.get('friend') user = User(current_user.user_id) ret = user.add_friend_confirm(friend) ret_json = jsonify(ret) app.logger.info("add_friend_confirm %s:[%s]" % (friend, ret_json)) return ret_json
def get_blacks(user_id): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) if current_user.user_id != user_id: if current_user.user_id != 'admin': app.logger.error("access deny for user not the true user") abort(400) user = User(user_id) ret = user.get_blocks(user_id) ret_json = jsonify(ret) app.logger.info("black_sb %s:[%s]" % (user_id, ret_json)) return ret_json
def user_upload_head_img(user_id): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) app.logger.info("start upload file") if current_user.user_id != user_id: app.logger.warn("user not assist:%s,%s\n", current_user.user_id, user_id) abort(401) # upload new head portrait to store and update info's url post_file = request.files.get('file1') app.logger.info("files :%s" % request.files) if post_file: # get file and save it to local tmp fname = secure_filename(post_file.filename) ext_name = fname.split('.')[-1] obj_id = str(ObjectId()) pic_name = '%s.%s' % (obj_id, ext_name) localfile = os.path.join(app.config['UPLOAD_FOLDER'], pic_name) app.logger.info("start upload file to local store:[%s],[%s]" % (fname, localfile)) post_file.save(localfile) # upload file to oss pic_url = 'zuohaoshi/%s' % current_user.user_id # pic_name = '%s' % fname # current_user.user_id + ext_name file_url = upload_file_to_store(pic_url, pic_name, localfile) if file_url is None: app.logger.error("file upload failed") abort(400) app.logger.info("end upload file to store:%s\n" % file_url) # update user's portrait info img_info = dict() img_info['head_img'] = '%s' % file_url # delete local tmp file os.remove(localfile) user = User(current_user.user_id) ret = user.modify_user(img_info) ret_json = jsonify(dict(ret, **img_info)) app.logger.info("modify user head image %s:[%s,%s]\n" % (current_user.user_id, ret, img_info)) return ret_json else: app.logger.error("missing something:file key is lost") abort(400)
def un_black_sb(): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) if request.json is None or 'black' not in request.json: app.logger.error("missing something:follow key is lost") abort(400) black = request.json.get('black') user = User(current_user.user_id) ret = user.unblock(current_user.user_id, black) ret_json = jsonify(ret) app.logger.info("black_sb %s:[%s]" % (black, ret_json)) return ret_json
def un_follow_sb(user_id): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) if request.json is None or 'follow' not in request.json: app.logger.error("missing something:follow key is lost") abort(400) follow = request.json.get('follow') user = User(current_user.user_id) ret = user.un_follow_sb(follow) ret_json = jsonify(ret) app.logger.info("un_follow_sb %s:[%s]" % (user_id, ret_json)) return ret_json
def modify_user(): app.logger.info("modify_user req:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) user = User(current_user.user_id) info = request.json if info is None: app.logger.error("missing something:info key is lost") abort(400) ret = user.modify_user(info) ret_json = jsonify(ret) app.logger.info("modify_user %s:[%s]" % (current_user.user_id, ret)) return ret_json
def del_user(): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) app.logger.info("current_user :%s" % current_user.user_id) user_id = request.json.get("uid") if user_id is None: app.logger.error("missing something:uid key is lost") abort(400) # may delete by self or admin user = User(current_user.user_id) ret = user.del_user(user_id) ret_json = jsonify(ret) app.logger.info("logout %s:[%s]" % (user_id, ret_json)) return ret_json
def verify_token(token=None): """ Verifies the token before a restricted application process occurs """ token = request.headers.get('x-access-token') or token user_id = User.verify_authentication_token(token) if user_id: g.current_user = User.query.filter_by(id=user_id).first() return True return False
def post(self): """ Handles post requests for registration of a new user """ post_data = request.get_json() fname = strip_white_space(post_data.get('fname')) or None lname = strip_white_space(post_data.get('lname')) or None email = strip_white_space(post_data.get('email'), skip_check_symbols=True) or None password = strip_white_space(post_data.get('password')) or None if not validate_email(email): return abort(400, 'Invalid Email Address') if not fname or not lname or not fname.isalpha() or not lname.isalpha( ): return abort(400, "First and last name must be provided") if not password: return abort(400, "Password must be provided") user = User(email=email, fname=fname.capitalize(), lname=lname.capitalize(), password=password) try: check = user.save_user() if check: auth_token = user.generate_authentication_token() user_data = dict(data=marshal(user, USER)) response = { 'status': 'success', 'message': 'Successfully Registered', 'auth_token': auth_token.decode('utf-8') } response.update(user_data) return response, 201 else: response = {'status': 'fail', 'message': 'User Exists'} return response, 409 except Exception as e: return abort(500, 'Error creating your account:{}'.format(e))
def user_login(): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) account = request.json.get('account') password = request.json.get('passwd') if account is None or password is None: app.logger.error("missing something") abort(400) ret = User.login(account, password) ret_json = jsonify(ret) app.logger.info("user_login %s:[%s]" % (account, ret_json)) return ret_json
def insert(): user1 = User(name='张三', password='******', email='*****@*****.**', gender=True) user2 = User(name='李四', password='******', email='*****@*****.**', gender=True) user3 = User(name='王五', password='******', email='*****@*****.**', gender=True) user4 = User(name='莫愁', password='******', email='*****@*****.**', gender=False) user5 = User(name='凤姐', password='******', email='*****@*****.**', gender=False) db.session.add_all([user1, user2, user3, user4, user5]) db.session.commit() return '插入成功'
def register_user(): app.logger.info("request:[%s],[%s],[%s]" % (request.headers, request.args, request.json)) # json:post,args:get,header have format # import pydevd # pydevd.settrace('192.168.3.1', port=12345, stdoutToServer=True, stderrToServer=True) # get identify code if request.method == 'GET': account = request.args.get("account") if account is None: app.logger.error("missing something") abort(400) # missing phone_num ret = User.register_user(account, None, None) return jsonify(ret) elif request.method == 'POST': account = request.json.get('account') identify_code = request.json.get('identify_code') password = request.json.get('passwd') if account is None or identify_code is None or password is None: app.logger.error("missing something") abort(400) ret = User.register_user(account, identify_code, password) return jsonify(ret)
def new_user(): user = User(**request.get_json()) db.session.add(user) db.session.commit() return '', 201, {'Location': url_for('api.get_user', id=user.id)}