def post_sell(): form = SellForm() user = get_user() if request.method == 'POST': if form.validate_on_submit(): file = request.files['image'] if file and allowed_file(file.filename): filename = secrets.token_hex(4) + '_' + secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) if user: product = {k: form.data[k] for k in ['title', 'volume', 'weight', ]} product['image'] = filename product['sell_date'] = int(datetime.now().timestamp()) product['status'] = 'Não recolhido' sells_data = db.table('sells').search(Query().store == user['email']) if len(sells_data) > 0: sells_data = {**sells_data[0]} sells_data['sells'].append(product) for sell in sells_data['sells']: try: sell['sell_date'] = sell['sell_date'].timestamp() except: pass db.table('sells').upsert(sells_data, Query().store == user['email']) return redirect(url_for('home')) else: flash('Problema ao submeter venda, tente novamente', 'danger') return render_template('post_sell.html', title='Postar Venda', form=form, user=user)
def stores(): amount = 0 sells = db.table('sells').all() for sell in sells: amount += len(sell['sells']) user = db.table('users').search(Query().email == sell['store'])[0] user.pop('password', None) sell['store_info'] = user price = '{:.2f}'.format(random.random() * 20 + 10) distance = '{:.2f}'.format(random.random() * 10 + 5) eta = '{:.2f}'.format(random.randint(10, 20)) return jsonify({'amount': amount, 'price': price, 'distance': distance, 'eta': eta, 'stores': sells})
def mark_store(store): sells_data = db.table('sells').search(Query().store == store) if len(sells_data) > 0: sells_data = {**sells_data[0]} sells = sells_data.get('sells') for sell in sells: sell['status'] = 'Recolhido' try: sell['sell_date'] = sell['sell_date'].timestamp() except: pass db.table('sells').upsert(sells_data, Query().store == store) return redirect(url_for('home'))
def login(): form = LoginForm() if form.validate_on_submit(): users = db.table('users').search(Query().email == form.email.data.lower()) if len(users) > 0: user = dict(users[0]) if form.password.data == user.get('password'): flash('Você entrou com sucesso!', 'success') user.pop('password', None) session['user'] = json.dumps(user) return redirect(url_for('home')) else: flash('Erro no login. Confira seu email e senha e tente novamente', 'danger') return render_template('login.html', title='Login', form=form)
def home(): user = get_user() sells = [] if user: sells_data = db.table('sells').search(Query().store == user['email']) if len(sells_data) > 0: sells_data = {**sells_data[0]} sells = sells_data.get('sells') for sell in sells: try: sell['sell_date'] = datetime.fromtimestamp(sell['sell_date']) except: pass return render_template('home.html', user=user, sells=sells)