Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update views.py #4

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
76 changes: 74 additions & 2 deletions app/views.py
Original file line number Diff line number Diff line change
@@ -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



Expand Down Expand Up @@ -31,7 +32,7 @@ def return_description(section_slug,subsection_slug):
@app.route('/get_section_articles/sections/<string:section_slug>/subsection/<string:subsection_slug>/articles/', methods=['GET'], defaults={'article_slug': None})
@app.route('/get_section_articles/sections/<string:section_slug>/subsection/<string:subsection_slug>/articles/<string:article_slug>', 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()]
Expand Down Expand Up @@ -128,4 +129,75 @@ def all_articles():
if limit is not None:
secure_articles = secure_articles[:int(limit)]
return jsonify( {"articles": secure_articles} )
#----------------------------------------------
#---------------------------------------------- POST endpoints
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('/articles', methods=['POST'])
def create_article():
article = models.Article(title = request.form["title"],
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 = get_section_by_id(request.form["section"]))
db.session.add(article)
db.session.commit()
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"],
description= request.form["description"])
db.session.add(section)
db.session.commit()
return jsonify({"Status": "Section has been sucessfully added"})
@app.route('/sections/<string:section_slug>', 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('/articles/<string:article_slug>', 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"})
@app.route('/sections/<string:section_slug>', 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('/articles/<string:article_slug>', 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 = get_section_by_id(request.form['section'])
article.datetime = datetime.datetime.utcnow()
db.session.commit()
return jsonify({"Status":"Article has been updated"})
#----------------------------------------------