changeset 673:92101013d5ac

For issue #28, add a quick search feature for new forum topics.
author Brian Neal <bgneal@gmail.com>
date Mon, 27 May 2013 15:04:52 -0500
parents 69e8aa135c2e
children 2af10c2cec21 6e6492468bb8
files forums/forms.py forums/static/js/forums.js sg101/templates/forums/show_form.html sg101/templates/search/search_ajax.html sg101/urls.py
diffstat 5 files changed, 100 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/forums/forms.py	Sun May 26 13:29:44 2013 -0500
+++ b/forums/forms.py	Mon May 27 15:04:52 2013 -0500
@@ -68,7 +68,7 @@
     locked.
     """
     name = forms.CharField(label='Subject', max_length=255,
-            widget=forms.TextInput(attrs={'size': 64}))
+            widget=forms.TextInput(attrs={'style': 'width:70%'}))
     body = forms.CharField(label='', required=False,
             widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
     user = None
--- a/forums/static/js/forums.js	Sun May 26 13:29:44 2013 -0500
+++ b/forums/static/js/forums.js	Mon May 27 15:04:52 2013 -0500
@@ -2,7 +2,6 @@
    var postText = $('#id_body');
    var postButton = $('#forums-reply-post');
    postButton.click(function () {
-      var text = $.trim(postText.val());
       $(this).attr('disabled', 'disabled').val('Posting reply...');
 
       var attachments = new Array()
@@ -11,7 +10,7 @@
       });
 
       $.ajax({
-         url: '/forums/quick-reply/', 
+         url: '/forums/quick-reply/',
          type: 'POST',
          data: {
             body : postText.val(),
@@ -33,7 +32,7 @@
             initAttachments();
          },
          error: function (xhr, textStatus, ex) {
-            alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + 
+            alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
                xhr.responseText);
             postButton.removeAttr('disabled').val('Submit Reply');
             initAttachments();
@@ -52,7 +51,7 @@
              $.ajax({
                url: '/forums/flag-post/',
                type: 'POST',
-               data: {id: id}, 
+               data: {id: id},
                dataType: 'text',
                success: function (response, textStatus) {
                   alert(response);
@@ -73,7 +72,7 @@
              $.ajax({
                url: '/forums/delete-post/',
                type: 'POST',
-               data: {id: id}, 
+               data: {id: id},
                dataType: 'text',
                success: function (response, textStatus) {
                   alert(response);
@@ -101,7 +100,7 @@
       $('#attach-another').remove();
    }
 
-   function processEmbeds(data, textStatus) 
+   function processEmbeds(data, textStatus)
    {
       vidDiv.find('img').remove();
       $.each(data, function(index, value) {
@@ -138,7 +137,7 @@
          post_id = post_input.val();
          vidDiv.prepend('<img src="/static/icons/ajax_busy.gif" alt="Busy" />');
          $.ajax({
-            url: '/forums/fetch_attachments/', 
+            url: '/forums/fetch_attachments/',
             type: 'GET',
             data: {
                pid : post_id
@@ -147,7 +146,7 @@
             success: processEmbeds,
             error: function (xhr, textStatus, ex) {
                vidDiv.find('img').remove();
-               alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + 
+               alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
                   xhr.responseText);
             }
          });
@@ -161,7 +160,7 @@
          });
          attachments.remove();
          $.ajax({
-            url: '/oembed/fetch_saved/', 
+            url: '/oembed/fetch_saved/',
             type: 'GET',
             data: {
                embeds: embeds
@@ -171,7 +170,7 @@
             success: processEmbeds,
             error: function (xhr, textStatus, ex) {
                vidDiv.find('img').remove();
-               alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + 
+               alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
                   xhr.responseText);
             }
          });
@@ -245,7 +244,7 @@
          var button = $(this);
          button.attr('disabled', 'disabled');
          $.ajax({
-            url: '/oembed/fetch/', 
+            url: '/oembed/fetch/',
             type: 'POST',
             data: {
                q : vidText.val()
@@ -267,7 +266,7 @@
                });
             },
             error: function (xhr, textStatus, ex) {
-               alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + 
+               alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
                   xhr.responseText);
                button.removeAttr('disabled');
             }
@@ -287,4 +286,43 @@
    }
 
    initAttachments();
+
+   var topicTitle = $('#id_name');
+   var topicSearchButton = $('#search_topics');
+   var searchBusy = $('#search-busy-icon');
+   topicSearchButton.click(function () {
+      var text = $.trim(topicTitle.val());
+      if (!text) return;
+
+      $(this).attr('disabled', 'disabled');
+      $('#quick-search-results').remove();
+      searchBusy.toggle();
+
+      $.ajax({
+         url: '/search/ajax/',
+         type: 'GET',
+         data: {
+            q : text,
+            models : 'forums.topic'
+         },
+         traditional: true,
+         dataType: 'html',
+         success: function (data, textStatus) {
+            topicSearchButton.removeAttr('disabled');
+            searchBusy.hide();
+            searchBusy.after(data);
+            $('#hide-search-results').click(function() {
+               var results = $('#quick-search-results');
+               results.fadeOut(1500, function() { results.remove(); });
+            });
+         },
+         error: function (xhr, textStatus, ex) {
+            topicSearchButton.removeAttr('disabled');
+            searchBusy.hide();
+            alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
+               xhr.responseText);
+         }
+         });
+      return false;
+   });
 });
--- a/sg101/templates/forums/show_form.html	Sun May 26 13:29:44 2013 -0500
+++ b/sg101/templates/forums/show_form.html	Mon May 27 15:04:52 2013 -0500
@@ -2,7 +2,29 @@
 <form action="." method="post" id="forums_post_form">{% csrf_token %}
 <fieldset>
 <legend>{{ legend_text }}</legend>
+
+{% if legend_text == "New Topic" %}
+
+<p><label for="id_name">{{ form.name.label }}:</label> {{ form.name.errors }}{{ form.name }}
+<input type="button" id="search_topics" value="Quick Search" title="Search for similar topics" />
+<div id="topic_search_results">
+</div>
+<div id="topic-search-results">
+<img id="search-busy-icon" src="{{ STATIC_URL }}icons/ajax_busy.gif" alt="Please wait" style="display:none;"/>
+</div>
+</p>
+<p>{{ form.body.errors }}{{ form.body }}</p>
+{% if form.sticky %}
+<p><label for="id_sticky">{{ form.sticky.label }}:</label> {{ form.sticky.errors }}{{ form.sticky }}</p>
+{% endif %}
+{% if form.locked %}
+<p><label for="id_locked">{{ form.locked.label }}:</label> {{ form.locked.errors }}{{ form.locked }}</p>
+{% endif %}
+
+{% else %}
 {{ form.as_p }}
+{% endif %}
+
 {% comment_dialogs %}
 <input type="submit" value="{{ submit_value }}" {% if is_ajax %}id="forums-reply-post"{% endif %} />
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sg101/templates/search/search_ajax.html	Mon May 27 15:04:52 2013 -0500
@@ -0,0 +1,23 @@
+{% load core_tags %}
+<div id="quick-search-results">
+<fieldset><legend>Quick search results</legend>
+{% if page.object_list %}
+<ul>
+   {% for result in page.object_list %}
+      <li><a href="{{ result.object.get_absolute_url }}">{{ result.object.search_title }}</a></li>
+   {% endfor %}
+</ul>
+
+   {% if page.paginator.num_pages > 1 %}
+   <p>
+   First page of results shown. There are {{ page.paginator.num_pages }} pages of results total.
+   For more results, visit the
+   <a href="/search/?{% encode_params request.GET 'q' 'models' %}">search page</a>.
+   </p>
+   {% endif %}
+   <p><input type="button" id="hide-search-results" value="Hide" /></p>
+{% else %}
+   <p>No results found for <em>{{ query }}</em>.</p>
+</fieldset>
+</div>
+{% endif %}
--- a/sg101/urls.py	Sun May 26 13:29:44 2013 -0500
+++ b/sg101/urls.py	Mon May 27 15:04:52 2013 -0500
@@ -91,6 +91,10 @@
     url(r'^search/$',
         search_view_factory(form_class=CustomModelSearchForm, load_all=True),
         name='haystack_search'),
+    url(r'^search/ajax/$',
+        search_view_factory(template='search/search_ajax.html',
+                            form_class=CustomModelSearchForm, load_all=True),
+        name='haystack_search_ajax'),
 )
 
 # For serving media files in development only: