def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] options = parse_vars(argv[2:]) setup_logging(config_uri) settings = get_appsettings(config_uri, name='testscaffold', options=options) engine = get_engine(settings) session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) with transaction.manager: user = User(user_name="admin", email='foo@localhost') user.set_password('admin') admin_object = Group(group_name='Administrators') group_permission = GroupPermission(perm_name='root_administration') dbsession.add(admin_object) admin_object.permissions.append(group_permission) admin_object.users.append(user) test_group = Group(group_name='Other group') dbsession.add(test_group) for x in range(1, 25): user = User(user_name="test{}".format(x), email='foo{}@localhost'.format(x)) user.set_password('test') test_group.users.append(user)
def register(self): """ Render register page with form Also handles oAuth flow for registration """ request = self.request login_url = request.route_url("ziggurat.routes.sign_in") login_form = UserLoginForm(request.POST, context={"request": request}) # some logic to handle came_from variable that we can use # to redirect user that tried to access forbidden resource if request.query_string: query_string = "?%s" % request.query_string else: query_string = "" referrer = "%s%s" % (request.path, query_string) for url in [login_url, "/register"]: if url in referrer: # never use the login form itself as came_from # or we may end up with a redirect loop referrer = "/" break registration_form = UserCreateForm(request.POST, context={"request": request}) # populate form from oAuth session data returned by velruse social_data = request.session.get("zigg.social_auth") if request.method != "POST" and social_data: log.info("social_auth", extra={"social_data": social_data}) form_data = { "email": social_data["user"].get("email"), "user_password": str(uuid.uuid4()), } # repopulate form this time from oauth data registration_form = UserCreateForm( context={"request": request}, **form_data ) if request.method == "POST" and registration_form.validate(): # insert new user here new_user = User() registration_form.populate_obj(new_user) new_user.regenerate_security_code() new_user.status = 1 new_user.set_password(new_user.password) new_user.registration_ip = request.environ.get("REMOTE_ADDR") new_user.persist(flush=True, db_session=request.dbsession) log.info( "register", extra={"new_user": new_user.user_name, "user_id": new_user.id}, ) # bind 3rd party identity if social_data: request.registry.notify(SocialAuthEvent(request, new_user, social_data)) title = _( "${project} :: Start information", mapping={"project": "testscaffold"} ) email_vars = {"user": new_user, "email_title": self.translate(title)} ev = EmailEvent( request, recipients=[new_user.email], tmpl_vars=email_vars, tmpl_loc="testscaffold:" "templates/emails/registered.jinja2", ) request.registry.notify(ev) request.session.flash( { "msg": self.translate(_("You have successfully registered.")), "level": "success", } ) headers = remember(request, new_user.id) return HTTPFound(location=request.route_url("/"), headers=headers) return {"registration_form": registration_form, "login_form": login_form}
def register(self): """ Render register page with form Also handles oAuth flow for registration """ request = self.request login_url = request.route_url('ziggurat.routes.sign_in') login_form = UserLoginForm(request.POST, context={'request': request}) # some logic to handle came_from variable that we can use # to redirect user that tried to access forbidden resource if request.query_string: query_string = '?%s' % request.query_string else: query_string = '' referrer = '%s%s' % (request.path, query_string) for url in [login_url, '/register']: if url in referrer: # never use the login form itself as came_from # or we may end up with a redirect loop referrer = '/' break registration_form = UserCreateForm(request.POST, context={'request': request}) # populate form from oAuth session data returned by velruse social_data = request.session.get('zigg.social_auth') if request.method != 'POST' and social_data: log.info('social_auth', extra={'social_data': social_data}) form_data = {'email': social_data['user'].get('email')} form_data['user_password'] = str(uuid.uuid4()) # repopulate form this time from oauth data registration_form = UserCreateForm(context={'request': request}, **form_data) if request.method == "POST" and registration_form.validate(): # insert new user here new_user = User() registration_form.populate_obj(new_user) new_user.persist(flush=True, db_session=request.dbsession) new_user.regenerate_security_code() new_user.status = 1 new_user.set_password(new_user.password) new_user.registration_ip = request.environ.get('REMOTE_ADDR') log.info('register', extra={'new_user': new_user.user_name}) # bind 3rd party identity if social_data: request.registry.notify( SocialAuthEvent(request, new_user, social_data)) title = _("${project} :: Start information", mapping={'project': 'testscaffold'}) email_vars = { 'user': new_user, 'email_title': self.translate(title) } ev = EmailEvent(request, recipients=[new_user.email], tmpl_vars=email_vars, tmpl_loc='testscaffold:' 'templates/emails/registered.jinja2') request.registry.notify(ev) request.session.flash({ 'msg': self.translate(_('You have successfully registered.')), 'level': 'success' }) headers = remember(request, new_user.id) return HTTPFound(location=request.route_url('/'), headers=headers) return { "registration_form": registration_form, 'login_form': login_form }