def recharge(): if request.method == 'POST': email = request.form.get("email") if not (email): return jsonify(result=-600, msg=get_error_msg(-600)) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.get_user_info_by_email(email) if result is None: return jsonify(result=-106, msg=get_error_msg(-106)) return jsonify(result=1, msg='Success', user=result) elif request.method == 'PUT': email = request.form.get("email") if not (email): return jsonify(result=-600, msg=get_error_msg(-600)) total = request.form.get("total") if not (total): return jsonify(result=-600, msg=get_error_msg(-600)) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) kwargs = type('Dummy', (object, ), {"email": email, "total": total}) result = _db_process.admin_topup_user(kwargs) return jsonify(result=1, msg='Success') return render_template('admin/recharge.html')
def update_order_detail(): form_dic = request.form.to_dict() for key in form_dic.keys(): if form_dic[key] is None or form_dic[key] == '': return jsonify(result=-600, msg=get_error_msg(-600)) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) list_currency_rate = _db_process.admin_get_list_currency() filterData = list( filter(lambda x: x['id'] == int(form_dic['p_currency']), list_currency_rate)) if not len(filterData): return jsonify(result=-99, msg=get_error_msg(-99)) currency_rate = filterData[0]['rate'] item_price_vnd = float(form_dic['p_price']) * currency_rate * int( form_dic['p_quantity']) form_dic['p_vnd_price'] = item_price_vnd order_info = _db_process.get_order_from_detail_id(form_dic['id']) if (order_info is None): return jsonify(result=-102, msg=get_error_msg(-102)) order_detail = _db_process.get_order_detail([form_dic['id']]) form_dic['price_vnd'] = int(order_info['price_vnd']) - int( order_detail[0]['p_vnd_price']) + int(item_price_vnd) form_dic['order_id'] = order_info['id'] _db_process.admin_update_order_detail(form_dic) return jsonify(result=1, msg='Success')
def order_detail(): user_id = current_user.get_id() #dat coc don hang, method put if request.method == 'PUT': user_id = current_user.get_id() order_code = request.form.get("code") if order_code is None or is_not_blank(order_code) == False: return jsonify(result=-600, msg=get_error_msg(-600)) order_array = order_code.split(".") if len(order_array) != 2: return jsonify(result=-600, msg=get_error_msg(-600)) kwargs = type('Dummy', (object, ), { "id": order_array[1], "code": order_array[0], "user_id": user_id }) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) order_info = _db_process.get_order_info(kwargs) if order_info is None: return jsonify(result=-102, msg=get_error_msg(-102)) user = _db_process.get_user_info(user_id) price_vnd = order_info['price_vnd'] price_deposit = math.floor(int(price_vnd) * 70 / 100) if (int(user['balance']) < price_deposit): return jsonify(result=-107, msg=get_error_msg(-107)) kwargs.deposit_value = price_deposit kwargs.order_id = order_info['id'] kwargs.user_id = user_id _db_process.user_deposit_order(kwargs) return jsonify(result=1, msg='Success') order_code = request.args.get('code') if order_code is None: return redirect(url_for('user.dashboard', msg='code empty')) order_array = order_code.split(".") if len(order_array) != 2: return redirect(url_for('user.dashboard', msg='code invalid')) kwargs = type('Dummy', (object, ), { "id": order_array[1], "code": order_array[0], "user_id": user_id }) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) order_info = _db_process.get_order_info(kwargs) if order_info is None: return redirect(url_for('user.dashboard', msg='order null')) order_detail_id_array = order_info['order_item'].split(',') order_detail = _db_process.get_order_detail(order_detail_id_array) list_fee = _db_process.admin_get_list_fee() return render_template('user/order_detail.html', order=order_info, detail=order_detail, list_fee=json.dumps(list_fee))
def create_order(): if request.method == 'POST': user_id = current_user.get_id() form_dic = request.form.to_dict() order_data = form_dic['order_data'] d = json.loads(order_data) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) list_currency_rate = _db_process.admin_get_list_currency() price_vnd = 0 for item in d: # check du lieu dau vao if is_not_blank(item['name']) == False or is_not_blank(item['link']) == False or is_not_blank(item['color']) == False or is_not_blank(item['size']) == False or is_not_blank(item['quantity']) == False \ or is_not_blank(item['price']) == False or is_not_blank(item['image']) == False or is_not_blank(item['currency']) == False: return jsonify(result=-600, msg=get_error_msg(-600)) # lay ty gia filterData = list( filter(lambda x: x['id'] == int(item['currency']), list_currency_rate)) if not len(filterData): return jsonify(result=-99, msg=get_error_msg(-99)) currency_rate = filterData[0]['rate'] item['price_vnd'] = float(item['price']) * currency_rate * int( item['quantity']) price_vnd += item['price_vnd'] if (item['is_upload'] == True): image_upload = request.files[item['image']] filename = secure_filename(image_upload.filename) image_upload.save( os.path.join(os.getcwd() + "/app/static/upload/image", filename)) item['image'] = filename memory_code = randomstring(6) kwargs = type( 'Dummy', (object, ), { "data": d, "memory_code": memory_code, "price_vnd": price_vnd, "user_id": user_id }) result = _db_process.user_create_order(kwargs) if result > 0: return jsonify(result=1, msg='Success') else: msg = get_error_msg(result) return jsonify(result=-1, msg=msg) return render_template('user/create_order.html')
def update_order(): status = int(request.form.get("status")) order_id = request.form.get("id") if order_id is None or order_id == '': return jsonify(result=-600, msg=get_error_msg(-600)) urole = current_user.urole if status < 0 or status > 9: return jsonify(result=-600, msg=get_error_msg(-600)) if urole == 'BUYER': if status != 3 and status != 4: return jsonify(result=-103, msg=get_error_msg(-103)) kwargs = type('Dummy', (object, ), {"status": status, "id": order_id}) elif urole == 'SHIPPER': if status != 6 and status != 7: return jsonify(result=-103, msg=get_error_msg(-103)) kwargs = type('Dummy', (object, ), {"status": status, "id": order_id}) else: user_deposit = int(request.form.get("user_deposit")) total_weight = float(request.form.get("total_weight")) final_price = int(request.form.get("final_price")) if (user_deposit < 0 or total_weight < 0 or final_price < 0): return jsonify(result=-600, msg=get_error_msg(-600)) kwargs = type( 'Dummy', (object, ), { "status": status, "user_deposit": user_deposit, "total_weight": total_weight, "final_price": final_price, "id": order_id }) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.admin_update_order(kwargs) return jsonify(result=1, msg='Success')
def currency_rate(): if request.method == 'POST': form_dic = request.form.to_dict() kwargs = collections.namedtuple("OjbDic", form_dic.keys())(*form_dic.values()) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.admin_insert_update_currency(kwargs) if result >= 0: return jsonify(result=1, msg='Success') else: msg = get_error_msg(result) return jsonify(result=-1, msg=msg) return render_template('admin/currency_rate.html')
def signup(): # user_object = user_model.User(1) # login_user(user_object) # return redirect(url_for('admin.index')) form = RegisterForm() if form.validate_on_submit(): hashPass = hashPassword(form.password.data, form.email.data) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.register(form.email.data, hashPass) if result > 0: return redirect(url_for('user.signin')) else: msg = get_error_msg(result) flash(msg) return render_template('signup.html', form=form, title='Register')
def fee(): if request.method == 'POST': kwargs = type( 'Dummy', (object, ), { "type": request.form.get("type"), "data": json.loads(request.form.get("data")) }) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.admin_insert_update_fee(kwargs) if result > 0: return jsonify(result=1, msg='Success') else: msg = get_error_msg(result) return jsonify(result=-1, msg=msg) return render_template('admin/fee.html')
def changepass(): user_id = current_user.get_id() form = ChangepassForm() if form.validate_on_submit(): user_name = current_user.get_name() hashOldPass = hashPassword(form.old_pass.data, user_name) hashNewPass = hashPassword(form.new_pass.data, user_name) kwargs = type('Dummy', (object, ), { "old_pass": hashOldPass, "new_pass": hashNewPass, "id": user_id }) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.user_change_pass(kwargs) if result > 0: return jsonify(result=1, msg='Success') else: msg = get_error_msg(result) return jsonify(result=-1, msg=msg) return render_template('user/changepass.html', form=form)
def signin(): # user_object = user_model.User(1) # login_user(user_object) # return redirect(url_for('admin.index')) form = LoginForm() if form.validate_on_submit(): hashPass = hashPassword(form.password.data, form.email.data) _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.login(form.email.data, hashPass) if type(result) == int: flash(get_error_msg(result)) else: login_user(result) urole = current_user.urole session['user_name'] = form.email.data session['role'] = urole if urole == "ADMIN" or urole == "MOD" or urole == "ASSIST" or urole == "BUYER" or urole == "SHIPPER": return redirect(url_for('admin.index')) else: return redirect(url_for('user.dashboard')) return render_template('signin.html', form=form, title='Login')
def create_user(): form = CreateUserForm() if request.method == 'POST': if form.validate_on_submit(): urole = current_user.urole created_user_role = get_user_role(form.usertype.data) if (created_user_role == "MOD" and urole != "ADMIN"): return jsonify( result=-1, msg='Bạn không đủ quyền để tạo loại tài khoản này') hashPass = hashPassword(form.password.data, form.email.data) _db_process = DatabaseProcess( current_app.config['DB_CONNECTSTRING']) result = _db_process.register(form.email.data, hashPass, form.usertype.data, form.mobile.data) if result > 0: return jsonify(result=1, msg='Success') else: return jsonify(result=-1, msg=get_error_msg(result)) return render_template('admin/create_user.html', form=form)
def fee_config(): if request.method == 'POST': kwargs = type( 'Dummy', (object, ), { "type": request.form.get("type"), "data": json.loads(request.form.get("data")) }) fee_type = request.form.get("is_edit") if fee_type is not None: if fee_type.isdigit() == False: return jsonify(result=-1, msg='Không thể cập nhật bản ghi') kwargs.is_update = 1 _db_process = DatabaseProcess(current_app.config['DB_CONNECTSTRING']) result = _db_process.admin_insert_update_fee(kwargs) if result > 0: return jsonify(result=1, msg='Success') else: msg = get_error_msg(result) return jsonify(result=-1, msg=msg) fee_type = request.args.get('type') if fee_type is not None: if fee_type.isdigit() == False: return redirect(url_for('admin.fee_config')) else: _db_process = DatabaseProcess( current_app.config['DB_CONNECTSTRING']) result = _db_process.admin_get_list_fee() filterData = list( filter(lambda x: x['type'] == int(fee_type), result)) return render_template('admin/fee_config.html', list_data=filterData, type=fee_type) return render_template('admin/fee_config.html')