web/views/basecontrollers.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 28 May 2009 20:06:45 +0200
changeset 1995 ec95eaa2b711
parent 1977 606923dff11b
child 1996 2490e18fd3c8
permissions -rw-r--r--
turn renderers into appobjects
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
"""Set of base controllers, which are directly plugged into the application
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
object to handle publication.
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
:organization: Logilab
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1889
diff changeset
     7
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
: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: 1889
diff changeset
     9
: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
    10
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
from smtplib import SMTP
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
import simplejson
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
from logilab.common.decorators import cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
945
912b604f0e42 missing import
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 882
diff changeset
    19
from cubicweb import NoSelectableObject, ValidationError, ObjectNotFound, typed_eid
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 945
diff changeset
    20
from cubicweb.utils import strptime
692
800592b8d39b replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 644
diff changeset
    21
from cubicweb.selectors import yes, match_user_groups
1838
d4cbcc15c01c work-around for #343301 (display garbage in non xhtml browsers)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1798
diff changeset
    22
from cubicweb.view import STRICT_DOCTYPE, STRICT_DOCTYPE_NOEXT
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from cubicweb.common.mail import format_mail
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1560
diff changeset
    24
from cubicweb.web import ExplicitLogin, Redirect, RemoteCallFailed, json_dumps
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
from cubicweb.web.controller import Controller
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
from cubicweb.web.views import vid_from_rset
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    27
from cubicweb.web.views.formrenderers import FormRenderer
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
    from cubicweb.web.facet import (FilterRQLBuilder, get_facet,
408
a8814ff6824e reactivate tests and fix bug triggering removal of undesired relation (eg type restriction) in some cases
sylvain.thenault@logilab.fr
parents: 353
diff changeset
    30
                                    prepare_facets_rqlst)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    HAS_SEARCH_RESTRICTION = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
except ImportError: # gae
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
    HAS_SEARCH_RESTRICTION = False
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    34
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    35
1838
d4cbcc15c01c work-around for #343301 (display garbage in non xhtml browsers)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1798
diff changeset
    36
def xhtml_wrap(self, source):
d4cbcc15c01c work-around for #343301 (display garbage in non xhtml browsers)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1798
diff changeset
    37
    # XXX factor out, watch view.py ~ Maintemplate.doctype
d4cbcc15c01c work-around for #343301 (display garbage in non xhtml browsers)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1798
diff changeset
    38
    if self.req.xhtml_browser():
d4cbcc15c01c work-around for #343301 (display garbage in non xhtml browsers)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1798
diff changeset
    39
        dt = STRICT_DOCTYPE
1839
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1838
diff changeset
    40
    else:
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1838
diff changeset
    41
        dt = STRICT_DOCTYPE_NOEXT
1838
d4cbcc15c01c work-around for #343301 (display garbage in non xhtml browsers)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1798
diff changeset
    42
    head = u'<?xml version="1.0"?>\n' + dt
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    43
    return head + u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">%s</div>' % source.strip()
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    44
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    45
def jsonize(func):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    46
    """decorator to sets correct content_type and calls `simplejson.dumps` on
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    47
    results
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    48
    """
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    49
    def wrapper(self, *args, **kwargs):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    50
        self.req.set_content_type('application/json')
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1560
diff changeset
    51
        return json_dumps(func(self, *args, **kwargs))
1527
c8ca1782e252 controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1467
diff changeset
    52
    wrapper.__name__ = func.__name__
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    53
    return wrapper
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    54
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    55
def xhtmlize(func):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    56
    """decorator to sets correct content_type and calls `xmlize` on results"""
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    57
    def wrapper(self, *args, **kwargs):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    58
        self.req.set_content_type(self.req.html_content_type())
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    59
        result = func(self, *args, **kwargs)
1838
d4cbcc15c01c work-around for #343301 (display garbage in non xhtml browsers)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1798
diff changeset
    60
        return xhtml_wrap(self, result)
1527
c8ca1782e252 controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1467
diff changeset
    61
    wrapper.__name__ = func.__name__
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    62
    return wrapper
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    63
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    64
def check_pageid(func):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    65
    """decorator which checks the given pageid is found in the
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    66
    user's session data
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    67
    """
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    68
    def wrapper(self, *args, **kwargs):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    69
        data = self.req.get_session_data(self.req.pageid)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    70
        if data is None:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    71
            raise RemoteCallFailed(self.req._('pageid-not-found'))
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    72
        return func(self, *args, **kwargs)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    73
    return wrapper
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    74
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    75
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
class LoginController(Controller):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    id = 'login'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    def publish(self, rset=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        """log in the application"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        if self.config['auth-mode'] == 'http':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
            # HTTP authentication
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
            raise ExplicitLogin()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
            # Cookie authentication
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
            return self.appli.need_login_content(self.req)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    88
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
class LogoutController(Controller):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    id = 'logout'
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    91
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    def publish(self, rset=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        """logout from the application"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        return self.appli.session_handler.logout(self.req)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
class ViewController(Controller):
823
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
    98
    """standard entry point :
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
    99
    - build result set
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   100
    - select and call main template
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   101
    """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    id = 'view'
823
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   103
    template = 'main-template'
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   104
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    def publish(self, rset=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        """publish a request, returning an encoded string"""
823
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   107
        view, rset = self._select_view_and_rset(rset)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   108
        self.add_to_breadcrumbs(view)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   109
        self.validate_cache(view)
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 823
diff changeset
   110
        template = self.appli.main_template_id(self.req)
823
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   111
        return self.vreg.main_template(self.req, template, rset=rset, view=view)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   112
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   113
    def _select_view_and_rset(self, rset):
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   114
        req = self.req
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   115
        if rset is None and not hasattr(req, '_rql_processed'):
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   116
            req._rql_processed = True
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   117
            rset = self.process_rql(req.form.get('rql'))
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   118
        if rset and rset.rowcount == 1 and '__method' in req.form:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   119
            entity = rset.get_entity(0, 0)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   120
            try:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   121
                method = getattr(entity, req.form.pop('__method'))
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   122
                method()
1827
93840d187f26 allow the __method() hook to raise a Redirect exception
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1798
diff changeset
   123
            except Redirect: # propagate redirect that might occur in method()
93840d187f26 allow the __method() hook to raise a Redirect exception
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1798
diff changeset
   124
                raise
823
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   125
            except Exception, ex:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   126
                self.exception('while handling __method')
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   127
                req.set_message(req._("error while handling __method: %s") % req._(ex))
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   128
        vid = req.form.get('vid') or vid_from_rset(req, rset, self.schema)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   129
        try:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   130
            view = self.vreg.select_view(vid, req, rset)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   131
        except ObjectNotFound:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   132
            self.warning("the view %s could not be found", vid)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   133
            req.set_message(req._("The view %s could not be found") % vid)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   134
            vid = vid_from_rset(req, rset, self.schema)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   135
            view = self.vreg.select_view(vid, req, rset)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   136
        except NoSelectableObject:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   137
            if rset:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   138
                req.set_message(req._("The view %s can not be applied to this query") % vid)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   139
            else:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   140
                req.set_message(req._("You have no access to this view or it's not applyable to current data"))
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   141
            self.warning("the view %s can not be applied to this query", vid)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   142
            vid = vid_from_rset(req, rset, self.schema)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   143
            view = self.vreg.select_view(vid, req, rset)
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   144
        return view, rset
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   145
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   146
    def add_to_breadcrumbs(self, view):
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   147
        # update breadcrumps **before** validating cache, unless the view
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   148
        # specifies explicitly it should not be added to breadcrumb or the
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   149
        # view is a binary view
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   150
        if view.add_to_breadcrumbs and not view.binary:
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   151
            self.req.update_breadcrumbs()
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   152
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   153
    def validate_cache(self, view):
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   154
        view.set_http_cache_headers()
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 808
diff changeset
   155
        self.req.validate_cache()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
    def execute_linkto(self, eid=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
        """XXX __linkto parameter may cause security issue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
        defined here since custom application controller inheriting from this
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
        one use this method?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
        req = self.req
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
        if not '__linkto' in req.form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        if eid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
            eid = typed_eid(req.form['eid'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
        for linkto in req.list_form_param('__linkto', pop=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
            rtype, eids, target = linkto.split(':')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
            assert target in ('subject', 'object')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
            eids = eids.split('_')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
            if target == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
                rql = 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % rtype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
                rql = 'SET Y %s X WHERE X eid %%(x)s, Y eid %%(y)s' % rtype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
            for teid in eids:
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   177
                req.execute(rql, {'x': eid, 'y': typed_eid(teid)}, ('x', 'y'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
class FormValidatorController(Controller):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
    id = 'validateform'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
    def publish(self, rset=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
        vreg = self.vreg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
            ctrl = vreg.select(vreg.registry_objects('controllers', 'edit'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
                               req=self.req, appli=self.appli)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
        except NoSelectableObject:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
            status, args = (False, {None: self.req._('not authorized')})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
                ctrl.publish(None, fromjson=True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
            except ValidationError, err:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
                status, args = self.validation_error(err)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
            except Redirect, err:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
                    self.req.cnx.commit() # ValidationError may be raise on commit
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
                except ValidationError, err:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
                    status, args = self.validation_error(err)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
                else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
                    status, args = (True, err.location)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
            except Exception, err:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
                self.req.cnx.rollback()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
                self.exception('unexpected error in validateform')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
                    status, args = (False, self.req._(unicode(err)))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
                except UnicodeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
                    status, args = (False, repr(err))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
                status, args = (False, '???')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
        self.req.set_content_type('text/html')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
        jsarg = simplejson.dumps( (status, args) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
        return """<script type="text/javascript">
1868
af1ae863606d update handleFormValidationResponse calls
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 1857
diff changeset
   214
 window.parent.handleFormValidationResponse('entityForm', null, null, %s);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
</script>""" %  simplejson.dumps( (status, args) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
    def validation_error(self, err):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
        self.req.cnx.rollback()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
            eid = err.entity.eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
        except AttributeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
            eid = err.entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
        return (False, (eid, err.errors))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
class JSonController(Controller):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    id = 'json'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
    def publish(self, rset=None):
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   230
        """call js_* methods. Expected form keys:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   231
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   232
        :fname: the method name without the js_ prefix
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   233
        :args: arguments list (json)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   234
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   235
        note: it's the responsability of js_* methods to set the correct
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   236
        response content type
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   237
        """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
        self.req.pageid = self.req.form.get('pageid')
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   239
        fname = self.req.form['fname']
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   240
        try:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   241
            func = getattr(self, 'js_%s' % fname)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   242
        except AttributeError:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   243
            raise RemoteCallFailed('no %s method' % fname)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   244
        # no <arg> attribute means the callback takes no argument
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   245
        args = self.req.form.get('arg', ())
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   246
        if not isinstance(args, (list, tuple)):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   247
            args = (args,)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   248
        args = [simplejson.loads(arg) for arg in args]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
        try:
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   250
            result = func(*args)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   251
        except RemoteCallFailed:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   252
            raise
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   253
        except Exception, ex:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   254
            self.exception('an exception occured while calling js_%s(%s): %s',
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   255
                           fname, args, ex)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   256
            raise RemoteCallFailed(repr(ex))
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   257
        if result is None:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   258
            return ''
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   259
        # get unicode on @htmlize methods, encoded string on @jsonize methods
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   260
        elif isinstance(result, unicode):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   261
            return result.encode(self.req.encoding)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   262
        return result
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   263
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   264
    def _rebuild_posted_form(self, names, values, action=None):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   265
        form = {}
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   266
        for name, value in zip(names, values):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   267
            # remove possible __action_xxx inputs
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   268
            if name.startswith('__action'):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   269
                continue
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   270
            # form.setdefault(name, []).append(value)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   271
            if name in form:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   272
                curvalue = form[name]
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   273
                if isinstance(curvalue, list):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   274
                    curvalue.append(value)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   275
                else:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   276
                    form[name] = [curvalue, value]
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   277
            else:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   278
                form[name] = value
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   279
        # simulate click on __action_%s button to help the controller
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   280
        if action:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   281
            form['__action_%s' % action] = u'whatever'
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   282
        return form
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
    def _exec(self, rql, args=None, eidkey=None, rocheck=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
        """json mode: execute RQL and return resultset as json"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
        if rocheck:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
            self.ensure_ro_rql(rql)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
            return self.req.execute(rql, args, eidkey)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
        except Exception, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
            self.exception("error in _exec(rql=%s): %s", rql, ex)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
            return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   295
    @xhtmlize
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   296
    def js_view(self):
643
616191014b8b [jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 603
diff changeset
   297
        # XXX try to use the page-content template
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
        req = self.req
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
        rql = req.form.get('rql')
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   300
        if rql:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
            rset = self._exec(rql)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   302
        else:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   303
            rset = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   304
        vid = req.form.get('vid') or vid_from_rset(req, rset, self.schema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   305
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
            view = self.vreg.select_view(vid, req, rset)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   307
        except NoSelectableObject:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
            vid = req.form.get('fallbackvid', 'noresult')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
            view = self.vreg.select_view(vid, req, rset)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   310
        divid = req.form.get('divid', 'pageContent')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
        # we need to call pagination before with the stream set
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
        stream = view.set_stream()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
        if req.form.get('paginate'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
            if divid == 'pageContent':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
                # mimick main template behaviour
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
                stream.write(u'<div id="pageContent">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   317
                vtitle = self.req.form.get('vtitle')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
                if vtitle:
447
0e52d72104a6 pylint fixes
sylvain.thenault@logilab.fr
parents: 408
diff changeset
   319
                    stream.write(u'<h1 class="vtitle">%s</h1>\n' % vtitle)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
            view.pagination(req, rset, view.w, not view.need_navigation)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
            if divid == 'pageContent':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
                stream.write(u'<div id="contentmain">')
1723
30c3a713ab61 View.dispatch -> View.render
sylvain.thenault@logilab.fr
parents: 1713
diff changeset
   323
        view.render()
643
616191014b8b [jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 603
diff changeset
   324
        extresources = req.html_headers.getvalue(skiphead=True)
808
8d739f6e8ef5 JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 762
diff changeset
   325
        if extresources:
8d739f6e8ef5 JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 762
diff changeset
   326
            stream.write(u'<div class="ajaxHtmlHead">\n') # XXX use a widget ?
8d739f6e8ef5 JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 762
diff changeset
   327
            stream.write(extresources)
8d739f6e8ef5 JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 762
diff changeset
   328
            stream.write(u'</div>\n')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
        if req.form.get('paginate') and divid == 'pageContent':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
            stream.write(u'</div></div>')
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   331
        return stream.getvalue()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   333
    @xhtmlize
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   334
    def js_prop_widget(self, propkey, varname, tabindex=None):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   335
        """specific method for CWProperty handling"""
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   336
        entity = self.vreg.etype_class('CWProperty')(self.req, None, None)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   337
        entity.eid = varname
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   338
        entity['pkey'] = propkey
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   339
        form = self.vreg.select_object('forms', 'edition', self.req, None,
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   340
                                       entity=entity)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   341
        form.form_build_context()
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   342
        vfield = form.field_by_name('value')
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   343
        renderer = FormRenderer(self.req)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   344
        return vfield.render(form, renderer, tabindex=tabindex) \
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   345
               + renderer.render_help(form, vfield)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   347
    @xhtmlize
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   348
    def js_component(self, compid, rql, registry='components', extraargs=None):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   349
        if rql:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   350
            rset = self._exec(rql)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   351
        else:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   352
            rset = None
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   353
        comp = self.vreg.select_object(registry, compid, self.req, rset)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   354
        if extraargs is None:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   355
            extraargs = {}
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   356
        else: # we receive unicode keys which is not supported by the **syntax
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   357
            extraargs = dict((str(key), value)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   358
                             for key, value in extraargs.items())
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   359
        extraargs = extraargs or {}
1723
30c3a713ab61 View.dispatch -> View.render
sylvain.thenault@logilab.fr
parents: 1713
diff changeset
   360
        return comp.render(**extraargs)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   361
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   362
    @check_pageid
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   363
    @xhtmlize
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   364
    def js_inline_creation_form(self, peid, ttype, rtype, role):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   365
        view = self.vreg.select_view('inline-creation', self.req, None,
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   366
                                     etype=ttype, peid=peid, rtype=rtype,
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   367
                                     role=role)
1723
30c3a713ab61 View.dispatch -> View.render
sylvain.thenault@logilab.fr
parents: 1713
diff changeset
   368
        return view.render(etype=ttype, peid=peid, rtype=rtype, role=role)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   369
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   370
    @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
    def js_validate_form(self, action, names, values):
1527
c8ca1782e252 controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1467
diff changeset
   372
        return self.validate_form(action, names, values)
c8ca1782e252 controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1467
diff changeset
   373
c8ca1782e252 controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1467
diff changeset
   374
    def validate_form(self, action, names, values):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
        # XXX this method (and correspoding js calls) should use the new
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
        #     `RemoteCallFailed` mechansim
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        self.req.form = self._rebuild_posted_form(names, values, action)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
        vreg = self.vreg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   380
            ctrl = vreg.select(vreg.registry_objects('controllers', 'edit'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
                               req=self.req)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
        except NoSelectableObject:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
            return (False, {None: self.req._('not authorized')})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
            ctrl.publish(None, fromjson=True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
        except ValidationError, err:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
            self.req.cnx.rollback()
1857
46f975203f4f err.entity may only be an eid or None at this point
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1844
diff changeset
   388
            return (False, (err.entity, err.errors))
1798
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   389
        except Redirect, redir:
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   390
            return (True, redir.location)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
        except Exception, err:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
            self.req.cnx.rollback()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
            self.exception('unexpected error in js_validateform')
1759
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   394
            return (False, self.req._(str(err).decode('utf-8')))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
        return (False, '???')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   397
    @jsonize
1798
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   398
    def js_edit_field(self, action, names, values, rtype, eid, default):
1527
c8ca1782e252 controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1467
diff changeset
   399
        success, args = self.validate_form(action, names, values)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        if success:
1560
7dd2a81b8bc8 [basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1527
diff changeset
   401
            # Any X,N where we don't seem to use N is an optimisation
7dd2a81b8bc8 [basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1527
diff changeset
   402
            # printable_value won't need to query N again
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   403
            rset = self.req.execute('Any X,N WHERE X eid %%(x)s, X %s N' % rtype,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
                                    {'x': eid}, 'x')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
            entity = rset.get_entity(0, 0)
1795
abffc24f93c5 on inline form validation, also provide the 'unspecified' placeholder value for empty fields
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1760
diff changeset
   406
            value = entity.printable_value(rtype)
1798
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   407
            return (success, args, value or default)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   408
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
            return (success, args, None)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   410
1560
7dd2a81b8bc8 [basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1527
diff changeset
   411
    @jsonize
1759
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   412
    def js_edit_relation(self, action, names, values,
1798
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   413
                         rtype, role, eid, vid, default):
1759
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   414
        success, args = self.validate_form(action, names, values)
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   415
        if success:
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   416
            entity = self.req.eid_rset(eid).get_entity(0, 0)
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   417
            rset = entity.related(rtype, role)
1798
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   418
            if rset:
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   419
                output = self.view(vid, rset)
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   420
            else:
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   421
                output = default
cc86fe8efaaa pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1795
diff changeset
   422
            return (success, args, output)
1759
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   423
        else:
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   424
            return (success, args, None)
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   425
61d026ced19f preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1736
diff changeset
   426
    @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   427
    def js_i18n(self, msgids):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
        """returns the translation of `msgid`"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
        return [self.req._(msgid) for msgid in msgids]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   431
    @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
    def js_format_date(self, strdate):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
        """returns the formatted date for `msgid`"""
1380
aa1586de4563 missing required date format
sylvain.thenault@logilab.fr
parents: 1320
diff changeset
   434
        date = strptime(strdate, '%Y-%m-%d %H:%M:%S')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
        return self.format_date(date)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   437
    @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
    def js_external_resource(self, resource):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   439
        """returns the URL of the external resource named `resource`"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   440
        return self.req.external_resource(resource)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   441
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   442
    @check_pageid
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   443
    @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   444
    def js_user_callback(self, cbname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   445
        page_data = self.req.get_session_data(self.req.pageid, {})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   446
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
            cb = page_data[cbname]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
            return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
        return cb(self.req)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
    if HAS_SEARCH_RESTRICTION:
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   453
        @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
        def js_filter_build_rql(self, names, values):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   455
            form = self._rebuild_posted_form(names, values)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
            self.req.form = form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
            builder = FilterRQLBuilder(self.req)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   458
            return builder.build_rql()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   459
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   460
        @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   461
        def js_filter_select_content(self, facetids, rql):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
            rqlst = self.vreg.parse(self.req, rql) # XXX Union unsupported yet
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
            mainvar = prepare_facets_rqlst(rqlst)[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
            update_map = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   465
            for facetid in facetids:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
                facet = get_facet(self.req, facetid, rqlst.children[0], mainvar)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
                update_map[facetid] = facet.possible_values()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
            return update_map
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   470
    def js_unregister_user_callback(self, cbname):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   471
        self.req.unregister_callback(self.req.pageid, cbname)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   472
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   473
    def js_unload_page_data(self):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   474
        self.req.del_session_data(self.req.pageid)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   475
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   476
    def js_cancel_edition(self, errorurl):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   477
        """cancelling edition from javascript
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   478
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   479
        We need to clear associated req's data :
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   480
          - errorurl
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   481
          - pending insertions / deletions
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   482
        """
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   483
        self.req.cancel_edition(errorurl)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   484
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
    def js_delete_bookmark(self, beid):
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   486
        rql = 'DELETE B bookmarked_by U WHERE B eid %(b)s, U eid %(u)s'
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   487
        self.req.execute(rql, {'b': typed_eid(beid), 'u' : self.req.user.eid})
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   488
1844
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   489
    def js_node_clicked(self, treeid, nodeeid):
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   490
        """add/remove eid in treestate cookie"""
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   491
        from cubicweb.web.views.treeview import treecookiename
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   492
        cookies = self.req.get_cookie()
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   493
        statename = treecookiename(treeid)
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   494
        treestate = cookies.get(statename)
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   495
        if treestate is None:
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   496
            cookies[statename] = nodeeid
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   497
            self.req.set_cookie(cookies, statename)
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   498
        else:
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   499
            marked = set(filter(None, treestate.value.split(';')))
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   500
            if nodeeid in marked:
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   501
                marked.remove(nodeeid)
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   502
            else:
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   503
                marked.add(nodeeid)
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   504
            cookies[statename] = ';'.join(marked)
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   505
            self.req.set_cookie(cookies, statename)
ec51bf1b8be3 avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1839
diff changeset
   506
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   507
    def js_set_cookie(self, cookiename, cookievalue):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   508
        # XXX we should consider jQuery.Cookie
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   509
        cookiename, cookievalue = str(cookiename), str(cookievalue)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   510
        cookies = self.req.get_cookie()
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   511
        cookies[cookiename] = cookievalue
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   512
        self.req.set_cookie(cookies, cookiename)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   513
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   514
    # relations edition stuff ##################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
    def _add_pending(self, eidfrom, rel, eidto, kind):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
        key = 'pending_%s' % kind
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   518
        pendings = self.req.get_session_data(key, set())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   519
        pendings.add( (typed_eid(eidfrom), rel, typed_eid(eidto)) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   520
        self.req.set_session_data(key, pendings)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   521
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   522
    def _remove_pending(self, eidfrom, rel, eidto, kind):
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   523
        key = 'pending_%s' % kind
1713
d817f23439ba bix a bug: correct the sended parameter 'no need for id in the string parameter name'
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1635
diff changeset
   524
        pendings = self.req.get_session_data(key)
d817f23439ba bix a bug: correct the sended parameter 'no need for id in the string parameter name'
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1635
diff changeset
   525
        pendings.remove( (typed_eid(eidfrom), rel, typed_eid(eidto)) )
d817f23439ba bix a bug: correct the sended parameter 'no need for id in the string parameter name'
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1635
diff changeset
   526
        self.req.set_session_data(key, pendings)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   527
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   528
    def js_remove_pending_insert(self, (eidfrom, rel, eidto)):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   529
        self._remove_pending(eidfrom, rel, eidto, 'insert')
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   530
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   531
    def js_add_pending_inserts(self, tripletlist):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   532
        for eidfrom, rel, eidto in tripletlist:
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   533
            self._add_pending(eidfrom, rel, eidto, 'insert')
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   534
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   535
    def js_remove_pending_delete(self, (eidfrom, rel, eidto)):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   536
        self._remove_pending(eidfrom, rel, eidto, 'delete')
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   537
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   538
    def js_add_pending_delete(self, (eidfrom, rel, eidto)):
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   539
        self._add_pending(eidfrom, rel, eidto, 'delete')
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   540
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   541
    # XXX specific code. Kill me and my AddComboBox friend
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   542
    @jsonize
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   543
    def js_add_and_link_new_entity(self, etype_to, rel, eid_to, etype_from, value_from):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   544
        # create a new entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   545
        eid_from = self.req.execute('INSERT %s T : T name "%s"' % ( etype_from, value_from ))[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   546
        # link the new entity to the main entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
        rql = 'SET F %(rel)s T WHERE F eid %(eid_to)s, T eid %(eid_from)s' % {'rel' : rel, 'eid_to' : eid_to, 'eid_from' : eid_from}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   548
        return eid_from
603
18c6c31bbaf4 [controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 581
diff changeset
   549
18c6c31bbaf4 [controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 581
diff changeset
   550
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
class SendMailController(Controller):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
    id = 'sendmail'
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
   553
    __select__ = match_user_groups('managers', 'users')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   554
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   555
    def recipients(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   556
        """returns an iterator on email's recipients as entities"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   557
        eids = self.req.form['recipient']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   558
        # make sure we have a list even though only one recipient was specified
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   559
        if isinstance(eids, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   560
            eids = (eids,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
        rql = 'Any X WHERE X eid in (%s)' % (','.join(eids))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   562
        rset = self.req.execute(rql)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   563
        for entity in rset.entities():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   564
            entity.complete() # XXX really?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   565
            yield entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   569
    def smtp(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   570
        mailhost, port = self.config['smtp-host'], self.config['smtp-port']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   571
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   572
            return SMTP(mailhost, port)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   573
        except Exception, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   574
            self.exception("can't connect to smtp server %s:%s (%s)",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   575
                             mailhost, port, ex)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   576
            url = self.build_url(__message=self.req._('could not connect to the SMTP server'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   577
            raise Redirect(url)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   578
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   579
    def sendmail(self, recipient, subject, body):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   580
        helo_addr = '%s <%s>' % (self.config['sender-name'],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   581
                                 self.config['sender-addr'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   582
        msg = format_mail({'email' : self.req.user.get_email(),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   583
                           'name' : self.req.user.dc_title(),},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   584
                          [recipient], body, subject)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   585
        self.smtp.sendmail(helo_addr, [recipient], msg.as_string())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   586
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   587
    def publish(self, rset=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   588
        # XXX this allow anybody with access to an cubicweb application to use it as a mail relay
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   589
        body = self.req.form['mailbody']
1467
972517be96dc sendmail form should now work as before
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1419
diff changeset
   590
        subject = self.req.form['subject']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   591
        for recipient in self.recipients():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   592
            text = body % recipient.as_email_context()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   593
            self.sendmail(recipient.get_email(), subject, text)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   594
        # breadcrumbs = self.req.get_session_data('breadcrumbs', None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   595
        url = self.build_url(__message=self.req._('emails successfully sent'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   596
        raise Redirect(url)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   598
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   599
class MailBugReportController(SendMailController):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
    id = 'reportbug'
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
   601
    __select__ = yes()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   602
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   603
    def publish(self, rset=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   604
        body = self.req.form['description']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   605
        self.sendmail(self.config['submit-mail'], _('%s error report') % self.config.appid, body)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   606
        url = self.build_url(__message=self.req._('bug report sent'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   607
        raise Redirect(url)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   608