src/pyams_utils/url.py
changeset 392 1af0ba410187
parent 388 e768f0e1e52f
child 398 f87a8558edcc
equal deleted inserted replaced
391:49d63e4bf171 392:1af0ba410187
    29 
    29 
    30     The original title is translated to remove accents, converted to lowercase, and words
    30     The original title is translated to remove accents, converted to lowercase, and words
    31     shorter than three characters are removed; terms are joined by hyphens.
    31     shorter than three characters are removed; terms are joined by hyphens.
    32     """
    32     """
    33     return '-'.join(filter(lambda x: len(x) >= min_word_length,
    33     return '-'.join(filter(lambda x: len(x) >= min_word_length,
    34                            translate_string(title, escape_slashes=True, force_lower=True, spaces='-',
    34                            translate_string(title, escape_slashes=False, force_lower=True,
    35                                             remove_punctuation=True, keep_chars='-').split('-')))
    35                                             spaces='-', remove_punctuation=True,
       
    36                                             keep_chars='-').split('-')))
    36 
    37 
    37 
    38 
    38 #
    39 #
    39 # Request display context
    40 # Request display context
    40 #
    41 #
    62 
    63 
    63     # if we pass a string to absolute_url(), argument is returned as-is!
    64     # if we pass a string to absolute_url(), argument is returned as-is!
    64     if isinstance(context, str):
    65     if isinstance(context, str):
    65         return context
    66         return context
    66 
    67 
    67     # if we have several parents without name in the lineage, the resource URL contains a double slash
    68     # if we have several parents without name in the lineage, the resource URL contains a double
    68     # which generates "NotFound" exceptions; so we replace it with a single slash...
    69     # slash which generates "NotFound" exceptions; so we replace it with a single slash...
    69     result = resource_url(context, request).replace('//', '/').replace(':/', '://')
    70     result = resource_url(context, request).replace('//', '/').replace(':/', '://')
    70     if result.endswith('/'):
    71     if result.endswith('/'):
    71         result = result[:-1]
    72         result = result[:-1]
    72     if view_name:
    73     if view_name:
    73         if view_name.startswith('#'):
    74         if view_name.startswith('#'):
    82             qs = '?' + urlencode(query, doseq=True)
    83             qs = '?' + urlencode(query, doseq=True)
    83         result += qs
    84         result += qs
    84     return result
    85     return result
    85 
    86 
    86 
    87 
    87 @adapter_config(name='absolute_url', context=(Interface, Interface, Interface), provides=ITALESExtension)
    88 @adapter_config(name='absolute_url', context=(Interface, Interface, Interface),
       
    89                 provides=ITALESExtension)
    88 class AbsoluteUrlTalesExtension(ContextRequestViewAdapter):
    90 class AbsoluteUrlTalesExtension(ContextRequestViewAdapter):
    89     """extension:absolute_url(context, view_name) TALES extension
    91     """extension:absolute_url(context, view_name) TALES extension
    90 
    92 
    91     A PyAMS TALES extension used to get access to an object URL from a page template.
    93     A PyAMS TALES extension used to get access to an object URL from a page template.
    92     """
    94     """
   116         return url_adapter.get_url(view_name, query)
   118         return url_adapter.get_url(view_name, query)
   117     else:
   119     else:
   118         return absolute_url(context, request, view_name, query)
   120         return absolute_url(context, request, view_name, query)
   119 
   121 
   120 
   122 
   121 @adapter_config(name='canonical_url', context=(Interface, Interface, Interface), provides=ITALESExtension)
   123 @adapter_config(name='canonical_url', context=(Interface, Interface, Interface),
       
   124                 provides=ITALESExtension)
   122 class CanonicalUrlTalesExtension(ContextRequestViewAdapter):
   125 class CanonicalUrlTalesExtension(ContextRequestViewAdapter):
   123     """extension:canonical_url(context, view_name) TALES extension
   126     """extension:canonical_url(context, view_name) TALES extension
   124 
   127 
   125     A PyAMS TALES extension used to get access to an object's canonical URL from a page template.
   128     A PyAMS TALES extension used to get access to an object's canonical URL from a page template.
   126     """
   129     """
   151         display_context = request.annotations.get(DISPLAY_CONTEXT, request.context)
   154         display_context = request.annotations.get(DISPLAY_CONTEXT, request.context)
   152     adapter = request.registry.getMultiAdapter((context, request), IRelativeURL)
   155     adapter = request.registry.getMultiAdapter((context, request), IRelativeURL)
   153     return adapter.get_url(display_context, view_name, query)
   156     return adapter.get_url(display_context, view_name, query)
   154 
   157 
   155 
   158 
   156 @adapter_config(name='relative_url', context=(Interface, Interface, Interface), provides=ITALESExtension)
   159 @adapter_config(name='relative_url', context=(Interface, Interface, Interface),
       
   160                 provides=ITALESExtension)
   157 class RelativeUrlTalesExtension(ContextRequestViewAdapter):
   161 class RelativeUrlTalesExtension(ContextRequestViewAdapter):
   158     """extension:relative_url(context, view_name, query) TALES extension
   162     """extension:relative_url(context, view_name, query) TALES extension
   159 
   163 
   160     A PyAMS TALES extension used to get an object's relative URL based on current request display context.
   164     A PyAMS TALES extension used to get an object's relative URL based on current request display
       
   165     context.
   161     """
   166     """
   162 
   167 
   163     def render(self, context=None, view_name=None, query=None):
   168     def render(self, context=None, view_name=None, query=None):
   164         if context is None:
   169         if context is None:
   165             context = self.context
   170             context = self.context