Skip to content

Commit

Permalink
Refactoring chapter3
Browse files Browse the repository at this point in the history
  • Loading branch information
dongweiming committed Jul 8, 2016
1 parent e830d41 commit 442d455
Show file tree
Hide file tree
Showing 54 changed files with 223 additions and 370 deletions.
33 changes: 0 additions & 33 deletions chapter3/section2/app_context.py

This file was deleted.

31 changes: 0 additions & 31 deletions chapter3/section2/app_context_proxy.py

This file was deleted.

48 changes: 0 additions & 48 deletions chapter3/section2/user_url.py

This file was deleted.

62 changes: 0 additions & 62 deletions chapter3/section2/user_url_v2.py

This file was deleted.

File renamed without changes.
29 changes: 29 additions & 0 deletions chapter3/section3/app_with_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding=utf-8
from flask import Flask, request, jsonify

from ext import db
from users import User

app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)

with app.app_context():
db.drop_all()
db.create_all()


@app.route('/users', methods=['POST'])
def users():
username = request.form.get('name')

user = User(username)
print 'User ID: {}'.format(user.id)
db.session.add(user)
db.session.commit()

return jsonify({'id': user.id})


if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000)
32 changes: 0 additions & 32 deletions chapter3/section3/bytecode_cache.py

This file was deleted.

5 changes: 5 additions & 0 deletions chapter3/section3/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# coding=utf-8
from consts import DB_URI
DEBUG = True
SQLALCHEMY_DATABASE_URI = DB_URI
SQLALCHEMY_TRACK_MODIFICATIONS = False
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions chapter3/section3/ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# coding=utf-8
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
18 changes: 0 additions & 18 deletions chapter3/section3/my_loader.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions chapter3/section3/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# coding=utf-8
from ext import db


class User(db.Model):
__tablename__ = 'users'

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50))

def __init__(self, name):
self.name = name
67 changes: 67 additions & 0 deletions chapter3/section4/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# coding=utf-8
import random

from flask import Flask, g, render_template
from ext import db
from users import User
app = Flask(__name__, template_folder='../../templates')
app.config.from_object('config')
db.init_app(app)


def get_current_user():
users = User.query.all()
return random.choice(users)


@app.before_first_request
def setup():
db.drop_all()
db.create_all()
fake_users = [
User('xiaoming', '[email protected]'),
User('dongwweiming', '[email protected]'),
User('admin', '[email protected]')
]
db.session.add_all(fake_users)
db.session.commit()


@app.before_request
def before_request():
g.user = get_current_user()


@app.teardown_appcontext
def teardown(exc=None):
if exc is None:
db.session.commit()
else:
db.session.rollback()
db.session.remove()
g.user = None


@app.context_processor
def template_extras():
return {'enumerate': enumerate, 'current_user': g.user}


@app.errorhandler(404)
def page_not_found(error):
return 'This page does not exist', 404


@app.template_filter('capitalize')
def reverse_filter(s):
return s.capitalize()


@app.route('/users')
def user_view():
users = User.query.all()
return render_template('chapter3/section4/user.html', users=users)


if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000)
Loading

0 comments on commit 442d455

Please sign in to comment.