-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpanier_relative.py
148 lines (128 loc) · 4.49 KB
/
panier_relative.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
# -*- coding: utf-8 -*-
from flask import *
from flask import current_app as app
from sqlalchemy import *
from sqlalchemy.sql import *
from werkzeug.utils import secure_filename
import os
import re
from model import*
from jinja2 import TemplateNotFound
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
panier_relative = Blueprint('panier_relative', __name__,template_folder='templates',static_folder = 'static')
def sendMail (adressedest, cart, nomUser):
mailrotonde="rotondeinsatest@gmail.com"
mdprotonde="motdepasse1!"
fromaddr = mailrotonde
toaddr = adressedest
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Reservation spectacle"
html= render_template("mail.html", nomUser=nomUser, places=cart)
msg.attach(MIMEText(html, 'html'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "motdepasse1!")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
def isInCart(item, cart):
for idx, show in enumerate(cart):
if item['nomSpectacle'] == show['nomSpectacle'] and item['date'] == show['date']:
return idx
return -1
def calculCart(items):
print("entering calculCart with items = "+str(items))
display_cart = []
for item in items:
idx = isInCart(item,display_cart)
if idx == -1 or display_cart == []:
date = get_date(date=dateJSONToPy(str(item['date'])))
#print(date)
left = date.placesRestantes
display_cart.append({'nomSpectacle' : item['nomSpectacle'], 'date':item['date'], 'qte' : 1, 'left' : left})
else:
display_cart[idx]['qte']+=1
return display_cart
def udpateQte(cont):
global display_cart
for i, show in enumerate(display_cart):
index = 'qte'+str(i+1)
toDelete = []
change = int(cont[index]) - int(show['qte'])
print(change)
print(str(cont[index]) +" - "+ str(show['qte'])+" = "+str(change))
if(change ==0):
print("non changed")
else:
while (change != 0):
if(change < 0):
for j, place in enumerate(session['panier']):
if place['nomSpectacle'] == show['nomSpectacle'] and place['date'] == show['date']:
#print("on supprime une des places : "+place['nomSpectacle'] + " " + place['date'] +" "+ str(j))
session['panier'].pop(j)
change += 1
break
else:
place = Place(nomSpectacle=show['nomSpectacle'],date=show['date'],nomUser="", valide=0)
placeJSON = place.serialize()
print("on ajoute une places : "+str(placeJSON))
session['panier'].append(placeJSON)
change -= 1
print("voici le panier de la session : "+ str(session['panier']))
session.update()
## PANIER
@panier_relative.route('/panier', methods=['POST','GET'])
def panier():
if "panier" not in session:
#flash("There is nothing in your cart.")
return render_template("panier.html", display_cart = {}, total = 0)
if request.method == "GET":
"""TODO: Display the contents of the shopping cart."""
print("\n\n\n\n\n\n\n\nEntering in GET\n\n\n\n\n\n\n\n\n");
if "panier" not in session:
#flash("There is nothing in your cart.")
return render_template("panier.html", display_cart = {}, total = 0)
else:
global display_cart
display_cart = calculCart(session['panier'])
return render_template("panier.html", display_cart = display_cart, total = 10)
if request.method == "POST":
print("\n\n\n\n\n\n\n\nEntering in POST\n\n\n\n\n\n\n\n\n");
if "panier" not in session :
return redirect(url_for('logout'))
else:
if request.form['foo']=='valider':
cont = request.form
print("\n\n\n\n"+str(cont)+"\n\n\n\n")
print(display_cart)
udpateQte(cont);
if 'nom' not in request.form or request.form['nom'] == "":
return redirect(url_for('panier_relative.panier'))
else:
print("on m'appelle")
panier = session['panier']
print(panier)
name = request.form['nom']
mail = request.form['mail']
display_cart = calculCart(session['panier'])
for show in display_cart:
print("requesting date")
date = dateJSONToPy(str(show['date']))
added=0
datemodif=get_date(date=date)
for i in range(1, show['qte']+1):
added+=1
print(i)
place = Place(ordre=added,nomSpectacle=show['nomSpectacle'],nomUser=name,date=date, adresseMail=mail, valide=0)
insert_place(place)
res=update_placesRestantes(datemodif,added)
if(res==-1):
return redirect(url_for('panier_relative.panier'))
session.pop('panier')
places=get_places_mail(mail)
sendMail(mail, places, name)
return redirect(url_for('logout'))