This repository was archived by the owner on Oct 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_makale.py
More file actions
230 lines (195 loc) · 7.85 KB
/
Copy pathflask_makale.py
File metadata and controls
230 lines (195 loc) · 7.85 KB
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
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 email_validator import validate_email, EmailNotValidError
from functools import wraps
#kullanıcı Giriş Decarator
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if "logged_in" in session:
return f(*args, **kwargs)
else:
flash("Bu sayfayı görüntülemek için lütfen giriş yapın","danger")
return redirect(url_for("login"))
return decorated_function
#Kayıt Formu
class RegisterForm(Form):
name = StringField("İsim Soyisim",validators=[validators.Length(min = 4,max = 25)])
username = StringField("Kullanıcı Adı",validators=[validators.Length(min = 4,max = 35)])
email = StringField("Email Adresi",validators=[
validators.Email(message = "Lütfen geçerli bir email girin")])
password = PasswordField("Parola",validators=[
validators.DataRequired(message = "Lütfen bir parola belirleyin"),
validators.EqualTo(fieldname = "confirm",message = "Parolanız uyuşmuyor")
])
confirm = PasswordField(("Parola Doğrula"))
class LoginForm(Form):
username = StringField("Kullanıcı Adı")
password = PasswordField("Parola")
app = Flask(__name__)
app.secret_key = "mlyrdr"
app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = ""
app.config["MYSQL_DB"] = "mlyrdr"
app.config["MYSQL_CURSORCLASS"] = "DictCursor"
mysql = MySQL(app)
@app.route("/")
def index():
return render_template("index.html",)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/articles")
def articles():
cursor = mysql.connection.cursor()
sorgu = "Select * From articles"
result = cursor.execute(sorgu)
if result > 0:
articles = cursor.fetchall()
return render_template("articles.html",articles = articles)
else:
return render_template("articles.html")
@app.route("/dashboard")
@login_required
def dashboard():
cursor = mysql.connection.cursor()
sorgu = "Select * From articles where author = %s"
result = cursor.execute(sorgu,(session["username"],))
if result > 0:
articles = cursor.fetchall()
return render_template("dashboard.html",articles = articles)
else:
return render_template("dashboard.html")
#Kayıt Olma
@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 Olundu","success")
return redirect(url_for("login"))
else:
return render_template("register.html", form = form)
@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"
result = cursor.execute(sorgu,(username,))
if result > 0:
data = cursor.fetchone()
real_password = data["password"]
if sha256_crypt.verify(password_entered,real_password):
flash("Başarıyla Giriş Yaptınız","success")
session["logged_in"] = True
session["username"] = username
return redirect(url_for("index"))
else:
flash("Parolanızı Yanlış girdiniz","danger")
return redirect(url_for("login"))
else:
flash("Böyle bir kullanıcı bulunmuyor","danger")
return redirect(url_for("login"))
return render_template("login.html", form = form)
@app.route("/article/<string:id>")
def article(id):
cursor = mysql.connection.cursor()
sorgu = "Select * from articles where id = %s"
result = cursor.execute(sorgu,(id,))
if result > 0:
article = cursor.fetchone()
return render_template("article.html",article = article)
else:
return render_template("article.html")
@app.route("/logout")
def logout():
session.clear()
return redirect(url_for("index"))
@app.route("/addarticle",methods = ["GET","POST"])
def addarticle():
form = ArticleForm(request.form)
if request.method == "POST" and form.validate():
title = form.title.data
content = form.content.data
cursor = mysql.connection.cursor()
sorgu = "Insert into articles(title,author,content) VALUES(%s,%s,%s)"
cursor.execute(sorgu,(title,session["username"],content))
mysql.connection.commit()
cursor.close()
flash("Makale Başarıyla Eklendi","success")
return redirect(url_for("dashboard"))
return render_template("addarticle.html",form=form)
@app.route("/delete/<string:id>")
@login_required
def delete(id):
cursor = mysql.connection.cursor()
sorgu = "Select * from articles where author = %s and id = %s"
result = cursor.execute(sorgu,(session["username"],id))
if result > 0:
sorgu2 = "Delete from articles where id = %s"
cursor.execute(sorgu2,(id,))
mysql.connection.commit()
return redirect((url_for("dashboard")))
else:
flash("Böyle bir makale yok veya bu işleme yetkiniz yok","danger")
return redirect(url_for("index"))
@app.route("/edit/<string:id>", methods = ["GET","POST"])
@login_required
def update(id):
if request.method == "GET":
cursor = mysql.connection.cursor()
sorgu = "Select * from articles where id = %s and author = %s"
result = cursor.execute(sorgu,(id,session["username"]))
if result == 0:
flash("Böyle bir makale yok veya bu işleme yetkiniz yok","danger")
return redirect((url_for("index")))
else:
article = cursor.fetchone()
form = ArticleForm()
form.title.data = article["title"]
form.content.data = article["content"]
return render_template("update.html",form = form)
else:
form = ArticleForm(request.form)
newTitle = form.title.data
newContent = form.content.data
sorgu2 = "Update articles Set title = %s,content = %s where id = %s"
cursor = mysql.connection.cursor()
cursor.execute(sorgu2,(newTitle,newContent,id))
mysql.connection.commit()
flash("Makale başarıyla güncellendi","success")
return redirect((url_for("dashboard")))
class ArticleForm(Form):
title = StringField("Makale Başlığı",validators=[validators.Length(min=5,max=100)])
content = TextAreaField("Makale İçeriği",validators=[validators.Length(min=10)])
@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 articles where title like '%" + keyword +"%'"
result = cursor.execute(sorgu)
if result == 0:
flash("Aranan kelimeye uygun makale bulunamadı","warning")
return redirect(url_for("articles"))
else:
articles = cursor.fetchall()
return render_template("articles.html",articles = articles)
if __name__ == "__main__":
app.run(debug=True)