diff --git a/LICENSE b/LICENSE index ae02130..a650faa 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/angular_flask/__init__.py b/angular_flask/__init__.py index 7b6a647..5b5e036 100644 --- a/angular_flask/__init__.py +++ b/angular_flask/__init__.py @@ -12,4 +12,3 @@ import angular_flask.core import angular_flask.models import angular_flask.controllers - diff --git a/angular_flask/controllers.py b/angular_flask/controllers.py index 8389a4c..0713854 100644 --- a/angular_flask/controllers.py +++ b/angular_flask/controllers.py @@ -6,22 +6,24 @@ 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 @@ -29,26 +31,26 @@ def basic_pages(**kwargs): crud_url_models = app.config['CRUD_URL_MODELS'] + @app.route('//') @app.route('//') 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 - - - diff --git a/angular_flask/core.py b/angular_flask/core.py index 8b48d7b..5c2e24d 100644 --- a/angular_flask/core.py +++ b/angular_flask/core.py @@ -6,4 +6,3 @@ db = SQLAlchemy(app) api_manager = APIManager(app, flask_sqlalchemy_db=db) - diff --git a/angular_flask/models.py b/angular_flask/models.py index 0656cf4..62bd5dd 100644 --- a/angular_flask/models.py +++ b/angular_flask/models.py @@ -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 '' % self.title + def __repr__(self): + return '' % 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} diff --git a/angular_flask/settings.py b/angular_flask/settings.py index 4ffd0a5..312570f 100644 --- a/angular_flask/settings.py +++ b/angular_flask/settings.py @@ -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' - diff --git a/manage.py b/manage.py index 263780e..2d956a5 100644 --- a/manage.py +++ b/manage.py @@ -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() diff --git a/runserver.py b/runserver.py index c5988b4..aed7757 100644 --- a/runserver.py +++ b/runserver.py @@ -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()