web/__init__.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Fri, 22 May 2009 01:29:35 +0200
branchstable
changeset 1903 c30034fe074f
parent 1801 672acc730ce5
child 1977 606923dff11b
permissions -rw-r--r--
[test] fix test for metainformation now includes base-url
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
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 0
diff changeset
     6
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
__docformat__ = "restructuredtext en"
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    10
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
1645
374586978230 missing import
sylvain.thenault@logilab.fr
parents: 1635
diff changeset
    12
from decimal import Decimal
374586978230 missing import
sylvain.thenault@logilab.fr
parents: 1635
diff changeset
    13
from datetime import datetime, date, timedelta
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    14
from simplejson import dumps
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
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
    16
from logilab.common.deprecation import obsolete
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
    17
1645
374586978230 missing import
sylvain.thenault@logilab.fr
parents: 1635
diff changeset
    18
from cubicweb.common.uilib import urlquote
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    19
from cubicweb.web._exceptions import *
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
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
INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
class stdmsgs(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
    """standard ui message (in a class for bw compat)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
    BUTTON_OK     = _('button_ok')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
    BUTTON_APPLY  = _('button_apply')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
    BUTTON_CANCEL = _('button_cancel')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    BUTTON_DELETE = _('button_delete')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    YES = _('yes')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    NO  = _('no')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
def eid_param(name, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    assert eid is not None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    if eid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        eid = ''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    return '%s:%s' % (name, eid)
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
from logging import getLogger
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
LOGGER = getLogger('cubicweb.web')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    45
# XXX deprecated
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
FACETTES = set()
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    47
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    48
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    49
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    50
def json_dumps(value):
1645
374586978230 missing import
sylvain.thenault@logilab.fr
parents: 1635
diff changeset
    51
    if isinstance(value, Decimal):
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    52
        value = float(value)
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    53
    elif isinstance(value, (date, datetime)):
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    54
        value = value.strftime('%Y-%m-%d %H:%M')
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    55
    elif isinstance(value, timedelta):
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    56
        value = (value.days * 24*60*60) + value.seconds
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    57
    try:
1645
374586978230 missing import
sylvain.thenault@logilab.fr
parents: 1635
diff changeset
    58
        return dumps(value)
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    59
    except TypeError:
1645
374586978230 missing import
sylvain.thenault@logilab.fr
parents: 1635
diff changeset
    60
        return dumps(repr(value))
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    61
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    62
def jsonize(function):
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    63
    def newfunc(*args, **kwargs):
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
    64
        return json_dumps(function(*args, **kwargs))
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
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
    67
@obsolete('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)