web/__init__.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 May 2010 20:47:55 +0200
changeset 5556 9ab2b4c74baf
parent 5424 8ecbcbff9777
child 5713 605f571198eb
permissions -rw-r--r--
[entity] introduce a new 'adapters' registry This changeset introduces the notion in adapters (as in Zope Component Architecture) in a cubicweb way, eg using a specific registry of appobjects. This allows nicer code structure, by avoid clutering entity classes and moving code usually specific to a place of the ui (or something else) together with the code that use the interface. We don't use actual interface anymore, they are implied by adapters (which may be abstract), whose reg id is an interface name. Appobjects that used to 'implements(IFace)' should now be rewritten by: * coding an IFaceAdapter(EntityAdapter) defining (implementing if desired) the interface, usually with __regid__ = 'IFace' * use "adaptable('IFace')" as selector instead Also, the implements_adapter_compat decorator eases backward compatibility with adapter's methods that may still be found on entities implementing the interface. Notice that unlike ZCA, we don't support automatic adapters chain (yagni?). All interfaces defined in cubicweb have been turned into adapters, also some new ones have been introduced to cleanup Entity / AnyEntity classes namespace. At the end, the pluggable mixins mecanism should disappear in favor of adapters as well.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""CubicWeb web client core. You'll need a apache-modpython or twisted
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
publisher to get a full CubicWeb web application
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    20
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
__docformat__ = "restructuredtext en"
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    23
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
5389
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    25
import sys
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    26
if sys.version_info < (2,6):
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    27
    import simplejson as json
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    28
else:
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    29
    import json
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    30
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    31
dumps = json.dumps
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    32
2808
497424219fb0 fix urlquote imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
    33
from urllib import quote as urlquote
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
2613
5e19c2bb370e R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    35
from logilab.common.deprecation import deprecated
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1645
diff changeset
    36
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    37
from cubicweb.web._exceptions import *
3741
3864c7dfddf0 use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2934
diff changeset
    38
from cubicweb.utils import CubicWebJsonEncoder
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
class stdmsgs(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    """standard ui message (in a class for bw compat)"""
3803
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3758
diff changeset
    45
    BUTTON_OK     = (_('button_ok'), 'OK_ICON')
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3758
diff changeset
    46
    BUTTON_APPLY  = (_('button_apply'), 'APPLY_ICON')
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3758
diff changeset
    47
    BUTTON_CANCEL = (_('button_cancel'), 'CANCEL_ICON')
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3758
diff changeset
    48
    BUTTON_DELETE = (_('button_delete'), 'TRASH_ICON')
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3758
diff changeset
    49
    YES = (_('yes'), None)
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3758
diff changeset
    50
    NO  = (_('no'), None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
def eid_param(name, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    assert eid is not None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
    if eid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        eid = ''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    return '%s:%s' % (name, eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
from logging import getLogger
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
LOGGER = getLogger('cubicweb.web')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    63
# XXX deprecated
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
FACETTES = set()
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    65
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    66
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    67
def json_dumps(value):
3741
3864c7dfddf0 use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2934
diff changeset
    68
    return dumps(value, cls=CubicWebJsonEncoder)
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    69
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    70
def jsonize(function):
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    71
    def newfunc(*args, **kwargs):
3758
d43c9709434d yet another brain fart
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3741
diff changeset
    72
        value = function(*args, **kwargs)
3741
3864c7dfddf0 use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2934
diff changeset
    73
        try:
3864c7dfddf0 use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2934
diff changeset
    74
            return json_dumps(value)
3864c7dfddf0 use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2934
diff changeset
    75
        except TypeError:
3864c7dfddf0 use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2934
diff changeset
    76
            return json_dumps(repr(value))
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    77
    return newfunc
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    78
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2613
diff changeset
    79
@deprecated('[3.4] use req.build_ajax_replace_url() instead')
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    80
def ajax_replace_url(nodeid, rql, vid=None, swap=False, **extraparams):
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    81
    """builds a replacePageChunk-like url
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    82
    >>> ajax_replace_url('foo', 'Person P')
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    83
    "javascript: replacePageChunk('foo', 'Person%20P');"
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    84
    >>> ajax_replace_url('foo', 'Person P', 'oneline')
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    85
    "javascript: replacePageChunk('foo', 'Person%20P', 'oneline');"
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    86
    >>> ajax_replace_url('foo', 'Person P', 'oneline', name='bar', age=12)
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    87
    "javascript: replacePageChunk('foo', 'Person%20P', 'oneline', {'age':12, 'name':'bar'});"
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    88
    >>> ajax_replace_url('foo', 'Person P', name='bar', age=12)
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    89
    "javascript: replacePageChunk('foo', 'Person%20P', 'null', {'age':12, 'name':'bar'});"
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    90
    """
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    91
    params = [repr(nodeid), repr(urlquote(rql))]
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    92
    if extraparams and not vid:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    93
        params.append("'null'")
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    94
    elif vid:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    95
        params.append(repr(vid))
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    96
    if extraparams:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    97
        params.append(json_dumps(extraparams))
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    98
    if swap:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    99
        params.append('true')
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   100
    return "javascript: replacePageChunk(%s);" % ', '.join(params)