def register(): try: result = {'response': 'success', 'info': ''} name = request.json.get("name", None) password = request.json.get("password", None) email = request.json.get("email", None) if None in [name, password, email]: result.update({"response": "fail", "info": "请求参数错误"}) return jsonify(result) en_password = AESCipher.encrypt(password), with get_session() as db_session: u = db_session.query(User).filter( or_(User.name == name, User.email == email)).first() if u: result.update({"response": "fail", "info": "当前用户已存在"}) return jsonify(result) else: u = User() u.name = name u.password = en_password u.email = email db_session.add(u) db_session.commit() return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def create(cls, **params) -> (bool, any): """ 新增用户关注商品 """ user_id = params['user_id'] goods_id = params['goods_id'] user = User.get_by_id(user_id) if not user: return False, '添加关注失败,用户不存在' goods = Goods.get_by_id(goods_id) if not goods: return False, '添加关注失败,商品不存在' query = UserGoods.select().where(UserGoods.user_id == user_id, UserGoods.goods_id == goods_id, UserGoods.deleted == 0).first() if query: return False, '添加关注失败,已关注过商品,无需重复关注' user_goods = UserGoods() user_goods.user_id = user_id user_goods.user_name = user.name user_goods.goods_id = goods_id user_goods.goods_name = goods.name user_goods.website_type = goods.website_type user_goods.prd_no = goods.prd_no user_goods.prd_opt_no = goods.prd_opt_no user_goods.save() return True, user_goods
def api_login_user(): '''用户登录''' # 验证表单 data = {'m':None,'p':None} for key in data.keys(): if key not in request.form: if key == "m": message = "邮箱地址" elif key == "p": message = "密码" else: pass return jsonify({'code':'400','type':'错误','body':'{}需要被填写'.format(message)}),400 else: data[key] = request.form[key] # 验证邮箱 if not is_mail_address(raw=data["m"]): return jsonify({'code':'400','type':'错误','body':'错误的邮箱地址'}),400 # 验证密码 else: pass mail = data['m'] password = data['p'] # 查询用户 q = User().info(user_id=to_md5(raw=mail)) if q["code"] == 200: r = q["query"] if r.user_key == to_md5(raw=password,mix_text=r.user_salt): return jsonify({ "user_id":r.user_id, "user_key":r.user_key }) else: return jsonify({'code':403,'type':'错误','body':'邮箱与密码不匹配'}),403 else: return jsonify({'code':q["code"],'type':'错误','body':q["errmsg"]}),q["code"]
def api_new_user(): '''新建用户''' # 验证表单 data = {'m':None,'p1':None,'p2':None} for key in data.keys(): if key not in request.form: if key == "m": message = "邮箱地址" elif key == "p1": message = "第一次密码" elif key == "p2": message = "第二次密码" else: pass return jsonify({'code':'400','type':'错误','body':'{}需要被填写'.format(message)}),400 else: data[key] = request.form[key] # 验证邮箱 if not is_mail_address(raw=data["m"]): return jsonify({'code':'400','type':'错误','body':'错误的邮箱地址'}),400 # 验证密码 elif not data["p1"] == data["p2"]: return jsonify({'code':'400','type':'错误','body':'两次密码不一致'}),400 else: pass # 新建用户 r = User().new(mail=data["m"],password=data["p1"]) if r["code"] == 200: return "",200 else: return jsonify({'code':r["code"],'type':'错误','body':r["errmsg"]}),r["code"]
def register_user(): # If POST in request, user has already completed and submitted the registration form # After saving the user to database, the user is forwarded to a new alerts page if request.method == 'POST': email = request.form['email'] password = request.form['password'] try: User.register_user(email, password) session['email'] = email return render_template('alerts/index.html') return email except UserErrors.UserError as e: # user errors are defined in model.user.errors return e.message # If no POST in request, user is presented new registration page to be completed return render_template('users/register.html')
def create(cls, **params) -> (bool, any): query = User.select().where(User.name == params['name'], User.deleted == 0) if query: return False, '添加失败,用户已存在' user = User() user.name = params['name'] user.mobile = params.get('name') user.address = params.get('address') user.save() return True, user
def find_page(cls, **params) -> dict: page = params['page'] page_size = params['page_size'] keyword = params.get('keyword') conditions = (User.deleted == 0, ) if keyword: conditions += (User.name.contains(keyword), ) query = User.select().where(*conditions).order_by(-User.create_time) total_page, count, page, query_list = cls.page_count_list_process( query, page, page_size) pc_list = MysqlExtend.mysql_to_python(query_list, pop_keys=['deleted']) return dict(total_page=total_page, count=count, page=page, data_list=pc_list)
def login_user(): # If POST in request, then the user has submitted the form with content if request.method == 'POST': email = request.form['email'] password = request.form['password'] try: if User.is_login_valid(email, password): session['email'] = email alerts = Alert.find_many_by_email(email) return render_template('alerts/index.html', alerts=alerts) except UserErrors.UserError as e: # user errors are defined in model.user.errors return e.message # If no POST in request user is presented the login form to be completed return render_template('users/login.html')
def decoration(*args, **kwargs): if request.method == "OPTIONS": return make_response("") if "x-user-id" in request.headers: if "x-user-key" in request.headers: input_user_id = request.headers["x-user-id"] input_user_key = request.headers["x-user-key"] if len(input_user_id) != 32: return "",400 if len(input_user_key) != 32: return "",400 q = User().info(user_id=input_user_id) if q["code"] == 200: r = q["query"] if r.user_key == input_user_key: return function(user_id=r.user_id,user_key=r.user_key,*args, **kwargs) return "",403
def delete(cls, **params): query = User.get_by_id(params['id']) query.deleted = True query.update_time = datetime.now() query.save()
def init_user(): user = User() user.name = '李雷' user.mobile = '13771801234' user.address = '地址' user.save() user2 = User() user2.name = '韩梅梅' user2.mobile = '13771801204' user2.address = '地址' user2.save()
def _create_user(self, user_name): user = User(user_name) self.user_repository.create(user) return user
def __post_init__(self): self.item = Item.get_by_id(self.item_id) self.user = User.find_by_email(self.user_email)
def get_it(self, user_name): timeline = Timeline() posts_by_user = self.user_repository.get_posts_by_user(User(user_name)) timeline.add(posts_by_user) return timeline