@staticmethod defgenerate_fake(count=100): from random import seed, randint import forgery_py
seed() for i in range(count): p = Post(title=forgery_py.lorem_ipsum.title(), summary=forgery_py.lorem_ipsum.sentences(randint(1,2)), content=forgery_py.lorem_ipsum.sentences(randint(5,8)), timestamp=forgery_py.date.date(True), author_id=User.query.filter_by(is_administrator=True).first().id ) db.session.add(p) db.session.commit()
# app/main/views.py from flask import render_template, session, redirect, url_for
from . import main from .forms import NameForm from .. import db from ..models import User
@main.route('/', methods=['GET', 'POST']) defindex(): form = NameForm() if form.validate_on_submit(): # ... return redirect(url_for('.index')) return render_template('index.html', form=form, name=session.get('name'), known=session.get('known', False))
4. 启动脚本
顶级文件夹中的manage.py文件用于启动程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/env python import os from app import create_app, db from app.models import User, Role from flask.ext.script import Manager, Shell from flask.ext.migrate import Migrate, MigrateCommand
defregister(self, username, password, password2=None, email=None): """Helper function to register a user""" if password2 isNone: password2 = password if email isNone: email = username + '@example.com' return self.app.post('/register', data={ 'username': username, 'password': password, 'password2': password2, 'email': email }, follow_redirects=True)
deftest_register(self): """Make sure registering works""" rv = self.register('user1', 'default') assert'You were successfully registered and can login now'in rv.data rv = self.register('user1', 'default') assert'The username is already taken'in rv.data rv = self.register('', 'default') assert'You have to enter a username'in rv.data rv = self.register('meh', '') assert'You have to enter a password'in rv.data rv = self.register('meh', 'x', 'y') assert'The two password do not match'in rv.data rv = self.register('meh', 'foo', email='broken') assert'You have to enter a valid email address'in rv.data
deftest_timelines(self): """Make sure that timelines work""" self.register_and_login('foo', 'default') self.add_message('the message by foo') self.logout() self.register_and_login('bar', 'default') self.add_message('the message by bar') rv = self.app.get('/public') assert'the message by foo'in rv.data assert'the message by bar'in rv.data
# test bar's timeline rv = self.app.get('/') assert'the message by foo'notin rv.data assert'the message by bar'in rv.data
# test follow function rv = self.app.get('/foo/follow', follow_redirects=True) assert'You are now following "foo"'in rv.data
rv = self.app.get('/') assert'the message by foo'in rv.data assert'the message by bar'in rv.data
# test the user timeline rv = self.app.get('/bar') assert'the message by foo'notin rv.data assert'the message by bar'in rv.data rv = self.app.get('/foo') assert'the message by foo'in rv.data assert'the message by bar'notin rv.data
# test unfollow function rv = self.app.get('/foo/unfollow', follow_redirects=True) assert'You are no longer following "foo"'in rv.data rv = self.app.get('/') assert'the message by foo'notin rv.data assert'the message by bar'in rv.data
Step 6: 部署程序
Web app在经过测试后,就可以部署上线了,以便用户通过互联网进行访问。下面我们将使用Heroku来部署我们的MiniWeibo。