def create_user_meta(self, user, **kwargs): if isinstance(user, (int, long)): user = User.query.get(user) for (k, v) in kwargs.items(): um = UserMeta() um.key = k um.value = v um.user_id = user.id self.commit_model(um)
def create(username, password, name=None, **kwargs): """Create and return a new instance of :class:`pooldlib.postgresql.models.User`. Any unspecified kwargs will be assumed to be metadata to be associated with the new user. All errors are bubbled up. :param username: The username of the new user. :type username: string :param password: The password for the new user. :type password: string :param name: The full name of the new user (optional). :type password: string :param kwargs: Metadata to associate with the new user. :type kwargs: kwarg dictionary :raises: :class:`pooldlib.exceptions.InvalidPasswordError` :class:`pooldlib.exceptions.UsernameUnavailableError` :class:`pooldlib.exceptions.EmailUnavailableError` :returns: :class:`pooldlib.postgresql.models.User` """ validate_password(password, exception_on_invalid=True) if 'email' in kwargs: if email_exists(kwargs['email']): msg = 'The email address %s is already assigned to another user.' msg %= kwargs['email'] raise EmailUnavailableError(msg) # Only store lower-case emails in the system kwargs['email'] = kwargs['email'].lower() u = UserModel() u.username = username u.password = password if name: u.name = name with transaction_session() as session: try: session.add(u) session.commit() except SQLAlchemyIntegrityError: msg = "Username %s already in use." % username raise UsernameUnavailableError(msg) meta = list() for (k, v) in kwargs.items(): um = UserMetaModel() um.key = k um.value = v um.user = u meta.append(um) with transaction_session(auto_commit=True) as session: for um in meta: session.add(um) return u
def create_user(self, username, name, email=None): u = User() u.name = name u.username = username # Passwords must contain at least one number, so we force it... u.password = username + '1' u.verified = True u.enabled = True self.commit_model(u) if email is not None: um = UserMeta() um.key = 'email' um.value = email um.user = u self.commit_model(um) return u
def update(user, username=None, name=None, password=None, **kwargs): """Update properties of a specific User data model instance. Any unspecified keyword arguments will be assumed to be metadata. Existing metadata will be updated to the newly supplied value, and any new metadata keys will be associated with the user. :func:`pooldlib.api.user.reset_password` can be used to update a users password as well. To delete a user's metadata, pass ``None`` as the value for the to be deleted key in the kwarg key-value pair. The ``name`` property of the User model can be cleared by passing an empty string ('') as the value for the name kwarg. :param user: User for which to update ``User`` model data. :type user: :class:`pooldlib.postgresql.models.User` :param username: New username to associate with `User` instance. :type username: string :param name: New name to associate with `User` data model instance. :type name: string :param kwargs: key-value pairs to associate with the `User` data model instance as metadata. :type kwargs: kwarg dictionary :raises: :class:`pooldlib.exceptions.InvalidPasswordError` :returns: :class:`pooldlib.postgresql.models.User` """ if username: user.update_field('username', username) if name is not None: name = None if name == '' else name user.update_field('name', name, nullable=True) if password: validate_password(password, exception_on_invalid=True) user.update_field('password', password) if 'email' in kwargs: if email_exists(kwargs['email'], user=user): msg = 'The email address %s is already assigned to another user.' msg %= kwargs['email'] raise EmailUnavailableError(msg) kwargs['email'] = kwargs['email'].lower() update_meta = [m for m in user.metadata if m.key in kwargs] create_meta = [(k, v) for (k, v) in kwargs.items() if not hasattr(user, k)] meta_delta = list() meta_remove = list() for user_meta in update_meta: value = kwargs[user_meta.key] if value is None: meta_remove.append(user_meta) else: user_meta.value = value meta_delta.append(user_meta) for (k, v) in create_meta: m = UserMetaModel() m.key = k m.value = v m.user = user meta_delta.append(m) with transaction_session() as session: # Technically not needed, but gives the context content session.add(user) try: session.flush() except SQLAlchemyIntegrityError, e: if username is not None: msg = "Username %s already in use." % username raise UsernameUnavailableError(msg) raise e for m in meta_delta: session.add(m) session.flush() for m in meta_remove: session.delete(m) session.commit()