Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,54 @@ def get_article(id):
article.content = markdown.markdown(article.content)
return render_template('article.html', article=article)

##########################################################
# Start of new code
##########################################################

# DELETE
@app.delete("/article/<int:id>")
def delete_article(id):
article = Article.query.get_or_404(id)
db.session.delete(article)
db.session.commit()
return """
<div>
<div class="m-2 p-2 bg-success rounded">Successfully Deleted Article!</div>
<a href="/" class="btn btn-primary">Return Home</a>
</div>
"""

# CREATE
@app.get("/article/new")
def create_article_form():
return render_template("form_create.html")

@app.post("/article")
def create_article():
article = get_article_from_request(request)
db.session.add(article)
db.session.commit()
return render_template("articles_list.html", articles=get_articles())

# UPDATE
@app.get("/article/<int:id>/edit")
def edit_article_form(id):
article = Article.query.get_or_404(id)
return render_template("form_edit.html", article=article)

@app.put("/article/<int:id>")
def edit_article(id):
existing_article = Article.query.get_or_404(id)
new_article_data = get_article_from_request(request)
existing_article.title = new_article_data.title
existing_article.content = new_article_data.content
db.session.commit()
return render_template("article_details.html", article=existing_article)

##########################################################
# End of new code
##########################################################

##########################################################
# End of Routes Section
##########################################################
Expand Down
6 changes: 3 additions & 3 deletions templates/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<h1>{{ article.title }}</h1>
<p>📅{{ article.date }}</p>
<p>
<button class="btn btn-sm btn-warning">Edit</button>
<button class="btn btn-sm btn-danger">Delete</button>
<button hx-get="/article/{{article.id}}/edit" hx-target="#articlebody" class="btn btn-sm btn-warning">Edit</button>
<button hx-delete="/article/{{article.id}}" hx-target="#articlebody" class="btn btn-sm btn-danger">Delete</button>
</p>
{{ article.content | safe }}
{{ article.content | safe }}
</div>
{% endblock %}
7 changes: 7 additions & 0 deletions templates/article_details.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>{{ article.title }}</h1>
<p>📅{{ article.date }}</p>
<p>
<button hx-get="/article/{{article.id}}/edit" hx-target="#articlebody" class="btn btn-sm btn-warning">Edit</button>
<button hx-delete="/article/{{article.id}}" hx-target="#articlebody" class="btn btn-sm btn-danger">Delete</button>
</p>
{{ article.content | safe }}
8 changes: 8 additions & 0 deletions templates/articles_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="m-2 p-2 bg-success rounded">Successfully Created Article!</div>
<button hx-get="/article/new" hx-target="#articlesarea" class="btn btn-lg btn-success">New Article</button>
{% for article in articles %}
<div class="container bg-body-tertiary rounded p-5 m-4">
<h2><a href="/article/{{ article.id }}">{{ article.title }}</a></h2>
<p>📅{{ article.date }}</p>
</div>
{% endfor %}
2 changes: 1 addition & 1 deletion templates/form_create.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form>
<form hx-post="/article" hx-swap="outerHTML">
<h2>New Article</h2>
<div class="mb-3">
<label for="title" class="form-label">Title</label>
Expand Down
2 changes: 1 addition & 1 deletion templates/form_edit.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form>
<form hx-put="/article/{{article.id}}" hx-swap="outerHTML">
<h2>Edit Article</h2>
<div class="mb-3">
<label for="title" class="form-label">Title</label>
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1>Welcome to My Blog!</h1>

<div id="articlesarea">
<button class="btn btn-lg btn-success">New Article</button>
<button hx-get="/article/new" hx-target="#articlesarea" class="btn btn-lg btn-success">New Article</button>
{% for article in articles %}
<div class="container bg-body-tertiary rounded p-5 m-4">
<h2><a href="/article/{{ article.id }}">{{ article.title }}</a></h2>
Expand Down
2 changes: 1 addition & 1 deletion templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<title>My Blog</title>
</head>

<body>
<body hx-boost="true">
<nav class="navbar bg-body-tertiary mb-3">
<div class="container-fluid">
<a class="navbar-brand" href="/">My Blog</a>
Expand Down