Skip to content

Commit

Permalink
Restructuring application.
Browse files Browse the repository at this point in the history
  • Loading branch information
Faerbit committed Jun 22, 2015
1 parent c9d8732 commit a088354
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
12 changes: 12 additions & 0 deletions todo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from flask import Flask
from flask.ext.cors import CORS

DATABASE=os.getenv("DATABASE_URL", "sqlite://")
DATABASE=DATABASE.strip()

app = Flask(__name__)
app.config.from_object(__name__)
CORS(app, resources=r'/*', allow_headers="Content-Type")

import todo.views
43 changes: 2 additions & 41 deletions todo/main.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
#!/usr/bin/env python3

import os
from flask import Flask, Response, jsonify, request
from flask import json
from flask.ext.cors import CORS

DATABASE=os.getenv("DATABASE_URL", "sqlite://")
DATABASE=DATABASE.strip()

app = Flask(__name__)
app.config.from_object(__name__)
CORS(app, resources=r'/*', allow_headers="Content-Type")

# placed down here to prevent circular imports
# messing up things
from todo.database import db_session
from todo.models import Entry

@app.route("/", methods=["GET", "POST", "DELETE"])
def index():
if request.method == "POST":
request_json = request.get_json()
entry = Entry(request_json["title"])
db_session.add(entry)
db_session.commit()
return jsonify(title=request_json["title"])
else:
response = "["
for entry in Entry.query.all():
response += json.dumps(dict(title=entry.title))
response += "]"
return response

@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()

def run():
app.run()
from todo import app

if __name__ == "__main__":
run()

app.run()
File renamed without changes.
26 changes: 26 additions & 0 deletions todo/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from todo import app

from flask import jsonify, request
from flask import json

from todo.database import db_session
from todo.models import Entry

@app.route("/", methods=["GET", "POST", "DELETE"])
def index():
if request.method == "POST":
request_json = request.get_json()
entry = Entry(request_json["title"])
db_session.add(entry)
db_session.commit()
return jsonify(title=request_json["title"])
else:
response = "["
for entry in Entry.query.all():
response += json.dumps(dict(title=entry.title))
response += "]"
return response

@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()

0 comments on commit a088354

Please sign in to comment.