src/pyams_utils/url.py
changeset 1 3f89629b9e54
child 34 b70afbacf63b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/url.py	Thu Feb 19 00:46:48 2015 +0100
@@ -0,0 +1,48 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from persistent.interfaces import IPersistent
+from pyams_utils.interfaces.tales import ITALESExtension
+
+# import packages
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyramid.url import resource_url
+from zope.interface import Interface
+
+
+def absolute_url(context, request, view_name=None):
+    """Get resource absolute_url"""
+    result = resource_url(context, request)
+    if result.endswith('/'):
+        result = result[:-1]
+    if view_name:
+        if view_name.startswith('#'):
+            result += view_name
+        else:
+            result += '/' + view_name
+    return result
+
+
+@adapter_config(name='absolute_url', context=(IPersistent, Interface, Interface), provides=ITALESExtension)
+class AbsoluteUrlTalesExtension(ContextRequestViewAdapter):
+    """extension:absolute_url(context) TALES extension"""
+
+    def render(self, context):
+        if context is None:
+            context = self.context
+        return absolute_url(context, self.request)