incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [28/50] git commit: [#6105] ticket:321 Refactor markdown macros
Date Fri, 24 May 2013 17:05:04 GMT
[#6105]  ticket:321  Refactor markdown macros


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

Branch: refs/heads/db/6255
Commit: 9caef26cb1f0862194c98074bd1d7daab6a2fd7d
Parents: 760cf5e
Author: Yuriy Arhipov <yuriyarhipovua@yandex.ru>
Authored: Mon Apr 29 01:10:48 2013 +0400
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Thu May 23 13:34:16 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/macro.py                         |   80 +++++++--------
 Allura/allura/lib/widgets/macros.py                |   22 ++++
 Allura/allura/templates/macro/__init__.py          |    1 +
 Allura/allura/templates/macro/blog_posts.html      |   10 ++
 Allura/allura/templates/macro/members.html         |    9 ++
 .../allura/templates/macro/neighborhood_feeds.html |   10 ++
 Allura/allura/templates/macro/project_admins.html  |    6 +
 Allura/allura/tests/test_globals.py                |   15 +--
 8 files changed, 101 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/lib/macro.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/macro.py b/Allura/allura/lib/macro.py
index 2e51ecb..be1f8fb 100644
--- a/Allura/allura/lib/macro.py
+++ b/Allura/allura/lib/macro.py
@@ -82,53 +82,36 @@ class parse(object):
         else:
             return None
 
-template_neighborhood_feeds = string.Template('''
-<div class="neighborhood_feed_entry">
-<h3><a href="$href">$title</a></h3>
-<p>
-by <em>$author</em>
-<small>$ago</small>
-</p>
-<p>$description</p>
-</div>
-''')
 @macro('neighborhood-wiki')
 def neighborhood_feeds(tool_name, max_number=5, sort='pubdate'):
     from allura import model as M
+    from allura.lib.widgets.macros import NeighborhoodFeeds
     feed = M.Feed.query.find(
         dict(
             tool_name=tool_name,
             neighborhood_id=c.project.neighborhood._id))
     feed = feed.sort(sort, pymongo.DESCENDING).limit(int(max_number)).all()
-    output = '\n'.join(
-        template_neighborhood_feeds.substitute(dict(
+    output = ((dict(
                 href=item.link,
                 title=item.title,
                 author=item.author_name,
                 ago=h.ago(item.pubdate),
                 description=item.description))
         for item in feed)
-    return output
-
-template_neighborhood_blog_posts = string.Template('''
-<div class="neighborhood_feed_entry">
-<h3><a href="$href">$title</a></h3>
-<p>
-by <em>$author</em>
-<small>$ago</small>
-</p>
-$description
-</div>
-''')
+    feeds = NeighborhoodFeeds(feeds=output)
+    g.resource_manager.register(feeds)
+    response = feeds.display(feeds=output)
+    return response
+
 @macro('neighborhood-wiki')
 def neighborhood_blog_posts(max_number=5, sort='timestamp', summary=False):
     from forgeblog import model as BM
+    from allura.lib.widgets.macros import BlogPosts
     posts = BM.BlogPost.query.find(dict(
         neighborhood_id=c.project.neighborhood._id,
         state='published'))
     posts = posts.sort(sort, pymongo.DESCENDING).limit(int(max_number)).all()
-    output = '\n'.join(
-        template_neighborhood_blog_posts.substitute(dict(
+    output = ((dict(
                 href=post.url(),
                 title=post.title,
                 author=post.author().display_name,
@@ -137,19 +120,23 @@ def neighborhood_blog_posts(max_number=5, sort='timestamp', summary=False):
         for post in posts if post.app and
                              security.has_access(post, 'read', project=post.app.project)()
and
                              security.has_access(post.app.project, 'read', project=post.app.project)())
-    return output
+
+    posts = BlogPosts(posts=output)
+    g.resource_manager.register(posts)
+    response = posts.display(posts=output)
+    return response
 
 @macro()
 def project_blog_posts(max_number=5, sort='timestamp', summary=False, mount_point=None):
     from forgeblog import model as BM
+    from allura.lib.widgets.macros import BlogPosts
     app_config_ids = []
     for conf in c.project.app_configs:
         if conf.tool_name.lower() == 'blog' and (mount_point is None or conf.options.mount_point==mount_point):
             app_config_ids.append(conf._id)
     posts = BM.BlogPost.query.find({'state':'published','app_config_id':{'$in':app_config_ids}})
     posts = posts.sort(sort, pymongo.DESCENDING).limit(int(max_number)).all()
-    output = '\n'.join(
-        template_neighborhood_blog_posts.substitute(dict(
+    output = ((dict(
                 href=post.url(),
                 title=post.title,
                 author=post.author().display_name,
@@ -157,7 +144,10 @@ def project_blog_posts(max_number=5, sort='timestamp', summary=False,
mount_poin
                 description=summary and '&nbsp;' or g.markdown.convert(post.text)))
         for post in posts if security.has_access(post, 'read', project=post.app.project)()
and
                              security.has_access(post.app.project, 'read', project=post.app.project)())
-    return output
+    posts = BlogPosts(posts=output)
+    g.resource_manager.register(posts)
+    response = posts.display(posts=output)
+    return response
 
 def get_projects_for_macro(category=None, display_mode='grid', sort='last_updated',
         show_total=False, limit=100, labels='', award='', private=False,
@@ -352,31 +342,35 @@ def img(src=None, **kw):
     else:
         return '<img src="./attachment/%s" %s/>' % (src, ' '.join(attrs))
 
-
-template_project_admins = string.Template('<li><a href="$url">$name</a></li>')
 @macro()
 def project_admins():
     admins = c.project.users_with_role('Admin')
-    output = ''.join(
-        template_project_admins.substitute(dict(
+    from allura.lib.widgets.macros import ProjectAdmins
+    output = ((dict(
             url=user.url(),
             name=jinja2.escape(user.display_name)))
         for user in admins)
-    return u'<h6>Project Admins:</h6><ul class="md-users-list">{0}</ul>'.format(output)
+    users = ProjectAdmins(users=output)
+    g.resource_manager.register(users)
+    response = users.display(users=output)
+    return response
 
-template_members = string.Template('<li><a href="$url">$name</a>$admin</li>')
 @macro()
 def members(limit=20):
+    from allura.lib.widgets.macros import Members
     limit = asint(limit)
     admins = set(c.project.users_with_role('Admin'))
     members = sorted(c.project.users(), key=attrgetter('display_name'))
-    output = ''.join(
-        template_members.substitute(dict(
+    output = [dict(
             url=user.url(),
             name=jinja2.escape(user.display_name),
             admin=' (admin)' if user in admins else '',
-            ))
-        for user in members[:limit])
-    if len(members) > limit:
-        output = output + '<li class="md-users-list-more"><a href="%s_members">All
Members</a></li>' % c.project.url()
-    return u'<h6>Project Members:</h6><ul class="md-users-list">{0}</ul>'.format(output)
+            )
+        for user in members[:limit]]
+
+    over_limit = len(members) > limit
+    users = Members(users=output, over_limit=over_limit)
+    g.resource_manager.register(users)
+    response = users.display(users=output, over_limit=over_limit)
+    return response
+

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/lib/widgets/macros.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/macros.py b/Allura/allura/lib/widgets/macros.py
index 62a3487..0921ab9 100644
--- a/Allura/allura/lib/widgets/macros.py
+++ b/Allura/allura/lib/widgets/macros.py
@@ -34,3 +34,25 @@ class DownloadButton(ew.Widget):
         yield ew.jinja2_ew.JSScript('''
             $(function(){$(".download-button-%s").load("%s");
         });''' % (self.project._id,self.project.best_download_url()))
+
+class NeighborhoodFeeds(ew.Widget):
+    template = 'jinja:allura:templates/macro/neighborhood_feeds.html'
+    params=['feeds']
+    feeds = None
+
+class BlogPosts(ew.Widget):
+    template = 'jinja:allura:templates/macro/blog_posts.html'
+    params = ['posts']
+    posts = None
+
+class ProjectAdmins(ew.Widget):
+    template = 'jinja:allura:templates/macro/project_admins.html'
+    params = ['users']
+    users = None
+
+class Members(ew.Widget):
+    template = 'jinja:allura:templates/macro/members.html'
+    params = ['users', 'over_limit']
+    users = None
+    over_limit = None
+

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/templates/macro/__init__.py
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/macro/__init__.py b/Allura/allura/templates/macro/__init__.py
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/Allura/allura/templates/macro/__init__.py
@@ -0,0 +1 @@
+

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/templates/macro/blog_posts.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/macro/blog_posts.html b/Allura/allura/templates/macro/blog_posts.html
new file mode 100644
index 0000000..c5213bb
--- /dev/null
+++ b/Allura/allura/templates/macro/blog_posts.html
@@ -0,0 +1,10 @@
+{% for post in posts %}
+    <div class="neighborhood_feed_entry">
+    <h3><a href="{{post.href}}">{{post.title}}</a></h3>
+    <p>
+    by <em>{{post.author}}</em>
+    <small>{{post.ago}}</small>
+    </p>
+    {{post.description}}
+    </div>
+{% endfor %}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/templates/macro/members.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/macro/members.html b/Allura/allura/templates/macro/members.html
new file mode 100644
index 0000000..440760c
--- /dev/null
+++ b/Allura/allura/templates/macro/members.html
@@ -0,0 +1,9 @@
+<h6>Project Members:</h6>
+	<ul class="md-users-list">
+		{% for user in users -%}
+			<li><a href="{{user.url}}">{{user.name}}</a>{{user.admin}}</li>
+		{%- endfor %}
+		{% if over_limit -%}
+				<li class="md-users-list-more"><a href="{{c.project.url()}}_members">All
Members</a></li>
+		{%- endif %}
+	</ul>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/templates/macro/neighborhood_feeds.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/macro/neighborhood_feeds.html b/Allura/allura/templates/macro/neighborhood_feeds.html
new file mode 100644
index 0000000..7a23126
--- /dev/null
+++ b/Allura/allura/templates/macro/neighborhood_feeds.html
@@ -0,0 +1,10 @@
+<div class="neighborhood_feed_entry">
+{% for feed in feeds%}
+    <h3><a href="{{feed.href}}">{{feed.title}}</a></h3>
+    <p>
+    by <em>{{feed.author}}</em>
+    <small>{{feed.ago}}</small>
+    </p>
+    <p>{{feed.description}}</p>
+</div>
+{%endfor%}

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/templates/macro/project_admins.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/macro/project_admins.html b/Allura/allura/templates/macro/project_admins.html
new file mode 100644
index 0000000..2b346bc
--- /dev/null
+++ b/Allura/allura/templates/macro/project_admins.html
@@ -0,0 +1,6 @@
+<h6>Project Admins:</h6>
+<ul class="md-users-list">
+    {% for user in users -%}
+        <li><a href="{{user.url}}">{{user.name}}</a></li>
+    {%- endfor %}
+</ul>

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9caef26c/Allura/allura/tests/test_globals.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index 71629ed..69b840f 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -175,14 +175,11 @@ def test_macro_members():
     p_test.add_user(M.User.by_username('test-user-0'), ['Member'])
     ThreadLocalORMSession.flush_all()
     r = g.markdown_wiki.convert('[[members limit=2]]')
-    assert_equal(r,
-        '<div class="markdown_content">'
-            '<h6>Project Members:</h6>'
-            '<ul class="md-users-list">'
-                '<li><a href="/u/test-admin/">Test Admin</a> (admin)</li>'
-                '<li><a href="/u/test-user/">Test User</a></li>'
-                '<li class="md-users-list-more"><a href="/p/test/_members">All
Members</a></li>'
-            '</ul>\n'
+    assert_equal(r, '<div class="markdown_content"><h6>Project Members:</h6>\n'
+        '<ul class="md-users-list">\n'
+        '<li><a href="/u/test-admin/">Test Admin</a> (admin)</li><li><a
href="/u/test-user/">Test User</a></li>\n'
+        '<li class="md-users-list-more"><a href="/p/test/_members">All Members</a></li>\n'
+        '</ul>\n'
         '</div>')
 
 @with_setup(teardown=setUp) # reset everything we changed
@@ -190,7 +187,7 @@ def test_macro_project_admins():
     user = M.User.by_username('test-admin')
     user.display_name = u'Test Ådmin'
     r = g.markdown_wiki.convert('[[project_admins]]')
-    assert_equal(r, u'<div class="markdown_content"><h6>Project Admins:</h6><ul
class="md-users-list"><li><a href="/u/test-admin/">Test Ådmin</a></li></ul>\n</div>')
+    assert_equal(r, u'<div class="markdown_content"><h6>Project Admins:</h6>\n<ul
class="md-users-list">\n<li><a href="/u/test-admin/">Test \xc5dmin</a></li>\n</ul>\n</div>')
 
 @with_setup(teardown=setUp) # reset everything we changed
 def test_macro_project_admins_one_br():


Mime
View raw message