Skip to content

Commit 4a1d6d0

Browse files
committed
improve communication b/t angular and api; improve data importing; make a nice little blog layout
1 parent 7fd6af6 commit 4a1d6d0

File tree

16 files changed

+110
-41
lines changed

16 files changed

+110
-41
lines changed

AngularFlask/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
app.config.from_object('AngularFlask.settings')
99

10+
app.url_map.strict_slashes = False
11+
1012
import AngularFlask.core
1113
import AngularFlask.models
1214
import AngularFlask.controllers

AngularFlask/controllers.py

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,59 @@
66

77
from AngularFlask import app
88

9-
# special file handlers
10-
@app.route('/favicon.ico')
11-
def favicon():
12-
return send_from_directory(os.path.join(app.root_path, 'static'), 'img/favicon.ico')
9+
###
10+
# controllers/routing for API endpoints
11+
# (auto-generated from the models listed in app.config['API_MODELS'])
12+
###
13+
from AngularFlask.core import api_manager
14+
from AngularFlask.models import *
1315

14-
# 404 error handler
15-
@app.errorhandler(404)
16-
def page_not_found(e):
17-
return render_template('404.html'), 404
16+
api_models = app.config['API_MODELS']
17+
for model_name in api_models:
18+
model_class = api_models[model_name]
19+
api_manager.create_api(model_class, methods=['GET', 'POST'])
20+
21+
session = api_manager.session
1822

19-
# basic page url handler
23+
###
24+
# controllers/routing for basic pages (pass routing onto the Angular app)
25+
###
2026
@app.route('/')
2127
@app.route('/about')
28+
@app.route('/blog')
2229
def basic_pages(**kwargs):
2330
return make_response(open('AngularFlask/templates/index.html').read())
2431

25-
# API
26-
from AngularFlask.core import api_manager
27-
from AngularFlask.models import Post
28-
29-
session = api_manager.session
30-
31-
api_manager.create_api(Post, methods=['GET', 'POST'])
32-
33-
# RESTful page url handler
32+
###
33+
# controllers/routing for CRUD-style endpoints, or ones that refer to a particular resource or collection
34+
# (pass routing onto the Angular app only if the corresponding resource exists in the db)
35+
###
3436
from sqlalchemy.sql import exists
3537

36-
supported_models = ['post']
38+
crud_url_models = app.config['CRUD_URL_MODELS']
3739

38-
@app.route('/<model_name>')
40+
@app.route('/<model_name>/')
3941
@app.route('/<model_name>/<item_id>')
4042
def rest_pages(model_name, item_id=None):
41-
if model_name in supported_models:
42-
model = eval(model_name.capitalize())
43-
if session.query(exists().where(model.id == item_id)).scalar():
43+
if model_name in crud_url_models:
44+
model_class = crud_url_models[model_name]
45+
if item_id is None or session.query(exists().where(model_class.id == item_id)).scalar():
4446
return make_response(open('AngularFlask/templates/index.html').read())
4547
abort(404)
4648

49+
###
50+
# special file handlers
51+
##
52+
@app.route('/favicon.ico')
53+
def favicon():
54+
return send_from_directory(os.path.join(app.root_path, 'static'), 'img/favicon.ico')
55+
56+
###
57+
# error handlers
58+
##
59+
@app.errorhandler(404)
60+
def page_not_found(e):
61+
return render_template('404.html'), 404
62+
63+
4764

AngularFlask/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime
22

33
from AngularFlask.core import db
4+
from AngularFlask import app
45

56
class Post(db.Model):
67
id = db.Column(db.Integer, primary_key=True)
@@ -18,3 +19,9 @@ def __init__(self, title, body, pub_date=None):
1819
def __repr__(self):
1920
return '<Post %r>' % self.title
2021

22+
# models for which we want to create API endpoints
23+
app.config['API_MODELS'] = { 'post': Post }
24+
25+
# models for which we want to create CRUD-style URL endpoints,
26+
# and pass the routing onto our AngularJS application
27+
app.config['CRUD_URL_MODELS'] = { 'post': Post }

AngularFlask/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
DEBUG = True
33
SECRET_KEY = '7bfb14e2315513c0fc0e1a6f8e343ba3904c61b10665d3dd'
44

5-
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/AngularFlask.db'
5+
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/AngularFlask4.db'
66

AngularFlask/static/css/main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
}
3030

3131
.header-push {
32-
height: 370px;
32+
height: 250px;
3333
}
3434

3535
.main {

AngularFlask/static/js/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ angular.module('AngularFlask', ['angularFlaskServices'])
2020
templateUrl: '/static/partials/post-detail.html',
2121
controller: PostDetailController
2222
})
23+
/* Create a "/blog" route that takes the user to the same place as "/post" */
24+
.when('/blog', {
25+
templateUrl: 'static/partials/post-list.html',
26+
controller: PostListController
27+
})
2328
.otherwise({
2429
redirectTo: '/'
2530
})

AngularFlask/static/js/controllers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ function AboutController($scope) {
1111
}
1212

1313
function PostListController($scope, Post) {
14-
$scope.posts = Post.query();
14+
$scope.postsQ = Post.get({}, function(posts) {
15+
$scope.posts = posts.objects;
16+
});
1517
}
1618

1719
function PostDetailController($scope, $routeParams, Post) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
<p>This is an auto-generated AngularJS + Flask application.</p>
2+
<p>This is a basic AngularJS + Flask application.</p>
33
</div>

AngularFlask/static/partials/landing.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ <h1 class="heading">Welcome Home.</h1>
99
<div class="header-push">
1010
</div>
1111

12+
<div>
13+
<h3><a href="/blog">Check out my blog</a></h3>
14+
</div>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<div>
2-
<p>Title: {{ postTitle }}</p>
3-
<p>Body: {{ postBody }}</p>
2+
<h2>{{ postTitle }}</h2>
3+
<h4>{{ postBody }}</h4>
4+
5+
<p><a href="/blog">back</a></p>
46
</div>

0 commit comments

Comments
 (0)