From d501b79338f1bede1daea972f2ec101fd8885ac5 Mon Sep 17 00:00:00 2001 From: jerry1ye10 Date: Sat, 29 Jul 2017 23:53:24 -0400 Subject: [PATCH 1/3] Update views.py --- app/views.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/app/views.py b/app/views.py index f29c9fa..db8d565 100644 --- a/app/views.py +++ b/app/views.py @@ -1,5 +1,6 @@ from flask import render_template, flash, redirect, request, session, url_for, jsonify, make_response from app import app, db, models +import datetime @@ -31,7 +32,7 @@ def return_description(section_slug,subsection_slug): @app.route('/get_section_articles/sections//subsection//articles/', methods=['GET'], defaults={'article_slug': None}) @app.route('/get_section_articles/sections//subsection//articles/', methods=['GET']) def articles_in_section(section_slug,subsection_slug,article_slug): - if article_slug != None and article_slug != "None": + if article_slug is not None and article_slug is not "None": articles = [models.Article.query.filter( models.Article.slug == article_slug ).first()] @@ -128,4 +129,78 @@ def all_articles(): if limit is not None: secure_articles = secure_articles[:int(limit)] return jsonify( {"articles": secure_articles} ) -#---------------------------------------------- +#---------------------------------------------- POST endpoints +def find_section_of_article(section_name): + section = models.Section.query.filter(models.Section.name==section_name).first() + if section == None: + return None + else: + return section +@app.route('/create_article/articles', methods=['POST']) +def create_article(): + article = models.Article(title = request.form["title"], + slug = request.form["slug"], + content = request.form["content"], + datetime = datetime.datetime.utcnow(), + volume = int(request.form["volume"]), + issue = int(request.form["issue"]), + isDraft = request.form["isDraft"], + section = find_section_of_article(request.form["section"]), + subsection = None) #remove this after section and subsection become part of the same model + db.session.add(article) + db.session.commit() + return jsonify({"status":"Section has been added"}) +@app.route('/create_section/sections/', methods=['POST']) +def create_section(): + section = models.Section(name= request.form["name"], + slug= request.form["slug"], + description= request.form["description"]) + db.session.add(section) + db.session.commit() + return jsonify({"Status": "Section has been sucessfully added"}) +#---------------------------------------------- DELETE endpoints +@app.route('/delete_section/sections/', methods =['DELETE']) +def delete_section(section_slug): + section = models.Section.query.filter_by(slug=section_slug).first() + db.session.delete(section) + db.session.commit() + return jsonify({"Status":"Article has been deleted"}) +@app.route('/delete_article/articles/', methods = ['DELETE']) +def delete_article(article_slug): + article = models.Article.query.filter_by(slug = article_slug).first() + db.session.delete(article) + db.session.commit() + return jsonify({"Status":"Article has been deleted"}) +#---------------------------------------------- PUT endpoints +@app.route('/update_section/sections/', methods =['PUT']) +def update_section(section_slug): + section = models.Section.query.filter_by(slug=section_slug).first() + if request.form['name'] is not None: + section.name = request.form['name'] + if request.form['slug'] is not None: + section.slug = request.form['slug'] + if request.form['description'] is not None: + section.description = request.form['description'] + db.session.commit() + return jsonify({"Status":"Section has been updated"}) +@app.route('/update_article/articles/', methods =['PUT']) +def update_article(article_slug): + article = models.Article.query.filter_by(slug = article_slug).first() + if request.form['title'] is not None: + article.title = request.form['title'] + if request.form['slug'] is not None: + article.slug = request.form['slug'] + if request.form['content'] is not None: + article.content = request.form['content'] + if request.form['volume'] is not None: + article.volume = request.form['volume'] + if request.form['issue'] is not None: + article.issue = request.form['issue'] + if request.form['isDraft'] is not None: + article.isDraft = request.form['isDraft'] + if request.form['section'] is not None: + article.section = find_section_of_article(request.form['section']) + article.datetime = datetime.datetime.utcnow() + db.session.commit() + return jsonify({"Status":"Article has been updated"}) +#---------------------------------------------- From 3bb841f4fc214f9006b2a8c95edf206ff6cb089e Mon Sep 17 00:00:00 2001 From: jerry1ye10 Date: Sun, 30 Jul 2017 12:57:57 -0400 Subject: [PATCH 2/3] Update views.py Fixed jsonify returns of create article endpoint and delete section, were returning inaccurate strings as the value in the dictionary return. --- app/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views.py b/app/views.py index db8d565..00b0944 100644 --- a/app/views.py +++ b/app/views.py @@ -149,7 +149,7 @@ def create_article(): subsection = None) #remove this after section and subsection become part of the same model db.session.add(article) db.session.commit() - return jsonify({"status":"Section has been added"}) + return jsonify({"status":"Article has been added"}) @app.route('/create_section/sections/', methods=['POST']) def create_section(): section = models.Section(name= request.form["name"], @@ -164,7 +164,7 @@ def delete_section(section_slug): section = models.Section.query.filter_by(slug=section_slug).first() db.session.delete(section) db.session.commit() - return jsonify({"Status":"Article has been deleted"}) + return jsonify({"Status":"Section has been deleted"}) @app.route('/delete_article/articles/', methods = ['DELETE']) def delete_article(article_slug): article = models.Article.query.filter_by(slug = article_slug).first() From c79ddb0bdf7edf312a4520ff346aca4b7bbdc003 Mon Sep 17 00:00:00 2001 From: jerry1ye10 Date: Sun, 30 Jul 2017 15:24:44 -0400 Subject: [PATCH 3/3] Update views.py Fixed styling issues with CMS code such as renaming of function to get_section_by_id. Removes subsection as an attribute of the create_article endpoint, renamed endpoints --- app/views.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/app/views.py b/app/views.py index 00b0944..825900c 100644 --- a/app/views.py +++ b/app/views.py @@ -130,27 +130,26 @@ def all_articles(): secure_articles = secure_articles[:int(limit)] return jsonify( {"articles": secure_articles} ) #---------------------------------------------- POST endpoints -def find_section_of_article(section_name): - section = models.Section.query.filter(models.Section.name==section_name).first() +def get_section_by_id(section_id): + section = models.Section.query.filter(models.Section.id==section_id).first() if section == None: return None else: return section -@app.route('/create_article/articles', methods=['POST']) +@app.route('/articles', methods=['POST']) def create_article(): article = models.Article(title = request.form["title"], - slug = request.form["slug"], + slug = None, content = request.form["content"], datetime = datetime.datetime.utcnow(), volume = int(request.form["volume"]), issue = int(request.form["issue"]), isDraft = request.form["isDraft"], - section = find_section_of_article(request.form["section"]), - subsection = None) #remove this after section and subsection become part of the same model + section = get_section_by_id(request.form["section"])) db.session.add(article) db.session.commit() - return jsonify({"status":"Article has been added"}) -@app.route('/create_section/sections/', methods=['POST']) + return jsonify({"status":"Section has been added"}) +@app.route('/sections', methods=['POST']) def create_section(): section = models.Section(name= request.form["name"], slug= request.form["slug"], @@ -158,21 +157,19 @@ def create_section(): db.session.add(section) db.session.commit() return jsonify({"Status": "Section has been sucessfully added"}) -#---------------------------------------------- DELETE endpoints -@app.route('/delete_section/sections/', methods =['DELETE']) +@app.route('/sections/', methods =['DELETE']) def delete_section(section_slug): section = models.Section.query.filter_by(slug=section_slug).first() db.session.delete(section) db.session.commit() - return jsonify({"Status":"Section has been deleted"}) -@app.route('/delete_article/articles/', methods = ['DELETE']) + return jsonify({"Status":"Article has been deleted"}) +@app.route('/articles/', methods = ['DELETE']) def delete_article(article_slug): article = models.Article.query.filter_by(slug = article_slug).first() db.session.delete(article) db.session.commit() return jsonify({"Status":"Article has been deleted"}) -#---------------------------------------------- PUT endpoints -@app.route('/update_section/sections/', methods =['PUT']) +@app.route('/sections/', methods =['PUT']) def update_section(section_slug): section = models.Section.query.filter_by(slug=section_slug).first() if request.form['name'] is not None: @@ -183,7 +180,7 @@ def update_section(section_slug): section.description = request.form['description'] db.session.commit() return jsonify({"Status":"Section has been updated"}) -@app.route('/update_article/articles/', methods =['PUT']) +@app.route('/articles/', methods =['PUT']) def update_article(article_slug): article = models.Article.query.filter_by(slug = article_slug).first() if request.form['title'] is not None: @@ -199,7 +196,7 @@ def update_article(article_slug): if request.form['isDraft'] is not None: article.isDraft = request.form['isDraft'] if request.form['section'] is not None: - article.section = find_section_of_article(request.form['section']) + article.section = get_section_by_id(request.form['section']) article.datetime = datetime.datetime.utcnow() db.session.commit() return jsonify({"Status":"Article has been updated"})