示例#1
0
    def _make_user_cell(self, record):
        doc_info = get_doc_info_by_id(self.domain, record.user_id)
        user = WebUser.get_by_user_id(record.user_id)
        if self.domain not in user.get_domains() and 'link' in doc_info:
            doc_info['link'] = None

        return pretty_doc_info(doc_info)
示例#2
0
def get_display_data(data,
                     prop_def,
                     processors=None,
                     timezone=pytz.utc,
                     info_url=None):
    # when prop_def came from a couchdbkit document, it will be a LazyDict with
    # a broken pop method.  This conversion also has the effect of a shallow
    # copy, which we want.
    prop_def = dict(prop_def)

    default_processors = {
        'yesno':
        yesno,
        'doc_info':
        lambda value: pretty_doc_info(get_doc_info_by_id(
            data['domain'], value))
    }
    processors = processors or {}
    processors.update(default_processors)

    expr_name = _get_expr_name(prop_def)
    expr = prop_def.pop('expr')
    name = prop_def.pop('name', None) or _format_slug_string_for_display(expr)
    format = prop_def.pop('format', None)
    process = prop_def.pop('process', None)
    timeago = prop_def.get('timeago', False)

    val = eval_expr(expr, data)

    if prop_def.pop('parse_date', None):
        val = _parse_date_or_datetime(val)
    # is_utc is deprecated in favor of is_phone_time
    # but preserving here for backwards compatibility
    # is_utc = False is just reinterpreted as is_phone_time = True
    is_phone_time = prop_def.pop('is_phone_time',
                                 not prop_def.pop('is_utc', True))
    if isinstance(val, datetime.datetime):
        if not is_phone_time:
            val = ServerTime(val).user_time(timezone).done()
        else:
            val = PhoneTime(val, timezone).user_time(timezone).done()

    try:
        val = conditional_escape(processors[process](val))
    except KeyError:
        val = mark_safe(_to_html(val, timeago=timeago))
    if format:
        val = mark_safe(format.format(val))

    return {
        "expr":
        expr_name,
        "name":
        name,
        "value":
        val,
        "info_url":
        info_url.replace("__placeholder__", expr)
        if info_url is not None else None,
    }
示例#3
0
 def _make_row(self, record, link):
     row = [
         '{} -> {}'.format(link.master_domain, link.linked_domain),
         server_to_user_time(record.date, self.timezone),
         self._make_model_cell(record),
         pretty_doc_info(get_doc_info_by_id(self.domain, record.user_id))
     ]
     return row
示例#4
0
 def _make_row(self, record, link):
     row = [
         '{} -> {}'.format(link.master_domain, link.linked_domain),
         server_to_user_time(record.date, self.timezone),
         self._make_model_cell(record),
         pretty_doc_info(get_doc_info_by_id(self.domain, record.user_id))
     ]
     return row
def get_display_data(data, prop_def, processors=None, timezone=pytz.utc):
    # when prop_def came from a couchdbkit document, it will be a LazyDict with
    # a broken pop method.  This conversion also has the effect of a shallow
    # copy, which we want.
    prop_def = dict(prop_def)

    default_processors = {
        'yesno':
        yesno,
        'doc_info':
        lambda value: pretty_doc_info(get_doc_info_by_id(
            data['domain'], value))
    }
    processors = processors or {}
    processors.update(default_processors)

    def format_key(key):
        key = key.replace('_', ' ')
        return key.replace('-', ' ')

    expr = prop_def.pop('expr')
    name = prop_def.pop('name', format_key(expr))
    format = prop_def.pop('format', None)
    process = prop_def.pop('process', None)

    # todo: nested attributes, jsonpath, indexing into related documents
    val = data.get(expr, None)

    if prop_def.pop('parse_date', None):
        val = parse_date_or_datetime(val)
    is_utc = prop_def.pop('is_utc', True)
    if isinstance(val, datetime.datetime):
        if is_utc:
            if val.tzinfo is None:
                val = val.replace(tzinfo=pytz.utc)
            val = adjust_datetime_to_timezone(val, val.tzinfo, timezone.zone)
        else:
            val = val.replace(tzinfo=timezone)

    try:
        val = conditional_escape(processors[process](val))
    except KeyError:
        val = mark_safe(
            to_html(None,
                    val,
                    timezone=timezone,
                    key_format=format_key,
                    collapse_lists=True,
                    **prop_def))
    if format:
        val = mark_safe(format.format(val))

    return {"expr": expr, "name": name, "value": val}
def get_display_data(data, prop_def, processors=None, timezone=pytz.utc):
    # when prop_def came from a couchdbkit document, it will be a LazyDict with
    # a broken pop method.  This conversion also has the effect of a shallow
    # copy, which we want.
    prop_def = dict(prop_def)

    default_processors = {
        'yesno': yesno,
        'doc_info': lambda value: pretty_doc_info(
            get_doc_info_by_id(data['domain'], value)
        )
    }
    processors = processors or {}
    processors.update(default_processors)

    def format_key(key):
        key = key.replace('_', ' ')
        return key.replace('-', ' ')

    expr = prop_def.pop('expr')
    name = prop_def.pop('name', format_key(expr))
    format = prop_def.pop('format', None)
    process = prop_def.pop('process', None)

    # todo: nested attributes, jsonpath, indexing into related documents
    val = data.get(expr, None)

    if prop_def.pop('parse_date', None):
        val = parse_date_or_datetime(val)
    is_utc = prop_def.pop('is_utc', True)
    if isinstance(val, datetime.datetime):
        if is_utc:
            if val.tzinfo is None:
                val = val.replace(tzinfo=pytz.utc)
            val = adjust_datetime_to_timezone(val, val.tzinfo, timezone.zone)
        else:
            val = val.replace(tzinfo=timezone)

    try:
        val = conditional_escape(processors[process](val))
    except KeyError:
        val = mark_safe(to_html(None, val, 
            timezone=timezone, key_format=format_key, collapse_lists=True,
            **prop_def))
    if format:
        val = mark_safe(format.format(val))

    return {
        "expr": expr,
        "name": name,
        "value": val
    }
示例#7
0
def get_display_data(data, prop_def, processors=None, timezone=pytz.utc):
    # when prop_def came from a couchdbkit document, it will be a LazyDict with
    # a broken pop method.  This conversion also has the effect of a shallow
    # copy, which we want.
    prop_def = dict(prop_def)

    default_processors = {
        'yesno':
        yesno,
        'doc_info':
        lambda value: pretty_doc_info(get_doc_info_by_id(
            data['domain'], value))
    }
    processors = processors or {}
    processors.update(default_processors)

    expr_name = _get_expr_name(prop_def)
    expr = prop_def.pop('expr')
    name = prop_def.pop('name', None) or _format_slug_string_for_display(expr)
    format = prop_def.pop('format', None)
    process = prop_def.pop('process', None)
    timeago = prop_def.get('timeago', False)
    has_history = prop_def.pop('has_history', False)

    val = eval_expr(expr, data)

    if prop_def.pop('parse_date', None):
        try:
            val = _parse_date_or_datetime(val)
        except Exception:
            # ignore exceptions from date parsing
            pass
    is_phone_time = prop_def.pop('is_phone_time', False)
    if isinstance(val, datetime.datetime):
        if not is_phone_time:
            val = ServerTime(val).user_time(timezone).done()
        else:
            val = PhoneTime(val, timezone).user_time(timezone).done()

    try:
        val = conditional_escape(processors[process](val))
    except KeyError:
        val = mark_safe(_to_html(val, timeago=timeago))
    if format:
        val = mark_safe(format.format(val))

    return {
        "expr": expr_name,
        "name": name,
        "value": val,
        "has_history": has_history,
    }
示例#8
0
def get_display_data(data, prop_def, processors=None, timezone=pytz.utc):
    # when prop_def came from a couchdbkit document, it will be a LazyDict with
    # a broken pop method.  This conversion also has the effect of a shallow
    # copy, which we want.
    prop_def = dict(prop_def)

    default_processors = {
        'yesno': yesno,
        'doc_info': lambda value: pretty_doc_info(
            get_doc_info_by_id(data['domain'], value)
        )
    }
    processors = processors or {}
    processors.update(default_processors)

    expr_name = _get_expr_name(prop_def)
    expr = prop_def.pop('expr')
    name = prop_def.pop('name', None) or _format_slug_string_for_display(expr)
    format = prop_def.pop('format', None)
    process = prop_def.pop('process', None)
    timeago = prop_def.get('timeago', False)
    has_history = prop_def.pop('has_history', False)

    val = eval_expr(expr, data)

    if prop_def.pop('parse_date', None):
        val = _parse_date_or_datetime(val)
    # is_utc is deprecated in favor of is_phone_time
    # but preserving here for backwards compatibility
    # is_utc = False is just reinterpreted as is_phone_time = True
    is_phone_time = prop_def.pop('is_phone_time',
                                 not prop_def.pop('is_utc', True))
    if isinstance(val, datetime.datetime):
        if not is_phone_time:
            val = ServerTime(val).user_time(timezone).done()
        else:
            val = PhoneTime(val, timezone).user_time(timezone).done()

    try:
        val = conditional_escape(processors[process](val))
    except KeyError:
        val = mark_safe(_to_html(val, timeago=timeago))
    if format:
        val = mark_safe(format.format(val))

    return {
        "expr": expr_name,
        "name": name,
        "value": val,
        "has_history": has_history,
    }
示例#9
0
class DisplayProcessor(
        collections.namedtuple("DisplayProcessor", "processor, returns_html")):
    def __call__(self, value, data):
        return self.processor(value, data)


VALUE_DISPLAY_PROCESSORS = {
    'date':
    DisplayProcessor(lambda value, data: _parse_date_or_datetime(value),
                     False),
    'yesno':
    DisplayProcessor(lambda value, data: conditional_escape(yesno(value)),
                     False),
    'doc_info':
    DisplayProcessor(
        lambda value, data: pretty_doc_info(
            get_doc_info_by_id(data['domain'], value)), True)
}

register = template.Library()


def _is_list_like(val):
    return isinstance(val, collections.Iterable) and not isinstance(val, str)


def _parse_date_or_datetime(val):
    def parse():
        if not val:
            return None

        # datetime is a subclass of date