-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
361 lines (291 loc) · 15.2 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from flask import render_template, request, session, redirect, url_for, flash
from flask_login import current_user
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime
import random
from app import create_app
from app.auth import auth
from app.auth.views import load_user
from app.models import producto, imagenesProducto, cliente, pedido, pedidoProducto, db
from app.mailing import s
from flask_login.utils import login_required
from flask_login import current_user
from decouple import config as config_decouple
import os
app = create_app()
@app.errorhandler(404)
def not_found(error):
return render_template('404.html', error=error)
@app.errorhandler(500)
def not_found(error):
return render_template('500.html', error=error)
@app.errorhandler(505)
def not_found(error):
return render_template('505.html', error=error)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/contacto', defaults={'_route': 'contacto'})
@app.route('/tienda', defaults={'_route': 'tienda'})
@app.route('/flores', defaults={'_route': 'flores'})
@app.route('/error404', defaults={'_route': '404'})
def navigationPages(_route):
if _route == 'tienda':
context = {
'productos': producto.query.limit(12).all(),
'imagenes': imagenesProducto.query.all(),
'random': random
}
return render_template(_route+'.html', **context);
return render_template(_route+'.html');
@app.route('/tienda/<int:id>', methods =['GET'])
def shopAdd(id):
data = producto.query.get(id)
imagenes = imagenesProducto.query.get(data.idImagenes)
context = {
'producto' : data,
'addProducts': producto.query.limit(6).all(),
'imagenes' : imagenes,
'allImages': imagenesProducto.query.all(),
'random': random
}
return render_template('productDescription.html', **context)
def magerDicts(dict1, dict2):
if isinstance(dict1, list) and isinstance (dict2, list):
return dict1 + dict2
elif isinstance(dict1, dict) and isinstance(dict2, dict):
return dict(list(dict1.items()) + list(dict2.items()))
return False
@app.route('/tienda/add', methods = ['GET', 'POST'])
def addCart():
try:
product_id = request.form.get('product_id')
quantity = request.form.get('quantity')
product = producto.query.get(product_id)
imagenes = imagenesProducto.query.get(product.idImagenes)
if int(quantity) > int(product.cantidadProducto):
flash(f'Solo puedes agregar maximo: {int(product.cantidadProducto)} productos de este tipo', 'error')
else:
if product_id and quantity and request.method == 'POST':
dictItems = {product_id:{'precio': product.precioProducto,'nombre': product.nombreProducto, 'cantidad': quantity, 'imagen': imagenes.imagen1}}
if 'Shoppingcart' in session:
if product_id in session['Shoppingcart']:
for key, item in session['Shoppingcart'].items():
if int(key) == int(product_id):
session.modified = True
item['cantidad'] = int(item['cantidad']) + int(quantity)
if item['cantidad'] > int(product.cantidadProducto):
flash(f'Solo puedes agregar maximo: {int(product.cantidadProducto)} productos de este tipo', 'error')
session.modified = True
item['cantidad'] = int(product.cantidadProducto)
else:
session['Shoppingcart'] = magerDicts(session['Shoppingcart'], dictItems)
return redirect(request.referrer)
else:
session['Shoppingcart'] = dictItems
return redirect(request.referrer)
except Exception as e:
print(e)
finally:
return redirect(request.referrer)
@app.route('/compra', methods=['GET', 'POST'])
def buyShopping():
if request.method == 'GET' and session.get('Shoppingcart') is None:
flash('para ver tu carrito tienes que primero agregar algo', 'info')
if request.referrer == None:
return redirect(url_for('index'))
return redirect(request.referrer)
else:
total = 0
for key, product in session['Shoppingcart'].items():
total += int(product['precio']) * int(product['cantidad'])
return render_template('compra.html', total = total)
@app.route('/compra/delete/<int:id>', methods=['GET', 'POST'])
def deleteItem(id):
if 'Shoppingcart' not in session and len(session['Shoppingcart']) <= 0:
return redirect(url_for('navigationPages', _route = 'tienda'))
try:
session.modified = True
for key, item in session['Shoppingcart'].items():
if int(key) == id:
session['Shoppingcart'].pop(key, None)
if not session['Shoppingcart']:
session.pop('Shoppingcart', None)
return redirect(url_for('navigationPages', _route = 'tienda'))
return redirect(request.referrer)
except Exception as e:
print(e)
return redirect(url_for('buyShopping'))
@app.route('/compra/clear')
def clearCart():
try:
session.pop('Shoppingcart', None)
return redirect(url_for('navigationPages', _route = 'tienda'))
except Exception as e:
print(e)
@app.route('/checkout', methods=['GET', 'POST'])
@login_required
def checkout():
if (request.method == 'GET'):
total = 0
for key, product in session['Shoppingcart'].items():
total += int(product['precio']) * int(product['cantidad'])
return render_template('checkout.html', total = total)
if (request.method == 'POST'):
nameClient = request.form['nameClient']
lastNameClient = request.form['lastNameClient']
directionClient = request.form['directionClient']
postalCodeClient = request.form['postalCodeClient']
mailClient = request.form['mailClient']
phoneClient = request.form['phoneClient']
# Bank info
cardNumber = request.form['cardNumber']
cardDateMonth = request.form['cardDateMonth']
cardDateYear = request.form['cardDateYear']
cardCVV = request.form['cardCVV']
paypalClient = request.form['paypalClient']
# Calculations
total = 0
for key, product in session['Shoppingcart'].items():
total += int(product['precio']) * int(product['cantidad'])
# Create client
if (nameClient == '' or lastNameClient == '' or directionClient == '' or postalCodeClient == '' or mailClient == '' or phoneClient == ''):
flash('Todos los campos son obligatorios', 'error')
return redirect(url_for('checkout'))
else:
if current_user.idTipoUsuario != 2:
flash('Solo los usuarios que son clientes pueden hacer compras', 'error')
return redirect(url_for('index'))
else:
currentUserIsClient = cliente.query.filter_by(idUsuario = current_user.idUsuario).first()
if currentUserIsClient:
especificacionPedido = "Compra del usuario: " + str(current_user.usuario)
newPedido = pedido(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), total, especificacionPedido, 'Activo', currentUserIsClient.idCliente)
db.session.add(newPedido)
db.session.commit()
for key in session['Shoppingcart']:
precio = int(session['Shoppingcart'][key]['precio'])
inventario = int(session['Shoppingcart'][key]['cantidad'])
subtotal = precio * inventario
nuevoProducto = pedidoProducto(newPedido.idPedido, key, subtotal , int(session['Shoppingcart'][key]['cantidad']))
db.session.add(nuevoProducto)
db.session.commit()
msg = MIMEMultipart('alternative')
contextMail = {
'idPedido' : newPedido.idPedido,
'fecha' : newPedido.fechaPedido,
'total' : newPedido.totalPedido,
'estadoPedido': newPedido.estadoPedido,
'productos' : session['Shoppingcart'],
'cliente' : currentUserIsClient
}
file = render_template('mail_newFacture.html', **contextMail )
text = """\
this is your buy invoice
here are the products of your buy
total: {}
""".format(newPedido.totalPedido)
msg['From']= 'david@mi.com.co'
msg['To']= str(current_user.emailUsuario)
msg['Subject']= "#{} Thanks for your purchase on FLOWY".format(newPedido.idPedido)
part1 = MIMEText(text, "plain")
part2 = MIMEText(file, "html")
msg.attach(part1)
msg.attach(part2)
s.send_message(msg)
flash('comprado!', 'success')
clearCart()
return redirect(url_for('index'))
else:
if (cardNumber != None or cardDateMonth != None or cardDateYear != None or cardCVV != None):
newClient = cliente(directionClient, phoneClient, nameClient, 'CC', cardNumber, None, cardCVV, cardDateMonth, cardDateYear, nameClient+lastNameClient, current_user.idUsuario)
db.session.add(newClient)
db.session.commit()
especificacionPedido = "Compra del usuario: " + str(current_user.usuario)
newPedido = pedido(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), total, especificacionPedido, 'Activo', newClient.idCliente)
db.session.add(newPedido)
db.session.commit()
for key in session['Shoppingcart']:
precio = int(session['Shoppingcart'][key]['precio'])
inventario = int(session['Shoppingcart'][key]['cantidad'])
subtotal = precio * inventario
nuevoProducto = pedidoProducto(newPedido.idPedido, key, subtotal , int(session['Shoppingcart'][key]['cantidad']))
db.session.add(nuevoProducto)
db.session.commit()
msg = MIMEMultipart('alternative')
contextMail = {
'idPedido' : newPedido.idPedido,
'fecha' : newPedido.fechaPedido,
'total' : newPedido.totalPedido,
'estadoPedido': newPedido.estadoPedido,
'productos' : session['Shoppingcart'],
'cliente' : currentUserIsClient
}
file = render_template('mail_newFacture.html', **contextMail )
text = """\
this is your buy invoice
here are the products of your buy
total: {}
""".format(newPedido.totalPedido)
msg['From']= 'david@mi.com.co'
msg['To']= str(current_user.emailUsuario)
msg['Subject']= "#{} Thanks for your purchase on FLOWY".format(newPedido.idPedido)
part1 = MIMEText(text, "plain")
part2 = MIMEText(file, "html")
msg.attach(part1)
msg.attach(part2)
s.send_message(msg)
flash('comprado!', 'success')
clearCart()
return redirect(url_for('index'))
elif (paypalClient != None):
newClient = cliente(directionClient, phoneClient, nameClient, 'CC', None, paypalClient, None, None, None, nameClient+lastNameClient, current_user.idUsuario)
db.session.add(newClient)
db.session.commit()
especificacionPedido = "Compra del usuario: " + str(current_user.usuario)
newPedido = pedido(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), total, especificacionPedido, 'Activo', newClient.idCliente)
db.session.add(newPedido)
db.session.commit()
for key in session['Shoppingcart']:
precio = int(session['Shoppingcart'][key]['precio'])
inventario = int(session['Shoppingcart'][key]['cantidad'])
subtotal = precio * inventario
nuevoProducto = pedidoProducto(newPedido.idPedido, key, subtotal , int(session['Shoppingcart'][key]['cantidad']))
db.session.add(nuevoProducto)
db.session.commit()
msg = MIMEMultipart('alternative')
contextMail = {
'idPedido' : newPedido.idPedido,
'fecha' : newPedido.fechaPedido,
'total' : newPedido.totalPedido,
'estadoPedido': newPedido.estadoPedido,
'productos' : session['Shoppingcart'],
'cliente' : currentUserIsClient
}
file = render_template('mail_newFacture.html', **contextMail )
text = """\
this is your buy invoice
here are the products of your buy
total: {}
""".format(newPedido.totalPedido)
msg['From']= 'david@mi.com.co'
msg['To']= str(current_user.emailUsuario)
msg['Subject']= "#{} Thanks for your purchase on FLOWY".format(newPedido.idPedido)
part1 = MIMEText(text, "plain")
part2 = MIMEText(file, "html")
msg.attach(part1)
msg.attach(part2)
s.send_message(msg)
flash('comprado!', 'success')
clearCart()
return redirect(url_for('index'))
else:
flash('Todos los campos son obligatorios', 'error')
return redirect(url_for('checkout'))
if __name__ == '__main__':
print("trying to run floowy app")
os.system('python dbCreator.py')
port = int(os.environ.get('PORT', 5000))
print("port", os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=1)