diff --git a/app.py b/app.py index df8b847..414eca5 100644 --- a/app.py +++ b/app.py @@ -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/") +def delete_article(id): + article = Article.query.get_or_404(id) + db.session.delete(article) + db.session.commit() + return """ +
+
Successfully Deleted Article!
+ Return Home +
+ """ + +# 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//edit") +def edit_article_form(id): + article = Article.query.get_or_404(id) + return render_template("form_edit.html", article=article) + +@app.put("/article/") +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 ########################################################## diff --git a/templates/article.html b/templates/article.html index 9e3ed24..2ce2bd8 100644 --- a/templates/article.html +++ b/templates/article.html @@ -4,9 +4,9 @@

{{ article.title }}

📅{{ article.date }}

- - + +

- {{ article.content | safe }} + {{ article.content | safe }} {% endblock %} diff --git a/templates/article_details.html b/templates/article_details.html new file mode 100644 index 0000000..f83cc74 --- /dev/null +++ b/templates/article_details.html @@ -0,0 +1,7 @@ +

{{ article.title }}

+

📅{{ article.date }}

+

+ + +

+{{ article.content | safe }} diff --git a/templates/articles_list.html b/templates/articles_list.html new file mode 100644 index 0000000..64a019f --- /dev/null +++ b/templates/articles_list.html @@ -0,0 +1,8 @@ +
Successfully Created Article!
+ +{% for article in articles %} +
+

{{ article.title }}

+

📅{{ article.date }}

+
+{% endfor %} \ No newline at end of file diff --git a/templates/form_create.html b/templates/form_create.html index 397e2f1..a0cd573 100644 --- a/templates/form_create.html +++ b/templates/form_create.html @@ -1,4 +1,4 @@ -
+

New Article

diff --git a/templates/form_edit.html b/templates/form_edit.html index 7acacd9..759c9b8 100644 --- a/templates/form_edit.html +++ b/templates/form_edit.html @@ -1,4 +1,4 @@ - +

Edit Article

diff --git a/templates/index.html b/templates/index.html index ab0ecaa..692b08e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,7 +3,7 @@

Welcome to My Blog!

- + {% for article in articles %}

{{ article.title }}

diff --git a/templates/layout.html b/templates/layout.html index 3e5da84..9a8076c 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -10,7 +10,7 @@ My Blog - +