Skip to content

Commit

Permalink
DigitalCampus#302: Added quicksearch (functioning in combination with…
Browse files Browse the repository at this point in the history
… advanced search)
  • Loading branch information
jjoseba committed Apr 18, 2016
1 parent 0d4acc4 commit 6235fbc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions oppia/profile/forms.py
Expand Up @@ -27,6 +27,7 @@ class LoginForm(forms.Form):
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.form_action = reverse('profile_login')
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-2'
Expand Down
22 changes: 20 additions & 2 deletions oppia/profile/views.py
Expand Up @@ -12,7 +12,7 @@
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.db import IntegrityError
from django.db.models import Count, Max, Min, Avg
from django.db.models import Count, Max, Min, Avg, Q
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response
from django.template import RequestContext
Expand Down Expand Up @@ -407,6 +407,18 @@ def upload_view(request):
'results': results},
context_instance=RequestContext(request),)

def get_query(query_string, search_fields):
''' Returns a query, that is a combination of Q objects. That combination
aims to search keywords within a model by testing the given search fields.
'''
query = None # Query to search in every field
for field_name in search_fields:
q = Q(**{"%s__icontains" % field_name: query_string})
query = q if query is None else (query | q)

return query

def search_users(request):

if not request.user.is_staff:
Expand All @@ -428,7 +440,6 @@ def search_users(request):
filters["%s__icontains" % row] = search_form.cleaned_data[row]
else:
filters[row] = search_form.cleaned_data[row]

if filters:
print filters
users = users.filter(**filters)
Expand All @@ -437,6 +448,12 @@ def search_users(request):
if not filtered:
users = users.all()

query_string = None
if ('q' in request.GET) and request.GET['q'].strip():
query_string = request.GET['q']
filter_query = get_query(query_string, ['username','first_name', 'last_name','email',])
users = users.filter(filter_query)

ordering = request.GET.get('order_by', None)
if ordering is None:
ordering = 'first_name'
Expand All @@ -458,6 +475,7 @@ def search_users(request):

return render_to_response('oppia/profile/search_user.html',
{
'quicksearch':query_string,
'search_form':search_form,
'advanced_search':filtered,
'page': users,
Expand Down
13 changes: 7 additions & 6 deletions oppia/templates/oppia/profile/search_user.html
Expand Up @@ -13,34 +13,35 @@ <h2>{% trans 'Search users' %}</h2>

<div class="search-block">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<form method="get" class="form-horizontal">
<div class="panel panel-default">

<div class="panel-heading" role="tab" id="headingOne">
<div class="row">
<div class="col-sm-3 col-xs-10">
<form method="get">

<div class="input-group">

<input type="text" name="quicksearch" class="form-control" aria-label="{% trans 'Search' %}" placeholder="{% trans 'Search...' %}">
<input type="text" name="q" {% if quicksearch %}value="{{ quicksearch }}" {% endif %}class="form-control" aria-label="{% trans 'Search' %}" placeholder="{% trans 'Search...' %}">
<span class="input-group-btn"><button class="btn" type="submit" ><span class="glyphicon glyphicon-search"></button></span></span>

</div>
</form>

</div>
<div class="col-sm-9 col-xs-2 text-right">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"><span class="glyphicon glyphicon-plus-sign"></span> <span class="hidden-xs">advanced search </span></a>
</div>
</div>

</div>
<div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
<div id="collapseOne" class="panel-collapse {% if not advanced_search %}collapse{% endif %}" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
{% crispy search_form %}
</div>
</div>
</form>
</div>

</div>
</form>
</div>

{% include "oppia/profile/users_paginated_list.html" %}
Expand Down

0 comments on commit 6235fbc

Please sign in to comment.