-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
301 lines (242 loc) · 9.55 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from flask import Flask, render_template, request, redirect, url_for, flash, session, abort
from flask_bootstrap import Bootstrap5
from bluprints import admin_bp, cart
from database import Database, UserConnection
from flask_dropzone import Dropzone
from flask_babel import Babel, gettext
from flask_wtf import CSRFProtect
from forms.forms import LoginForm, AddToCart, PharmacyDiscount, RegisterUserForm
from werkzeug.security import generate_password_hash
from werkzeug.security import check_password_hash
from flask_login import login_user, LoginManager, current_user, logout_user, login_required
from flask_socketio import SocketIO, emit
from helpers import calc_discount, is_active
from dotenv import load_dotenv
from datetime import datetime, timedelta
from flask_migrate import Migrate
import flask_sockets
import flask_excel as excel
from datetime import datetime
import os
# Load env
load_dotenv()
# INIT APP
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
app.config['BABEL_DEFAULT_LOCALE'] = 'es'
# DROPZONE
app.config['DROPZONE_ALLOWED_FILE_CUSTOM'] = True
app.config['DROPZONE_ALLOWED_FILE_TYPE'] = '.csv, .xlsx, .xls'
app.config['DROPZONE_MAX_FILE_SIZE'] = 10
app.config['DROPZONE_ENABLE_CSRF'] = True
csrf = CSRFProtect(app)
# DATABASE
db = Database(app)
# CREATE TABLES
db.create_tables()
# MIGRATIONS
migrate = Migrate(app, db.db)
# Plugins
Bootstrap5(app)
babel = Babel(app)
# excel
excel.init_excel(app)
# Dropzone
dropzone = Dropzone(app)
# websockets IO
# socketio = SocketIO(app, async_mode='eventlet')
socketio = SocketIO()
socketio.init_app(app)
# Login manager
login_manager = LoginManager()
login_manager.init_app(app)
# BLUEPRINTS
app.register_blueprint(admin_bp.construct_blueprint(db=db, app=app), url_prefix='/admin')
app.register_blueprint(cart.construct_blueprint(db=db, socketio=socketio, app=app))
@login_manager.user_loader
def load_user(user_id):
return db.get_user(user_id=user_id)
@app.route('/register', methods=['GET', 'POST'])
def first_register():
all_user = db.all_users()
if not all_user:
form = RegisterUserForm()
if form.validate_on_submit():
new_user = db.add_user(
name=form.name.data,
last_name=form.last_name.data,
email=form.email.data,
password=generate_password_hash(form.password.data, method='pbkdf2:sha256', salt_length=8),
role='admin'
)
return redirect(url_for('login'))
return render_template('register.html', form=form)
else:
return abort(404)
@app.route('/login', methods=['GET', 'POST'])
def login():
# check for if user is authenticated
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
# check if users exists
user = db.check_user(username.lower())
if user:
if check_password_hash(user.password, password):
if user.active == 'yes':
login_user(user)
user_connection = UserConnection.check_user(user_id=current_user.id)
if user_connection:
# update date of last connection
UserConnection.update_connection(user_connection, user_ip=request.remote_addr)
else:
# Register user in user_connection
UserConnection.register_user(user_id=current_user.id, user_ip=request.remote_addr)
return redirect(url_for('index'))
# active user
if user.pharmacy:
name = user.pharmacy[0].name
else:
name = user.email
text = f'Hola nos comunicamos de parte de: {name} y queremos activar el usuario'
link = f'https://api.whatsapp.com/send?phone=+584243773513&text={text}'
tag = f'<a href="{link}" target="_blank">usuario</a>'
flash(gettext(f'Por favor active su {tag}'))
else:
flash(gettext('Wrong user or password!'))
else:
flash(gettext('Wrong user or password'))
return render_template('admin/home/login.html', form=form)
@app.route('/user/profile', methods=['GET'])
@login_required
@is_active
def user_profile():
user = db.get_user(current_user.id)
suppliers = db.all_suppliers()
discount = {}
if current_user.discount:
for user_discount in current_user.discount:
discount[user_discount.supplier_id] = {
"discount_supplier": user_discount.discount
}
return render_template('user_profile.html', user=user, suppliers=suppliers, user_discount=discount)
@app.route('/')
@login_required
@is_active
def index():
return render_template('index.html')
@app.route('/search', methods=['GET'])
@login_required
@is_active
def search():
form = AddToCart()
prices_discount = []
discount_suggest = []
if request.args.get('barcode'):
barcode = request.args.get('barcode')
results = db.search_products(barcode, per_page=15)
suggest = request.args.get('query')
if current_user.discount:
user_discount = db.get_user_discounts(current_user.id)
suppliers = [item for item in user_discount]
prices_discount = calc_discount(results, suppliers, user_discount)
elif request.args.get('query'):
results = db.search_products(request.args.get('query'), per_page=15)
barcode = request.args.get('query')
suggest = request.args.get('query')
suggested_results = []
if current_user.discount:
user_discount = db.get_user_discounts(current_user.id)
suppliers = [item for item in user_discount]
prices_discount = calc_discount(results, suppliers, user_discount)
discount_suggest = []
else:
return redirect(url_for('index'))
if request.args.get('barcode'):
if request.args.get('barcode') != request.args.get('query'):
suggested_results = db.search_products(suggest, per_page=15)
if current_user.discount:
user_discount = db.get_user_discounts(current_user.id)
suppliers = [item for item in user_discount]
discount_suggest = calc_discount(suggested_results, suppliers, user_discount)
else:
suggested_results = []
discount_suggest = []
return render_template('search.html', results=results, search_query=barcode,
suggested_results=suggested_results,
suggest=suggest, form=form, prices_discount=prices_discount,
discount_suggest=discount_suggest, get_text=gettext)
@app.route('/logout')
def logout():
session.clear()
logout_user()
response = redirect(url_for('login'))
response.cache_control.no_cache = True
return response
@socketio.on('connect')
def handle_connect():
# check for user last connection
if current_user.user_connection:
last_connection = current_user.user_connection.last_connection
if last_connection + timedelta(hours=1) < datetime.now():
user = UserConnection.check_user(current_user.id)
UserConnection.update_connection(user=user, user_ip=request.remote_addr)
else:
UserConnection.register_user(user_id=current_user.id, user_ip=request.remote_addr)
print(f'user connected!')
@socketio.on('search_query')
def handle_search_query(search_query, per_page):
if search_query:
results = db.search_products(q=search_query, per_page=per_page)
emit('search_results', {'results': [item.serialize() for item in results if item.prices],
'pages': [page for page in results.iter_pages()],
'page': results.page,
'total': results.total,
'has_prev': results.has_prev,
'has_next': results.has_next,
'search_query': search_query,
})
else:
emit('search_results', {'results': []})
@socketio.on('update_discount')
def handle_search_query_discount(supplier_id, discount_amount):
discount_amount = float(discount_amount) / 100
# check if exist discount
discount_user = db.get_user_discount(current_user.id, supplier_id)
if discount_user:
if discount_amount == 0:
db.delete_user_discount(user_discount=discount_user[0])
else:
db.edit_user_discount(discount_user[0], discount_amount)
else:
# Add discount
new_discount = db.add_discount(current_user.id, supplier_id, discount_amount)
# Cart
@app.context_processor
def get_cart():
if current_user.is_authenticated:
shopping_cart = db.view_cart(user_id=current_user.id)
return {"shopping_cart": shopping_cart}
else:
return {"shopping_cart": {}}
# Date
@app.context_processor
def date():
current_year = datetime.now().year
return {'current_year': current_year}
# Errors
@login_manager.unauthorized_handler
def unauthorized():
return redirect(url_for('login'))
@app.errorhandler(401)
def custom_401(error):
return render_template('admin/home/page-404.html')
@app.errorhandler(404)
def custom_404(error):
return render_template('admin/home/page-404.html')
if __name__ == "__main__":
# app.run(debug=True)
socketio.run(app, debug=False)