forked from dongweiming/web_develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e830d41
commit 442d455
Showing
54 changed files
with
223 additions
and
370 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# coding=utf-8 | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db = SQLAlchemy() |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.