python 3, pep8 and other various updates

This commit is contained in:
Miguel Grinberg 2014-09-19 07:47:13 -07:00
parent 6b193afe47
commit afa6c33e9e
41 changed files with 569 additions and 2984 deletions

View File

@ -1,4 +1,3 @@
web: gunicorn runp-heroku:app web: gunicorn runp-heroku:app
init: python db_create.py && pybabel compile -d app/translations init: python db_create.py
upgrade: python db_upgrade.py && pybabel compile -d app/translations upgrade: python db_upgrade.py

View File

@ -6,11 +6,7 @@ A decently featured microblogging web application written in Python and Flask th
Installation Installation
------------ ------------
The tutorial referenced above explains how to setup a virtual environment with all the required modules. As a convenience, the `setup.py` script will create this virtual environment for you. You can run this script again to refresh any missing modules. Note that to be able to run this script you have to have the following packages installed: The tutorial referenced above explains how to setup a virtual environment with all the required modules.
- Python 2.7
- Python development package (`python-dev` for most Linux distributions)
- git
The sqlite database must also be created before the application can run, and the `db_create.py` script takes care of that. See the [Database tutorial](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database) for the details. The sqlite database must also be created before the application can run, and the `db_create.py` script takes care of that. See the [Database tutorial](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database) for the details.

View File

@ -1,12 +1,14 @@
import os import os
from flask import Flask from flask import Flask
from flask.json import JSONEncoder
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager from flask.ext.login import LoginManager
from flask.ext.openid import OpenID from flask.ext.openid import OpenID
from flask.ext.mail import Mail from flask.ext.mail import Mail
from flask.ext.babel import Babel, lazy_gettext from flask.ext.babel import Babel, lazy_gettext
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, \
from momentjs import momentjs MAIL_PASSWORD
from .momentjs import momentjs
app = Flask(__name__) app = Flask(__name__)
app.config.from_object('config') app.config.from_object('config')
@ -19,22 +21,41 @@ oid = OpenID(app, os.path.join(basedir, 'tmp'))
mail = Mail(app) mail = Mail(app)
babel = Babel(app) babel = Babel(app)
class CustomJSONEncoder(JSONEncoder):
"""This class adds support for lazy translation texts to Flask's
JSON encoder. This is necessary when flashing translated texts."""
def default(self, obj):
from speaklater import is_lazy_string
if is_lazy_string(obj):
try:
return unicode(obj) # python 2
except NameError:
return str(obj) # python 3
return super(CustomJSONEncoder, self).default(obj)
app.json_encoder = CustomJSONEncoder
if not app.debug and MAIL_SERVER != '': if not app.debug and MAIL_SERVER != '':
import logging import logging
from logging.handlers import SMTPHandler from logging.handlers import SMTPHandler
credentials = None credentials = None
if MAIL_USERNAME or MAIL_PASSWORD: if MAIL_USERNAME or MAIL_PASSWORD:
credentials = (MAIL_USERNAME, MAIL_PASSWORD) credentials = (MAIL_USERNAME, MAIL_PASSWORD)
mail_handler = SMTPHandler((MAIL_SERVER, MAIL_PORT), 'no-reply@' + MAIL_SERVER, ADMINS, 'microblog failure', credentials) mail_handler = SMTPHandler((MAIL_SERVER, MAIL_PORT),
'no-reply@' + MAIL_SERVER, ADMINS,
'microblog failure', credentials)
mail_handler.setLevel(logging.ERROR) mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler) app.logger.addHandler(mail_handler)
if not app.debug and os.environ.get('HEROKU') is None: if not app.debug and os.environ.get('HEROKU') is None:
import logging import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler('tmp/microblog.log', 'a', 1 * 1024 * 1024, 10) file_handler = RotatingFileHandler('tmp/microblog.log', 'a',
1 * 1024 * 1024, 10)
file_handler.setLevel(logging.INFO) file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')) file_handler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
app.logger.addHandler(file_handler) app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO) app.logger.setLevel(logging.INFO)
app.logger.info('microblog startup') app.logger.info('microblog startup')
@ -49,4 +70,3 @@ if os.environ.get('HEROKU') is not None:
app.jinja_env.globals['momentjs'] = momentjs app.jinja_env.globals['momentjs'] = momentjs
from app import views, models from app import views, models

View File

@ -1,8 +1,8 @@
from threading import Thread from threading import Thread
def async(f): def async(f):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
thr = Thread(target=f, args=args, kwargs=kwargs) thr = Thread(target=f, args=args, kwargs=kwargs)
thr.start() thr.start()
return wrapper return wrapper

View File

@ -1,20 +1,21 @@
from flask import render_template from flask import render_template, current_app
from flask.ext.mail import Message from flask.ext.mail import Message
from app import mail from app import mail
from decorators import async from .decorators import async
from config import ADMINS from config import ADMINS
@async @async
def send_async_email(msg): def send_async_email(app, msg):
with app.app_context():
mail.send(msg) mail.send(msg)
def send_email(subject, sender, recipients, text_body, html_body): def send_email(subject, sender, recipients, text_body, html_body):
msg = Message(subject, sender=sender, recipients=recipients) msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body msg.body = text_body
msg.html = html_body msg.html = html_body
send_async_email(msg) send_async_email(current_app, msg)
#thr = threading.Thread(target = send_async_email, args = [msg])
#thr.start()
def follower_notification(followed, follower): def follower_notification(followed, follower):
@ -25,4 +26,3 @@ def follower_notification(followed, follower):
user=followed, follower=follower), user=followed, follower=follower),
render_template("follower_email.html", render_template("follower_email.html",
user=followed, follower=follower)) user=followed, follower=follower))

View File

@ -1,15 +1,17 @@
from flask.ext.wtf import Form from flask.ext.wtf import Form
from wtforms import TextField, BooleanField, TextAreaField
from wtforms.validators import Required, Length
from flask.ext.babel import gettext from flask.ext.babel import gettext
from app.models import User from wtforms import StringField, BooleanField, TextAreaField
from wtforms.validators import DataRequired, Length
from .models import User
class LoginForm(Form): class LoginForm(Form):
openid = TextField('openid', validators = [Required()]) openid = StringField('openid', validators=[DataRequired()])
remember_me = BooleanField('remember_me', default=False) remember_me = BooleanField('remember_me', default=False)
class EditForm(Form): class EditForm(Form):
nickname = TextField('nickname', validators = [Required()]) nickname = StringField('nickname', validators=[DataRequired()])
about_me = TextAreaField('about_me', validators=[Length(min=0, max=140)]) about_me = TextAreaField('about_me', validators=[Length(min=0, max=140)])
def __init__(self, original_nickname, *args, **kwargs): def __init__(self, original_nickname, *args, **kwargs):
@ -22,16 +24,22 @@ class EditForm(Form):
if self.nickname.data == self.original_nickname: if self.nickname.data == self.original_nickname:
return True return True
if self.nickname.data != User.make_valid_nickname(self.nickname.data): if self.nickname.data != User.make_valid_nickname(self.nickname.data):
self.nickname.errors.append(gettext('This nickname has invalid characters. Please use letters, numbers, dots and underscores only.')) self.nickname.errors.append(gettext(
'This nickname has invalid characters. '
'Please use letters, numbers, dots and underscores only.'))
return False return False
user = User.query.filter_by(nickname=self.nickname.data).first() user = User.query.filter_by(nickname=self.nickname.data).first()
if user != None: if user is not None:
self.nickname.errors.append(gettext('This nickname is already in use. Please choose another one.')) self.nickname.errors.append(gettext(
'This nickname is already in use. '
'Please choose another one.'))
return False return False
return True return True
class PostForm(Form): class PostForm(Form):
post = TextField('post', validators = [Required()]) post = StringField('post', validators=[DataRequired()])
class SearchForm(Form): class SearchForm(Form):
search = TextField('search', validators = [Required()]) search = StringField('search', validators=[DataRequired()])

View File

@ -1,22 +1,29 @@
from hashlib import md5 from hashlib import md5
import re
from app import db from app import db
from app import app from app import app
from config import WHOOSH_ENABLED from config import WHOOSH_ENABLED
import re
ROLE_USER = 0 import sys
ROLE_ADMIN = 1 if sys.version_info >= (3, 0):
enable_search = False
else:
enable_search = WHOOSH_ENABLED
if enable_search:
import flask.ext.whooshalchemy as whooshalchemy
followers = db.Table('followers',
followers = db.Table(
'followers',
db.Column('follower_id', db.Integer, db.ForeignKey('user.id')), db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
db.Column('followed_id', db.Integer, db.ForeignKey('user.id')) db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
) )
class User(db.Model): class User(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(64), unique = True) nickname = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True)
role = db.Column(db.SmallInteger, default = ROLE_USER)
posts = db.relationship('Post', backref='author', lazy='dynamic') posts = db.relationship('Post', backref='author', lazy='dynamic')
about_me = db.Column(db.String(140)) about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime) last_seen = db.Column(db.DateTime)
@ -33,12 +40,12 @@ class User(db.Model):
@staticmethod @staticmethod
def make_unique_nickname(nickname): def make_unique_nickname(nickname):
if User.query.filter_by(nickname = nickname).first() == None: if User.query.filter_by(nickname=nickname).first() is None:
return nickname return nickname
version = 2 version = 2
while True: while True:
new_nickname = nickname + str(version) new_nickname = nickname + str(version)
if User.query.filter_by(nickname = new_nickname).first() == None: if User.query.filter_by(nickname=new_nickname).first() is None:
break break
version += 1 version += 1
return new_nickname return new_nickname
@ -53,10 +60,14 @@ class User(db.Model):
return False return False
def get_id(self): def get_id(self):
return unicode(self.id) try:
return unicode(self.id) # python 2
except NameError:
return str(self.id) # python 3
def avatar(self, size): def avatar(self, size):
return 'http://www.gravatar.com/avatar/' + md5(self.email).hexdigest() + '?d=mm&s=' + str(size) return 'http://www.gravatar.com/avatar/%s?d=mm&s=%d' % \
(md5(self.email.encode('utf-8')).hexdigest(), size)
def follow(self, user): def follow(self, user):
if not self.is_following(user): if not self.is_following(user):
@ -69,14 +80,19 @@ class User(db.Model):
return self return self
def is_following(self, user): def is_following(self, user):
return self.followed.filter(followers.c.followed_id == user.id).count() > 0 return self.followed.filter(
followers.c.followed_id == user.id).count() > 0
def followed_posts(self): def followed_posts(self):
return Post.query.join(followers, (followers.c.followed_id == Post.user_id)).filter(followers.c.follower_id == self.id).order_by(Post.timestamp.desc()) return Post.query.join(
followers, (followers.c.followed_id == Post.user_id)).filter(
followers.c.follower_id == self.id).order_by(
Post.timestamp.desc())
def __repr__(self): # pragma: no cover def __repr__(self): # pragma: no cover
return '<User %r>' % (self.nickname) return '<User %r>' % (self.nickname)
class Post(db.Model): class Post(db.Model):
__searchable__ = ['body'] __searchable__ = ['body']
@ -89,6 +105,6 @@ class Post(db.Model):
def __repr__(self): # pragma: no cover def __repr__(self): # pragma: no cover
return '<Post %r>' % (self.body) return '<Post %r>' % (self.body)
if WHOOSH_ENABLED:
import flask.ext.whooshalchemy as whooshalchemy if enable_search:
whooshalchemy.whoosh_index(app, Post) whooshalchemy.whoosh_index(app, Post)

View File

@ -1,11 +1,14 @@
from jinja2 import Markup from jinja2 import Markup
class momentjs(object): class momentjs(object):
def __init__(self, timestamp): def __init__(self, timestamp):
self.timestamp = timestamp self.timestamp = timestamp
def render(self, format): def render(self, format):
return Markup("<script>\ndocument.write(moment(\"%s\").%s);\n</script>" % (self.timestamp.strftime("%Y-%m-%dT%H:%M:%S Z"), format)) return Markup(
"<script>\ndocument.write(moment(\"%s\").%s);\n</script>" %
(self.timestamp.strftime("%Y-%m-%dT%H:%M:%S Z"), format))
def format(self, fmt): def format(self, fmt):
return self.render("format(\"%s\")" % fmt) return self.render("format(\"%s\")" % fmt)
@ -15,4 +18,3 @@ class momentjs(object):
def fromNow(self): def fromNow(self):
return self.render("fromNow()") return self.render("fromNow()")

File diff suppressed because one or more lines are too long

View File

@ -53,7 +53,7 @@
{% endif %} {% endif %}
</ul> </ul>
<div class="nav-collapse collapse"> <div class="nav-collapse collapse">
{% if g.user.is_authenticated() and g.search_enabled %} {% if g.user.is_authenticated() %}
<form class="navbar-search pull-right" action="{{ url_for('search') }}" method="post" name="search">{{ g.search_form.hidden_tag() }}{{ g.search_form.search(size=20, placeholder=_('Search'), class="search-query")}}</form> <form class="navbar-search pull-right" action="{{ url_for('search') }}" method="post" name="search">{{ g.search_form.hidden_tag() }}{{ g.search_form.search(size=20, placeholder=_('Search'), class="search-query")}}</form>
{% endif %} {% endif %}
</div> </div>

View File

@ -7,20 +7,20 @@
<div class="well"> <div class="well">
<form class="form-horizontal" action="" method="post" name="edit"> <form class="form-horizontal" action="" method="post" name="edit">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
<div class="control-group{% if form.errors.post %} error{% endif %}"> <div class="control-group{% if form.nickname.errors %} error{% endif %}">
<label class="control-label" for="nickname">{{ _('Your nickname:') }}</label> <label class="control-label" for="nickname">{{ _('Your nickname:') }}</label>
<div class="controls"> <div class="controls">
{{ form.nickname(maxlength=64, class="span4") }} {{ form.nickname(maxlength=64, class="span4") }}
{% for error in form.errors.nickname %} {% for error in form.nickname.errors %}
<span class="help-inline">[{{ error }}]</span><br> <span class="help-inline">[{{ error }}]</span><br>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
<div class="control-group{% if form.errors.post %} error{% endif %}"> <div class="control-group{% if form.about_me.errors %} error{% endif %}">
<label class="control-label" for="about_me">{{ _('About yourself:') }}</label> <label class="control-label" for="about_me">{{ _('About yourself:') }}</label>
<div class="controls"> <div class="controls">
{{ form.about_me(cols=64, rows=4, class="span4") }} {{ form.about_me(cols=64, rows=4, class="span4") }}
{% for error in form.errors.about_me %} {% for error in form.about_me.errors %}
<span class="help-inline">[{{ error }}]</span><br> <span class="help-inline">[{{ error }}]</span><br>
{% endfor %} {% endfor %}
</div> </div>
@ -32,5 +32,4 @@
</div> </div>
</form> </form>
</div> </div>
</form>
{% endblock %} {% endblock %}

View File

@ -7,11 +7,11 @@
<div class="well"> <div class="well">
<form class="form-horizontal" action="" method="post" name="post"> <form class="form-horizontal" action="" method="post" name="post">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
<div class="control-group{% if form.errors.post %} error{% endif %}"> <div class="control-group{% if form.post.errors %} error{% endif %}">
<label class="control-label" for="post">{{ _('Say something:') }}</label> <label class="control-label" for="post">{{ _('Say something:') }}</label>
<div class="controls"> <div class="controls">
{{ form.post(size=30, maxlength=140) }} {{ form.post(size=30, maxlength=140) }}
{% for error in form.errors.post %} {% for error in form.post.errors %}
<span class="help-inline">[{{ error }}]</span><br> <span class="help-inline">[{{ error }}]</span><br>
{% endfor %} {% endfor %}
</div> </div>

View File

@ -26,11 +26,11 @@ function set_openid(openid, pr)
<a href="javascript:set_openid('{{ pr.url }}', '{{ pr.name }}');"><img src="/static/img/{{ pr.name.lower() }}.png" class="img-polaroid" style="margin:2px;" /></a> <a href="javascript:set_openid('{{ pr.url }}', '{{ pr.name }}');"><img src="/static/img/{{ pr.name.lower() }}.png" class="img-polaroid" style="margin:2px;" /></a>
{% endfor %} {% endfor %}
</div> </div>
<div class="control-group{% if form.errors.openid %} error{% endif %}"> <div class="control-group{% if form.openid.errors %} error{% endif %}">
<label class="control-label" for="openid">{{ _('Or enter your OpenID here:') }}</label> <label class="control-label" for="openid">{{ _('Or enter your OpenID here:') }}</label>
<div class="controls"> <div class="controls">
{{ form.openid(size=80, class="span4") }} {{ form.openid(size=80, class="span4") }}
{% for error in form.errors.openid %} {% for error in form.openid.errors %}
<span class="help-inline">[{{ error }}]</span><br> <span class="help-inline">[{{ error }}]</span><br>
{% endfor %} {% endfor %}
</div> </div>

View File

@ -1,15 +1,23 @@
import urllib, httplib try:
import httplib # Python 2
except ImportError:
import http.client as httplib # Python 3
try:
from urllib import urlencode # Python 2
except ImportError:
from urllib.parse import urlencode # Python 3
import json import json
from app import app from app import app
from flask.ext.babel import gettext from flask.ext.babel import gettext
from config import MS_TRANSLATOR_CLIENT_ID, MS_TRANSLATOR_CLIENT_SECRET from config import MS_TRANSLATOR_CLIENT_ID, MS_TRANSLATOR_CLIENT_SECRET
def microsoft_translate(text, sourceLang, destLang): def microsoft_translate(text, sourceLang, destLang):
if MS_TRANSLATOR_CLIENT_ID == "" or MS_TRANSLATOR_CLIENT_SECRET == "": if MS_TRANSLATOR_CLIENT_ID == "" or MS_TRANSLATOR_CLIENT_SECRET == "":
return gettext('Error: translation service not configured.') return gettext('Error: translation service not configured.')
try: try:
# get access token # get access token
params = urllib.urlencode({ params = urlencode({
'client_id': MS_TRANSLATOR_CLIENT_ID, 'client_id': MS_TRANSLATOR_CLIENT_ID,
'client_secret': MS_TRANSLATOR_CLIENT_SECRET, 'client_secret': MS_TRANSLATOR_CLIENT_SECRET,
'scope': 'http://api.microsofttranslator.com', 'scope': 'http://api.microsofttranslator.com',
@ -22,34 +30,35 @@ def microsoft_translate(text, sourceLang, destLang):
# translate # translate
conn = httplib.HTTPConnection('api.microsofttranslator.com') conn = httplib.HTTPConnection('api.microsofttranslator.com')
params = { params = {'appId': 'Bearer ' + token,
'appId': 'Bearer ' + token,
'from': sourceLang, 'from': sourceLang,
'to': destLang, 'to': destLang,
'text': text.encode("utf-8") 'text': text.encode("utf-8")}
} conn.request("GET",
conn.request("GET", '/V2/Ajax.svc/Translate?' + urllib.urlencode(params)) '/V2/Ajax.svc/Translate?' + urlencode(params))
response = json.loads("{\"response\":" + conn.getresponse().read().decode('utf-8-sig') + "}") response = json.loads('{"response":' +
conn.getresponse().read().decode('utf-8') + '}')
return response["response"] return response["response"]
except: except:
#return gettext('Error: Unexpected error.')
raise raise
def google_translate(text, sourceLang, destLang): def google_translate(text, sourceLang, destLang):
if not app.debug: if not app.debug:
return gettext('Error: translation service not available.') return gettext('Error: translation service not available.')
try: try:
params = urllib.urlencode({ params = urlencode({
'client': 't', 'client': 't',
'text': text.encode("utf-8"), 'text': text.encode("utf-8"),
'sl': sourceLang, 'sl': sourceLang,
'tl': destLang, 'tl': destLang,
'ie': 'UTF-8', 'ie': 'UTF-8',
'oe': 'UTF-8' 'oe': 'UTF-8'})
})
conn = httplib.HTTPSConnection("translate.google.com") conn = httplib.HTTPSConnection("translate.google.com")
conn.request("GET", "/translate_a/t?" + params, headers = { 'User-Agent': 'Mozilla/5.0' }) conn.request("GET", "/translate_a/t?" + params,
httpresponse = conn.getresponse().read().replace(",,,", ",\"\",\"\",").replace(",,", ",\"\",") headers={'User-Agent': 'Mozilla/5.0'})
httpresponse = conn.getresponse().read().replace(
",,,", ",\"\",\"\",").replace(",,", ",\"\",")
response = json.loads("{\"response\":" + httpresponse + "}") response = json.loads("{\"response\":" + httpresponse + "}")
return response["response"][0][0][0] return response["response"][0][0][0]
except: except:

Binary file not shown.

View File

@ -1,24 +1,30 @@
from flask import render_template, flash, redirect, session, url_for, request, g, jsonify from flask import render_template, flash, redirect, session, url_for, request, \
from flask.ext.login import login_user, logout_user, current_user, login_required g, jsonify
from flask.ext.login import login_user, logout_user, current_user, \
login_required
from flask.ext.sqlalchemy import get_debug_queries from flask.ext.sqlalchemy import get_debug_queries
from flask.ext.babel import gettext from flask.ext.babel import gettext
from app import app, db, lm, oid, babel
from forms import LoginForm, EditForm, PostForm, SearchForm
from models import User, ROLE_USER, ROLE_ADMIN, Post
from datetime import datetime from datetime import datetime
from emails import follower_notification
from guess_language import guessLanguage from guess_language import guessLanguage
from translate import microsoft_translate from app import app, db, lm, oid, babel
from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS, LANGUAGES, DATABASE_QUERY_TIMEOUT, WHOOSH_ENABLED from .forms import LoginForm, EditForm, PostForm, SearchForm
from .models import User, Post
from .emails import follower_notification
from .translate import microsoft_translate
from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS, LANGUAGES, \
DATABASE_QUERY_TIMEOUT
@lm.user_loader @lm.user_loader
def load_user(id): def load_user(id):
return User.query.get(int(id)) return User.query.get(int(id))
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
return request.accept_languages.best_match(LANGUAGES.keys()) return request.accept_languages.best_match(LANGUAGES.keys())
@app.before_request @app.before_request
def before_request(): def before_request():
g.user = current_user g.user = current_user
@ -28,24 +34,30 @@ def before_request():
db.session.commit() db.session.commit()
g.search_form = SearchForm() g.search_form = SearchForm()
g.locale = get_locale() g.locale = get_locale()
g.search_enabled = WHOOSH_ENABLED
@app.after_request @app.after_request
def after_request(response): def after_request(response):
for query in get_debug_queries(): for query in get_debug_queries():
if query.duration >= DATABASE_QUERY_TIMEOUT: if query.duration >= DATABASE_QUERY_TIMEOUT:
app.logger.warning("SLOW QUERY: %s\nParameters: %s\nDuration: %fs\nContext: %s\n" % (query.statement, query.parameters, query.duration, query.context)) app.logger.warning(
"SLOW QUERY: %s\nParameters: %s\nDuration: %fs\nContext: %s\n" %
(query.statement, query.parameters, query.duration,
query.context))
return response return response
@app.errorhandler(404) @app.errorhandler(404)
def internal_error(error): def not_found_error(error):
return render_template('404.html'), 404 return render_template('404.html'), 404
@app.errorhandler(500) @app.errorhandler(500)
def internal_error(error): def internal_error(error):
db.session.rollback() db.session.rollback()
return render_template('500.html'), 500 return render_template('500.html'), 500
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST']) @app.route('/index', methods=['GET', 'POST'])
@app.route('/index/<int:page>', methods=['GET', 'POST']) @app.route('/index/<int:page>', methods=['GET', 'POST'])
@ -56,10 +68,8 @@ def index(page = 1):
language = guessLanguage(form.post.data) language = guessLanguage(form.post.data)
if language == 'UNKNOWN' or len(language) > 5: if language == 'UNKNOWN' or len(language) > 5:
language = '' language = ''
post = Post(body = form.post.data, post = Post(body=form.post.data, timestamp=datetime.utcnow(),
timestamp = datetime.utcnow(), author=g.user, language=language)
author = g.user,
language = language)
db.session.add(post) db.session.add(post)
db.session.commit() db.session.commit()
flash(gettext('Your post is now live!')) flash(gettext('Your post is now live!'))
@ -70,6 +80,7 @@ def index(page = 1):
form=form, form=form,
posts=posts) posts=posts)
@app.route('/login', methods=['GET', 'POST']) @app.route('/login', methods=['GET', 'POST'])
@oid.loginhandler @oid.loginhandler
def login(): def login():
@ -84,6 +95,7 @@ def login():
form=form, form=form,
providers=app.config['OPENID_PROVIDERS']) providers=app.config['OPENID_PROVIDERS'])
@oid.after_login @oid.after_login
def after_login(resp): def after_login(resp):
if resp.email is None or resp.email == "": if resp.email is None or resp.email == "":
@ -96,7 +108,7 @@ def after_login(resp):
nickname = resp.email.split('@')[0] nickname = resp.email.split('@')[0]
nickname = User.make_valid_nickname(nickname) nickname = User.make_valid_nickname(nickname)
nickname = User.make_unique_nickname(nickname) nickname = User.make_unique_nickname(nickname)
user = User(nickname = nickname, email = resp.email, role = ROLE_USER) user = User(nickname=nickname, email=resp.email)
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
# make the user follow him/herself # make the user follow him/herself
@ -109,17 +121,19 @@ def after_login(resp):
login_user(user, remember=remember_me) login_user(user, remember=remember_me)
return redirect(request.args.get('next') or url_for('index')) return redirect(request.args.get('next') or url_for('index'))
@app.route('/logout') @app.route('/logout')
def logout(): def logout():
logout_user() logout_user()
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/user/<nickname>') @app.route('/user/<nickname>')
@app.route('/user/<nickname>/<int:page>') @app.route('/user/<nickname>/<int:page>')
@login_required @login_required
def user(nickname, page=1): def user(nickname, page=1):
user = User.query.filter_by(nickname=nickname).first() user = User.query.filter_by(nickname=nickname).first()
if user == None: if user is None:
flash(gettext('User %(nickname)s not found.', nickname=nickname)) flash(gettext('User %(nickname)s not found.', nickname=nickname))
return redirect(url_for('index')) return redirect(url_for('index'))
posts = user.posts.paginate(page, POSTS_PER_PAGE, False) posts = user.posts.paginate(page, POSTS_PER_PAGE, False)
@ -127,6 +141,7 @@ def user(nickname, page = 1):
user=user, user=user,
posts=posts) posts=posts)
@app.route('/edit', methods=['GET', 'POST']) @app.route('/edit', methods=['GET', 'POST'])
@login_required @login_required
def edit(): def edit():
@ -141,15 +156,15 @@ def edit():
elif request.method != "POST": elif request.method != "POST":
form.nickname.data = g.user.nickname form.nickname.data = g.user.nickname
form.about_me.data = g.user.about_me form.about_me.data = g.user.about_me
return render_template('edit.html', return render_template('edit.html', form=form)
form = form)
@app.route('/follow/<nickname>') @app.route('/follow/<nickname>')
@login_required @login_required
def follow(nickname): def follow(nickname):
user = User.query.filter_by(nickname=nickname).first() user = User.query.filter_by(nickname=nickname).first()
if user == None: if user is None:
flash('User ' + nickname + ' not found.') flash('User %s not found.' % nickname)
return redirect(url_for('index')) return redirect(url_for('index'))
if user == g.user: if user == g.user:
flash(gettext('You can\'t follow yourself!')) flash(gettext('You can\'t follow yourself!'))
@ -164,12 +179,13 @@ def follow(nickname):
follower_notification(user, g.user) follower_notification(user, g.user)
return redirect(url_for('user', nickname=nickname)) return redirect(url_for('user', nickname=nickname))
@app.route('/unfollow/<nickname>') @app.route('/unfollow/<nickname>')
@login_required @login_required
def unfollow(nickname): def unfollow(nickname):
user = User.query.filter_by(nickname=nickname).first() user = User.query.filter_by(nickname=nickname).first()
if user == None: if user is None:
flash('User ' + nickname + ' not found.') flash('User %s not found.' % nickname)
return redirect(url_for('index')) return redirect(url_for('index'))
if user == g.user: if user == g.user:
flash(gettext('You can\'t unfollow yourself!')) flash(gettext('You can\'t unfollow yourself!'))
@ -180,14 +196,16 @@ def unfollow(nickname):
return redirect(url_for('user', nickname=nickname)) return redirect(url_for('user', nickname=nickname))
db.session.add(u) db.session.add(u)
db.session.commit() db.session.commit()
flash(gettext('You have stopped following %(nickname)s.', nickname = nickname)) flash(gettext('You have stopped following %(nickname)s.',
nickname=nickname))
return redirect(url_for('user', nickname=nickname)) return redirect(url_for('user', nickname=nickname))
@app.route('/delete/<int:id>') @app.route('/delete/<int:id>')
@login_required @login_required
def delete(id): def delete(id):
post = Post.query.get(id) post = Post.query.get(id)
if post == None: if post is None:
flash('Post not found.') flash('Post not found.')
return redirect(url_for('index')) return redirect(url_for('index'))
if post.author.id != g.user.id: if post.author.id != g.user.id:
@ -198,6 +216,7 @@ def delete(id):
flash('Your post has been deleted.') flash('Your post has been deleted.')
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/search', methods=['POST']) @app.route('/search', methods=['POST'])
@login_required @login_required
def search(): def search():
@ -205,6 +224,7 @@ def search():
return redirect(url_for('index')) return redirect(url_for('index'))
return redirect(url_for('search_results', query=g.search_form.search.data)) return redirect(url_for('search_results', query=g.search_form.search.data))
@app.route('/search_results/<query>') @app.route('/search_results/<query>')
@login_required @login_required
def search_results(query): def search_results(query):
@ -213,6 +233,7 @@ def search_results(query):
query=query, query=query,
results=results) results=results)
@app.route('/translate', methods=['POST']) @app.route('/translate', methods=['POST'])
@login_required @login_required
def translate(): def translate():
@ -221,4 +242,3 @@ def translate():
request.form['text'], request.form['text'],
request.form['sourceLang'], request.form['sourceLang'],
request.form['destLang'])}) request.form['destLang'])})

View File

@ -13,7 +13,8 @@ OPENID_PROVIDERS = [
{'name': 'MyOpenID', 'url': 'https://www.myopenid.com'}] {'name': 'MyOpenID', 'url': 'https://www.myopenid.com'}]
if os.environ.get('DATABASE_URL') is None: if os.environ.get('DATABASE_URL') is None:
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db') + '?check_same_thread=False' SQLALCHEMY_DATABASE_URI = ('sqlite:///' + os.path.join(basedir, 'app.db') +
'?check_same_thread=False')
else: else:
SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL'] SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
@ -31,8 +32,8 @@ MAIL_SERVER = '' # your mailserver
MAIL_PORT = 25 MAIL_PORT = 25
MAIL_USE_TLS = False MAIL_USE_TLS = False
MAIL_USE_SSL = False MAIL_USE_SSL = False
MAIL_USERNAME = 'you' MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = 'your-password' MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
# available languages # available languages
LANGUAGES = { LANGUAGES = {

View File

@ -9,4 +9,5 @@ if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else: else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO)) api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO,
api.version(SQLALCHEMY_MIGRATE_REPO))

View File

@ -4,4 +4,5 @@ from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO from config import SQLALCHEMY_MIGRATE_REPO
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1) api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1)
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('Current database version: ' + str(v))

View File

@ -4,12 +4,16 @@ from migrate.versioning import api
from app import db from app import db
from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO from config import SQLALCHEMY_MIGRATE_REPO
migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
tmp_module = imp.new_module('old_model') tmp_module = imp.new_module('old_model')
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec old_model in tmp_module.__dict__ exec old_model in tmp_module.__dict__
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI,
SQLALCHEMY_MIGRATE_REPO,
tmp_module.meta, db.metadata)
open(migration, "wt").write(script) open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print 'New migration saved as ' + migration v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)) print('New migration saved as ' + migration)
print('Current database version: ' + str(v))

View File

@ -9,7 +9,6 @@ user = Table('user', post_meta,
Column('id', Integer, primary_key=True, nullable=False), Column('id', Integer, primary_key=True, nullable=False),
Column('nickname', String(length=64)), Column('nickname', String(length=64)),
Column('email', String(length=120)), Column('email', String(length=120)),
Column('role', SmallInteger, default=ColumnDefault(0)),
) )

View File

@ -9,7 +9,6 @@ user = Table('user', post_meta,
Column('id', Integer, primary_key=True, nullable=False), Column('id', Integer, primary_key=True, nullable=False),
Column('nickname', String(length=64)), Column('nickname', String(length=64)),
Column('email', String(length=120)), Column('email', String(length=120)),
Column('role', SmallInteger, default=ColumnDefault(0)),
Column('about_me', String(length=140)), Column('about_me', String(length=140)),
Column('last_seen', DateTime), Column('last_seen', DateTime),
) )

View File

@ -3,4 +3,5 @@ from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO from config import SQLALCHEMY_MIGRATE_REPO
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('Current database version: ' + str(v))

View File

@ -5,4 +5,3 @@ from app import app
app.config['PROFILE'] = True app.config['PROFILE'] = True
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
app.run(debug=True) app.run(debug=True)

View File

@ -1,28 +1,31 @@
Babel==0.9.6 Babel==1.3
Flask==0.9 Flask==0.10.1
Flask-Babel==0.8 Flask-Babel==0.9
Flask-Login==0.1.3 Flask-Login==0.2.11
Flask-Mail==0.8.2 Flask-Mail==0.9.0
Flask-OpenID==1.1.1 Flask-OpenID==1.2.1
Flask-SQLAlchemy==0.16 Flask-SQLAlchemy==2.0
Flask-WTF==0.8.3 Flask-WTF==0.10.2
git+git://github.com/miguelgrinberg/Flask-WhooshAlchemy Flask-WhooshAlchemy==0.56
Jinja2==2.6 Jinja2==2.7.3
MySQL-python==1.2.4 MarkupSafe==0.23
SQLAlchemy==0.7.9 SQLAlchemy==0.9.7
Tempita==0.5.1 Tempita==0.5.2
WTForms==1.0.3 WTForms==2.0.1
Werkzeug==0.8.3 Werkzeug==0.9.6
Whoosh==2.4.1 Whoosh==2.6.0
blinker==1.2 blinker==1.3
coverage==3.6 coverage==3.7.1
decorator==3.4.0 decorator==3.4.0
flup==1.0.3.dev-20110405 flipflop==1.0
guess-language==0.2 guess-language==0.2
gunicorn==0.17.2 gunicorn==19.1.1
psycopg2==2.5 itsdangerous==0.24
pbr==0.10.0
psycopg2==2.5.4
python-openid==2.2.5 python-openid==2.2.5
pytz==2013b pytz==2014.7
six==1.8.0
speaklater==1.3 speaklater==1.3
sqlalchemy-migrate==0.7.2 sqlalchemy-migrate==0.9.2
sqlparse==0.1.11

View File

@ -1,2 +0,0 @@
#!flask/bin/python
from app import app

View File

@ -1,9 +1,10 @@
#!flask/bin/python #!flask/bin/python
import os
# use mysql # use mysql
os.environ['DATABASE_URL'] = 'mysql://apps:apps@localhost/apps' os.environ['DATABASE_URL'] = 'mysql://apps:apps@localhost/apps'
from flup.server.fcgi import WSGIServer from flipflop import WSGIServer
from app import app from app import app
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,5 +1,5 @@
#!flask/bin/python #!flask/bin/python
from flup.server.fcgi import WSGIServer from flipflop import WSGIServer
from app import app from app import app
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,24 +0,0 @@
#!/usr/bin/python
import os, subprocess, sys
subprocess.call(['python', 'virtualenv.py', 'flask'])
if sys.platform == 'win32':
bin = 'Scripts'
else:
bin = 'bin'
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask<0.10'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask-login'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask-openid'])
if sys.platform == 'win32':
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', '--no-deps', 'lamson', 'chardet', 'flask-mail'])
else:
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask-mail'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'sqlalchemy==0.7.9'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask-sqlalchemy'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'sqlalchemy-migrate'])
#subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask-whooshalchemy'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'git+git://github.com/miguelgrinberg/Flask-WhooshAlchemy'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask-wtf'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flask-babel'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'guess-language'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'flup'])
subprocess.call([os.path.join('flask', bin, 'pip'), 'install', 'coverage'])

View File

@ -14,11 +14,13 @@ from app import app, db
from app.models import User, Post from app.models import User, Post
from app.translate import microsoft_translate from app.translate import microsoft_translate
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
def setUp(self): def setUp(self):
app.config['TESTING'] = True app.config['TESTING'] = True
app.config['CSRF_ENABLED'] = False app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
os.path.join(basedir, 'test.db')
db.create_all() db.create_all()
def tearDown(self): def tearDown(self):
@ -35,16 +37,17 @@ class TestCase(unittest.TestCase):
u = User(nickname='john', email='john@example.com') u = User(nickname='john', email='john@example.com')
db.session.add(u) db.session.add(u)
db.session.commit() db.session.commit()
assert u.is_authenticated() == True assert u.is_authenticated() is True
assert u.is_active() == True assert u.is_active() is True
assert u.is_anonymous() == False assert u.is_anonymous() is False
assert u.id == int(u.get_id()) assert u.id == int(u.get_id())
def test_avatar(self): def test_avatar(self):
# create a user # create a user
u = User(nickname='john', email='john@example.com') u = User(nickname='john', email='john@example.com')
avatar = u.avatar(128) avatar = u.avatar(128)
expected = 'http://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6' expected = 'http://www.gravatar.com/avatar/' + \
'd4c74594d841139328695756648b6bd6'
assert avatar[0:len(expected)] == expected assert avatar[0:len(expected)] == expected
def test_make_unique_nickname(self): def test_make_unique_nickname(self):
@ -70,21 +73,21 @@ class TestCase(unittest.TestCase):
db.session.add(u1) db.session.add(u1)
db.session.add(u2) db.session.add(u2)
db.session.commit() db.session.commit()
assert u1.unfollow(u2) == None assert u1.unfollow(u2) is None
u = u1.follow(u2) u = u1.follow(u2)
db.session.add(u) db.session.add(u)
db.session.commit() db.session.commit()
assert u1.follow(u2) == None assert u1.follow(u2) is None
assert u1.is_following(u2) assert u1.is_following(u2)
assert u1.followed.count() == 1 assert u1.followed.count() == 1
assert u1.followed.first().nickname == 'susan' assert u1.followed.first().nickname == 'susan'
assert u2.followers.count() == 1 assert u2.followers.count() == 1
assert u2.followers.first().nickname == 'john' assert u2.followers.first().nickname == 'john'
u = u1.unfollow(u2) u = u1.unfollow(u2)
assert u != None assert u is not None
db.session.add(u) db.session.add(u)
db.session.commit() db.session.commit()
assert u1.is_following(u2) == False assert not u1.is_following(u2)
assert u1.followed.count() == 0 assert u1.followed.count() == 0
assert u2.followers.count() == 0 assert u2.followers.count() == 0
@ -100,10 +103,14 @@ class TestCase(unittest.TestCase):
db.session.add(u4) db.session.add(u4)
# make four posts # make four posts
utcnow = datetime.utcnow() utcnow = datetime.utcnow()
p1 = Post(body = "post from john", author = u1, timestamp = utcnow + timedelta(seconds = 1)) p1 = Post(body="post from john", author=u1,
p2 = Post(body = "post from susan", author = u2, timestamp = utcnow + timedelta(seconds = 2)) timestamp=utcnow + timedelta(seconds=1))
p3 = Post(body = "post from mary", author = u3, timestamp = utcnow + timedelta(seconds = 3)) p2 = Post(body="post from susan", author=u2,
p4 = Post(body = "post from david", author = u4, timestamp = utcnow + timedelta(seconds = 4)) timestamp=utcnow + timedelta(seconds=2))
p3 = Post(body="post from mary", author=u3,
timestamp=utcnow + timedelta(seconds=3))
p4 = Post(body="post from david", author=u4,
timestamp=utcnow + timedelta(seconds=4))
db.session.add(p1) db.session.add(p1)
db.session.add(p2) db.session.add(p2)
db.session.add(p3) db.session.add(p3)
@ -160,6 +167,7 @@ class TestCase(unittest.TestCase):
assert microsoft_translate(u'English', 'en', 'es') == u'Inglés' assert microsoft_translate(u'English', 'en', 'es') == u'Inglés'
assert microsoft_translate(u'Español', 'es', 'en') == u'Spanish' assert microsoft_translate(u'Español', 'es', 'en') == u'Spanish'
if __name__ == '__main__': if __name__ == '__main__':
try: try:
unittest.main() unittest.main()

View File

@ -6,4 +6,3 @@ if sys.platform == 'win32':
else: else:
pybabel = 'flask/bin/pybabel' pybabel = 'flask/bin/pybabel'
os.system(pybabel + ' compile -d app/translations') os.system(pybabel + ' compile -d app/translations')

View File

@ -8,7 +8,8 @@ else:
if len(sys.argv) != 2: if len(sys.argv) != 2:
print "usage: tr_init <language-code>" print "usage: tr_init <language-code>"
sys.exit(1) sys.exit(1)
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot app') os.system(pybabel +
os.system(pybabel + ' init -i messages.pot -d app/translations -l ' + sys.argv[1]) ' extract -F babel.cfg -k lazy_gettext -o messages.pot app')
os.system(pybabel +
' init -i messages.pot -d app/translations -l ' + sys.argv[1])
os.unlink('messages.pot') os.unlink('messages.pot')

View File

@ -8,4 +8,3 @@ else:
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot app') os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot app')
os.system(pybabel + ' update -i messages.pot -d app/translations') os.system(pybabel + ' update -i messages.pot -d app/translations')
os.unlink('messages.pot') os.unlink('messages.pot')

File diff suppressed because it is too large Load Diff