Skip to content

Commit

Permalink
Makaleler için Güncelle ve sil özelliği eklendi. Arama butonu oluştur…
Browse files Browse the repository at this point in the history
…uldu. Progenin son hali makalelerdeki <blog.py'im> adlı makaleye eklendi
  • Loading branch information
canonka committed Mar 10, 2019
1 parent e28c0dd commit b0e81ad
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 1 deletion.
146 changes: 146 additions & 0 deletions blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
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ş 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ı görüntülemek için lütfen giriş yapın.","danger")
return redirect(url_for("login"))

return decorated_function

# kullanıcı kayıt formu
class RegisterForm(Form):
Expand Down Expand Up @@ -29,6 +42,7 @@ class LoginForm(Form):

mysql = MySQL(app)

# indexe yönlendirme yaptık.
@app.route("/")
def index():
return render_template("index.html")
Expand All @@ -38,6 +52,33 @@ def index():
def about():
return render_template("about.html")

#Makale Sayfası
@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")

#Kontrol Paneli oluşturuldu.
@app.route("/dashboard")
#login oldu mu olmadımı kontol edip öyle sayfaya gidiyor.
@login_required
def dashboard():
cursor = mysql.connection.cursor()
sorgu = "Select * From articles where author = %s"
#database sorgumuzu gerçekleştirdik.
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 register
@app.route("/register",methods = ["GET","POST"])
def register():
Expand Down Expand Up @@ -89,12 +130,117 @@ def login():
return redirect(url_for("login"))
return render_template("login.html",form=form)

#Makale Detay sayfası
@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")


#logout çıkış işlemi
@app.route("/logout")
def logout():
session.clear()
flash("Başarıyla çıkış yaptınız...","success")
return redirect(url_for("index"))

#Makale ekleme
@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)

#Makale Silme
@app.route("/delete/<string:id>")
#login_required olduğu için önce giriş yapmamızı isteyecek
@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("Bu işleme yetkiniz yoktur ","danger")
return redirect(url_for("index"))

#Makele güncelleme
@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 ya da 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:
#Post REQUEST kısmı
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))
#commit veritabanında değişiklik yaptığımızda kullanıyoruz.
mysql.connection.commit()
flash("Makale başarıyla güncellendi...","success")
return redirect(url_for("dashboard"))

#Makale Formu
class ArticleForm(Form):
title = StringField("Makale Başlığı", validators=[validators.Length(min = 5, max=75)])
content = TextAreaField("Makale İçeriği",validators=[validators.length(min = 10)])

#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 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)
16 changes: 16 additions & 0 deletions templates/addarticle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "layout.html" %}

{% block body %}

<h3>Makale Oluştur</h3>
<hr>

{% from "includes/formhelpers.html" import render_field %}

<form method="post">
{{ render_field(form.title,class = "form-control") }}
{{ render_field(form.content,class = "form-control") }}
<button type="submit" class="btn btn-danger">Makale Ekle</button>
</form>

{% endblock body %}
22 changes: 22 additions & 0 deletions templates/article.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "layout.html" %}

{% block body %}

{% if article %}

<h4>{{article.title}}</h4>
<hr>
Yazar : {{article.author}}
<hr>
Tarih : {{article.created_date}}
<hr>
İçerik : <br/>
{{article.content|safe}}
<hr>
{% else %}
<div class = "alert alert-warning">
Böyle bir makale bulunmuyor...
</div>
{% endif %}

{% endblock body %}
34 changes: 34 additions & 0 deletions templates/articles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% extends "layout.html" %}

{% block body %}

<h3>Makaleler</h3>
<hr>

{% if articles %}

<form action="/search" method = "post">

<div class="search">
<input type="text" name = "keyword" class="input-sm" maxlength="64" placeholder="Ara...">
<button type="submit" class="btn btn-danger">Ara</button>
</div>

</form>
<hr>

<ul class="list-group">
{% for article in articles %}

<li class="list-group-item"><a href="article/{{article.id}}">{{article.title}}</a></li>

{% endfor %}

</ul>

{% else %}
<div class = "alert alert-danger">Bu blogda henüz bir makale bulunmuyor.</div>
{% endif %}

{% endblock body %}

39 changes: 39 additions & 0 deletions templates/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends "layout.html" %}

{% block body %}
<h3>Kontrol Paneli</h3>
<small>Hoşgeldin, {{session["username"]}}</small>
<hr>

<a href="/addarticle" class = "btn btn-danger">Makale Ekle</a>

{% if articles %}
<table class="table table-hover">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Başlık</th>
<th scope="col">Yazar</th>
<th scope="col">Tarih</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for article in articles %}
<tr>
<th scope="row">{{article.id}}</th>
<td><a href="/article/{{article.id}}">{{article.title}}</a></td>
<td>{{article.author}}</td>
<td>{{article.created_date}}</td>
<td><a href="/edit/{{article.id}}" class="btn btn-danger">Güncelle</a></td>
<td><a href="/delete/{{article.id}}" class="btn btn-danger">Sil</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<div class="alert alert-danger">Henüz makaleniz bulunmuyor...</div>
{% endif %}

{% endblock body %}
3 changes: 3 additions & 0 deletions templates/includes/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<ul class="navbar-nav ml-auto">

{% if session["logged_in"] %}
<li class="nav-item active">
<a class="nav-link" href="/dashboard">Kontrol Paneli</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="/logout">Çıkış Yap</a>
</li>
Expand Down
9 changes: 8 additions & 1 deletion templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class = "container">
{% include "includes/messages.html" %}
{% block body %}

{% endblock %}
</div>

Expand All @@ -23,5 +23,12 @@
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdn.ckeditor.com/4.11.3/standard/ckeditor.js"></script>
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<script>
CKEDITOR.replace('content',{
allowedContent : true
});
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions templates/update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "layout.html" %}

{% block body %}

<h3>Makale Güncelle</h3>
<hr>

{% from "includes/formhelpers.html" import render_field %}

<form method="post">
{{ render_field(form.title,class = "form-control") }}
{{ render_field(form.content,class = "form-control") }}
<button type="submit" class="btn btn-danger">Güncelle</button>
</form>

{% endblock body %}

0 comments on commit b0e81ad

Please sign in to comment.