Return-Path: X-Original-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D586FEDD3 for ; Wed, 16 Jan 2013 16:43:42 +0000 (UTC) Received: (qmail 81362 invoked by uid 500); 16 Jan 2013 16:43:42 -0000 Delivered-To: apmail-incubator-allura-commits-archive@incubator.apache.org Received: (qmail 81329 invoked by uid 500); 16 Jan 2013 16:43:42 -0000 Mailing-List: contact allura-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: allura-dev@incubator.apache.org Delivered-To: mailing list allura-commits@incubator.apache.org Received: (qmail 81315 invoked by uid 99); 16 Jan 2013 16:43:42 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 16 Jan 2013 16:43:42 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 1C1661F5D3; Wed, 16 Jan 2013 16:43:42 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: johnsca@apache.org To: allura-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [1/2] git commit: [#5596] Added safer implementation of LinkField Message-Id: <20130116164342.1C1661F5D3@tyr.zones.apache.org> Date: Wed, 16 Jan 2013 16:43:42 +0000 (UTC) [#5596] Added safer implementation of LinkField Signed-off-by: Cory Johns 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 Authored: Tue Jan 15 16:32:09 2013 +0000 Committer: Cory Johns 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: http://example.com/ + + LinkField().display(value="http://example.com/") + + The following all render as: bar + + 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 -%} + {{ (value['text'] or text or label)|e }} + {%- else -%} + {{ (text or label or value)|e }} + {%- 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)