[snowy] Move note search to the sidebar



commit d32a8d4de783b640904de9e3772855f63d75e232
Author: Leon Handreke <leon handreke gmail com>
Date:   Wed Jun 9 16:46:13 2010 +0200

    Move note search to the sidebar
    
    Remove the old search view
    
    https://bugzilla.gnome.org/show_bug.cgi?id=603615

 notes/forms.py                         |   25 -------------------------
 notes/templates/notes/base.html        |   11 +++++++++--
 notes/templates/notes/note_detail.html |   14 ++++++++++----
 notes/templates/notes/note_search.html |   24 ------------------------
 notes/templatetags/notes.py            |   21 ++++++++++++++-------
 notes/urls.py                          |    1 -
 notes/views.py                         |   18 ------------------
 7 files changed, 33 insertions(+), 81 deletions(-)
---
diff --git a/notes/templates/notes/base.html b/notes/templates/notes/base.html
index 1ab20ee..b0fc2ee 100644
--- a/notes/templates/notes/base.html
+++ b/notes/templates/notes/base.html
@@ -4,8 +4,12 @@
 
 {% block sidebar %}
 {% if author %}
-<form method="GET" name="searchForm" action="{% url note_search author.username %}">
+<form method="GET" name="searchForm" action="">
+    {% if request.GET.query %}
+    <input type="text" id="search" name="query" accesskey="s" value="{{request.GET.query}}">
+    {% else %}
     <input type="text" id="search" class="dim" name="query" accesskey="s" value="{% trans "Search your notes" %}">
+    {% endif %}
 </form>
 {% endif %}
 {% endblock %}
@@ -18,7 +22,10 @@ $(document).ready(function() {
                     .val("")
                     .unbind("focus", undim);
     }
-    $("#search").bind("focus", undim);
+
+    if($("#search").attr('class') == "dim") {
+        $("#search").bind("focus", undim);
+    }
 
     $('#search').keyup(function(e) {
         // enter key
diff --git a/notes/templates/notes/note_detail.html b/notes/templates/notes/note_detail.html
index 47aa9df..22f9f90 100644
--- a/notes/templates/notes/note_detail.html
+++ b/notes/templates/notes/note_detail.html
@@ -16,13 +16,19 @@
 
 {% block sidebar %}
 {{ block.super }}
-{% user_notes_list request author as all_notes %}
+{% user_notes_list request author as list_notes %}
 <div id="sidebar-note-list">
     <ul>
-{% for n in all_notes %}
-        <li class="note-item{% if n.pinned %} pinned{% endif %}{% ifequal n note %} selected{% endifequal %}"><a href="{{ n.get_absolute_url }}">{{ n.title|safe }}</a></li>
-{% endfor %}
+        {% for n in list_notes %}
+        <li class="note-item{% if n.pinned %} pinned{% endif %}{% ifequal n note %} selected{% endifequal %}"><a href="{{ n.get_absolute_url }}{% if request.GET.query %}?query={{request.GET.query}}{% endif %}">{{ n.title|safe }}</a></li>
+        {% endfor %}
+        {% if request.GET.query %}
+        <div style="text-align: center; margin: 3px;">
+            <a href="."><input type="button" value="{% trans "Clear Search" %}" style="width:90%;"></a>
+        </div>
+        {% else %}
         <li class="more-item"><a href="{% url note_list author.username %}">{% trans "More Notes..." %}</a></li>
+        {% endif %}
     </ul>
     <hr />
     <ul>
diff --git a/notes/templatetags/notes.py b/notes/templatetags/notes.py
index d26db38..af08948 100644
--- a/notes/templatetags/notes.py
+++ b/notes/templatetags/notes.py
@@ -20,12 +20,14 @@ from django import template
 from snowy import settings
 from snowy.notes.models import Note, NoteTag
 
+from django.db.models import Q
+
 register = template.Library()
 
 @register.tag
 def user_notes_list(parser, tokens):
     """ Usage:
-        {% user_notes_list request author as all_notes %}
+        {% user_notes_list request author as list_notes %}
     """
     try:
         tag_name, request, author, _as, dest = tokens.split_contents()
@@ -46,16 +48,21 @@ class UserNotesListNode(template.Node):
     def render(self, context):
         request = self.request.resolve(context)
         author = self.author.resolve(context)
+        search_query = request.GET.get('query', None)
 
-        # XXX: Replace this with a NotesManager
-        all_notes = Note.objects.filter(author=author) \
+        list_notes = Note.objects.user_viewable(request.user, 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 ''
+        if search_query:
+            list_notes = list_notes.filter(Q(title__icontains=search_query) |
+                                           Q(content__icontains=search_query))
 
+        if not search_query:
+            # do not limit the amount of search results returned
+            list_notes = list_notes[:settings.SNOWY_LIST_MAX_NOTES]
+
+        context[self.dest] = list_notes
+        return ''
 
 @register.tag
 def user_notebook_list(parser, tokens):
diff --git a/notes/urls.py b/notes/urls.py
index 6f3a68f..6bc35cc 100644
--- a/notes/urls.py
+++ b/notes/urls.py
@@ -21,7 +21,6 @@ from snowy.notes.models import Note
 urlpatterns = patterns('',
     url(r'^$', 'snowy.notes.views.note_index', name='note_index'),
     url(r'^list/$', 'snowy.notes.views.note_list', name='note_list'),
-    url(r'^search/$', 'snowy.notes.views.note_search', name='note_search'),
     url(r'^(?P<note_id>\d+)/$', 'snowy.notes.views.note_detail', name='note_detail_no_slug'),
     url(r'^(?P<note_id>\d+)/(?P<slug>[^/]+)/$', 'snowy.notes.views.note_detail', name='note_detail'),
 )
diff --git a/notes/views.py b/notes/views.py
index 927f930..cce46f2 100644
--- a/notes/views.py
+++ b/notes/views.py
@@ -55,24 +55,6 @@ def note_list(request, username,
                               {'notes': notes},
                               context_instance=RequestContext(request))
 
-def note_search(request, username,
-                template_name='notes/note_search.html'):
-    author = get_object_or_404(User, username=username)
-    notes = []
-    if request.method == 'GET':
-        form = SearchForm(request.GET)
-        if form.is_valid():
-            query = request.GET['query']
-            notes = Note.objects.user_viewable(request.user, author) \
-                                .filter(Q(title__icontains=query) \
-                                        | Q(content__icontains=query))
-    else:
-        form = SearchForm()
-
-    return render_to_response(template_name,
-                              {'notes': notes, 'form': form},
-                              context_instance=RequestContext(request))
-
 def note_detail(request, username, note_id, slug='',
                 template_name='notes/note_detail.html'):
     def clean_content(xml, author):



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