web/__init__.py
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Fri, 23 Apr 2010 12:19:54 +0200
branchstable
changeset 5389 809d3b5b3d31
parent 5377 84d14ddfae13
child 5421 8167de96c523
permissions -rw-r--r--
[python2.6] careful: json-py provides a json package. We don't want that. To simplify usage, import json via cubicweb.web
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""CubicWeb web client core. You'll need a apache-modpython or twisted
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
publisher to get a full CubicWeb web application
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:organization: Logilab
4212
ab6573088b4a update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3803
diff changeset
     6
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1801
diff changeset
     8
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
__docformat__ = "restructuredtext en"
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    11
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
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
    13
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
    14
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
    15
    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
    16
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
    17
    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
    18
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
    19
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
    20
2808
497424219fb0 fix urlquote imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
    21
from urllib import quote as urlquote
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
2613
5e19c2bb370e R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    23
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
    24
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    25
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
    26
from cubicweb.utils import CubicWebJsonEncoder
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
class stdmsgs(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    """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
    33
    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
    34
    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
    35
    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
    36
    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
    37
    YES = (_('yes'), None)
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3758
diff changeset
    38
    NO  = (_('no'), None)
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
def eid_param(name, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    assert eid is not None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    if eid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        eid = ''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    return '%s:%s' % (name, eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
from logging import getLogger
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
LOGGER = getLogger('cubicweb.web')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    51
# XXX deprecated
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
FACETTES = set()
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    53
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    54
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    55
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
    56
    return dumps(value, cls=CubicWebJsonEncoder)
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    57
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    58
def jsonize(function):
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    59
    def newfunc(*args, **kwargs):
3758
d43c9709434d yet another brain fart
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3741
diff changeset
    60
        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
    61
        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
    62
            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
    63
        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
    64
            return json_dumps(repr(value))
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    65
    return newfunc
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    66
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2613
diff changeset
    67
@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
    68
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
    69
    """builds a replacePageChunk-like url
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    70
    >>> ajax_replace_url('foo', 'Person P')
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    71
    "javascript: replacePageChunk('foo', 'Person%20P');"
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    72
    >>> ajax_replace_url('foo', 'Person P', 'oneline')
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    73
    "javascript: replacePageChunk('foo', 'Person%20P', 'oneline');"
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    74
    >>> 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
    75
    "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
    76
    >>> 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
    77
    "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
    78
    """
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    79
    params = [repr(nodeid), repr(urlquote(rql))]
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    80
    if extraparams and not vid:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    81
        params.append("'null'")
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    82
    elif vid:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    83
        params.append(repr(vid))
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    84
    if extraparams:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    85
        params.append(json_dumps(extraparams))
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    86
    if swap:
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    87
        params.append('true')
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    88
    return "javascript: replacePageChunk(%s);" % ', '.join(params)