-
-
Notifications
You must be signed in to change notification settings - Fork 775
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
""" | ||
from django.conf.urls import include, url | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F401 'django.conf.urls.include' 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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