def fixture_add_user(username=u'chris', password=u'toast', active_user=True, wants_comment_notification=True): # Reuse existing user or create a new one test_user = User.query.filter_by(username=username).first() if test_user is None: test_user = User() test_user.username = username test_user.email = username + u'@example.com' if password is not None: test_user.pw_hash = gen_password_hash(password) if active_user: test_user.email_verified = True test_user.status = u'active' test_user.wants_comment_notification = wants_comment_notification test_user.save() # Reload test_user = User.query.filter_by(username=username).first() # ... and detach from session: Session.expunge(test_user) return test_user
def fixture_add_user(username=u'chris', password=u'toast', privileges=[], wants_comment_notification=True): # Reuse existing user or create a new one test_user = User.query.filter_by(username=username).first() if test_user is None: test_user = User() test_user.username = username test_user.email = username + u'@example.com' if password is not None: test_user.pw_hash = gen_password_hash(password) test_user.wants_comment_notification = wants_comment_notification for privilege in privileges: query = Privilege.query.filter(Privilege.privilege_name == privilege) if query.count(): test_user.all_privileges.append(query.one()) test_user.save() # Reload test_user = User.query.filter_by(username=username).first() # ... and detach from session: Session.expunge(test_user) return test_user
def register(request): """The registration view. Note that usernames will always be lowercased. Email domains are lowercased while the first part remains case-sensitive. """ # Redirects to indexpage if registrations are disabled if not mg_globals.app_config["allow_registration"]: messages.add_message( request, messages.WARNING, _('Sorry, registration is disabled on this instance.')) return redirect(request, "index") register_form = auth_forms.RegistrationForm(request.form) if request.method == 'POST' and register_form.validate(): # TODO: Make sure the user doesn't exist already 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 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) # redirect the user to their homepage... there will be a # message waiting for them to verify their email return redirect( request, 'mediagoblin.user_pages.user_home', user=user.username) return render_to_response( request, 'mediagoblin/auth/register.html', {'register_form': register_form})
def fixture_add_user(username=u'chris', password=u'toast', privileges=[], wants_comment_notification=True): # Reuse existing user or create a new one test_user = User.query.filter_by(username=username).first() if test_user is None: test_user = User() test_user.username = username test_user.email = username + u'@example.com' if password is not None: test_user.pw_hash = gen_password_hash(password) test_user.wants_comment_notification = wants_comment_notification for privilege in privileges: query = Privilege.query.filter(Privilege.privilege_name==privilege) if query.count(): test_user.all_privileges.append(query.one()) test_user.save() # Reload test_user = User.query.filter_by(username=username).first() # ... and detach from session: Session.expunge(test_user) return test_user
def create_basic_user(form): user = User() user.username = form.username.data user.email = form.email.data user.save() return user