Example #1
0
 def __getattr__(self, name):
     if hasattr(self.item, name):
         value = self.item.get_value(name, expand=True)
         if isinstance(value, BaseItem):
             return PrintValueGetter(value, self.request)
         elif isinstance(value, basestring):
             value = escape(value)
             value = value.replace("\r\n", "<text:line-break/>")
             value = value.replace("\r", "<text:line-break/>")
             value = value.replace("\n", "<text:line-break/>")
             return Markup(value)
         else:
             return prettify(self.request, value)
Example #2
0
File: base.py Project: toirl/ringo
    def __unicode__(self):
        """Will return the string representation for the item based on a
        configured format string in the modul settings. If no format str
        can be found return the id of the item.

        The format string must have the following syntax:
        format_str|field1,field2,....fieldN. where format_str in a
        string with %s placeholders and fields is a comma separated list
        of fields of the item"""
        modul = load_modul(self)
        format_str, fields = modul.get_str_repr()
        if format_str:
            return format_str % tuple([prettify(None, self.get_value(f)) for f in fields])
        else:
            return "%s" % str(self.id or self.__class__)
Example #3
0
def test_prettify_date(apprequest):
    from ringo.lib.helpers import prettify
    from datetime import datetime
    from dateutil import tz
    dt = datetime(1977, 3, 12, 0, 0, 0, )
    dt = dt.replace(tzinfo=tz.tzlocal())
    result = prettify(apprequest, dt)
    # The result of the prettify function for datetimes is not
    # constant and seems to depend on some system settings.
    # So test all known variants
    check1 = (result == u"3/12/77 12:00 AM")
    check2 = (result == u"3/12/77, 12:00 AM")
    check3 = (result == u"3/12/77, 1:00 AM")
    ok = check1 or check2 or check3
    assert ok
Example #4
0
def test_prettify_date(apprequest):
    from ringo.lib.helpers import prettify
    from datetime import datetime
    from dateutil import tz
    dt = datetime(
        1977,
        3,
        12,
        0,
        0,
        0,
    )
    dt = dt.replace(tzinfo=tz.tzlocal())
    result = prettify(apprequest, dt)
    assert result == u"1977-03-12 00:00"
Example #5
0
    def __unicode__(self):
        """Will return the string representation for the item based on a
        configured format string in the modul settings. If no format str
        can be found return the id of the item.

        The format string must have the following syntax:
        format_str|field1,field2,....fieldN. where format_str in a
        string with %s placeholders and fields is a comma separated list
        of fields of the item"""
        modul = load_modul(self)
        format_str, fields = modul.get_str_repr()
        if format_str:
            return format_str % tuple(
                [prettify(None, self.get_value(f)) for f in fields])
        else:
            return "%s" % str(self.id or self.__class__)
Example #6
0
    def filter(self, filter_stack, request=None, table="overview"):
        """This function will filter the items by only leaving
        those items in the list which match all search criteria in the
        filter stack. The number of items will get reduced with every
        iteration on the filter stack. The search is case sensitive.

        The filter stack is a list of tuples which describe the filter
        expression. Each tuple consists of three values:

        1. The search expression
        2. Optionally the name of the column on which the search will be
           applied.
        3. Boolean flag to indicate that the given search expression
           should be handled as a regular expression.

        The search support three modes. On default the search will look
        for the presence of the given search string within the values of
        the item. It will also match on parts of the string. So
        searching for 'Foo' will also match 'Foobar'. Another mode is
        using the search expression as a regular expression. The last
        mode makes use of special operators. The search supports the
        following operators: "<", "<=", "!=", ">" ">=" and "~" The
        operator can be provided with the search string.  It mus be the
        first word of the search expression. If a operator is present it
        will be used.

        The "~" operator will trigger a fuzzy search using the Double
        Metaphone algorithm for determining equal phonetics. If the
        phonetics do not match the Levenshtein distance will be
        calculated.

        The search will iterate over all items in the list. For each
        item the function will try to match the value of either all, or
        from the configured search field with the regular expression or
        configured operator. If the value matches, then the item is kept
        in the list.

        :filter_stack: Filter stack
        :request: Current request.
        :table: Name of the table config which is used for the search
                defaults to "overview".
        :returns: Filtered list of items
        """
        self.search_filter = filter_stack
        log.debug('Length filterstack: %s' % len(filter_stack))
        table_config = get_table_config(self.clazz, table)
        table_columns = {}

        # Save cols in the tableconfig for later access while getting values.
        for col in [
                col for col in table_config.get_columns()
                if col.get("searchable", True)
        ]:
            table_columns[col.get('name')] = col

        for search, search_field, regexpr in filter_stack:
            search_op = None
            # Get soperator
            x = search.split(" ")
            if x[0] in opmapping:
                search_op = x[0]
                search = " ".join(x[1:])
            # Build a regular expression
            if regexpr:
                re_expr = re.compile(search, re.IGNORECASE)
            else:
                re_expr = re.compile(re.escape(search), re.IGNORECASE)
            filtered_items = []
            log.debug('Filtering "%s" in "%s" with operator "%s" on %s items' %
                      (search, search_field, search_op, len(self.items)))
            if search_field != "":
                fields = [search_field]
            else:
                fields = table_columns.keys()
            for item in self.items:
                for field in fields:
                    expand = table_columns[field].get('expand')
                    renderer = table_config.get_renderer(table_columns[field])
                    if renderer:
                        value = renderer(request, item, field, table_config)
                    else:
                        value = item.get_value(field, expand=expand)
                    if hasattr(value, 'render'):
                        pretty_value = value.render(request)
                    elif isinstance(value, list):
                        if request and expand:
                            value = ", ".join(
                                [request.translate(unicode(v)) for v in value])
                        else:
                            value = ", ".join([unicode(v) for v in value])
                        pretty_value = value
                    else:
                        pretty_value = unicode(prettify(request, value))
                        if request and expand:
                            pretty_value = request.translate(pretty_value)
                    if search_op:
                        if request:
                            value = request.translate(unicode(value))
                        else:
                            value = unicode(value)
                        if opmapping[search_op](value, search):
                            filtered_items.append(item)
                            break
                    else:
                        if re_expr.search(pretty_value):
                            filtered_items.append(item)
                            break
            self.items = filtered_items
Example #7
0
def test_prettify_nondate(apprequest):
    from ringo.lib.helpers import prettify
    result = prettify(apprequest, "Test")
    assert result == "Test"
Example #8
0
def test_prettify_nondate(apprequest):
    from ringo.lib.helpers import prettify
    result = prettify(apprequest, "Test")
    assert result == "Test"