Browse Source

more search work

Casper Van Gheluwe 11 years ago
parent
commit
649ab0ccea
4 changed files with 113 additions and 4 deletions
  1. 1 0
      .gitignore
  2. 12 3
      flaskbb/forum/forms.py
  3. 1 1
      flaskbb/forum/views.py
  4. 99 0
      flaskbb/templates/forum/search_result.html

+ 1 - 0
.gitignore

@@ -18,6 +18,7 @@
 *.log
 *.sql
 *.sqlite
+whoosh_index
 
 # OS generated files #
 ######################

+ 12 - 3
flaskbb/forum/forms.py

@@ -13,7 +13,7 @@ import flask.ext.whooshalchemy
 from wtforms import TextAreaField, TextField, BooleanField, FormField, SelectMultipleField
 from wtforms.validators import Required
 
-from flaskbb.forum.models import Topic, Post, Report
+from flaskbb.forum.models import Topic, Post, Report, Forum, Category
 from flaskbb.user.models import User
 
 
@@ -73,7 +73,16 @@ class SearchForm(Form):
     def fetch_results(self):
         results = {}
         types = self.fetch_types()
+        query = self.search_query.data
         for type in types:
             if type == 'user':
-                results['user'] = User.query.whoosh_search(self.search_query)
-        print(results)
+                results['user'] = User.query.whoosh_search(query).all()
+            elif type == 'post':
+                results['post'] = Post.query.whoosh_search(query).all()
+            elif type == 'topic':
+                results['topic'] = Topic.query.whoosh_search(query).all()
+            elif type == 'forum':
+                results['forum'] = Forum.query.whoosh_search(query).all()
+            elif type == 'category':
+                results['category'] = Category.query.whoosh_search(query).all()
+        return results

+ 1 - 1
flaskbb/forum/views.py

@@ -514,8 +514,8 @@ def search_forum():
     if form.validate_on_submit():
         result_type = form.fetch_types()
         result_list = form.fetch_results()
+        print(result_list)
         return render_template("forum/search_result.html",
-                               type=result_type,
                                results=result_list)
     else:
         return render_template("forum/search.html", form=form)

+ 99 - 0
flaskbb/templates/forum/search_result.html

@@ -0,0 +1,99 @@
+{% set page_title = "Search Results" %}
+{% set active_forum_nav=True %}
+
+{% extends theme("layout.html") %}
+{% block content %}
+    {% from theme('macros.html') import render_pagination %}
+
+    {% if results['user'] %}
+    <table class="table table-bordered" style="vertical-align: middle">
+        <thead>
+        <tr>
+            <th colspan="7">
+                Users
+            </th>
+        </tr>
+        </thead>
+
+        <tbody>
+        <tr>
+            <td colspan="2">Username</td>
+            <td>E-mail</td>
+            <td>Joined</td>
+            <td>Last seen</td>
+            <td>Post count</td>
+        </tr>
+
+        {% for user in results['user'] %}
+            <tr>
+                <td>
+                    {% if user|is_online %}
+                        <span class="label label-success">Online</span>
+                    {% else %}
+                        <span class="label label-default">Offline</span>
+                    {% endif %}
+                </td>
+                <td>
+                    <a href="{{ user.url }}">{{ user.username }}</a>
+                </td>
+                <td>
+                    <a href="mailto:{{ user.email }}">{{ user.email }}</a>
+                </td>
+                <td>
+                    {{ user.date_joined|format_date('%b %d %Y') }}
+                </td>
+                <td>
+                    {%- if user.lastseen -%} {{ user.lastseen|time_since }} {%- else -%} Never seen {%- endif -%}
+                </td>
+                <td>
+                    {{ user.post_count }}
+                </td>
+            </tr>
+        {% endfor %}
+        </tbody>
+    </table>
+    {% endif %}
+
+    {% if results['post'] %}
+        <table class="table table-bordered" style="vertical-align: middle">
+            <thead>
+            <tr>
+                <th colspan="7">
+                    Posts
+                </th>
+            </tr>
+            </thead>
+
+            <tbody>
+
+            {% for post in results['post'] %}
+                <tr>
+                    <td >
+                <span class="pull-right">
+                    <strong>#{%- if posts.page == 1 -%} {{ loop.index }} {%- else -%} {{ loop.index + (posts.page - 1) * config["POSTS_PER_PAGE"] }} {%- endif -%}</strong>
+                </span>
+                <span class="pull-left">
+                    <a href="
+                    {%- if posts.page > 1 -%}
+                        {{ topic.url }}?page={{ posts.page }}#pid{{ post.id }}
+                    {%- else -%}
+                        {{ topic.url }}#pid{{ post.id }}
+                    {%- endif -%}
+                        ">{{ post.date_created|format_date('%d %B %Y') }}</a>
+                    {% if post.user_id and post.date_modified %}
+                        <small>
+                            (Last modified: {{ post.date_modified|format_date }} by
+                            <a href="{{ url_for('user.profile', username=post.modified_by) }}">
+                                {{ post.modified_by }}
+                            </a>.)
+                        </small>
+                    {% endif %}
+                </span>
+                    </td>
+                </tr>
+            {% endfor %}
+            </tbody>
+        </table>
+    {% endif %}
+
+{% endblock %}