forked from Pavelsvasilenko/dp21-4grupa_autonoma
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
210 lines (185 loc) · 7.45 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
from flask import Flask, flash, render_template, url_for, request, redirect, session
from pymongo import MongoClient
from bson.json_util import dumps
from random import randint
app = Flask(__name__)
app.secret_key = "rTf45as(rw2Ew_"
# login
# TkD0nfEVDOyNk3Bp
# Mongo klienta pievienošana.
client = MongoClient("mongodb+srv://login:TkD0nfEVDOyNk3Bp@cluster0.qd271.mongodb.net/database?retryWrites=true&w=majority")
# Informācija par divām datubāzēm
db = client.users
users_database = db.users_info
db = client.cars
cars_database = db.cars_info
# Panels, kura lietotājs var izveleties sev transportu.
@app.route('/cars', methods=["GET","POST"])
def cars():
if 'user' in session:
user_text = session['user']
elif 'admin' in session:
user_text = session['admin']
else:
user_text = "user"
if 'admin' in session or 'user' in session:
return render_template('cars.html', user_text=user_text)
else:
flash("Lūdzu, ieiet")
return redirect(url_for("index"))
@app.route('/data')
def data():
cars = cars_database.find()
data = list(cars)
return dumps(data)
# Ielogošana saitē. Ir divi lietotāju veidi, kuri var darboties saitē, vai nu parasts users, vai nu admins. Ir arī iespēja reģistreties saitē.
@app.route('/', methods=["GET","POST"])
def index():
if 'user' in session:
user_text = session['user']
elif 'admin' in session:
user_text = session['admin']
else:
user_text = "user"
if request.method == "POST":
login = request.form['login']
password = request.form['password']
if "admin" in session or "user" in session:
flash("Jūs jau esat iegājuši")
return render_template('index.html', user_text=user_text)
if login == "admin" and password == "admin":
session['admin'] = "admin"
return redirect(url_for("panel"))
user_database = users_database.find_one({"login":login})
if user_database == None:
flash("Parole vai e-pasts nav pareizs!")
return render_template('index.html', user_text=user_text)
if login == user_database['login'] and password == user_database['password']:
session['user'] = login
flash("Jūs esat iegājuši")
return render_template('index.html', user_text=login)
else:
flash("Parole vai e-pasts nav pareizs!")
return render_template('index.html', user_text=user_text)
else:
return render_template('index.html', user_text=user_text)
# Panelis paredzēts adminam. Šeit viņš var pievienot jaunu automobili vai noņemt.
@app.route('/panel', methods=["GET","POST"])
def panel():
if 'user' in session:
user_text = session['user']
elif 'admin' in session:
user_text = session['admin']
else:
user_text = "user"
if "admin" in session:
if request.method == 'POST':
place = request.form['place']
marka = request.form['marka']
model = request.form['model']
year = request.form['year']
img = request.form['img']
if place == "" or marka == "" or model == "" or year == "" or img == "":
flash("Ievadiet visu informāciju")
return render_template('panel.html', user_text=user_text)
else:
random_int = randint(999,999999)
data = {"id":random_int,"place":place,"marka":marka,"model":model,"year":year,"img":img,"buy":True}
cars_database.insert_one(data)
flash("Gatavs!")
return render_template('panel.html', user_text=user_text)
else:
return render_template('panel.html', user_text=user_text)
else:
flash("Jūs neesat admins!")
return redirect(url_for("index"))
# Šeit admins var apskatīt klientus (pircējus). Tā informacija tiek aizsargāta no parastiem useriem, tie nevar to apskaitīt, jo nav pieejams.
@app.route("/panel/buy/<int:id>", methods=["GET","POST"])
def customer(id):
if 'user' in session:
user_text = session['user']
elif 'admin' in session:
user_text = session['admin']
else:
user_text = "user"
if 'admin' in session:
user_who_buy = users_database.find_one({"buy":id})
if user_who_buy == None:
name = "None"
email = "None"
else:
name = user_who_buy['name']
email = user_who_buy['login']
car_who_buy = cars_database.find_one({"id":id})
car = car_who_buy['marka']
return render_template("who-customer.html", user_text=user_text, name=name, email=email, car=car)
else:
return redirect(url_for("index"))
# Šeit userim parādās visa informācija par nopirkto transportu.
@app.route("/cars/buy/<int:id>", methods=["GET","POST"])
def buy(id):
if 'user' in session:
user_text = session['user']
elif 'admin' in session:
user_text = session['admin']
else:
user_text = "user"
car_database = cars_database.find_one({"id":id})
if car_database['buy'] == False:
return redirect(url_for("index"))
if 'user' in session:
user = session['user']
users_database.update_one({"login":user}, {"$set": {"buy":id}})
car = car_database['marka']
cars_database.update_one({"id":id}, {"$set": {"buy":False}})
return render_template('check.html', user_text=user_text, id=id, car=car)
else:
return redirect(url_for("index"))
# Šeit admins var nodzēst automobiļi no saraksta.
@app.route("/panel/delete/<int:id>", methods=["GET","POST"])
def delete(id):
if 'user' in session:
user_text = session['user']
elif 'admin' in session:
user_text = session['admin']
else:
user_text = "user"
if 'admin' in session:
cars_database.delete_one({"id":id})
return redirect(url_for("panel"))
else:
return redirect(url_for("index"))
# Reģistracija
@app.route('/register', methods=["GET","POST"])
def register():
if 'user' in session:
user_text = session['user']
elif 'admin' in session:
user_text = session['admin']
else:
user_text = "user"
if request.method == "POST":
if 'user' in session or 'admin' in session:
flash("Jūs jau esat iegājuši")
return render_template('register.html', user_text=user_text)
login = request.form['login']
name = request.form['name']
password = request.form['password']
if login == "" or name == "" or password == "":
flash("Lūdzu, ievadiet visu informāciju")
return render_template('register.html', user_text=user_text)
data = {"login":login,"name":name,"password":password,"buy":0}
users_database.insert_one(data)
flash("Reģistrācija ir veiksmīga!")
return redirect(url_for("index"))
else:
return render_template('register.html', user_text=user_text)
# Izeja no saites
@app.route('/logout', methods=["GET","POST"])
def logout():
session.pop("admin", None)
session.pop("user", None)
flash("Jūs veiksmīgi izgājāt no sava konta")
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)