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