-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.py
268 lines (207 loc) · 8.15 KB
/
blog.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
from flask import Flask,render_template,flash,redirect,url_for,session,logging,request
from flask_mysqldb import MySQL
from wtforms import Form,StringField,TextAreaField,PasswordField,validators
from passlib.hash import sha256_crypt
from functools import wraps
#kullanıcı girişi decorator
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if "logged_in" in session:
return f(*args, **kwargs)
else:
flash("Bu sayfayı gormek için giriş yapın..","danger")
return redirect(url_for("login"))
return decorated_function
app = Flask(__name__)
app.secret_key="tanerblog"
app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = ""
app.config["MYSQL_DB"] = "tanerblog"
app.config["MYSQL_CURSORCLASS"]= "DictCursor"
mysql = MySQL(app)
#kayıt formu
class RegisterForm(Form):
name = StringField("Adınız",validators=[validators.Length(min=4,max=20),validators.DataRequired(message="İsminizi Girin")])
username = StringField("Kullanıcı Adı",validators=[validators.Length(min=5,max=25),validators.DataRequired(message="Kullanıcı adınızı girin")])
email = StringField("Email Adresi",validators=[validators.Length(min=12,max=48),validators.Email(message="Geçerli Email Adresi Girin")])
password = PasswordField("Şifreniz",validators=[validators.DataRequired(message="Lütfen şifrenizi boş bırakmayın"),
validators.EqualTo(fieldname="confirm",message="Şifreniz Uyuşmuyor..")
])
confirm = PasswordField("Şifre Doğrula")
#kayıt olma işlemi
@app.route("/register",methods=["GET", "POST"])
def register():
form = RegisterForm(request.form)
if request.method=='POST' and form.validate():
name = form.name.data
username = form.username.data
email = form.email.data
password = sha256_crypt.encrypt(form.password.data)
cursor = mysql.connection.cursor()
sorgu ="Insert into users(name,email,username,password) VALUES(%s, %s, %s, %s)"
cursor.execute(sorgu,(name,email,username,password))
mysql.connection.commit()
cursor.close()
flash("Başarıyla Kayıt Oldunuz..","success")
return redirect(url_for("login"))
else:
return render_template("register.html",form=form)
#login formu
class LoginForm(Form):
username = StringField("Kullanıcı Adı")
password = PasswordField("Şifre")
#login işlemi
@app.route("/login",methods=['GET','POST'])
def login():
form = LoginForm(request.form)
if request.method =="POST":
username = form.username.data
password_entered = form.password.data
cursor = mysql.connection.cursor()
sorgu = "Select * From users where username = %s"
deger = cursor.execute(sorgu,(username,))
if deger>0:
data = cursor.fetchone()
real_password = data["password"]
if sha256_crypt.verify((password_entered),real_password):
print(real_password)
flash("Giriş İşlemi Başarılı","success")
session["logged_in"]=True
session["username"]=username
return redirect(url_for("index"))
else:
flash("Yanlış şifre girdiniz!! ","dark")
return redirect(url_for("login"))
else:
flash("Böyle bir kullanıcı adı yok...!","danger")
return redirect(url_for("login"))
return render_template("login.html",form=form)
#logout işlemi
@app.route("/logout")
def logout():
session.clear()
return redirect(url_for("index"))
#index sayfasi
@app.route("/")
def index():
return render_template("index.html")
#hakkımda sayafası
@app.route("/about")
def about():
return render_template("hakkimda.html")
#kontrol paneli sayfasi
@app.route("/dashboard")
@login_required
def dashboard():
cursor = mysql.connection.cursor()
sorgu = "Select * From filmler where author = %s"
deger = cursor.execute(sorgu,(session["username"],))
if deger>0:
filmler = cursor.fetchall()
return render_template("dashboard.html",filmler=filmler)
else:
return render_template("dashboard.html")
#film form
class FilmForm(Form):
title = StringField("Film Başlığı",validators=[validators.Length(min=3,max=85)])
content = TextAreaField("Film İçerigi",validators=[validators.Length(min=10)])
#film ekleme
@app.route("/addfilm",methods=["GET","POST"])
def addfilm():
form = FilmForm(request.form)
if request.method=="POST" and form.validate():
title = form.title.data
content = form.content.data
cursor = mysql.connection.cursor()
sorgu ="Insert into filmler (title,author,content) VALUES(%s,%s,%s)"
cursor.execute(sorgu,(title,session["username"],content))
mysql.connection.commit()
cursor.close()
flash("Oluşturduğun film eklendi..","success")
return redirect(url_for("dashboard"))
return render_template("addfilm.html",form=form)
#filmler sayfası
@app.route("/filmler")
def filmler():
cursor = mysql.connection.cursor()
sorgu ="Select * From filmler"
deger = cursor.execute(sorgu)
if deger > 0:
filmler = cursor.fetchall()
return render_template("filmler.html",filmler=filmler)
else :
return render_template("filmler.html")
#film detay sayfası
@app.route("/film/<string:id>")
def film(id):
cursor = mysql.connection.cursor()
sorgu ="Select * From filmler where id = %s"
deger = cursor.execute(sorgu,(id,))
if deger >0 :
film = cursor.fetchone()
return render_template("film.html",film=film)
else:
return render_template("film.html")
#film silme
@app.route("/delete/<string:id>")
@login_required
def delete(id):
cursor = mysql.connection.cursor()
sorgu ="Select * from filmler where author = %s and id = %s"
deger = cursor.execute(sorgu,(session["username"],id))
if deger > 0:
sorgu2="Delete from filmler where id = %s"
cursor.execute(sorgu2,(id,))
mysql.connection.commit()
return redirect(url_for("dashboard"))
else:
flash("Bu filmi silme yetkiniz yoktur","warning")
return redirect(url_for("index"))
#film guncelleme
@app.route("/edit/<string:id>",methods=["GET","POST"])
@login_required
def guncelleme(id):
if request.method=="GET":
cursor = mysql.connection.cursor()
sorgu = "Select * from filmler where id = %s and author = %s"
sonuc = cursor.execute(sorgu,(id,session["username"]))
if sonuc == 0:
flash("Böyle bir film yok veya yetkiniz yok..!","danger")
return redirect(url_for("index"))
else:
film = cursor.fetchone()
form = FilmForm()
form.title.data=film["title"]
form.content.data=film["content"]
return render_template("update.html",form=form)
else:
#post request
form = FilmForm(request.form)
newtitle=form.title.data
newcontent=form.content.data
sorgu2="Update filmler Set title = %s, content = %s where id = %s"
cursor=mysql.connection.cursor()
cursor.execute(sorgu2,(newtitle,newcontent,id))
mysql.connection.commit()
flash("Film Güncellendi..","success")
return redirect(url_for("dashboard"))
#film arama url
@app.route("/search",methods=["GET","POST"])
def search():
if request.method=="GET":
return redirect(url_for("index"))
else:
keyword = request.form.get("keyword")
cursor = mysql.connection.cursor()
sorgu ="Select *from filmler where title like '%"+keyword+"%' "
sonuc = cursor.execute(sorgu)
if sonuc == 0:
flash("Aranan kelimeye uygun film bulunmadı..","dark")
return redirect(url_for("filmler"))
else:
filmler=cursor.fetchall()
return render_template("filmler.html",filmler=filmler)
if __name__ == "__main__":
app.run(debug=True)