-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
226 lines (154 loc) · 6.94 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
from datetime import datetime
from flask import Flask, flash, jsonify, render_template , request , redirect, url_for , abort
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin , login_user , LoginManager , current_user
from passlib.hash import sha256_crypt
from flask_wtf.csrf import CSRFProtect
import os
from flask import send_from_directory
app = Flask(__name__ , template_folder="templates" , static_folder="static")
app.secret_key = "this is password pls don't steal 4269"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///sheet.sqlite3"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
csrf = CSRFProtect(app)
csrf.init_app(app)
##MODELS
class User(db.Model , UserMixin):
_id = db.Column( db.Integer , primary_key = True)
username = db.Column(db.String(100) , unique= True , nullable = False)
password = db.Column(db.String(1000) , nullable = False)
html_bio = db.Column(db.Text , nullable = True)
name = db.Column(db.String(100) , nullable = True)
last_name = db.Column(db.String(100) , nullable = True)
date_joinde = db.Column(db.DateTime , default= datetime.utcnow)
def __init__(self, username : str , password , html_bio = None , name = None , last_name = None):
self.username = username
self.password = password
self.html_bio = html_bio
self.name = name
self.last_name = last_name
@property
def get_full_name(self):
return self.name + " " + self.last_name
def __repr__(self):
return self.username
def get_id(self):
return self._id
VALID = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;="
def has_valid_characters(username : str):
for i in username:
if i not in VALID:
return False
return True
@login_manager.user_loader
def load_user(user_id):
return User.query.filter_by(_id = user_id).first()
###VIEWS
@app.route("/")
def index():
return render_template("index.html")
@app.route("/signup" , methods =["GET" , "POST"] )
def signup():
if request.method == "POST":
username = request.form.get("username" , False)
password = request.form.get("password" , False)
name = request.form.get("name" , False)
last_name = request.form.get("last_name" , False)
if username:
if len(username) >= 100:
flash("یوزرنیم باید کمتر از 100 کاراکتر باشد جناپ")
elif " " in username:
flash ("آقای محترم اسپیس نذارید توی نام کاربری")
elif not has_valid_characters(username):
flash("اقای محرتم!!!!!!!!!کاراکتر حرام؟؟")
else:
for l in username:
if ord(l) < 33 or ord(l) > 126:
flash("یوزرنیم فارسی اقای محرتم")
break
else:
if User.query.filter_by(username=username).first() != None:
flash("یوزرنیم تکراری است اقای محترم")
else:
if not password == False and len(password) >= 8:
password = sha256_crypt.encrypt(password)
user = User(username=username , password=password , name=name , last_name=last_name)
db.session.add(user)
db.session.commit()
login_user(user)
return redirect("/")
if password == False:
flash ("آقای محترم رمز خالی نباشه وگرنه")
elif len(password) <= 8:
flash("آقای محترم رمزتون باید بیشتر از 8 کارکتر باشه")
else:
flash("اقای محرتم یوزرنیم خالی نباشه وگرنه")
return redirect(url_for("signup"))
if request.method == "GET":
return render_template("signup.html")
@app.route("/login" , methods= ["GET" , "POST"])
def login():
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
if User.query.filter_by(username=username).first() != None:
user = User.query.filter_by(username = username).first()
if sha256_crypt.verify(password, user.password):
login_user(user)
return redirect("/")
flash("اقای محترم پسسورد شما دارای ایراد است")
return redirect(url_for("login"))
flash("یوزرنیم در پایگاه داده یافت نشد")
return redirect(url_for("login"))
if request.method == "GET":
return render_template("login.html")
@app.route("/has_logged_in")
def has_logged_in():
return jsonify({
"is_authenticated" : current_user.is_authenticated
})
@app.route("/validation123" , methods = ["POST"])
def validation_endpoint():
username = request.form.get("username")
password = request.form.get("password")
error = ""
password_error = ""
if username == "":
error = "اقای محترم چرا خالی؟"
elif len(username) > 100:
error = "زیادی بلنده اسکل"
elif not has_valid_characters(username):
error = "دوست عزیزم.یوزرنیم شما حاوی کاراکتر های خطرناک می باشد"
elif " " in username:
error = "اسپیس نذارید. جدی می باشم.😤 "
if User.query.filter_by(username = username).first() != None:
error = "تکراریه اقای مترحم"
if len(password) < 8:
password_error = "اقای محترم چرا پسسورد کوتاه؟"
return jsonify({
"username_error" : error,
"password_error" : password_error
})
@app.route("/profile/<int:pk>/<username>")
def profile_view(pk , username):
if User.query.filter_by(_id = pk).filter_by(username = username).first() != None:
user = User.query.filter_by(_id = pk).first()
return render_template("profile.html" , user = user)
abort(404)
#errors ??
@app.errorhandler(404)
def page_not_found (e):
return render_template("404.html"), 404
@app.errorhandler(405)
def page_not_found (e):
return render_template("405.html"), 405
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == "__main__":
db.create_all()
app.run(debug=True)