예제 #1
0
    def sort_fields(self, value):
        """Sorted fields are stored in internal presentation."""

        parsed = OrderedDict()
        fields = Parser.format_list(value, sort_=False)
        for field in fields:
            match = re.match(
                r'''
            (?P<direction>-?)   # Catch sort direction sign (+/-).
            (?P<field>\S+)      # Catch fields.
            ''', field, re.IGNORECASE | re.VERBOSE)
            if match.group('field') and match.group(
                    'field') in self.ATTRIBUTES:
                parsed[match.group('field')] = 'DESC' if match.group(
                    'direction') == '-' else 'ASC'
            else:
                Cause.push(
                    Cause.HTTP_BAD_REQUEST,
                    'sort option validation failed for non existent field={}'.
                    format(field))
        self._logger.debug(
            '{}: content attribute sort order from user: {}'.format(
                self._derived, fields))
        self._logger.debug(
            '{}: content attribute internal sort structure: {}'.format(
                self._derived, parsed))
        self._sort_fields = parsed  # pylint: disable=attribute-defined-outside-init
예제 #2
0
    def languages(self, value):
        """Convert content languages to tuple of utf-8 encoded unicode strings."""

        if value is None:
            self._reset_fields['languages'] = 'languages'

        self._languages = Parser.format_list(value)  # pylint: disable=attribute-defined-outside-init
예제 #3
0
    def category(self, value):
        """Set content or field category.

        The content category is important when content is created. In case of
        the ``create`` operation, there must be only one category.
        """

        value = Parser.format_list(value)
        if len(value) == 1 and set(value).issubset(Const.CATEGORIES) or set(value).issubset(Const.FIELD_CATEGORIES):
            value = value[0]
        else:
            value = Const.UNKNOWN_CATEGORY

        self._category = value  # pylint: disable=attribute-defined-outside-init
예제 #4
0
    def select_distinct(self, column, scat, sall, stag, sgrp):
        """Select unique values from the given column.

        Because the database stores list attributes as a string where values
        are separated with delimiters, a conversion is needed. This method
        returns a dictionaty where each key is an unique value in requested
        column. The values in the returned dictionary are the counts for
        each distinct column value.

        Args:
            column (str): column name.
            scat (tuple): Search category keyword list.
            sall (tuple): Search all keyword list.
            stag (tuple): Search tag keyword list.
            sgrp (tuple): Search group keyword list.

        Returns:
            dict: Dictionary of unique values with counts.
        """

        uniques = {}
        if column not in self._columns:
            self._logger.security('unidentified column name cannot be accepted: %s', column)

            return uniques

        if self._connection:
            self._logger.debug('select distinct values from columns: %s', column)
            query, qargs = self._get_query(column, scat, sall, stag, sgrp, None, None, None, None, Database.QUERY_TYPE_FIELD)
            try:
                with closing(self._connection.cursor()) as cursor:
                    cursor.execute(query, qargs)
                    values = cursor.fetchall()
            except (sqlite3.Error, psycopg2.Error) as error:
                Cause.push(Cause.HTTP_500, 'selecting all from database failed with exception {}'.format(error))
        else:
            Cause.push(Cause.HTTP_500, 'internal error prevented selecting all content from database')

        for value in values:
            for tag in Parser.format_list(value[0]):
                if tag in uniques:
                    uniques[tag] = uniques[tag] + value[1]
                else:
                    uniques[tag] = value[1]

        return uniques
예제 #5
0
    def remove_fields(self, value):
        """Store 'removed fields'.

        The removed fields are presented as tuple and they are converted from
        requested 'fields' parameter."""

        remove_fields = ()
        requested_fields = Parser.format_list(value)
        valid = True
        for field in requested_fields:
            if field not in self.ATTRIBUTES:
                valid = False
                Cause.push(Cause.HTTP_BAD_REQUEST, 'resource field does not exist: {}'.format(field))

        if valid:
            remove_fields = tuple(set(self.ATTRIBUTES) - set(requested_fields))  # pylint: disable=attribute-defined-outside-init

        self._logger.debug('{}: content attributes that are removed: {}'.format(self._derived, remove_fields))
        self._remove_fields = remove_fields  # pylint: disable=attribute-defined-outside-init
예제 #6
0
    def versions(self, value):
        """Resource versions."""

        self._versions = Parser.format_list(value)
예제 #7
0
    def tags(self, value):
        """Resource tags."""

        self._tags = Parser.format_list(value)
예제 #8
0
    def groups(self, value):
        """Resource groups."""

        self._groups = Parser.format_list(value)
예제 #9
0
    def languages(self, value):
        """Resource languages."""

        self._languages = Parser.format_list(value)