[snowy] Add two template tags to query the user's notes and notebooks



commit f8fb908590e1949bf66fa9883d41b995791894e5
Author: Brad Taylor <brad getcoded net>
Date:   Sun May 31 21:55:55 2009 -0400

    Add two template tags to query the user's notes and notebooks
---
 notes/templates/notes/note_detail.html |    5 ++-
 notes/templatetags/notes.py            |   87 ++++++++++++++++++++++++++++++++
 notes/views.py                         |   10 +---
 3 files changed, 92 insertions(+), 10 deletions(-)

diff --git a/notes/templates/notes/note_detail.html b/notes/templates/notes/note_detail.html
index 8118e54..cb1ec37 100644
--- a/notes/templates/notes/note_detail.html
+++ b/notes/templates/notes/note_detail.html
@@ -1,6 +1,7 @@
 {% extends 'notes/base.html' %}
 
 {% load i18n %}
+{% load notes %}
 
 {% block extra_head %}
     <script type="text/javascript" src="{{ MEDIA_URL }}js/DUI.js" charset="utf-8"></script>
@@ -12,6 +13,7 @@
 
 {% block sidebar %}
 {{ block.super }}
+{% user_notes_list request user as all_notes %}
 <div id="sidebar-note-list">
     <ul>
 {% for n in all_notes %}
@@ -26,7 +28,8 @@
 </div>
 <div id="sidebar-notebook-list">
     <h3>{% trans "Notebooks" %}</h3>
-    <ul>
+    <ul> 
+{% user_notebook_list request user as all_notebooks %}
 {% for n in all_notebooks %}
         <li class="notebook-item"><a href="#">{{ n.get_name_for_display }}</a></li>
 {% endfor %}
diff --git a/notes/templatetags/__init__.py b/notes/templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/notes/templatetags/notes.py b/notes/templatetags/notes.py
new file mode 100644
index 0000000..6734e56
--- /dev/null
+++ b/notes/templatetags/notes.py
@@ -0,0 +1,87 @@
+#
+# Copyright (c) 2009 Brad Taylor <brad getcoded net>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+from django import template
+
+from snowy import settings
+from snowy.notes.models import Note, NoteTag
+
+register = template.Library()
+
+ register tag
+def user_notes_list(parser, tokens):
+    """ Usage:
+        {% user_notes_list request author as all_notes %}
+    """
+    try:
+        tag_name, request, author, _as, dest = tokens.split_contents()
+    except ValueError:
+        raise template.TemplateSyntaxError, \
+            "%r tag requires exactly four arguments" % tag_name
+    if _as != "as":
+        raise template.TemplateSyntaxError, \
+            "%r tag's third argument should be 'as'" % tag_name
+    return UserNotesListNode(request, author, dest)
+
+class UserNotesListNode(template.Node):
+    def __init__(self, request, author, dest):
+        self.request = template.Variable(request)
+        self.author = template.Variable(author)
+        self.dest = dest
+
+    def render(self, context):
+        request = self.request.resolve(context)
+	author = self.author.resolve(context)
+
+        # XXX: Replace this with a NotesManager
+        all_notes = Note.objects.filter(author=author) \
+                                .order_by('-pinned', '-user_modified')
+        if request.user != author:
+            all_notes = all_notes.filter(permissions=1)
+        
+        context[self.dest] = all_notes[:settings.SNOWY_LIST_MAX_NOTES]
+        return ''
+
+
+ register tag
+def user_notebook_list(parser, tokens):
+    """ Usage:
+        {% user_notebook_list request author as all_notebooks %}
+    """
+    try:
+        tag_name, request, author, _as, dest = tokens.split_contents()
+    except ValueError:
+        raise template.TemplateSyntaxError, \
+            "%r tag requires exactly four arguments" % tag_name
+    if _as != "as":
+        raise template.TemplateSyntaxError, \
+            "%r tag's third argument should be 'as'" % tag_name
+    return UserNotebookListNode(request, author, dest)
+
+class UserNotebookListNode(template.Node):
+    def __init__(self, request, author, dest):
+        self.request = template.Variable(request)
+        self.author = template.Variable(author)
+        self.dest = dest
+
+    def render(self, context):
+        request = self.request.resolve(context)
+	author = self.author.resolve(context)
+
+        context[self.dest] = NoteTag.objects.filter(author=author,
+	                                            is_notebook=True)[:5]
+        return ''
diff --git a/notes/views.py b/notes/views.py
index 8eb3977..04f98cf 100644
--- a/notes/views.py
+++ b/notes/views.py
@@ -73,16 +73,8 @@ def note_detail(request, username, note_id, slug='',
         if doc != None: doc.freeDoc()
         if result != None: result.freeDoc()
 
-    all_notes = Note.objects.filter(author=user) \
-                            .order_by('-pinned', '-user_modified')
-    if request.user != user:
-        all_notes = all_notes.filter(permissions=1) # Public
-
-    all_notes = all_notes[:settings.SNOWY_LIST_MAX_NOTES]
-    all_notebooks = NoteTag.objects.filter(author=user, is_notebook=True)[:5]
     return render_to_response(template_name,
                               {'title': note.title,
                                'note': note, 'body': body,
-                               'all_notes': all_notes,
-                               'all_notebooks': all_notebooks},
+                               'request': request, 'user': user},
                               context_instance=RequestContext(request))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]