Browse Source

search results, improved search form

Casper Van Gheluwe 11 years ago
parent
commit
237b56b7eb

+ 3 - 5
flaskbb/forum/forms.py

@@ -9,8 +9,7 @@
     :license: BSD, see LICENSE for more details.
     :license: BSD, see LICENSE for more details.
 """
 """
 from flask.ext.wtf import Form
 from flask.ext.wtf import Form
-import flask.ext.whooshalchemy
-from wtforms import TextAreaField, TextField, BooleanField, FormField, SelectMultipleField
+from wtforms import TextAreaField, TextField, SelectMultipleField
 from wtforms.validators import Required, Optional, Length
 from wtforms.validators import Required, Optional, Length
 
 
 from flaskbb.forum.models import Topic, Post, Report, Forum, Category
 from flaskbb.forum.models import Topic, Post, Report, Forum, Category
@@ -67,9 +66,9 @@ class UserSearchForm(Form):
 
 
 
 
 class SearchPageForm(Form):
 class SearchPageForm(Form):
-    search_query = TextField("Search", validators=[Required(), Length(min=3, max=50)])
+    search_query = TextField("Criteria", validators=[Required(), Length(min=3, max=50)])
     search_types = SelectMultipleField("Content", validators=[Required()], choices=[
     search_types = SelectMultipleField("Content", validators=[Required()], choices=[
-        ('post', 'Post'), ('topic', 'Topic'), ('forum', 'Forum'), ('category', 'Category'), ('user', 'Users')])
+        ('post', 'Post'), ('topic', 'Topic'), ('forum', 'Forum'), ('user', 'Users')])
 
 
     def get_results(self):
     def get_results(self):
         # Because the DB is not yet initialized when this form is loaded, the query objects cannot be instantiated
         # Because the DB is not yet initialized when this form is loaded, the query objects cannot be instantiated
@@ -78,7 +77,6 @@ class SearchPageForm(Form):
             'post': Post.query.whoosh_search,
             'post': Post.query.whoosh_search,
             'topic': Topic.query.whoosh_search,
             'topic': Topic.query.whoosh_search,
             'forum': Forum.query.whoosh_search,
             'forum': Forum.query.whoosh_search,
-            'category': Category.query.whoosh_search,
             'user': User.query.whoosh_search
             'user': User.query.whoosh_search
         }
         }
 
 

+ 1 - 1
flaskbb/forum/views.py

@@ -513,7 +513,7 @@ def search():
 
 
     if form.validate_on_submit():
     if form.validate_on_submit():
         result = form.get_results()
         result = form.get_results()
-        return render_template('forum/search_result.html', form=form, result=results)
+        return render_template('forum/search_result.html', form=form, result=result)
 
 
     return render_template('forum/search_form.html', form=form)
     return render_template('forum/search_form.html', form=form)
 
 

+ 17 - 14
flaskbb/templates/forum/search_form.html

@@ -1,23 +1,26 @@
 {% set page_title = "Search" %}
 {% set page_title = "Search" %}
 
 
 {% extends theme("layout.html") %}
 {% extends theme("layout.html") %}
-
-{% block navigation %}
-{% endblock %}
-
+{% from theme("macros.html") import horizontal_field %}
 {% block content %}
 {% block content %}
-    {% from theme("macros.html") import render_field %}
 
 
-    <form class="form" role="form" method="post">
+    <ul class="breadcrumb">
+        <li><a href="{{ url_for('forum.index') }}">Forum</a></li>
+        <li class="active">Search</li>
+    </ul>
+
+    <form class="form-horizontal" role="form" method="post">
+        <h2>Search</h2>
+        <hr>
         {{ form.hidden_tag() }}
         {{ form.hidden_tag() }}
-        <h3>Search</h3>
-        <div class="row">
-            {{ render_field(form.search_types) }}
-        </div>
-        <div class="row">
-            {{ render_field(form.search_query) }}
-        </div>
+        {{ horizontal_field(form.search_types)}}
+        {{ horizontal_field(form.search_query)}}
 
 
-        <button type="submit" class="btn btn-success">Search</button>
+        <div class="form-group">
+            <div class="col-sm-offset-3 col-sm-9">
+                <button type="submit" class="btn btn-success">Search</button>
+            </div>
+        </div>
     </form>
     </form>
+
 {% endblock %}
 {% endblock %}

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

@@ -0,0 +1,281 @@
+{% set page_title = "Search" %}
+
+{% extends theme("layout.html") %}
+{% block content %}
+    {% from theme('macros.html') import render_pagination, group_field, topic_pages %}
+
+    <ul class="breadcrumb">
+        <li><a href="{{ url_for('forum.index') }}">Forum</a></li>
+        <li class="active">Search</li>
+    </ul>
+
+    {% if result['post'] %}
+        <h3>Posts</h3>
+
+        <table class="table table-bordered">
+            <tbody>
+            {% if result['post'].all() %}
+                {% for post in result['post'].all() %}
+                <tr>
+                    <td>
+                        <table class="table table-borderless">
+                            <tr>
+                                {% if post.user_id %}
+                                    {% if post.user.avatar %}
+                                        <td width="1">
+                                            <img src="{{ post.user.avatar }}" alt="Avatar" height="100" width="100">
+                                        </td>
+                                    {% endif %}
+                                    <td>
+                                        <a href="{{ post.user.url }}">
+                                            <span style="font-weight:bold">{{ post.user.username }}</span> <!-- TODO: Implement userstyles -->
+                                        </a>
+                                        {%- if post.user|is_online %}
+                                            <span class="label label-success">Online</span>
+                                        {%- else %}
+                                            <span class="label label-default">Offline</span>
+                                        {%- endif %}
+                                        <div class="profile primary-group">
+                                            {{ post.user.primary_group.name }}
+                                        </div>
+                                    </td>
+
+                                    <td class="pull-right">
+                                        Posts: {{ post.user.post_count }}<br />
+                                        Registered since: {{ post.user.date_joined|format_date('%b %d %Y') }}<br />
+                                    </td>
+                                {% else %}
+                                    <td>
+                                        <strong>{{ post.username }}</strong>
+                                        <br />
+                                        Guest
+                                    </td>
+                                {% endif %}
+                            </tr>
+                        </table>
+                    </td>
+                </tr>
+
+                <tr>
+                    <td>
+                        <div class="post_body" id="pid{{ post.id }}">
+                            {% autoescape false %}
+                            {{ post.content|markup }}
+                            {% endautoescape %}
+                        </div>
+                    </td>
+                </tr>
+                {% endfor %}
+            {% else %}
+                <tr>
+                    <td>No posts found matching your search criteria</td>
+                </tr>
+            {% endif %}
+            </tbody>
+        </table>
+    {% endif %}
+
+    {% if result['user'] %}
+        <h3>Users</h3>
+
+        <table class="table table-bordered">
+            <thead>
+            <tr>
+                <th>#</th>
+                <th>Username</th>
+                <th>Posts</th>
+                <th>Date registered</th>
+                <th>Group</th>
+            </tr>
+            </thead>
+            <tbody>
+            {% if result['user'].all() %}
+                {% for user in result['user'].all() %}
+                    <tr>
+                        <td>{{ user.id }}</td>
+                        <td><a href="{{ user.url }}">{{ user.username }}</a></td>
+                        <td>{{ user.post_count }}</td>
+                        <td>{{ user.date_joined|format_date('%b %d %Y') }}</td>
+                        <td>{{ user.primary_group.name }}</td>
+                    </tr>
+                {% endfor %}
+            {% else %}
+                <tr>
+                    <td colspan="5">No topics found matching your search criteria</td>
+                </tr>
+            {% endif %}
+            </tbody>
+        </table>
+    {% endif %}
+
+    {% if result['topic'] %}
+        <h3>Topics</h3>
+
+        <table class="table table-bordered">
+            <thead>
+            <tr>
+                <th colspan="2">Topic</th>
+
+                <th>Posts</th>
+
+                <th>Views</th>
+
+                <th>Last Post</th>
+            </tr>
+            </thead>
+
+            <tbody>
+
+            {% if result['topic'].all() %}
+                {% for topic in result['topic'].all() %}
+                    <tr>
+                        <td width="4%">
+                            {% if topic.locked %}
+                                <span class="fa fa-locked" style="font-size: 2em"></span>
+                            {% else %}
+                                <span class="fa fa-comment-o" style="font-size: 2em"></span>
+                            {% endif %}
+
+                        </td>
+                        <td>
+                            <div>
+                                <a href="{{ topic.url }}">{{ topic.title }}</a>
+                                <!-- Topic Pagination -->
+                                {{ topic_pages(topic, config["POSTS_PER_PAGE"]) }}
+                                <br />
+                                {% if topic.user_id %}
+                                    <small>by <a href="{{ topic.user.url }}">{{ topic.user.username }}</a></small>
+                                {% else %}
+                                    <small>by {{ topic.username }}</small>
+                                {% endif %}
+                            </div>
+                        </td>
+                        <td>
+                            {{ topic.post_count }}
+                        </td>
+                        <td>
+                            {{ topic.views }}
+                        </td>
+                        <td>
+                            <a href="{{ topic.last_post.url }}">{{ topic.last_post.date_created|time_since }}</a><br />
+
+                            {% if topic.last_post.user_id %}
+                                <small>by <a href="{{ topic.last_post.user.url }}">{{ topic.last_post.user.username }}</a></small>
+                            {% else %}
+                                <small>{{ topic.last_post.username }}</small>
+                            {% endif %}
+                        </td>
+                    </tr>
+                {% endfor %}
+            {% else %}
+                <tr>
+                    <td colspan="5">No topics found matching your search criteria</td>
+                </tr>
+            {% endif %}
+
+            </tbody>
+        </table>
+    {% endif %}
+
+    {% if result['forum'] %}
+        <h3>Forums</h3>
+
+        <table class="table table-bordered">
+            <thead class="categoryhead">
+            <tr>
+                <th colspan="2"><strong>Forum</strong></th>
+                <th width="85" align="center" style="white-space: nowrap"><strong>Topics</strong></th>
+                <th width="85" align="center" style="white-space: nowrap"><strong>Posts</strong></th>
+                <th width="200" align="center" style="white-space: nowrap"><strong>Last Post</strong></th>
+            </tr>
+            </thead>
+            <tbody class="forumbody">
+            {% if result['forum'].all() %}
+                {% for forum in result['forum'].all() %}
+                <tr>
+                    <td align="center" valign="center" width="4%">
+
+                        {% if forum[0].external %}
+                        <span class="fa fa-external-link" style="font-size: 2em"></span>
+                    </td>
+
+                    <td valign="top">
+                        <strong><a href="{{ forum[0].external }}">{{ forum[0].title }}</a></strong>
+
+                        <div class="forum-description">
+                            {% autoescape false %}
+                            {{ forum[0].description|markup }}
+                            {% endautoescape %}
+                        </div>
+                    </td>
+
+                    <td valign="top" align="center" style="white-space: nowrap">-</td>
+                    <td valign="top" align="center" style="white-space: nowrap">-</td>
+                    <td valign="top" align="right" style="white-space: nowrap">-</td>
+                    <!-- End external -->
+                    {% else %}
+
+                    {% if forum[0].locked %}
+                        <span class="fa fa-lock" style="font-size: 2em"></span>
+                    {% elif forum[0]|forum_is_unread(forum[1], current_user) %}
+                        <span class="fa fa-comments" style="font-size: 2em"></span>
+                    {% else %}
+                        <span class="fa fa-comments-o" style="font-size: 2em"></span>
+                    {% endif %}
+
+                    </td>
+
+                    <td valign="top">
+                        <strong><a href="{{ forum[0].url }}">{{ forum[0].title }}</a></strong>
+
+                        <div class="forum-description">
+                            {% autoescape false %}
+                            {{ forum[0].description|markup }}
+                            {% endautoescape %}
+                            {% if forum[0].show_moderators %}
+                                <div class="forum-moderators">
+                                    Moderators:
+                                    {% for moderator in forum[0].moderators %}
+                                        <a href="{{ url_for('user.profile', username=moderator.username) }}">{{ moderator.username }}</a>{% if not loop.last %}, {% endif %}
+                                    {% endfor %}
+                                </div>
+                            {% endif %}
+                        </div>
+                    </td>
+
+                    <td valign="top" align="center" style="white-space: nowrap">{{ forum[0].topic_count }}</td>
+                    <td valign="top" align="center" style="white-space: nowrap">{{ forum[0].post_count }}</td>
+
+                    <td valign="top" align="right" style="white-space: nowrap">
+                        {% if forum[0].last_post_id %}
+                            <a href="{{ forum[0].last_post.url }}" title="{{ forum[0].last_post.topic.title }}">
+                                <strong>{{ forum[0].last_post.topic.title|crop_title }}</strong>
+                            </a>
+                            <br />
+                            {{ forum[0].last_post.date_created|time_since }}<br />
+
+                            {% if forum[0].last_post.user_id %}
+                                by <a href="{{ url_for('user.profile', username=forum[0].last_post.user.username) }}">{{ forum[0].last_post.user.username }}</a>
+                            {% else %}
+                                {{ forum[0].last_post.username }}
+                            {% endif %}
+
+                        {% else %}
+                            No posts
+                        {% endif %}
+                        {% endif %}
+                    </td>
+                </tr>
+                {% endfor %}
+            {% else %}
+                <tr>
+                    <td colspan="5">
+                        No forums found matching your search criteria
+                    </td>
+                </tr>
+            {% endif %}
+            </tbody>
+        </table>
+    {% endif %}
+
+{% endblock %}