Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial deform support. fix a couple of bugs in simple registration

while I'm at it.
  • Loading branch information...
commit 93d1f9bb0df4cc6ff77d549ee92a0265bfd3a6fb 1 parent a32b4d8
@niallo niallo authored
View
31 pyramid_registration/mongodb.py
@@ -1,6 +1,7 @@
import bcrypt
import colander
import datetime
+import deform
import pymongo
import random
import string
@@ -16,16 +17,28 @@ def username_validator(node, kw):
db = kw.get('db')
username = kw.get('username')
- assert db
- regex_validator = colander.Regex(regex, msg=msg)
- regex_validator(node, username)
- if db.users.find_one({"username":username}):
- raise colander.Invalid(node, "Username %s is in use" % username)
+ if username:
+ assert db
+ regex_validator = colander.Regex(regex, msg=msg)
+ regex_validator(node, username)
+ if db.users.find_one({"username":username}):
+ raise colander.Invalid(node, "Username %s is in use" % username)
+
+@colander.deferred
+def email_validator(node, kw):
+ db = kw.get('db')
+ email = kw.get('email')
+ if email:
+ assert db
+ email_validator = colander.Email()
+ email_validator(node, email)
+ if db.users.find_one({"email":email}):
+ raise colander.Invalid(node, "Email %s is in use" % email)
class AddUserSchema(colander.MappingSchema):
username = colander.SchemaNode(colander.String(),
validator=username_validator, missing=None)
- email = colander.SchemaNode(colander.String(), validator=colander.Email(),
+ email = colander.SchemaNode(colander.String(), validator=email_validator,
missing=None)
password = colander.SchemaNode(colander.String(),
validator=colander.Length(min=6), missing=None)
@@ -170,17 +183,17 @@ def add_user(self, struct):
Since this method supports both simple auth (username/email and password
persisted locally) and federated auth (authentication details maintained
by remote provider such as Facebook or google) all parameters are
- option.
+ optional.
"""
schema = AddUserSchema().bind(db=self.db,
- username=struct.get("username"))
+ username=struct.get("username"), email=struct.get("email"))
# invalid exception will bubble up for caller to handle
d = schema.deserialize(struct)
new_user = {}
username = d["username"]
if not username:
- username = make_temp_username()
+ username = make_temp_username(self.db)
new_user["username"] = username
if d["password"]:
View
32 pyramid_registration/templates/simple_reg.mak
@@ -2,36 +2,6 @@
<head>
</head>
<body>
-%if created:
-Your account has been suggessfully created! You may now log in.
-%else:
-<h2>Simple Registration Form</h2>
-% if errors:
- % for e in errors:
- <ul>
- <li><b>${e}</b></li>
- </ul>
- % endfor
-% endif
-<form method="POST" action="/registration/simple">
-<table>
-<tr>
-<td>Username</td><td><input name="username" type="text" /></td>
-</tr>
-<tr>
-<td>Password</td><td><input name="password" type="password" /></td>
-</tr>
-<tr>
-<td>Password Confirm</td><td><input name="password-confirm" type="password" /></td>
-</tr>
-<tr>
-<td>Email</td><td><input name="email" type="text" /></td>
-</tr>
-<tr>
-<td><input name="submit" type="submit" value="Register" /></td>
-</tr>
-</table>
-</form>
-%endif
+${form|n}
</body>
</html>
View
49 pyramid_registration/views.py
@@ -1,10 +1,20 @@
import colander
+import deform
+from mongodb import AddUserSchema, email_validator
from pyramid.response import Response
+
+class SignupSchema(colander.MappingSchema):
+ email = colander.SchemaNode(colander.String(), validator=email_validator,
+ widget=deform.widget.TextInputWidget(size=20))
+ password = colander.SchemaNode(colander.String(),
+ validator=colander.Length(min=6),
+ widget=deform.widget.PasswordWidget(size=20))
+
""" Views """
def facebook_registration(backend, request):
""" /registration/facebook view callable.
-
+
If supplied with an access_token request parameter, assumes client has
already performed the FB login dance.
@@ -27,7 +37,9 @@ def simple_registration_get(backend, request):
""" /registration/simple view callable for GET method.
Renders registration template.
"""
- return {"errors":[], "created":False}
+ schema = SignupSchema()
+ form = deform.Form(schema, buttons=('create',))
+ return {"form":form.render()}
def simple_registration_post(backend, request):
""" /registration/simple view callable for POST method.
@@ -35,30 +47,17 @@ def simple_registration_post(backend, request):
Register a user with username and password.
"""
- # XXX move to deform
- username = request.params.get("username", "")
- password = request.params.get("password", "")
- password_confirm = request.params.get("password-confirm", "")
- email = request.params.get("email", "")
-
- errors = []
- if password != password_confirm:
- errors.append("Password and password confirm do not match")
- if not password or not password_confirm:
- errors.append("Must supply a password")
- if not email:
- errors.append("Must supply an email")
+ controls = request.POST.items()
try:
- backend.add_user({"username":username,"password":password,"email":email})
- except colander.Invalid, e:
- for i in e:
- if isinstance(i, unicode) or isinstance(i, str):
- errors.append(i)
- if not errors:
- return {"errors":errors, "created":True}
-
- return {"errors":errors, "created":False}
-
+ schema = SignupSchema()
+ form = deform.Form(schema, buttons=('submit',))
+ form.validate(controls)
+ schema.bind(db=request.db, email=request.POST.get("email"))
+ d = schema.deserialize(request.POST)
+ backend.add_user({"password":d["password"], "email":d["email"]})
+ except deform.ValidationFailure, e:
+ return {'form':e.render()}
+ return {'form':'OK'}
def simple_login_post(backend, request):
""" /login/simple view callable for POST method requests.
View
2  setup.py
@@ -7,7 +7,7 @@
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
requires = ['pyramid', 'WebError', 'sqlalchemy', 'pymongo', 'zope.sqlalchemy',
- 'zope.interface', 'colander', 'py-bcrypt']
+ 'zope.interface', 'colander', 'deform', 'py-bcrypt']
test_requires = requires + ["mock", "webtest"]
setup(name='pyramid_registration',
Please sign in to comment.
Something went wrong with that request. Please try again.