Exemplo n.º 1
0
def test_best_match():
    accept = Accept("text/html, foo/bar")
    assert accept.best_match(["text/html", "foo/bar"]) == "text/html"
    assert accept.best_match(["foo/bar", "text/html"]) == "foo/bar"
    assert accept.best_match([("foo/bar", 0.5), "text/html"]) == "text/html"
    assert accept.best_match([("foo/bar", 0.5), ("text/html", 0.4)]) == "foo/bar"
    assert_raises(ValueError, accept.best_match, ["text/*"])
Exemplo n.º 2
0
 def generate_response(self, environ, start_response):
     if self.content_length is not None:
         del self.content_length
     headerlist = list(self.headerlist)
     accept_value = environ.get('HTTP_ACCEPT', '')
     accept = Accept(accept_value)
     match = accept.best_match(['application/json', 'text/html',
                                'text/plain'], default_match='text/plain')
     if match == 'text/html':
         content_type = 'text/html'
         body = self.html_body(environ)
     elif match == 'application/json':
         content_type = 'application/json'
         body = self.json_body(environ)
     else:
         content_type = 'text/plain'
         body = self.plain_body(environ)
     extra_kw = {}
     if isinstance(body, text_type):
         extra_kw.update(charset='utf-8')
     resp = Response(body,
         status=self.status,
         headerlist=headerlist,
         content_type=content_type,
         **extra_kw
     )
     resp.content_type = content_type
     return resp(environ, start_response)
Exemplo n.º 3
0
def test_accept_match():
    accept = Accept('Content-Type', 'text/html')
    #FIXME: Accept._match should be standalone function _match that is
    # attached as Accept._match during Accept.__init__.
    assert accept._match('*', 'text/html')
    assert accept._match('text/html', 'text/html')
    assert accept._match('TEXT/HTML', 'text/html')
    assert not accept._match('foo/bar', 'text/html')
Exemplo n.º 4
0
def test_best_match_with_complex_q():
    accept = Accept("text/html, foo/bar;q=0.55, baz/gort;q=0.59")
    assert accept.best_match(["text/html", "foo/bar"]) == "text/html"
    accept = Accept("text/html;q=0.5, foo/bar;q=0.586, baz/gort;q=0.5966")
    assert "baz/gort;q=0.597" in str(accept)
    assert "foo/bar;q=0.586" in str(accept)
    assert "text/html;q=0.5" in str(accept)
    assert accept.best_match(["text/html", "baz/gort"]) == "baz/gort"
Exemplo n.º 5
0
def test_best_match():
    accept = Accept('Content-Type', 'text/html, foo/bar')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    assert accept.best_match(['foo/bar', 'text/html']) == 'foo/bar'
    assert accept.best_match([('foo/bar', 0.5),
                              'text/html']) == 'text/html'
    assert accept.best_match([('foo/bar', 0.5),
                              ('text/html', 0.4)]) == 'foo/bar'
    assert_raises(ValueError, accept.best_match, ['text/*'])
Exemplo n.º 6
0
    def lookup_template_engine(self, tgl):
        """Return the template engine data.

        Provides a convenience method to get the proper engine,
        content_type, template, and exclude_names for a particular
        tg_format (which is pulled off of the request headers).

        """
        request = tgl.request
        response = tgl.response

        try:
            render_custom_format = request._render_custom_format[self.controller]
        except:
            render_custom_format = self.render_custom_format

        if render_custom_format:
            (content_type, engine, template, exclude_names, render_params
                ) = self.custom_engines[render_custom_format]
        else:
            if self.default_engine:
                content_type = self.default_engine
            elif self.engines:
                if request._response_type and request._response_type in self.engines:
                    accept_types = request._response_type
                else:
                    accept_types = request.headers.get('accept', '*/*')
                content_type = Accept(accept_types).best_match(self.engines_keys, self.engines_keys[0])
            else:
                content_type = 'text/html'

            # check for overridden content type from the controller call
            try:
                controller_content_type = response.headers['Content-Type']
                # make sure we handle content_types like 'text/html; charset=utf-8'
                content_type = controller_content_type.split(';')[0]
            except KeyError:
                pass

            # check for overridden templates
            try:
                cnt_override_mapping = request._override_mapping[self.controller]
                engine, template, exclude_names, render_params = cnt_override_mapping[content_type.split(";")[0]]
            except (AttributeError, KeyError):
                (engine, template, exclude_names, render_params
                    ) = self.engines.get(content_type, (None,) * 4)

        if 'charset' not in content_type and (content_type.startswith('text')
                or  content_type in ('application/xhtml+xml',
                        'application/xml', 'application/json')):
            content_type += '; charset=utf-8'

        return content_type, engine, template, exclude_names, render_params
Exemplo n.º 7
0
    def offer_sort_key(value):
        """
        (type_weight, params_weight)

        type_weight:
            - index of specific ``type/subtype`` in order list
            - ``max_weight * 2`` if no match is found

        params_weight:
            - index of specific ``type/subtype;params`` in order list
            - ``max_weight`` if not found
            - ``max_weight + 1`` if no params at all

        """
        parsed = Accept.parse_offer(value)

        type_w = find_order_index(
            parsed.type + '/' + parsed.subtype, max_weight
        )

        if parsed.params:
            param_w = find_order_index(value, max_weight)

        else:
            param_w = max_weight + 1

        return (type_w, param_w)
Exemplo n.º 8
0
def test_best_matches_with_fallback():
    accept = Accept('Content-Type', 'text/html, foo/bar')
    assert accept.best_matches('xxx/yyy') == ['text/html',
                                              'foo/bar',
                                              'xxx/yyy']
    accept = Accept('Content-Type', 'text/html;q=0.5, foo/bar')
    assert accept.best_matches('xxx/yyy') == ['foo/bar',
                                              'text/html',
                                              'xxx/yyy']
    assert accept.best_matches('foo/bar') == ['foo/bar']
    assert accept.best_matches('text/html') == ['foo/bar', 'text/html']
Exemplo n.º 9
0
def test_nil_add():
    nilaccept = NilAccept()
    accept = Accept('text/html')
    assert nilaccept + accept is accept
    new_accept = nilaccept + nilaccept
    assert isinstance(new_accept, accept.__class__)
    assert new_accept.header_value == ''
    new_accept = nilaccept + 'foo'
    assert isinstance(new_accept, accept.__class__)
    assert new_accept.header_value == 'foo'
Exemplo n.º 10
0
def test_accept_match_lang():
    accept = Accept('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7')
    #FIXME: Accept._match_lang should be standalone function _match_lang
    # that is attached as Accept._match during Accept.__init__.
    assert accept._match('*', 'da')
    assert accept._match('da', 'DA')
    assert accept._match('en', 'en-gb')
    assert accept._match('en-gb', 'en-gb')
    assert not accept._match('en-gb', 'fr-fr')
Exemplo n.º 11
0
    def test_unknown_laguage(self):
        from pyramid.httpexceptions import HTTPInternalServerError
        from c2cgeoportal.views.fulltextsearch import FullTextSearchView
        from webob.acceptparse import Accept

        request = self._create_dummy_request()
        request.registry.settings["default_locale_name"] = "it"
        request.accept_language = Accept("es")
        fts = FullTextSearchView(request)
        response = fts.fulltextsearch()
        self.assertTrue(isinstance(response, HTTPInternalServerError))
Exemplo n.º 12
0
Arquivo: views.py Projeto: edsu/id
def concept(request, lccn):
    concept = get_object_or_404(m.Concept, lccn=lccn)
    host = request.get_host()

    accept = Accept('Accept', request.META.get('HTTP_ACCEPT', 'text/html'))
    best_match = accept.best_match(['text/html', 'application/rdf+xml']) 
    if best_match == 'application/rdf+xml':
        r = concept_rdf(request, lccn)
    else:
        alt_labels = concept.alternate_labels.all().order_by('text')
        broader = concept.broader.all().order_by('pref_label')
        extra_broader = concept.extra_broader()
        narrower = concept.narrower.all().order_by('pref_label')
        related = concept.related.all().order_by('pref_label')
        r = render_to_response('concept.html', dictionary=locals(), 
                               context_instance=RequestContext(request))
    # let downstream caches know that the resonse can vary depending 
    # on the Accept header that the client sent 
    r['Vary'] = 'Accept' 
    return r
Exemplo n.º 13
0
def test_best_match():
    accept = Accept('text/html, foo/bar')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    assert accept.best_match(['foo/bar', 'text/html']) == 'foo/bar'
    assert accept.best_match([('foo/bar', 0.5),
                              'text/html']) == 'text/html'
    assert accept.best_match([('foo/bar', 0.5),
                              ('text/html', 0.4)]) == 'foo/bar'
    with pytest.raises(ValueError):
        accept.best_match(['text/*'])
Exemplo n.º 14
0
    def test_compresses_streaming(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=Accept("gzip"))
        response = Response(app_iter=iter([decompressed_body]))

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length is None
        assert response.body == compressed_body
Exemplo n.º 15
0
def test_first_match():
    accept = Accept('Content-Type', 'text/html, foo/bar')
    assert accept.first_match(['text/html', 'foo/bar']) == 'text/html'
    assert accept.first_match(['foo/bar', 'text/html']) == 'foo/bar'
    assert accept.first_match(['xxx/xxx', 'text/html']) == 'text/html'
    assert accept.first_match(['xxx/xxx']) == 'xxx/xxx'
    assert accept.first_match([None, 'text/html']) is None
    assert accept.first_match(['text/html', None]) == 'text/html'
    assert accept.first_match(['foo/bar', None]) == 'foo/bar'
    assert_raises(ValueError, accept.first_match, [])
Exemplo n.º 16
0
def concept(request, lccn):
    concept = get_object_or_404(m.Concept, lccn=lccn)
    host = request.get_host()

    accept = Accept('Accept', request.META.get('HTTP_ACCEPT', 'text/html'))
    best_match = accept.best_match(['text/html', 'application/rdf+xml'])
    if best_match == 'application/rdf+xml':
        r = concept_rdf(request, lccn)
    else:
        alt_labels = concept.alternate_labels.all().order_by('text')
        broader = concept.broader.all().order_by('pref_label')
        extra_broader = concept.extra_broader()
        narrower = concept.narrower.all().order_by('pref_label')
        related = concept.related.all().order_by('pref_label')
        r = render_to_response('concept.html',
                               dictionary=locals(),
                               context_instance=RequestContext(request))
    # let downstream caches know that the resonse can vary depending
    # on the Accept header that the client sent
    r['Vary'] = 'Accept'
    return r
Exemplo n.º 17
0
def test_nil_add():
    nilaccept = NilAccept('Connection-Close')
    accept = Accept('Content-Type', 'text/html')
    assert nilaccept + accept is accept
    new_accept = nilaccept + nilaccept
    assert isinstance(new_accept, accept.__class__)
    assert new_accept.header_name == 'Connection-Close'
    assert new_accept.header_value == ''
    new_accept = nilaccept + 'foo'
    assert isinstance(new_accept, accept.__class__)
    assert new_accept.header_name == 'Connection-Close'
    assert new_accept.header_value == 'foo'
Exemplo n.º 18
0
def test_best_matches():
    accept = Accept('Content-Type', 'text/html, foo/bar')
    assert accept.best_matches() == ['text/html', 'foo/bar']
    accept = Accept('Content-Type', 'text/html, foo/bar;q=0.5')
    assert accept.best_matches() == ['text/html', 'foo/bar']
    accept = Accept('Content-Type', 'text/html;q=0.5, foo/bar')
    assert accept.best_matches() == ['foo/bar', 'text/html']
Exemplo n.º 19
0
 def _purge_old(self):
     """ Delete any task on DB that has been completed for more than 3 days
     """
     query_param = '{"literal":"(now()-finished_at) > \'P0Y0M3DT0H0M0S\'"}'
     url = self.base_url + '?$$=' + query_param
     params = {'$$': query_param}
     request = DummyRequest(path=url, params=params)
     request.method = 'DELETE'
     from webob.acceptparse import Accept
     request.accept = Accept("application/json")
     request.matchdict = {'base': self.basename}
     doc_view = DocumentCustomView(DocumentContextFactory(request), request)
     response = doc_view.delete_collection()
Exemplo n.º 20
0
def test_best_match_with_complex_q():
    accept = Accept('text/html, foo/bar;q=0.55, baz/gort;q=0.59')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    accept = Accept('text/html;q=0.5, foo/bar;q=0.586, baz/gort;q=0.5966')
    assert "baz/gort;q=0.597" in str(accept)
    assert "foo/bar;q=0.586" in str(accept)
    assert "text/html;q=0.5" in str(accept)
    assert accept.best_match(['text/html', 'baz/gort']) == 'baz/gort'
Exemplo n.º 21
0
    def from_settings(cls, settings):
        """Create a new instance based on the `settings` dict.

        The locales supported by the application are read from the
        "locales" key. The format of the value is the same as that of
        the Accept-Language header, i.e.:

          <locale1>;q=<q1>,<locale2>;q=<q2>,...

        The q-values indicate the quality of the locale, but are
        optional.
        """
        locales = list(Accept.parse(settings.get("locales", "")))
        return cls(locales)
Exemplo n.º 22
0
    def process(self, environ, start_response):
        self._begin = datetime.datetime.now()
        if self._debug:
            open("/tmp/cog_sql", "w")
        try:
            self.clear()
            self._environ = environ
#            open("/tmp/cog_tmp_xxx", "w").write("{}".format(environ))
            self._script_name = self._environ.get('SCRIPT_NAME', '')
            self._server_name = self._environ['HTTP_HOST']
            self._url_scheme = self._environ['wsgi.url_scheme']
            self._url = "{}://{}{}".format(
                self._url_scheme, self._server_name, self._script_name)
            self._path_info = self._environ['PATH_INFO'].lower()
            alnum = "[a-z0-9]{4}"
            oid_pattern = '^/{}{}-{}-{}-{}-{}{}{}$'.format(*(alnum,)*8)
            if re.match(oid_pattern, self._path_info):
                obj = self.db.get_elt_by_oid(self._path_info[1:])
                self.add_json_res({'#page_ref':self.get_page_ref()})
                self.direct_obj_ref = obj.w3display
            self.load_site()
            self.__request = Request(environ)
            self.__lang = Accept(str(self.__request.accept_language))
            self.i18n = gettext.translation(
                'messages', '/usr/share/collorg/locale',
                [self.__lang.best_match(('en', 'fr', 'fr-FR')) or 'en'])
            if WebController.__static_body is None:
                WebController.__tags = self.__get_tags()
                WebController.__static_body = self.__get_static_body()
            self.__response = Response()
            self.__reset()
            if not(self._cog_method is None and '#page_ref' in self._json_res):
                    self.add_json_res({'#page_ref':self.get_page_ref()})
            self.__response.unicode_body = self._unicode(self.__exec())
            if(self.__response.content_type != 'text/html' or
                self.__response.status_int != 200):
                # Not an HTML response, we don't want to
                # do anything to it
                return self.__response(environ, start_response)
            # Make sure the content isn't gzipped:
            self.__response.decode_content()
            return self.__response(environ, start_response)
        except Exception as err:
            if self.db._cog_params['debug']:
                response = Response()
                response.unicode_body = self._unicode(err)
                return response(environ, start_response)
            if raise_error:
                raise err
Exemplo n.º 23
0
def create_dummy_request(additional_settings=None,
                         authentication=True,
                         user=None,
                         *args,
                         **kargs):
    if additional_settings is None:
        additional_settings = {}
    from c2cgeoportal_geoportal import default_user_validator
    from c2cgeoportal_geoportal import create_get_user_from_request
    from c2cgeoportal_geoportal.lib.authentication import create_authentication
    request = tests.create_dummy_request(
        {
            "host_forward_host": [],
            "mapserverproxy": {
                "default_ogc_server": "__test_ogc_server",
                "external_ogc_server": "__test_external_ogc_server",
            },
            "functionalities": {
                "registered": {},
                "anonymous": {},
                "available_in_templates": []
            },
            "layers": {
                "geometry_validation": True
            }
        }, *args, **kargs)
    request.accept_language = Accept("fr-CH,fr;q=0.8,en;q=0.5,en-US;q=0.3")
    request.registry.settings.update(additional_settings)
    request.referer = "http://example.com/app"
    request.path_info_peek = lambda: "main"
    request.interface_name = "main"
    request.get_user = _get_user
    request.registry.validate_user = default_user_validator
    request.client_addr = None
    request.c2c_request_id = 'test'
    if authentication and user is None:
        request._get_authentication_policy = lambda: create_authentication(
            {
                "authtkt_cookie_name": "__test",
                "authtkt_secret": "123",
            })
    elif user is not None:
        config.testing_securitypolicy(user)
    request.set_property(create_get_user_from_request(
        {"authorized_referers": [request.referer]}),
                         name="user",
                         reify=True)
    return request
Exemplo n.º 24
0
    def test_compresses_non_streaming(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=Accept("gzip"))
        response = Response(body=decompressed_body)
        response.md5_etag()

        original_etag = response.etag

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length == len(compressed_body)
        assert response.body == compressed_body
        assert response.etag != original_etag
Exemplo n.º 25
0
    def lookup_template_engine(self, tgl):
        """Return the template engine data.

        Provides a convenience method to get the proper engine,
        content_type, template, and exclude_names for a particular
        tg_format (which is pulled off of the request headers).

        """
        request = tgl.request
        response = tgl.response

        try:
            render_custom_format = request._render_custom_format[self.controller]
        except:
            render_custom_format = self.render_custom_format

        if render_custom_format:
            (content_type, engine, template, exclude_names, render_params
                ) = self.custom_engines[render_custom_format]
        else:
            if self.default_engine:
                content_type = self.default_engine
            elif self.engines:
                if response.content_type is not None:
                    # Check for overridden content type from the controller call
                    accept_types = response.content_type
                elif request._response_type and request._response_type in self.engines:
                    # Check for content type detected by request extensions
                    accept_types = request._response_type
                else:
                    accept_types = request.headers.get('accept', '*/*')
                content_type = Accept(accept_types).best_match(self.engines_keys, self.engines_keys[0])
            else:
                content_type = 'text/html'

            # check for overridden templates
            try:
                cnt_override_mapping = request._override_mapping[self.controller]
                engine, template, exclude_names, render_params = cnt_override_mapping[content_type.split(";")[0]]
            except (AttributeError, KeyError):
                (engine, template, exclude_names, render_params
                    ) = self.engines.get(content_type, (None,) * 4)

        return content_type, engine, template, exclude_names, render_params
Exemplo n.º 26
0
def test_quality():
    accept = Accept("text/html")
    assert accept.quality("text/html") == 1
    accept = Accept("text/html;q=0.5")
    assert accept.quality("text/html") == 0.5
Exemplo n.º 27
0
def normalize_accept_offer(offer):
    return str(Accept.parse_offer(offer))
Exemplo n.º 28
0
def normalize_accept_offer(offer, allow_range=False):
    if allow_range and '*' in offer:
        return offer.lower()
    return str(Accept.parse_offer(offer))
Exemplo n.º 29
0
def test_accept_str_with_q_not_1_multiple():
    value = 'text/html;q=0.5, foo/bar'
    accept = Accept(value)
    assert str(accept) == value
Exemplo n.º 30
0
def test_zero_quality():
    assert Accept('bar, *;q=0').best_match(['foo']) is None
    assert 'foo' not in Accept('*;q=0')
Exemplo n.º 31
0
def test_accept_repr():
    accept = Accept('text/html')
    assert repr(accept) == "<Accept('text/html')>"
Exemplo n.º 32
0
def test_init_accept_accept_charset_with_iso_8859_1():
    accept = Accept('iso-8859-1')
    assert accept._parsed == [('iso-8859-1', 1)]
Exemplo n.º 33
0
class WebController(Controller):
    """
    Web controller.
    """
    __static_body = None
    __topics = {} # topics are loaded on demand
    __site = None
    _d_topics = {} # all topics are loaded once!
    __main_topic = None
    def __init__(self, config_file):
        self.__lang = None
        self.i18n = None
        self.direct_obj_ref = None
        self.cog_exec_env = None
        self.__env_set = False
        self.__nsession = False
        self._begin = None
        self.__cog_target = None
        self._cog_raw = None
        self._cog_ajax = None
        self._script_name = None
        self._server_name = None
        self._url_scheme = None
        self._environ = None
        self.__request = None
        self._session_key = None
        self.__session = None
        self._user = None
        self.__cog_environment = None
        self.__get_request = None
        self.cog_ref_obj = {}
        Controller.__init__(self, config_file)
        self.cache = Client(self)
        self.cache.check()

    def clear(self):
        self.direct_obj_ref = None
        self.cog_exec_env = None
        self.__env_set = False
        self.__nsession = False
        self.__cog_target = None
        self._cog_raw = None
        self._cog_ajax = None
        self._environ = None
        self._user = None
        self.__request = None
        self._session_key = None
        self.__session = None
        self.__get_request = None
        self._json_res = {}
        self.cog_ref_obj = {}
        Controller.clear(self)

    @property
    def env_set(self):
        return self.__env_set

    def __rt(self, template_name):
        """
        reads the templates and returns it's content
        """
        params = self.db._cog_params
        tp = params['templates_path']
        return open("%s/%s.html"%(tp, template_name)).read()

    def __get_tags(self):
        return self.db.table('collorg.communication.tag').search_links()

    def __get_important_posts(self):
        return self.db.table(
            'collorg.communication.blog.view.by_post').important()

    @property
    def new_session(self):
        self.__nsession = True
        self._session_key = uuid.uuid4()
        return self._session_key

    def __get_static_body(self):
        self.db.table('collorg.core.base_table')
        dt = {} # templates dictionary
        params = self.db._cog_params
        html = self.__rt("html")
        dt['head'] = self.__rt("head")
        dt['title'] = self.db.name
        dt['style'] = self.__rt("style")
        dt['javascript'] = self.__rt("javascript")
        dt['head'] = dt['head'] % (
            self._charset, dt['style'], params['icon'], dt['title'],
            dt['javascript'])
        dt['ariadne'] = self.__rt("ariadne")
        dt['top_search'] = self.__rt("top_search")
        dt['body'] = self.__rt("body")
        dt['header'] = self.__rt("header")
        dt['homepage_link'] = self.__rt("homepage_link")
        dt['homepage_link'] = "" #dt['homepage_link'] % (self._script_name)
        dt['search'] = self.__rt("search")
        dt['login_zone'] = self.__rt("login_zone")
        dt['header'] %= (
            dt['homepage_link'], dt['login_zone'])
        dt['login_container'] = self.__rt("login_container")
        dt['body_content'] = self.__rt("body_content")
        dt['footer'] = self.__rt("footer")
        dt['collorg_link'] = self.__rt("collorg_link")
        dt['footer'] %= (dt['collorg_link'])
        dt['body'] %= (
            dt['ariadne'],
            dt['top_search'],
            dt['header'], dt['login_container'],
            dt['body_content'],
            dt['footer'])
        dt['debug'] = self.__rt("debug")
        dt['collorg_link'] = self.__rt("collorg_link")
        html %= (dt['head'].decode(self._charset),
                 dt['body'].decode(self._charset))
        return html

    def __pfce(self, obj):
        return html_escape(str(pformat(obj)))

    def __oid_req(self, **kwargs):
        """
        The controller received a cog_oid. The object representing one
        and only one elt of the table is instanciated.

        @returns: the method cog_method applied to this object.
        @called by: self.__exec
        """
        if not '_cog_fqtn_' in self.__dict__ or not self._cog_fqtn_:
            obj = self.db.get_elt_by_oid(self._cog_oid_)
        else:
            obj = self.db.table(self._cog_fqtn_)
            obj.cog_oid_.value = self._cog_oid_
            obj.get()
        self.check_action(obj, self._cog_method)
        self.cog_exec_env = obj.get_environment()
        if (self._user is None and obj._is_cog_post and
            obj.visibility_.value == 'public' and
            self._cog_method == 'w3display'):
                cache = obj._cog_get_cache('w3display')
                if cache is not None:
                    return cache
        return eval("obj.{}(**kwargs)".format(self._cog_method))

    def __fqtn_req(self, **kwargs):
        """
        No cog_oid received by the controller but a cog_fqtn.

        @returns: the method referenced by cog_method applied to the object
        instantiated without intention (aka the whole table).
        @called by: self.__exec
        """
        obj = self.db.table(self._cog_fqtn_)
        self.check_action(obj, self._cog_method)
        return eval("obj.{}(**kwargs)".format(self._cog_method))

    def load_site(self):
        #XXX REFACTOR
        path_info = self._path_info
        if self.direct_obj_ref:
            path_info = '/'
        url = "{}{}".format(
            self._environ['HTTP_HOST'], self._environ.get('SCRIPT_NAME', ''))
        site = self.db.table("collorg.web.site")
        if not (url, path_info) in self.__topics:
            topic, w3display_topic  = site.load_topic(
                url, path_info)
            if self.__site is None:
                self.__site = site.get()
            self.__topics[(url, path_info)] = (w3display_topic, topic)
###        self._d_topics = self.__site.load_topics()
        topic = self.__topics[(url, path_info)][1]
        self._cog_cmd = self.__topics[(url, path_info)][0]
        return topic

    @property
    def site(self):
        return self.__site

    @property
    def main_topic(self):
        # Post._w3get_recipient
        if not self.__main_topic:
            self.__main_topic = self.__site.load_topic()[0]
        return self.__main_topic

    @property
    def _session(self):
        return self._session_key

    @property
    def user(self):
        return self._user

    @property
    def cog_environment(self):
        return self.__cog_environment

    def __dumps_json_res(self):
        res = {}
        for key, val in self._json_res.items():
            res[key] = {'content':val, 'action':'html'}
        return json.dumps(res)

    def __get_query_params(self):
        qs = {}
        for query in self.__request.POST, self.__request.GET:
            for key, val in query.items():
                key = key.encode(self._charset)
                val = val.encode(self._charset)
                if key[-2:] == '[]':
                    key = key[:-2]
                    if key not in qs:
                        qs[key] = []
                    qs[key].append(val)
                else:
                    qs[key] = val
        return qs

    def set_ref_obj_oid(self, obj_oid, ref_obj_oid):
        if obj_oid:
            self.cache.set_(
                "ref_obj_oid", self._session_key, (obj_oid, ref_obj_oid))

    def set_env(self, env):
        if not self._session_key:
            return
        self.__env_set = True
        if self.__nsession:
            self.cache.set_("user", self._session_key, None)
            self.cache.set_(
                "creat_date", self._session_key, self._begin)
        self.cache.set_(
            "last_access_date", self._session_key, self._begin)

    def __get_env(self):
        if self._session_key:
            self.cache.get_("last_access_date", self._session_key)
            self.cache.set_(
                "last_access_date", self._session_key, self._begin)
            self.get_user()

    def get_user(self):
        if self._session_key:
            user_oid = self.cache.get_("user", self._session_key)
            if user_oid:
                self._user = self.db.table(
                    'collorg.actor.user', cog_oid_ = user_oid).get()
            else:
                self.delete_cookie('cog_session')

    def set_user(self, user_oid = None):
        self.cache.set_("user", self._session_key, user_oid)

    def del_user(self, key):
        self.cache.del_("user", key)

    def __get_params(self):
        self._kwargs = self.__get_query_params()
        self._cog_ajax = self._kwargs.get('cog_ajax')
        self.__cog_target = self._kwargs.get('cog_target')
        self._cog_raw = self._kwargs.get('cog_raw', None)
        self._cog_method = self._kwargs.get('cog_method', None)
        # cog_method must not contain non-word caracters
        assert self._cog_method is None or \
            re.search('\W', self._cog_method) is None
        if self._cog_method is not None and self._cog_method[0] == '_':
            # we never should receive a protected method...
            self._cog_method = "w3error"
            self._kwargs['cog_method'] = "w3error"
            self._kwargs['cog_error'] = "Can't call a protected method!"
        self._cog_ref_oid = self._kwargs.get('cog_ref_oid', None)
        self._cog_oid_ = self._kwargs.get('cog_oid_', None)
        self._session_key = None
        if 'HTTP_COOKIE' in self._environ:
            cookie_string = self._environ.get('HTTP_COOKIE')
            cookie = SimpleCookie()
            cookie.load(cookie_string)
            if 'cog_session' in cookie:
                self._session_key = cookie['cog_session'].value
        self.__cog_environment = self.__get_env()
        self._cog_fqtn_ = self._kwargs.get('cog_fqtn_', None)
        if self._cog_ref_oid and self._cog_ref_oid == self._cog_oid_:
            self._cog_oid_ = None
        self._kwargs['cog_controller'] = self
        self._kwargs['cog_first_call'] = True

    def __reset(self):
        """
        Some things that are always done first
        """
        self._json_res['#cog_debug_link'] = ''
        self._json_res['#cog_debug'] = ''
        self.__get_params()
        self.db._set_controller(self)
        #self._cog_cmd = self.__environment

    def get_page_ref(self, method = None, oid = None):
        method = method or self._cog_method
        oid = oid or self._cog_oid_
        if oid:
            ref = oid
        else:
            env = self._environ
            if env and 'QUERY_STRING' in env:
                ref = md5.new(env['QUERY_STRING']).hexdigest()
            else:
                ref = self.__site.cog_oid_.value
        page_ref = "{}-{}".format(method, ref)
        return page_ref

    def __exec(self):
        """
        constructs the call to the template.
        the template receives
        depends on the arguments passed.
        Case of ajax:
        * one of the three following arguments is expected
        ** a path (specific wsgi)
        ** an oid
        ** a fqtn
        """
        _ = self.i18n.gettext
        i18n_drag_drop = _(
            "Drag & drop your links here<br>for future reference")
        i18n_drop_drag = _(
            "Drag & drop the link from the cart to \n"
            "the page/textarea to add a reference.\n"
            "(write access is mandatory)")
        if self._cog_oid_:
            self._cog_cmd = self.__oid_req
        elif self._cog_fqtn_:
            self._cog_cmd = self.__fqtn_req
        elif self._cog_ajax is None and self.direct_obj_ref:
            self._cog_cmd = self.direct_obj_ref
        if self._cog_ajax is None:
            actor = self.db.table('collorg.actor.user')
            result = self.__static_body.format(
                self.__site.w3map_link(),
                _("search"), _("search"),
                _("by tag"), _("by user"),
                actor.w3login_link(),
                _(i18n_drop_drag).decode(self._charset),
                _(i18n_drag_drop).decode(self._charset))
            page = self._cog_cmd(**self._kwargs)
            if self._cog_raw:
                return page
            result %= (
                "", #self._json_res['#cog_top_nav_ariadne']),
                self._unicode(page))
            return result
        # ajax
        try:
            result = self._cog_cmd(**self._kwargs)
            if self._cog_raw:
                return result
            duration = datetime.datetime.now() - self._begin
        except Exception as err:
#            open("/tmp/cog_error_log", "a+").write("{}\n".format(err))
            if self.db._cog_params['debug']:
                link, error = self.debug(err.__str__)
                self._json_res['#cog_debug_link'] = link
                self._json_res['#cog_debug'] = self._unicode(error)
                return self.__dumps_json_res()
            if raise_error:
                raise "Web controller error: {}".format(err)
        self._json_res['#cog_duration'] = "%s" % (duration)
        if self._debug:
            link, debug = self.debug()
            self._json_res['#cog_debug_link'] = link
            self._json_res['#cog_debug'] = self._unicode(debug)
        self._json_res[self.__cog_target] = result
        return self.__dumps_json_res()

    def get_cookie(self, key):
        return self._session_key

    def delete_cookie(self, key):
        self.__response.delete_cookie(key)

    def set_cookie(self, key):
        value = self.new_session
        self.__response.set_cookie(key, bytes(value))
        return value

    def process(self, environ, start_response):
        self._begin = datetime.datetime.now()
        if self._debug:
            open("/tmp/cog_sql", "w")
        try:
            self.clear()
            self._environ = environ
#            open("/tmp/cog_tmp_xxx", "w").write("{}".format(environ))
            self._script_name = self._environ.get('SCRIPT_NAME', '')
            self._server_name = self._environ['HTTP_HOST']
            self._url_scheme = self._environ['wsgi.url_scheme']
            self._url = "{}://{}{}".format(
                self._url_scheme, self._server_name, self._script_name)
            self._path_info = self._environ['PATH_INFO'].lower()
            alnum = "[a-z0-9]{4}"
            oid_pattern = '^/{}{}-{}-{}-{}-{}{}{}$'.format(*(alnum,)*8)
            if re.match(oid_pattern, self._path_info):
                obj = self.db.get_elt_by_oid(self._path_info[1:])
                self.add_json_res({'#page_ref':self.get_page_ref()})
                self.direct_obj_ref = obj.w3display
            self.load_site()
            self.__request = Request(environ)
            self.__lang = Accept(str(self.__request.accept_language))
            self.i18n = gettext.translation(
                'messages', '/usr/share/collorg/locale',
                [self.__lang.best_match(('en', 'fr', 'fr-FR')) or 'en'])
            if WebController.__static_body is None:
                WebController.__tags = self.__get_tags()
                WebController.__static_body = self.__get_static_body()
            self.__response = Response()
            self.__reset()
            if not(self._cog_method is None and '#page_ref' in self._json_res):
                    self.add_json_res({'#page_ref':self.get_page_ref()})
            self.__response.unicode_body = self._unicode(self.__exec())
            if(self.__response.content_type != 'text/html' or
                self.__response.status_int != 200):
                # Not an HTML response, we don't want to
                # do anything to it
                return self.__response(environ, start_response)
            # Make sure the content isn't gzipped:
            self.__response.decode_content()
            return self.__response(environ, start_response)
        except Exception as err:
            if self.db._cog_params['debug']:
                response = Response()
                response.unicode_body = self._unicode(err)
                return response(environ, start_response)
            if raise_error:
                raise err

    def debug(self, error = None):
        output = []
        css_style = 'debug hidden toggle'
        css_error = ''
        if error:
            css_style += ' debug_error'
            css_error = 'debug_error'
        title = error and "Error" or "debug"
        link = ( '<span class="link toggle %s" '
            'to_toggle="#cog_debug">%s</span>' % (css_error, title))
        if error:
            output.append('<h2>Error</h2>')
            output.append('<pre>%s</pre>' % (self.__pfce(error)))
            output.append("<h2>Traceback</h2>")
            output.append("<pre>%s</pre>"%(
                traceback.format_exc()))
        output.append('<h2>Main variables</h2>')
        output.append('<pre>')
        output.append('cog_method = %s\n' % (self._cog_method))
        output.append('cog_fqtn_ = %s\n' % (self._cog_fqtn_))
        output.append('cog_oid_ = %s\n' % (self._cog_oid_))
        output.append('cog_ref_oid = %s\n' % (self._cog_ref_oid))
        output.append('</pre>')
        if error:
            post_error = self.db.table(
                'collorg.application.communication.error')
            try:
                post_error.hit(
                    self._cog_fqtn_, self._cog_method, traceback.format_exc())
            except Exception, err:
                sys.stderr.write("Warning! {}\n".format(err))
#                open("/tmp/cog_error_log", "a+").write("{}\n".format(err))
        output.append('<h2>Query string</h2>')
        output.append('<pre>qs = %s</pre>' % (self.__pfce(self.__get_request)))
        output.append('<h2>Command executed</h2>')
        output.append('<pre>cmd = %s</pre>' %(self.__pfce(self._cog_cmd)))
        output.append('<h2>Environ</h2>')
        output.append('<pre>%s</pre>' % (self.__pfce(self._environ)))
        u_output = [self._unicode(elt) for elt in output]
        return link, "\n".join(u_output)
Exemplo n.º 34
0
def test_parse_accept_badq():
    assert list(Accept.parse("value1; q=0.1.2")) == [('value1', 1)]
Exemplo n.º 35
0
def test_quality():
    accept = Accept('Content-Type', 'text/html')
    assert accept.quality('text/html') == 1
    accept = Accept('Content-Type', 'text/html;q=0.5')
    assert accept.quality('text/html') == 0.5
Exemplo n.º 36
0
def test_quality_not_found():
    accept = Accept('Content-Type', 'text/html')
    assert accept.quality('foo/bar') is None
Exemplo n.º 37
0
def test_nil_radd_masterclass():
    # Is this "reaching into" __radd__ legit?
    nilaccept = NilAccept()
    accept = Accept('text/html')
    assert nilaccept.__radd__(accept) is accept
Exemplo n.º 38
0
def test_contains_not():
    accept = Accept('text/html')
    assert not 'foo/bar' in accept
Exemplo n.º 39
0
def test_init_accept_accept_charset_wildcard():
    accept = Accept('*')
    assert accept._parsed == [('*', 1)]
Exemplo n.º 40
0
def test_quality():
    accept = Accept('text/html')
    assert accept.quality('text/html') == 1
    accept = Accept('text/html;q=0.5')
    assert accept.quality('text/html') == 0.5
Exemplo n.º 41
0
def test_accept_str():
    accept = Accept('text/html')
    assert str(accept) == 'text/html'
Exemplo n.º 42
0
def test_quality_not_found():
    accept = Accept('text/html')
    assert accept.quality('foo/bar') is None
Exemplo n.º 43
0
def test_accept_str_with_q_not_1():
    value = 'text/html;q=0.5'
    accept = Accept(value)
    assert str(accept) == value
Exemplo n.º 44
0
def test_best_match_with_one_lower_q():
    accept = Accept('text/html, foo/bar;q=0.5')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    accept = Accept('text/html;q=0.5, foo/bar')
    assert accept.best_match(['text/html', 'foo/bar']) == 'foo/bar'
Exemplo n.º 45
0
def test_accept_add_other_accept():
    accept = Accept('text/html') + Accept('foo/bar')
    assert str(accept) == 'text/html, foo/bar'
    accept += Accept('bar/baz;q=0.5')
    assert str(accept) == 'text/html, foo/bar, bar/baz;q=0.5'
Exemplo n.º 46
0
def test_init_accept_content_type():
    accept = Accept('text/html')
    assert accept._parsed == [('text/html', 1)]
Exemplo n.º 47
0
def test_best_match_with_one_lower_q():
    accept = Accept("text/html, foo/bar;q=0.5")
    assert accept.best_match(["text/html", "foo/bar"]) == "text/html"
    accept = Accept("text/html;q=0.5, foo/bar")
    assert accept.best_match(["text/html", "foo/bar"]) == "foo/bar"
Exemplo n.º 48
0
def test_accept_match():
    for mask in ['*', 'text/html', 'TEXT/HTML']:
        assert 'text/html' in Accept(mask)
    assert 'text/html' not in Accept('foo/bar')
Exemplo n.º 49
0
def test_quality_not_found():
    accept = Accept("text/html")
    assert accept.quality("foo/bar") is None
Exemplo n.º 50
0
def test_nil_radd():
    nilaccept = NilAccept()
    accept = Accept('text/html')
    assert isinstance('foo' + nilaccept, accept.__class__)
    assert ('foo' + nilaccept).header_value == 'foo'
Exemplo n.º 51
0
def checkMimetype(acceptHeader):
    accept = Accept('Accept', acceptHeader)
    best = accept.best_match(['application/rdf', 'application/rdf+xml', 'text/n3', 'application/xml', 'application/json', 'text/xml', 'application/xhtml+xml'])
    if best is None:
        best = "text/html"
    return best
Exemplo n.º 52
0
def test_best_match_with_one_lower_q():
    accept = Accept('Content-Type', 'text/html, foo/bar;q=0.5')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    accept = Accept('Content-Type', 'text/html;q=0.5, foo/bar')
    assert accept.best_match(['text/html', 'foo/bar']) == 'foo/bar'