incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [1/2] git commit: [#5596] Added safer implementation of LinkField
Date Wed, 16 Jan 2013 16:43:42 GMT
[#5596] Added safer implementation of LinkField

Signed-off-by: Cory Johns <johnsca@geek.net>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/3cc75d1c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/3cc75d1c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/3cc75d1c

Branch: refs/heads/cj/5596
Commit: 3cc75d1c01b33b8b36f53de4da56b4299dd9df5b
Parents: 1647757
Author: Cory Johns <johnsca@geek.net>
Authored: Tue Jan 15 16:32:09 2013 +0000
Committer: Cory Johns <johnsca@geek.net>
Committed: Tue Jan 15 19:09:04 2013 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/auth.py          |    5 +--
 Allura/allura/lib/widgets/form_fields.py   |   52 +++++++++++++++++++++++
 Allura/allura/lib/widgets/subscriptions.py |    2 +-
 3 files changed, 54 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3cc75d1c/Allura/allura/controllers/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/auth.py b/Allura/allura/controllers/auth.py
index 9f8bf8d..fa7a254 100644
--- a/Allura/allura/controllers/auth.py
+++ b/Allura/allura/controllers/auth.py
@@ -397,14 +397,11 @@ class PreferencesController(BaseController):
                 continue
             if app_config is None:
                 continue
-            title = mb.artifact_title
-            if mb.artifact_url:
-                title = ew.LinkField(label=j2_escape(mb.artifact_title), href=j2_escape(mb.artifact_url)).display()
             subscriptions.append(dict(
                     subscription_id=mb._id,
                     project_name=project.name,
                     mount_point=app_config.options['mount_point'],
-                    artifact_title=title,
+                    artifact_title=dict(text=mb.artifact_title, href=mb.artifact_url),
                     topic=mb.topic,
                     type=mb.type,
                     frequency=mb.frequency.unit,

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3cc75d1c/Allura/allura/lib/widgets/form_fields.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/form_fields.py b/Allura/allura/lib/widgets/form_fields.py
index f205965..f536896 100644
--- a/Allura/allura/lib/widgets/form_fields.py
+++ b/Allura/allura/lib/widgets/form_fields.py
@@ -454,3 +454,55 @@ class DisplayOnlyField(ew.HiddenField):
         text=None,
         value=None,
         with_hidden_input=None)
+
+class LinkField(ew.LinkField):
+    '''
+    A safer jinja2 implementation of LinkField which allows a mapping value which
+    contains the href and text values separately, as well as a flag to render
+    the field as plaintext if no href value is specified.  Both the href and
+    text values will be escaped properly.
+
+    The HREF of the link will be first one of value['href'], attrs['href'],
+    href, or value (if value is not a mapping) that is set.
+
+    The text of the link will be first one of value['text'], text, label, or
+    value (if value is not a mapping) that is set.
+
+    If plaintext_if_no_href is True and none of value['href'], attrs['href'],
+    nor href are set, then the field will be rendered as plaintext using the
+    text of the link, above.
+
+    Examples:
+
+        This renders as: <a href="http://example.com/">http://example.com/</a>
+
+            LinkField().display(value="http://example.com/")
+
+        The following all render as: <a href="/foo">bar</a>
+
+            LinkField(href='/foo').display(value='bar')
+            LinkField(text='bar').display(value='/foo')
+            LinkField().display(value=dict(href='/foo', text='bar'))
+            LinkField(label='bar').display(value=dict(href='/foo'))
+            LinkField(href='/foo').display(value=dict(text='bar'))
+            LinkField(attrs={'href':'/foo'}).display(value='bar')
+            LinkField(plaintext_if_no_href=True).display(value=dict(href='/foo', text='bar'))
+            LinkField(plaintext_if_no_href=True).display(href='/foo', value='bar')
+
+        These render as the plaintext: bar
+
+            LinkField(plaintext_if_no_href=True).display(value=dict(text='bar'))
+            LinkField(plaintext_if_no_href=True, label='bar').display(value=dict())
+            LinkField(plaintext_if_no_href=True, label='foo').display(value='bar')
+    '''
+    template=ew.Snippet('''{% if plaintext_if_no_href and not (value['href'] or attrs['href']
or href) -%}
+            {{ (value['text'] or (value is not mapping and value or None) or text or label)|e
}}
+        {%- elif value is mapping -%}
+            <a {{ widget.j2_attrs({'href':value['href'] or href}, attrs) }}>{{ (value['text']
or text or label)|e }}</a>
+        {%- else -%}
+            <a {{ widget.j2_attrs({'href':href or value}, attrs) }}>{{ (text or label
or value)|e }}</a>
+        {%- endif %}''', 'jinja2')
+    defaults=dict(
+        ew.LinkField.defaults,
+        value=None,
+        plaintext_if_no_href=False)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3cc75d1c/Allura/allura/lib/widgets/subscriptions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/subscriptions.py b/Allura/allura/lib/widgets/subscriptions.py
index e9efa00..07352ba 100644
--- a/Allura/allura/lib/widgets/subscriptions.py
+++ b/Allura/allura/lib/widgets/subscriptions.py
@@ -23,7 +23,7 @@ class _SubscriptionTable(ew.TableField):
         topic = ffw.DisplayOnlyField(label='Topic', show_label=True, with_hidden_input=False)
         type = ffw.DisplayOnlyField(label='Type', show_label=True, with_hidden_input=False)
         frequency = ffw.DisplayOnlyField(label='Frequency', show_label=True, with_hidden_input=False)
-        artifact_title = ffw.DisplayOnlyField(label='Artifact', show_label=True, with_hidden_input=False)
+        artifact_title = ffw.LinkField(label='Artifact', show_label=True, plaintext_if_no_href=True)
         # unsubscribe = SubmitButton()
         subscribed = ew.Checkbox(suppress_label=True)
 


Mime
View raw message