aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/auth/tools.py
diff options
context:
space:
mode:
authorRodney Ewing <ewing.rj@gmail.com>2013-05-24 12:52:14 -0700
committerElrond <elrond+mediagoblin.org@samba-tng.org>2013-05-28 01:22:19 +0200
commitf9e032212dff4d54de644cb5537bc0bef6d24c7f (patch)
tree2015f4511bf1ac68ce1928baa745b687fbb47c16 /mediagoblin/auth/tools.py
parent75fc93686d0763ced6a5769e99e570a4c8fd3273 (diff)
downloadmediagoblin-f9e032212dff4d54de644cb5537bc0bef6d24c7f.tar.lz
mediagoblin-f9e032212dff4d54de644cb5537bc0bef6d24c7f.tar.xz
mediagoblin-f9e032212dff4d54de644cb5537bc0bef6d24c7f.zip
added a register_user function
cherry picked from rodney757 and fixed for out of order picking.
Diffstat (limited to 'mediagoblin/auth/tools.py')
-rw-r--r--mediagoblin/auth/tools.py51
1 files changed, 50 insertions, 1 deletions
diff --git a/mediagoblin/auth/tools.py b/mediagoblin/auth/tools.py
index bb7d2683..db6b6e37 100644
--- a/mediagoblin/auth/tools.py
+++ b/mediagoblin/auth/tools.py
@@ -14,6 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import uuid
import logging
import wtforms
@@ -22,7 +23,8 @@ from sqlalchemy import or_
from mediagoblin import mg_globals
from mediagoblin.auth import lib as auth_lib
from mediagoblin.db.models import User
-from mediagoblin.tools.mail import normalize_email, send_email
+from mediagoblin.tools.mail import (normalize_email, send_email,
+ email_debug_message)
from mediagoblin.tools.template import render_template
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
@@ -94,6 +96,53 @@ def send_verification_email(user, request):
rendered_email)
+def basic_extra_validation(register_form, *args):
+ users_with_username = User.query.filter_by(
+ username=register_form.data['username']).count()
+ users_with_email = User.query.filter_by(
+ email=register_form.data['email']).count()
+
+ extra_validation_passes = True
+
+ if users_with_username:
+ register_form.username.errors.append(
+ _(u'Sorry, a user with that name already exists.'))
+ extra_validation_passes = False
+ if users_with_email:
+ register_form.email.errors.append(
+ _(u'Sorry, a user with that email address already exists.'))
+ extra_validation_passes = False
+
+ return extra_validation_passes
+
+
+def register_user(request, register_form):
+ """ Handle user registration """
+ extra_validation_passes = basic_extra_validation(register_form)
+
+ if extra_validation_passes:
+ # Create the user
+ user = User()
+ user.username = register_form.data['username']
+ user.email = register_form.data['email']
+ user.pw_hash = auth_lib.bcrypt_gen_password_hash(
+ register_form.password.data)
+ user.verification_key = unicode(uuid.uuid4())
+ user.save()
+
+ # log the user in
+ request.session['user_id'] = unicode(user.id)
+ request.session.save()
+
+ # send verification email
+ email_debug_message(request)
+ send_verification_email(user, request)
+
+ return user
+
+ return None
+
+
def check_login_simple(username, password, username_might_be_email=False):
search = (User.username == username)
if username_might_be_email and ('@' in username):