Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP grants/index: search functionality #2522

Merged
merged 3 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import retail.emails
import retail.views
import tdi.views
import grants.views
from dashboard.router import router as dbrouter
from external_bounties.router import router as ebrouter
from kudos.router import router as kdrouter
Expand Down Expand Up @@ -85,7 +86,9 @@
url(r'^api/v0.1/', include(kdrouter.urls)),
url(r'^actions/api/v0.1/', include(dbrouter.urls)), # same as active, but not cached in cluodfront
url(r'^api/v0.1/users_search/', dashboard.views.get_users, name='users_search'),
url(r'^api/v0.1/grants_search/', grants.views.get_grants, name='grants_search'),
url(r'^api/v0.1/kudos_search/', dashboard.views.get_kudos, name='kudos_search'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

# Health check endpoint
re_path(r'^health/', include('health_check.urls')),
re_path(r'^lbcheck/?', healthcheck.views.lbcheck, name='lbcheck'),
Expand Down
7 changes: 7 additions & 0 deletions app/assets/v2/css/grants/grant.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
background-color: #F2F6F9;
}

.grants-search .search i {
position: absolute;
right: 1.4rem;
top: 0.86rem;
display: none;
}

/* Tabs */
.ui-widget.ui-widget-content.ui-tabs {
background: transparent !important;
Expand Down
53 changes: 52 additions & 1 deletion app/assets/v2/js/grants/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
$(document).ready(() => {
const grants = {};

grants.search = () => {
$('#grants__search--text').on('click', event => {
event.preventDefault();
if ($('#grants__search--text').val())
$('#close-icon').show();
else
$('#close-icon').hide();
});

$('#grants__search--text').on('input', () => {
if ($('#grants__search--text').val())
$('#close-icon').show();
else
$('#close-icon').hide();
});

$('#close-icon').on('click', () => {
$('#grants__search--text').val('');
$('#close-icon').hide();
});

$('#search__grant').on('click', event => {
event.preventDefault();
const filter = $('#grants__search--text').val();

window.history.pushState('', '', '/grants?q=' + filter);
if (grants.search_request && grants.search_request.readyState !== 4) {
grants.search_request.abort();
}

const url = '/grants?q=' + filter;

location.replace(url);
grants.search_request = $.get(url, (results, status) => {
// TODO : Repaint Grants
});
});
};

grants.sort = () => {
$('#sort_option').select2({
minimumResultsForSearch: Infinity
});
$('.select2-selection__rendered').removeAttr('title');
};

$(document).ready(() => {
let keywords = getParam('q');

if (keywords)
$('#grants__search--text').val(keywords);

grants.search();
grants.sort();
});
5 changes: 3 additions & 2 deletions app/grants/templates/grants/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ <h1 class="font-title-xl">{% trans 'Dev Grants' %}</h1>
<form>
<div class="row">
<div class="search col-9 col-md-5 offset-md-1 col-lg-6 form__flex-group">
<input type="search" class="form__input" placeholder="Search Grants">
<input id="grants__search--text" type="search" class="form__input" placeholder="Search Grants">
<i id="close-icon" class="far fa-times-circle"></i>
</div>
<div class="col-2 col-lg-1 px-0">
<button class="button button--primary">
<button id="search__grant" class="button button--primary">
<span class="d-none d-sm-inline">{% trans 'Search' %}</span>
<i class="d-sm-none fas fa-search"></i>
</button>
Expand Down
1 change: 1 addition & 0 deletions app/grants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
from django.conf.urls import include, url

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F401 'django.conf.urls.include' imported but unused
F401 'django.conf.urls.url' imported but unused

from django.urls import path, re_path

from grants.views import grant_details, grant_fund, grant_new, grants, milestones, profile, subscription_cancel
Expand Down
41 changes: 40 additions & 1 deletion app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.http import HttpResponse, JsonResponse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F401 'django.http.JsonResponse' imported but unused


from grants.forms import MilestoneForm
from grants.models import Grant, Milestone, Subscription
from marketing.models import Keyword
from web3 import HTTPProvider, Web3


logger = logging.getLogger(__name__)
w3 = Web3(HTTPProvider(settings.WEB3_HTTP_PROVIDER))

Expand All @@ -50,8 +52,16 @@ def grants(request):
limit = request.GET.get('limit', 25)
page = request.GET.get('page', 1)
sort = request.GET.get('sort', '-created_on')
keyword = request.GET.get('q')

_grants = Grant.objects.all().order_by(sort)
if keyword:
grants_by_title = Grant.objects.filter(title__icontains=keyword)
grants_by_desc = Grant.objects.filter(description__icontains=keyword)
grants_by_url = Grant.objects.filter(reference_url__icontains=keyword)
grants_pks = (grants_by_desc | grants_by_title | grants_by_url).values_list('pk', flat=True)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F841 local variable 'grants_pks' is assigned to but never used

_grants = Grant.objects.filter(title__icontains = keyword).order_by(sort)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E251 unexpected spaces around keyword / parameter equals

else:
_grants = Grant.objects.all().order_by(sort)

paginator = Paginator(_grants, limit)
grants = paginator.get_page(page)
Expand All @@ -65,6 +75,35 @@ def grants(request):
return TemplateResponse(request, 'grants/index.html', params)


def get_grants(request):
"""Search grants by name, description and url."""

if request.is_ajax():
q = request.GET.get('q')
grants_by_title = Grant.objects.filter(title__icontains=q)
grants_by_desc = Grant.objects.filter(description__icontains=q)
grants_by_url = Grant.objects.filter(reference_url__icontains=q)
grants_pks = (grants_by_desc | grants_by_title | grants_by_url).values_list('pk', flat=True)
grants = Grant.objects.filter(pk__in=grants_pks).order_by('name')
results = []

for grant in grants:
_grant = {}
_grant['id'] = grant.id
_grant['active'] = grant.active
_grant['title'] = grant.title
_grant['description'] = grant.description
_grant['reference_url'] = grant.reference_url
# _grant['logo'] = grant.logo
results.append(_grant)

data = json.dumps(results)
else:
raise Http404
mimetype = 'application/json'
return HttpResponse(data, mimetype)


def grant_details(request, grant_id):
"""Display the Grant details page."""
profile = request.user.profile if request.user.is_authenticated else None
Expand Down