Changeset 49:76c064ef79a1


Ignore:
Timestamp:
Mar 26, 2009 9:57:57 PM (4 years ago)
Author:
alafin <alafin@…>
Branch:
default
Message:

add #2 forum search with djapian engine

Files:
21 added
5 edited

Legend:

Unmodified
Added
Removed
  • apps/account/settings.py

    r0 r49  
    11from django.conf import settings 
    2  
    32 
    43ACCOUNT_DOMAIN = getattr(settings, 'ACCOUNT_DOMAIN', 'fix.your.settings.com') 
  • apps/forum/templates/forum/search_posts.html

    r0 r49  
    99        </div> 
    1010</div> 
    11  
     11{% if posts %} 
    1212{% for post in posts %} 
    1313<div class="blockpost searchposts roweven"> 
    14         <h2>{% link post.topic.forum %}&nbsp;&raquo;&nbsp;{% link post.topic %}&nbsp;&raquo;&nbsp;<a href="{{ post.get_absolute_url }}">{% forum_time post.created %}</a></h2> 
     14        <h2>{% link post.instance.topic.forum %}&nbsp;&raquo;&nbsp;{% link post.instance.topic %}&nbsp;&raquo;&nbsp;<a href="{{ post.instance.get_absolute_url }}">{% forum_time post.instance.created %}</a></h2> 
    1515 
    1616        <div class="box"> 
     
    1818                        <div class="postleft"> 
    1919                                <dl> 
    20                                         <dt><strong><a href="{% url forum_profile post.user %}">{{ post.user }}</a></strong></dt> 
    21                                         <dd>{% trans "Replies:" %} {{ post.topic.post_count }}</dd> 
     20                                        <dt><strong><a href="{% url forum_profile post.instance.user %}">{{ post.instance.user }}</a></strong></dt> 
     21                                        <dd>{% trans "Replies:" %} {{ post.instance.topic.post_count }}</dd> 
    2222                                        <dd><div class="icon"><div class="nosize"><!-- --></div></div> 
    2323</dd> 
    2424 
    25                                         <dd><p class="clearb"><a href="{{ post.get_absolute_url }}">{% trans "Go to post" %}</a></p></dd> 
     25                                        <dd><p class="clearb"><a href="{{ post.instance.get_absolute_url }}">{% trans "Go to post" %}</a></p></dd> 
    2626                                </dl> 
    2727                        </div> 
    2828                        <div class="postright"> 
    2929                                <div class="postmsg"> 
    30                                         {{ post.body_html|safe }} 
     30                                        {{ post.instance.body_html|safe }} 
    3131 
    3232                                </div> 
     
    3737</div> 
    3838{% endfor %} 
     39{% else %} 
     40<div id="msg" class="block"> 
     41        <h2><span>{% trans "Info" %}</span></h2> 
     42        <div class="box"> 
     43 
     44                <div class="inbox"> 
     45                <p>{% trans "Your search returned no hits." %}</p> 
     46                <p><a href="javascript: history.go(-1)">{% trans "Go back" %}</a></p> 
     47                </div> 
     48        </div> 
     49</div> 
     50{% endif %} 
    3951 
    4052<div class="postlinksb"> 
  • apps/forum/templates/forum/search_topics.html

    r42 r49  
    1010        </div> 
    1111</div> 
    12  
     12{% if topics %} 
    1313<div id="vf" class="blocktable"> 
    1414        <h2><span>{% trans "Search results" %}</span></h2> 
     
    5050        </div> 
    5151</div> 
     52{% else %} 
     53<div id="msg" class="block"> 
     54        <h2><span>{% trans "Info" %}</span></h2> 
     55        <div class="box"> 
     56 
     57                <div class="inbox"> 
     58                <p>{% trans "Your search returned no hits." %}</p> 
     59                <p><a href="javascript: history.go(-1)">{% trans "Go back" %}</a></p> 
     60                </div> 
     61        </div> 
     62</div> 
     63{% endif %} 
    5264 
    5365<div class="linksb"> 
  • apps/forum/views.py

    r47 r49  
    2020from apps.forum import settings as forum_settings 
    2121from apps.forum.util import urlize, smiles 
     22from apps.forum.index import post_indexer 
    2223 
    2324@render_to('forum/index.html') 
     
    118119@render_to('forum/search_topics.html') 
    119120def search(request): 
     121    post_indexer.update() 
    120122    if 'action' in request.GET: 
    121123        action = request.GET['action'] 
     
    134136            posts = Post.objects.filter(user__id=user_id) 
    135137            topics = [post.topic for post in posts] 
    136         elif action == 'search': 
    137             posts = Post.objects.all() 
    138             form = PostSearchForm(request.GET) 
    139             posts = form.filter(posts) 
    140             topics = [post.topic for post in posts] 
    141             if action == 'topics': 
    142                 return {'topics': topics} 
     138        elif action == 'search':           
     139            keywords = request.GET.get('keywords') 
     140            author = request.GET.get('author') 
     141            forum = request.GET.get('forum') 
     142            search_in = request.GET.get('search_in') 
     143            sort_by = request.GET.get('sort_by') 
     144            sort_dir = request.GET.get('sort_dir') 
     145             
     146            # TODO: Need refactoring  
     147            if keywords and author: 
     148                if search_in == 'all': 
     149                    if forum == '0': 
     150                        query = 'user:%s AND (topic:%s OR body:%s)' % (author, keywords, keywords) 
     151                    else: 
     152                        query = 'user:%s AND forum:%s AND (topic:%s OR body:%s)' % (author, forum, keywords, keywords) 
     153                elif search_in == 'message': 
     154                    if forum == '0': 
     155                        query = 'user:%s AND body:%s' % (author, keywords) 
     156                    else: 
     157                        query = 'user:%s AND forum:%s AND body:%s' % (author, forum, keywords) 
     158                elif search_in == 'topic': 
     159                    if forum == '0': 
     160                        query = 'user:%s AND topic:%s' % (author, keywords) 
     161                    else: 
     162                        query = 'user:%s AND forum:%s AND topic:%s' % (author, forum, keywords) 
     163            elif keywords: 
     164                if search_in == 'all': 
     165                    if forum == '0': 
     166                        query = 'topic:%s OR body:%s' % (keywords, keywords) 
     167                    else: 
     168                        query = 'forum:%s AND (topic:%s OR body:%s)' % (forum, keywords, keywords) 
     169                elif search_in == 'message': 
     170                    if forum == '0': 
     171                        query = 'body:%s' % (keywords) 
     172                    else: 
     173                        query = 'forum:%s AND body:%s' % (forum, keywords) 
     174                elif search_in == 'topic': 
     175                    if forum == '0': 
     176                        query = 'topic:%s' % (keywords) 
     177                    else: 
     178                        query = 'forum:%s AND topic:%s' % (forum, keywords) 
     179            elif author: 
     180                if forum == '0': 
     181                    query = 'user:%s' % (author) 
     182                else: 
     183                    query = 'forum:%s AND user:%s' % (forum, author) 
     184 
     185            if sort_by == '0': 
     186                order = 'created' 
     187            elif sort_by == '1': 
     188                order = 'user' 
     189            elif sort_by == '2': 
     190                order = 'topic' 
     191            elif sort_by == '3': 
     192                order = 'forum' 
     193             
     194            if sort_dir == 'DESC': 
     195                order = '-' + order 
     196            posts = post_indexer.search(query).order_by(order) 
     197             
     198            if 'topics' in request.GET['show_as']: 
     199                topics = [] 
     200                for post in posts: 
     201                    if post.instance.topic not in topics: 
     202                        topics.append(post.instance.topic) 
     203                return {'topics': topics}, 'forum/search_topics.html' 
    143204            elif 'posts' in request.GET['show_as']: 
    144205                return {'posts': posts}, 'forum/search_posts.html' 
     
    305366    if form.is_valid(): 
    306367        post = form.save(); 
     368        post_indexer.update() 
    307369        return HttpResponseRedirect(post.get_absolute_url()) 
    308370 
     
    477539    if form.is_valid(): 
    478540        post = form.save() 
     541        post_indexer.update() 
    479542        return HttpResponseRedirect(post.get_absolute_url()) 
    480543 
     
    592655 
    593656    post.delete() 
     657    post_indexer.update() 
    594658    profile = get_object_or_404(Profile, user=post.user) 
    595659    profile.post_count = Post.objects.filter(user=post.user).count() 
  • settings.py

    r45 r49  
    116116    'forum', 
    117117    'djapian', 
     118    'django_evolution', 
    118119) 
    119120 
     
    138139ACCOUNT_AUTH_KEY_TIMEOUT = 60 * 60 * 24 
    139140 
     141# Djapian settings 
     142DJAPIAN_DATABASE_PATH = os.path.join(PROJECT_ROOT, 'djapian_db') 
     143 
    140144try: 
    141     from local_settings import * 
     145   from local_settings import * 
    142146except ImportError: 
    143147    pass 
Note: See TracChangeset for help on using the changeset viewer.