-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
150 lines (122 loc) · 4.57 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
from flask import Flask, request, redirect, render_template, session, flash
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = '1423'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///shop.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=False)
price = db.Column(db.Integer, nullable=False)
path = db.Column(db.String, nullable=False)
def __str__(self):
return str(self.id) + ' ' + self.title + ' ' + str(self.price) + ' ' + self.path
def get_title(self):
return self.title
def get_price(self):
return ''.join([str(self.price), 'р.'])
def get_path(self):
return self.path
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
un = db.Column(db.String, nullable=False)
pw = db.Column(db.String, nullable=False)
mail = db.Column(db.String, nullable=False)
def __str__(self):
return str(self.id) + ' ' + self.un + ' ' + self.pw + ' ' + self.mail
@app.route('/register', methods=['GET', 'POST'])
def registerpage():
if str(session.get('un', '')) != '':
return render_template('nreg.html')
else:
if request.method == 'GET':
return render_template("register.html", warning='')
elif request.method == "POST":
data = request.form.to_dict()
un = data['login']
pw = data['password']
mail = data['mail']
logged = False
for user in User.query.all():
if str(user).split()[1] == un:
logged = True
break
if not logged:
user = User(un=un, pw=pw, mail=mail)
try:
db.session.add(user)
db.session.commit()
return redirect('/login')
except:
return 'Ошибка'
else:
return render_template('register.html', warning='Такой пользователь есть.')
@app.route('/login', methods=['GET', 'POST'])
def loginpage():
if str(session.get('un', '')) != '':
return render_template('nlog.html')
else:
if request.method == 'GET':
return render_template('login.html', warning='')
elif request.method == 'POST':
data = request.form.to_dict()
un = data['login']
pw = data['password']
name = False
pas = False
for user in User.query.all():
if str(user).split()[1] == un:
name = True
if str(user).split()[2] == pw:
pas = True
session['mail'] = str(user).split()[3]
break
if name and pas:
session['un'] = un
return redirect('/profile')
elif name and (not pas):
return render_template('login.html', warning='Неверный пароль.')
else:
return render_template('login.html', warning='Такого пользователя не существует.')
@app.route('/profile', methods=["GET", "POST"])
def profile():
if request.method == 'GET':
if str(session.get('un', '')) != '' and str(session.get('un', '')) is not None:
return render_template('profile.html', un=str(session.get('un', '')), email=str(session.get('mail', '')))
else:
return render_template('nprofile.html')
else:
session.pop('mail', None)
session.pop('un', None)
return render_template('nprofile.html')
def to_3_by_tuple(items):
tp = []
vr = []
for i, el in enumerate(items):
if (i + 1) % 4 == 0:
vr.append(el)
tp.append(vr)
vr = []
else:
vr.append(el)
if len(vr) != 0:
tp.append(vr)
return tp
@app.errorhandler(404)
def page_not_found(e):
return render_template('error.html'), 404
def main():
app.run(port=8080, host='127.0.0.1')
@app.route('/home', methods=['GET', 'POST'])
def homepage():
if request.method == 'GET':
return render_template('homepage.html', items=to_3_by_tuple(Item.query.all()), str=str)
else:
print('Отправлено')
return render_template('homepage.html', items=to_3_by_tuple(Item.query.all()), str=str)
@app.route('/')
def index():
return redirect('/home')
if __name__ == '__main__':
main()