Permalink
Browse files

Update to new flask extension structure and improve PEP8 compatibility

  • Loading branch information...
1 parent 836bd68 commit 9141ee716c3e138a49f432068c070b08ab249f9c @techniq techniq committed Jun 14, 2012
View
@@ -5,7 +5,7 @@
A microblog example application written as Flask tutorial with
Flask using flask-sqlalchemy and flask-wtf extensions.
-
+
Adapted from original (c) Armin Ronacher.
:copyright: (c) 2010 by Dan Jacob.
@@ -16,9 +16,9 @@
from contextlib import closing
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
-from flaskext.wtf import Form, TextField, TextAreaField, \
+from flask.ext.wtf import Form, TextField, TextAreaField, \
PasswordField, SubmitField, Required, ValidationError
-from flaskext.sqlalchemy import SQLAlchemy
+from flask.ext.sqlalchemy import SQLAlchemy
# configuration
DEBUG = True
@@ -35,28 +35,31 @@
db = SQLAlchemy(app)
+
class Entry(db.Model):
-
+
__tablename__ = "entries"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(200))
text = db.Column(db.UnicodeText)
-
+
db.create_all()
+
class EntryForm(Form):
title = TextField("Title", validators=[Required()])
text = TextAreaField("Text")
submit = SubmitField("Share")
+
class LoginForm(Form):
username = TextField("Username")
password = PasswordField("Password")
submit = SubmitField("Login")
-
+
def validate_username(self, field):
if field.data != USERNAME:
raise ValidationError, "Invalid username"
@@ -65,6 +68,7 @@ def validate_password(self, field):
if field.data != PASSWORD:
raise ValidationError, "Invalid password"
+
@app.route('/')
def show_entries():
entries = Entry.query.order_by(Entry.id.desc())
@@ -1,5 +1,5 @@
from flask import Flask, render_template, flash, session, redirect, url_for
-from flaskext.wtf import Form, TextAreaField, RecaptchaField, Required
+from flask.ext.wtf import Form, TextAreaField, RecaptchaField, Required
DEBUG = True
SECRET_KEY = 'secret'
View
@@ -1,18 +1,17 @@
-from flask import Flask, render_template, request
+from flask import Flask, render_template
+from flask.ext.wtf import Form, FileField, FieldList
-from flaskext.wtf import Form, FileField, FieldList, required
class FileUploadForm(Form):
-
uploads = FieldList(FileField())
-
DEBUG = True
SECRET_KEY = 'secret'
app = Flask(__name__)
app.config.from_object(__name__)
+
@app.route("/", methods=("GET", "POST",))
def index():
@@ -26,8 +25,8 @@ def index():
for upload in form.uploads.entries:
filedata.append(upload)
- return render_template("index.html",
- form=form,
+ return render_template("index.html",
+ form=form,
filedata=filedata)
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
- flaskext.wtf
+ flask.ext.wtf
~~~~~~~~~~~~
Flask-WTF extension
@@ -21,20 +21,20 @@
from wtforms.widgets import *
from wtforms import ValidationError
-from flaskext.wtf import html5
-from flaskext.wtf.form import Form
-from flaskext.wtf import recaptcha
+from flask.ext.wtf import html5
+from flask.ext.wtf.form import Form
+from flask.ext.wtf import recaptcha
-from flaskext.wtf.recaptcha.fields import RecaptchaField
-from flaskext.wtf.recaptcha.widgets import RecaptchaWidget
-from flaskext.wtf.recaptcha.validators import Recaptcha
+from flask.ext.wtf.recaptcha.fields import RecaptchaField
+from flask.ext.wtf.recaptcha.widgets import RecaptchaWidget
+from flask.ext.wtf.recaptcha.validators import Recaptcha
fields.RecaptchaField = RecaptchaField
widgets.RecaptchaWidget = RecaptchaWidget
validators.Recaptcha = Recaptcha
-from flaskext.wtf.file import FileField
-from flaskext.wtf.file import FileAllowed, FileRequired, file_allowed, \
+from flask.ext.wtf.file import FileField
+from flask.ext.wtf.file import FileAllowed, FileRequired, file_allowed, \
file_required
fields.FileField = FileField
@@ -51,7 +51,7 @@ class FileAllowed(object):
Validates that the uploaded file is allowed by the given
Flask-Uploads UploadSet.
- `upload_set` : instance of **flaskext.uploads.UploadSet**
+ `upload_set` : instance of **flask.ext.uploads.UploadSet**
`message` : error message
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,5 @@
+from flask.ext.wtf.recaptcha import fields
+from flask.ext.wtf.recaptcha import validators
+from flask.ext.wtf.recaptcha import widgets
+
+__all__ = fields.__all__ + validators.__all__ + widgets.__all__
@@ -1,7 +1,7 @@
from wtforms.fields import Field
-from flaskext.wtf.recaptcha import widgets
-from flaskext.wtf.recaptcha.validators import Recaptcha
+from flask.ext.wtf.recaptcha import widgets
+from flask.ext.wtf.recaptcha.validators import Recaptcha
__all__ = ["RecaptchaField"]
@@ -10,6 +10,7 @@
__all__ = ["Recaptcha"]
+
class Recaptcha(object):
"""Validates a ReCaptcha."""
_error_codes = {
@@ -38,7 +39,7 @@ def __call__(self, form, field):
def _validate_recaptcha(self, challenge, response, remote_addr):
"""Performs the actual validation."""
-
+
if current_app.testing:
return True
@@ -54,7 +55,6 @@ def _validate_recaptcha(self, challenge, response, remote_addr):
'response': response
})
-
response = urllib2.urlopen(RECAPTCHA_VERIFY_SERVER, data)
if response.code != 200:
@@ -30,6 +30,7 @@
__all__ = ["RecaptchaWidget"]
+
class RecaptchaWidget(object):
def recaptcha_html(self, server, query, options):
@@ -38,18 +39,18 @@ def recaptcha_html(self, server, query, options):
frame_url='%snoscript?%s' % (server, query),
options=json.dumps(options)
)
-
+
def __call__(self, field, error=None, **kwargs):
"""Returns the recaptcha input HTML."""
-
+
if current_app.config.get('RECAPTCHA_USE_SSL', False):
server = RECAPTCHA_SSL_API_SERVER
else:
server = RECAPTCHA_API_SERVER
-
+
try:
public_key = current_app.config['RECAPTCHA_PUBLIC_KEY']
except KeyError:
View
@@ -1 +0,0 @@
-__import__('pkg_resources').declare_namespace(__name__)
@@ -1,5 +0,0 @@
-from flaskext.wtf.recaptcha import fields
-from flaskext.wtf.recaptcha import validators
-from flaskext.wtf.recaptcha import widgets
-
-__all__ = fields.__all__ + validators.__all__ + widgets.__all__
View
@@ -10,7 +10,7 @@
* `documentation <http://packages.python.org/Flask-WTF>`_
* `development version
- <http://bitbucket.org/danjac/flask-wtf/get/tip.gz#egg=Flask-WTF>`_
+ <http://github.com/rduplain/flask-wtf>`_
"""
@@ -19,7 +19,7 @@
setup(
name='Flask-WTF',
- version='0.6',
+ version='0.7',
url='http://github.com/rduplain/flask-wtf',
license='BSD',
author='Dan Jacob',
@@ -28,10 +28,10 @@
maintainer_email='ron.duplain@gmail.com',
description='Simple integration of Flask and WTForms',
long_description=__doc__,
- packages=['flaskext',
- 'flaskext.wtf',
- 'flaskext.wtf.recaptcha'],
- namespace_packages=['flaskext'],
+ packages=[
+ 'flask_wtf',
+ 'flask_wtf.recaptcha'
+ ],
test_suite='nose.collector',
zip_safe=False,
platforms='any',
View
@@ -1,4 +1,4 @@
# This tests whether __all__ namespace of WTForms is available.
-# Fails as AttributeError immediately if an import is missing in flaskext.wtf.
-from flaskext.wtf import *
+# Fails as AttributeError immediately if an import is missing in flask.ext.wtf.
+from flask.ext.wtf import *
View
@@ -1,14 +1,16 @@
from __future__ import with_statement
from flask import Flask, render_template, jsonify
-from flaskext.testing import TestCase as _TestCase
-from flaskext.wtf import Form, TextField, HiddenField, SubmitField, Required
+from flask.ext.testing import TestCase as _TestCase
+from flask.ext.wtf import Form, TextField, HiddenField, SubmitField, Required
+
class MyForm(Form):
SECRET_KEY = "a poorly kept secret."
name = TextField("Name", validators=[Required()])
submit = SubmitField("Submit")
+
class HiddenFieldsForm(Form):
SECRET_KEY = "a poorly kept secret."
name = HiddenField()
@@ -21,26 +23,28 @@ def __init__(self, *args, **kwargs):
super(HiddenFieldsForm, self).__init__(*args, **kwargs)
self.method.name = '_method'
+
class SimpleForm(Form):
SECRET_KEY = "a poorly kept secret."
pass
+
class TestCase(_TestCase):
-
+
def create_app(self):
app = Flask(__name__)
app.secret_key = "secret"
-
+
@app.route("/", methods=("GET", "POST"))
def index():
-
+
form = MyForm()
if form.validate_on_submit():
name = form.name.data.upper()
else:
name = ''
-
- return render_template("index.html",
+
+ return render_template("index.html",
form=form,
name=name)
@@ -77,9 +81,8 @@ def ajax_submit():
success=True,
errors=None)
- return jsonify(name=None,
+ return jsonify(name=None,
errors=form.errors,
success=False)
-
return app
View
@@ -1,5 +1,6 @@
from base import TestCase
-from flaskext.wtf import html5
+from flask.ext.wtf import html5
+
class DummyField(object):
def __init__(self, data, name='f', label='', id='', type='TextField'):
@@ -16,7 +17,6 @@ def __init__(self, data, name='f', label='', id='', type='TextField'):
iter_choices = lambda x: iter(x.data)
-
class HTML5Tests(TestCase):
field = DummyField("name", id="name", name="name")
Oops, something went wrong.

0 comments on commit 9141ee7

Please sign in to comment.