~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Adapters for registry objects for the webservice."""

__metaclass__ = type
__all__ = []

from lazr.restful.interfaces import (
    IFieldHTMLRenderer,
    IReference,
    IWebServiceClientRequest,
    )
from zope import component
from zope.interface import (
    implementer,
    Interface,
    )
from zope.schema.interfaces import IText

from lp.app.browser.lazrjs import standard_text_html_representation
from lp.app.browser.tales import format_link


@component.adapter(Interface, IReference, IWebServiceClientRequest)
@implementer(IFieldHTMLRenderer)
def reference_xhtml_representation(context, field, request):
    """Render an object as a link to the object."""

    def render(value):
        # The value is a webservice link to the object, we want field value.
        obj = getattr(context, field.__name__, None)
        try:
            return format_link(obj, empty_value='')
        except NotImplementedError:
            return value
    return render


@component.adapter(Interface, IText, IWebServiceClientRequest)
@implementer(IFieldHTMLRenderer)
def text_xhtml_representation(context, field, request):
    """Render text as XHTML using the webservice."""
    return standard_text_html_representation