def create_activation(request, user): db = get_session(request) Activation = request.registry.getUtility(IActivationClass) activation = Activation() db.add(activation) user.activation = activation db.flush() # TODO Create a hook for the app to give us body and subject! # TODO We don't need pystache just for this! body = pystache.render( _("Please validate your email and activate your account by visiting:\n" "{{ link }}"), { 'link': request.route_url('activate', user_id=user.id, code=user.activation.code) } ) subject = _("Please activate your account!") message = Message(subject=subject, recipients=[user.email], body=body) mailer = get_mailer(request) mailer.send(message)
def get_by_email(cls, request, email): """Fetch a user by email address.""" session = get_session(request) return session.query(cls).filter( sa.func.lower(cls.email) == email.lower() ).first()
def get_by_username_or_email(cls, request, username, email): session = get_session(request) return session.query(cls).filter( or_( func.lower(cls.username) == username.lower(), cls.email == email)).first()
def unique_username(node, value): '''Colander validator that ensures the username does not exist.''' req = node.bindings['request'] User = req.registry.getUtility(IUserClass) if get_session(req).query(User).filter(User.username == value).count(): Str = req.registry.getUtility(IUIStrings) raise c.Invalid(node, Str.registration_username_exists)
def create_activation(request, user_account, route_name='horus_activate', template=None, subject=None, send_activation_mail=True): if not template: template = 'horus:templates/mail/activation.mako' subject = subject and subject or u"Itemfire: " + request.translate(u'Please activate your e-mail address!') db = get_session(request) Activation = request.registry.getUtility(IHorusActivationClass) activation = Activation() db.add(activation) user_account.activation = activation db.flush() if send_activation_mail: tpldata = { 'recipient_name': user_account.email, 'link': request.route_url(route_name, user_account_id=user_account.id, code=user_account.activation.code), } body = render(template, tpldata, request) send_mail(request, subject=subject, recipients=[user_account.email], html=body)
def get_by_activation(cls, request, activation): session = get_session(request) user = session.query(cls).filter( cls.activation_id == activation.id_value).first() return user
def get_by_security_code(cls, request, security_code): session = get_session(request) user = session.query(cls).filter( cls.security_code == security_code).first() return user
def __init__(self, request): self._request = request self.settings = request.registry.settings self.User = request.registry.getUtility(IHorusUserClass) self.UserAccount = request.registry.getUtility(IHorusUserAccountClass) self.Activation = request.registry.getUtility(IHorusActivationClass) self.db = get_session(request)
def get_by_username(cls, request, username): session = get_session(request) lhs = func.replace(cls.username, '.', '') rhs = username.replace('.', '') return session.query(cls).filter( func.lower(lhs) == rhs.lower()).first()
def get_by_username_or_email(cls, request, username, email): session = get_session(request) lhs = func.replace(cls.username, '.', '') rhs = username.replace('.', '') return session.query(cls).filter( or_(func.lower(lhs) == rhs.lower(), cls.email == email)).first()
def create_activation(request, user): """Create through-the-web user sign up with his/her email. We don't want to force the users to pick up an usernames, so we just generate an username. The user is free to change their username later. """ assert user.id user.username = user.generate_username() user.registration_source = "email" db = get_session(request) Activation = request.registry.getUtility(IActivationClass) activation = Activation() db.add(activation) user.activation = activation db.flush() # TODO Create a hook for this, don't assume create_activation() call # TODO We don't need pystache just for this! context = { 'link': request.route_url('activate', user_id=uuid_to_slug(user.uuid), code=user.activation.code) } send_templated_mail(request, [user.email], "login/email/activate", context) site_creator = get_site_creator(request.registry) site_creator.check_empty_site_init(request.dbsession, user)
def get_templates_for_uri_and_type(cls, request, uri, ttype): session = get_session(request) return session.query(cls).filter( and_( func.lower(cls.uri) == func.lower(uri), func.lower(cls.type) == func.lower(ttype) ) ).all()
def get_active_subscriptions_for_a_type(cls, request, ttype): session = get_session(request) return session.query(cls).filter( and_( cls.active, func.lower(cls.type) == func.lower(ttype) ) ).all()
def unique_username(node, value): '''Colander validator that ensures the username does not exist.''' req = node.bindings['request'] user_ctor = req.registry.getUtility(interfaces.IUserClass) query = get_session(req).query(user_ctor) if query.filter(user_ctor.username.ilike(value)).count(): str_ = req.registry.getUtility(interfaces.IUIStrings) raise colander.Invalid(node, str_.registration_username_exists)
def __init__(self, request): self.request = request self.session = get_session(request) self.Entry = request.registry.getUtility(IHieroEntryClass) self.Category = request.registry.getUtility(IHieroCategoryClass) self.EntryTag = request.registry.getUtility(IHieroEntryTagClass) self.Tag = request.registry.getUtility(IHieroTagClass) self.Series = request.registry.getUtility(IHieroSeriesClass)
def __init__(self, request): self._request = request self.settings = request.registry.settings getUtility = request.registry.getUtility self.User = getUtility(IUserClass) self.Activation = getUtility(IActivationClass) self.Str = getUtility(IUIStrings) self.db = get_session(request)
def get_by_security_code(cls, request, security_code): session = get_session(request) user = session.query(cls).filter( cls.security_code == security_code ).first() return user
def get_by_activation(cls, request, activation): session = get_session(request) user = session.query(cls).filter( cls.activation_id == activation.id_value ).first() return user
def unique_email(node, val): '''Colander validator that ensures the email does not exist.''' req = node.bindings['request'] User = req.registry.getUtility(IUserClass) other = get_session(req).query(User).filter(User.email.ilike(val)).first() if other: S = req.registry.getUtility(IUIStrings) raise c.Invalid(node, S.registration_email_exists.format(other.email))
def get_by_username(cls, request, username): session = get_session(request) lhs = func.replace(cls.username, '.', '') rhs = username.replace('.', '') return session.query(cls).filter( func.lower(lhs) == rhs.lower() ).first()
def email_exists(node, val): '''Colander validator that ensures a User exists with the email.''' req = node.bindings['request'] User = req.registry.getUtility(IUserClass) exists = get_session(req).query(User).filter(User.email.ilike(val)).count() if not exists: Str = req.registry.getUtility(IUIStrings) raise c.Invalid(node, Str.reset_password_email_must_exist.format(val))
def get_by_email(cls, request, email): session = get_session(request) return session.query(cls).filter( and_( func.lower(cls.email) == email.lower(), cls.provider == 'local', ) ).first()
def get_by_username_or_email(cls, request, username, email): session = get_session(request) return session.query(cls).filter( or_( func.lower(cls.username) == username.lower(), cls.email == email ) ).first()
def get_by_openid(cls, request, openid, provider): session = get_session(request) return session.query(cls).filter( and_( cls.openid == str(openid), cls.provider == provider, ) ).first()
def get_by_username(cls, request, username): session = get_session(request) return session.query(cls).filter( and_( func.lower(cls.username) == username.lower(), cls.provider == 'local', ) ).first()
def get_by_activation(cls, request, activation): """Fetch a user by activation instance.""" session = get_session(request) user = session.query(cls).filter( cls.activation_id == activation.id_value ).first() return user
def create_subscription(request, uri, active): session = get_session(request) subs = Subscriptions( uri=uri, type=types.REPLY_TYPE, active=active ) session.add(subs) session.flush()
def get_by_username_or_email(cls, request, username, email): session = get_session(request) uid = _username_to_uid(username) return session.query(cls).filter( or_( cls.uid == uid, cls.email == email ) ).first()
def get_by_username_or_email(cls, request, username, email): session = get_session(request) lhs = func.replace(cls.username, '.', '') rhs = username.replace('.', '') return session.query(cls).filter( or_( func.lower(lhs) == rhs.lower(), cls.email == email ) ).first()
def __init__(self, *args, **kwargs): super(Activation, self).__init__(*args, **kwargs) # XXX: Horus currently has a bug where the Activation model isn't # flushed before the email is generated, causing the link to be # broken (hypothesis/h#1156). # # Fixed in horus@90f838cef12be249a9e9deb5f38b37151649e801 request = get_current_request() db = get_session(request) db.add(self) db.flush()
def get_by_id(cls, request, userid): """ Fetch a user by integer id or by full `userid`. If `userid` is a string of the form "acct:[email protected]" and "domain.tld" is the app's current domain, then fetch the user with username "name". Otherwise, lookup the user with integer primary key `userid`. """ match = re.match(r'acct:([^@]+)@{}'.format(request.domain), userid) if match: return cls.get_by_username(request, match.group(1)) session = get_session(request) return session.query(cls).filter(cls.id == userid).first()
def get_all(cls, request, page=None, limit=None): session = get_session(request) query = session.query(cls) query = query.order_by(cls.published_on.desc()) if limit: query = query.limit(limit) if page and limit: offset = (page - 1) * limit query = query.offset(offset) return query
def get_all(cls, request, page=None, limit=None): """Gets all records of the specific item with option page and limits. """ session = get_session(request) query = session.query(cls) if limit: query = query.limit(limit) if page and limit: offset = (page - 1) * limit query = query.offset(offset) return query
def unsubscribe(request): token = request.matchdict['token'] payload = request.registry.notification_serializer.loads(token) subscriptions = Subscriptions.get_templates_for_uri_and_type( request, payload['uri'], payload['type'], ) db = get_session(request) for s in subscriptions: if s.active: s.active = False db.add(s) return {}
def test_get_session(self): from hem.interfaces import IDBSession from hem.db import get_session request = testing.DummyRequest() request.registry = Mock() session = Mock() getUtility = Mock() getUtility.return_value = session request.registry.getUtility = getUtility new_session = get_session(request) getUtility.assert_called_with(IDBSession) assert new_session == session
def tag_widget(node, kw): choices = [] request = kw.get('request') session = get_session(request) Tag = request.registry.getUtility(IHieroTagClass) tags = session.query(Tag).all() for tag in tags: choices.append((str(tag.id), tag.title)) widget = deform.widget.SelectWidget( values=choices, template='hiero:templates/widgets/select_tags', multiple=True) widget.request = request return widget
def create_activation(request, user): db = get_session(request) Activation = request.registry.getUtility(IActivationClass) activation = Activation() db.add(activation) user.activation = activation db.flush() # TODO Create a hook for the app to give us body and subject! # TODO We don't need pystache just for this! body = pystache.render( _("Please validate your email and activate your account by visiting:\n" "{{ link }}"), { 'link': request.route_url( 'activate', user_id=user.id, code=user.activation.code) }) subject = _("Please activate your account!") message = Message(subject=subject, recipients=[user.email], body=body) mailer = get_mailer(request) mailer.send(message)
def get_by_code(cls, request, code): session = get_session(request) return session.query(cls).filter(cls.code == code).first()
def get_by_key(cls, request, key): return get_session(request).query(cls).filter(cls.key == key).first()
def get_by_username(cls, request, username): session = get_session(request) uid = _username_to_uid(username) return session.query(cls).filter(cls.uid == uid).first()
def get_by_slug(cls, request, slug): """Gets an entry by its slug """ session = get_session(request) return session.query(cls).filter(cls.slug == slug)
def get_by_slug(cls, request, slug): """Gets all active entries""" session = get_session(request) return session.query(cls).filter(cls.slug == slug)
def get_by_title(cls, request, title): """Gets a tag by its title """ session = get_session(request) return session.query(cls).filter(cls.title == title)
def get_by_email(cls, request, email): session = get_session(request) return session.query(cls).filter( func.lower(cls.email) == email.lower()).first()
def get_by_pk(cls, request, pk): """Gets an object by its primary key""" session = get_session(request) return session.query(cls).filter(cls.pk == pk).first()
def get_by_username(cls, request, username): session = get_session(request) return session.query(cls).filter( func.lower(cls.username) == username.lower()).first()
def get_by_id(cls, request, id): """Gets an object by its primary key.""" session = get_session(request) pk = getattr(cls, cls._idAttribute) return session.query(cls).filter(pk == id).first()