Skip to content

make style changes to conform to pep8 #1

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

Merged
merged 1 commit into from
Apr 21, 2015
Merged
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Ryan Shea - http://ryaneshea.com/
Copyright (c) 2015 Ryan Shea - http://shea.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 0 additions & 1 deletion angular_flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@
import angular_flask.core
import angular_flask.models
import angular_flask.controllers

34 changes: 18 additions & 16 deletions angular_flask/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,51 @@

from angular_flask import app

# routing for API endpoints (generated from the models designated as API_MODELS)
# routing for API endpoints, generated from the models designated as API_MODELS
from angular_flask.core import api_manager
from angular_flask.models import *

for model_name in app.config['API_MODELS']:
model_class = app.config['API_MODELS'][model_name]
api_manager.create_api(model_class, methods=['GET', 'POST'])
model_class = app.config['API_MODELS'][model_name]
api_manager.create_api(model_class, methods=['GET', 'POST'])

session = api_manager.session


# routing for basic pages (pass routing onto the Angular app)
@app.route('/')
@app.route('/about')
@app.route('/blog')
def basic_pages(**kwargs):
return make_response(open('angular_flask/templates/index.html').read())
return make_response(open('angular_flask/templates/index.html').read())


# routing for CRUD-style endpoints
# passes routing onto the angular frontend if the requested resource exists
from sqlalchemy.sql import exists

crud_url_models = app.config['CRUD_URL_MODELS']


@app.route('/<model_name>/')
@app.route('/<model_name>/<item_id>')
def rest_pages(model_name, item_id=None):
if model_name in crud_url_models:
model_class = crud_url_models[model_name]
if item_id is None or session.query(exists().where(
model_class.id == item_id)).scalar():
return make_response(open(
'angular_flask/templates/index.html').read())
abort(404)
if model_name in crud_url_models:
model_class = crud_url_models[model_name]
if item_id is None or session.query(exists().where(
model_class.id == item_id)).scalar():
return make_response(open(
'angular_flask/templates/index.html').read())
abort(404)


# special file handlers and error handlers
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'img/favicon.ico')
return send_from_directory(os.path.join(app.root_path, 'static'),
'img/favicon.ico')


@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404



1 change: 0 additions & 1 deletion angular_flask/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
db = SQLAlchemy(app)

api_manager = APIManager(app, flask_sqlalchemy_db=db)

29 changes: 15 additions & 14 deletions angular_flask/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
from angular_flask.core import db
from angular_flask import app


class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
pub_date = db.Column(db.DateTime)
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
pub_date = db.Column(db.DateTime)

def __init__(self, title, body, pub_date=None):
self.title = title
self.body = body
if pub_date is None:
pub_date = datetime.utcnow()
self.pub_date = pub_date
def __init__(self, title, body, pub_date=None):
self.title = title
self.body = body
if pub_date is None:
pub_date = datetime.utcnow()
self.pub_date = pub_date

def __repr__(self):
return '<Post %r>' % self.title
def __repr__(self):
return '<Post %r>' % self.title

# models for which we want to create API endpoints
app.config['API_MODELS'] = { 'post': Post }
app.config['API_MODELS'] = {'post': Post}

# models for which we want to create CRUD-style URL endpoints,
# and pass the routing onto our AngularJS application
app.config['CRUD_URL_MODELS'] = { 'post': Post }
app.config['CRUD_URL_MODELS'] = {'post': Post}
3 changes: 1 addition & 2 deletions angular_flask/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

DEBUG = True
SECRET_KEY = 'temporary_secret_key' # make sure to change this
SECRET_KEY = 'temporary_secret_key' # make sure to change this

SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/angular_flask.db'

80 changes: 43 additions & 37 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,54 @@
from angular_flask.core import db
from angular_flask.models import Post


def create_sample_db_entry(api_endpoint, payload):
url = 'http://localhost:5000/' + api_endpoint
r = requests.post(url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})
print r.text

url = 'http://localhost:5000/' + api_endpoint
r = requests.post(
url, data=json.dumps(payload),
headers={'Content-Type': 'application/json'})
print r.text


def create_db():
db.create_all()
db.create_all()


def drop_db():
db.drop_all()
db.drop_all()


def main():
parser = argparse.ArgumentParser(description='Manage this Flask application.')
parser.add_argument('command', help='the name of the command you want to run')
parser.add_argument('--seedfile', help='the file with data for seeding the database')
args = parser.parse_args()

if args.command == 'create_db':
create_db()

print "DB created!"
elif args.command == 'delete_db':
drop_db()

print "DB deleted!"
elif args.command == 'seed_db' and args.seedfile:
with open(args.seedfile, 'r') as f:
seed_data = json.loads(f.read())

for item_class in seed_data:
items = seed_data[item_class]
print items
for item in items:
print item
create_sample_db_entry('api/' + item_class, item)

print "\nSample data added to database!"
else:
raise Exception('Invalid command')
parser = argparse.ArgumentParser(
description='Manage this Flask application.')
parser.add_argument(
'command', help='the name of the command you want to run')
parser.add_argument(
'--seedfile', help='the file with data for seeding the database')
args = parser.parse_args()

if args.command == 'create_db':
create_db()

print "DB created!"
elif args.command == 'delete_db':
drop_db()

print "DB deleted!"
elif args.command == 'seed_db' and args.seedfile:
with open(args.seedfile, 'r') as f:
seed_data = json.loads(f.read())

for item_class in seed_data:
items = seed_data[item_class]
print items
for item in items:
print item
create_sample_db_entry('api/' + item_class, item)

print "\nSample data added to database!"
else:
raise Exception('Invalid command')

if __name__ == '__main__':
main()



main()
7 changes: 4 additions & 3 deletions runserver.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from angular_flask import app


def runserver():
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)

if __name__ == '__main__':
runserver()
runserver()