def create_database(): db.create_all() ProductType.init_data() Product.init_data() Role.init_data() User.init_data() OrderStatus.init_data()
def post(self): # 获取表单数据 args = parser.parse_args() username = args.get('username') password = args.get('password') email = args.get('email') # re 邮箱验证 mailre = "[a-z0-9_]+@[a-z0-9]+\.[a-z]{2,4}" if len(re.findall(mailre, email, re.I)) != 0: print(username, password, email) u_token = uuid.uuid4() print(u_token) # password = genetrate_password(password) # print(password) # 插入 user = User(username=username, email=email, u_token=u_token) user.generate_password(password) try: db.session.add(user) db.session.commit() # 发送激活邮 # 设置缓存,用来保存用户信息{字典} cache.set(str(u_token), username, timeout=60 * 2) ''' subject, recipients, emailTmp, **kwargs ''' send_mail_util.delay( subject='账号激活', recipients=[email], emailTmp='activationUserEmail', username=username, url='http://127.0.0.1:5000/activation?u_token=' + str(u_token)) # async_send_mail_util(subject='账号激活', # recipients=[email], # emailTmp='activationUserEmail', # username=username, # url='http://127.0.0.1:5000/activation?u_token=' + str(u_token)) except Exception as e: # 回滚 # print(e) logs().error(e) db.session.rollback() return {"msg": "用户已存在", 'status': 422} else: return {"msg": "邮箱格式错误", 'status': 422} return {"msg": "用户注册成功", 'status': 201}
def post(self): args = request.form username = args.get('username', None) password = args.get('password', None) email = args.get('email', None) if not username or not password or not email: return {"msg": "Missing username parameter"}, 400 if UserModel.query.filter_by(username=username).first(): return {"msg": "User always res"}, 400 # write to database u = UserModel(username=username, password=password, email=email) u.save() print(u, u.username) return u
def register(): if request.method == 'POST': username=request.form['inputUsername'] password=request.form['inputPassword'] firstname=request.form['inputFirstname'] lastname=request.form['inputLastname'] address=request.form['inputAddress'] user_exist=User.find_by_username(username) if user_exist: flash('The username already exist') else: User.create_client(username, password, firstname, lastname, address).save_to_db() session['username'] = username return redirect('/') return render_template('register.html')
def login(): if request.method == 'POST': username = request.form['inputUsername'] password = request.form['inputPassword'] login_user=User.check_login(username, password) if login_user: session['username'] = username return redirect('/') else: flash('Incorrect login information') return render_template('login.html')
def checkout(): user = User.find_by_username(session['username']) dict_cart = json.loads(request.form['cart']) registerReceipt = lambda product, quantity: { "name": product.name, "description": f"{quantity} unit(s) - {product.price} EUR per unit", "value": product.price * quantity } receipt = [ registerReceipt(Product.find_by_id(key), dict_cart[key]) for key in dict_cart ] total = sum([register['value'] for register in receipt]) return render_template('checkout.html', user=user, receipt=receipt, total=total)
def complete_transaction(): current_user = User.find_by_username(session['username']) if request.method == 'POST': deliveryAddress = request.form['inputDeliveryAddress'] first_name = request.form['inputFirstName'] last_name = request.form['inputLastName'] billing_address = request.form['inputBillingAddress'] dict_cart = json.loads(request.form['cart']) current_order = Order(first_name, last_name, billing_address, current_user.id) current_order.save_to_db() for product_id in dict_cart: current_order.add_product(product_id, dict_cart[product_id]) if current_user.role.name == 'Administrator': return render_template('orders.html', orders=Order.get_all(), allow_update=True) else: return render_template('orders.html', orders=Order.find_by_user_id(current_user.id), allow_update=False)
def secure_function(*args, **kwargs): if ('username' in session.keys() and session['username'] and User.find_by_username( session['username']).role.name in permissions): return f(*args, **kwargs) return redirect(url_for('user.login'))