Exemple #1
0
    def create_user(self, *args, **kwargs):
        """
        Create user in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is filled field of user model.
        """
        if 'username' not in kwargs or 'password' not in kwargs:
            raise RequiredModelFieldsNotDefined(
                "Username and password fields are required")

        kwargs['password'] = generate_password_hash(kwargs['password'])

        user_defined_fields = self.__user_defined_fields(kwargs)
        default_user_data = {
            'first_name': '',
            'last_name': '',
            'is_active': True,
            'is_superuser': False,
            'is_staff': False,
            'is_user': False,
        }

        default_user_data.update(user_defined_fields)
        user_data = [default_user_data[key] for key in self.fields_without_pk]

        try:
            self.db_manager.execute_sql(SQL_USER_ADD, user_data)
        except OperationalError as exc:
            logger.error(exc)
Exemple #2
0
    def update_user(self, *args, **kwargs):
        """
        Update user row in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is updated field of user model.
        """
        username = kwargs.pop('username', None)
        if not username:
            raise SearchCriteriaRequired(
                "Username for WHEN statement is required.")  # NOQA

        if len(kwargs) < 1:
            raise NotEnoughArguments()

        if 'password' in kwargs.keys():
            kwargs['password'] = generate_password_hash(kwargs['password'])

        updated_fields = self.__user_defined_fields(kwargs)
        update_query, query_args = construct_update_sql(**updated_fields)
        query_args.append(username)
        try:
            self.db_manager.execute_sql(update_query, query_args)
        except OperationalError as exc:
            logger.error(exc)
Exemple #3
0
    def create_user(self, *args, **kwargs):
        """Create user in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is filled field of user model.
        """
        if 'username' not in kwargs or 'password' not in kwargs:
            raise RequiredModelFieldsNotDefined(
                "Username and password fields are required"
            )

        kwargs['password'] = generate_password_hash(kwargs['password'])

        user_defined_fields = self.__user_defined_fields(kwargs)
        default_user_data = {
            'first_name': '',
            'last_name': '',
            'is_active': True,
            'is_superuser': False,
            'is_staff': False,
            'is_user': False,
        }

        default_user_data.update(user_defined_fields)
        user_data = [default_user_data[key] for key in self.fields_without_pk]

        try:
            self.db_manager.execute_sql(SQL_USER_ADD, user_data)
        except OperationalError as exc:
            logger.error(exc)
Exemple #4
0
    def update_user(self, *args, **kwargs):
        """Update user row in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is updated field of user model.
        """
        username = kwargs.pop('username', None)
        if not username:
            raise SearchCriteriaRequired(
                "Username for WHEN statement is required."
            )

        if len(kwargs) < 1:
            raise NotEnoughArguments()

        if 'password' in kwargs.keys():
            kwargs['password'] = generate_password_hash(kwargs['password'])

        updated_fields = self.__user_defined_fields(kwargs)
        update_query, query_args = construct_update_sql(**updated_fields)
        query_args.append(username)
        try:
            self.db_manager.execute_sql(update_query, query_args)
        except OperationalError as exc:
            logger.error(exc)
Exemple #5
0
    def get_user_by_token(self, token):
        """
        Get user object from the database, based on the his token.

        :param token: passed token as a dictionary object.
        """
        user_id = token['user_id']
        try:
            user_row = self.db_manager.execute_sql(SQL_USER_GET,
                                                   (user_id, )).fetchone()
            if user_row:
                user_data = convert_user_raw_data_to_dict(user_row)
            else:
                user_data = {}
        except OperationalError as exc:
            logger.error(exc)
            user_data = {}
        return User(**user_data)
Exemple #6
0
    def get_user_by_token(self, token):
        """Get user object from the database, based on the his token.

        :param token: passed token as a dictionary object.
        """
        user_id = token['user_id']
        try:
            user_row = self.db_manager.execute_sql(
                SQL_USER_GET, (user_id, )
            ).fetchone()
            if user_row:
                user_data = convert_user_raw_data_to_dict(user_row)
            else:
                user_data = {}
        except OperationalError as exc:
            logger.error(exc)
            user_data = {}
        return User(**user_data)
Exemple #7
0
    def get_user_by_username(self, username, with_id=False):
        """
        Get user by his username from the database.

        :param username: username as a string.
        :param with_id: boolean flag, which means necessity to append to the
                        result object primary key of database row or not.
        """
        try:
            if with_id:
                sql = SQL_USER_GET_WITH_ID
            else:
                sql = SQL_USER_GET_BY_USERNAME

            user_row = self.db_manager.execute_sql(sql, (username, )).fetchone()  # NOQA
            if user_row:
                user_data = convert_user_raw_data_to_dict(user_row, with_id)
            else:
                user_data = {}
        except OperationalError as exc:
            logger.error(exc)
            user_data = {}
        return User(**user_data)
Exemple #8
0
    def get_user_by_username(self, username, with_id=False):
        """
        Get user by his username from the database.

        :param username: username as a string.
        :param with_id: boolean flag, which means necessity to append to the
                        result object primary key of database row or not.
        """
        try:
            if with_id:
                sql = SQL_USER_GET_WITH_ID
            else:
                sql = SQL_USER_GET_BY_USERNAME

            user_row = self.db_manager.execute_sql(
                sql, (username, )).fetchone()  # NOQA
            if user_row:
                user_data = convert_user_raw_data_to_dict(user_row, with_id)
            else:
                user_data = {}
        except OperationalError as exc:
            logger.error(exc)
            user_data = {}
        return User(**user_data)