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

GITC-406: exact title match for grants search #9637

Merged
merged 14 commits into from
Nov 8, 2021
40 changes: 37 additions & 3 deletions app/assets/v2/js/grants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@ $(document).ready(() => {

});

function debounce(func, wait) {
let timeout;
let immediate;

return function() {
// reference the context and args for the setTimeout function
let context = this; let
args = arguments;

// allow user-triggered debounce from the func's supplied argument
if (args[0].debounce !== undefined && args[0].debounce === true)
immediate = false;
else if (args[0].debounce === undefined || args[0].debounce === false)
immediate = true;
chibie marked this conversation as resolved.
Show resolved Hide resolved

const callNow = immediate && !timeout;

clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
if (!immediate) {
// call the original function with apply
func.apply(context, args);
}
}, wait);

if (callNow)
func.apply(context, args);
};
}
chibie marked this conversation as resolved.
Show resolved Hide resolved

if (document.getElementById('grants-showcase')) {
const baseParams = {
Expand Down Expand Up @@ -69,7 +99,6 @@ if (document.getElementById('grants-showcase')) {
{'name': 'ALGORAND', 'label': 'Algorand'}
];


var appGrants = new Vue({
delimiters: [ '[[', ']]' ],
el: '#grants-showcase',
Expand Down Expand Up @@ -175,18 +204,23 @@ if (document.getElementById('grants-showcase')) {
vm.fetchGrants();

},
changeQuery: function(query) {
changeQuery: debounce(function(query) {
let vm = this;

vm.fetchedPages = [];
vm.$set(vm, 'params', {...vm.params, ...query});

if (vm.tabSelected === 'grants') {
if (vm.params.keyword && vm.params.sort_option == 'weighted_shuffle') {
vm.params.sort_option = '';
} else if (vm.params.keyword == '' && vm.params.sort_option != 'weighted_shuffle') {
vm.params.sort_option = 'weighted_shuffle';
chibie marked this conversation as resolved.
Show resolved Hide resolved
}
vm.fetchGrants();
} else {
vm.updateUrlParams();
}
},
}, 500),
filterCollection: async function(collection_id) {
let vm = this;

Expand Down
2 changes: 1 addition & 1 deletion app/grants/templates/grants/explorer.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
v-model="params.keyword"
@focus="searchVisible=true"
@blur="searchVisible=false"
@input="changeQuery({page: 1})"
@input="changeQuery({page: 1, debounce: true})"
@keyup.enter="changeQuery({page: 1})"/>
<div class="navCart dropdown gc-cart" v-if="!searchVisible && sticky_active">
<a href="" class="gc-cart__icon d-block" role="button" @click="$refs.navCart.init()"
Expand Down
1 change: 1 addition & 0 deletions app/grants/templates/grants/shared/top-filters.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
{label: 'Trending', value: '-metadata__upcoming'},
{label: 'Undiscovered Gems', value: '-metadata__gem'},
{label: 'Newest', value: '-created_on'},
{label: 'Most Relevant', value: ''},
{label: 'Match Estimate (This round)', value: '-clr_prediction_curve__0__1'},
{label: 'Amount Raised (This round)', value: '-amount_received_in_round'},
{label: 'Contributors (This round)', value: '-positive_round_contributor_count'},
Expand Down
37 changes: 21 additions & 16 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ def get_grants(request):
}
_grants = get_grants_by_filters(**filters)

if sort == '' and keyword:
# return grants result starting with exact title matches
exact_matches = [grant for grant in _grants if grant.title == keyword]
non_exact_matches = [grant for grant in _grants if grant.title != keyword]
_grants = exact_matches + non_exact_matches

if collection_id and collection_id.isnumeric():
# 4.1 Fetch grants by collection
_collections = get_collections(
Expand Down Expand Up @@ -680,24 +686,16 @@ def get_grants_by_filters(
contributions = profile.grant_contributor.filter(subscription_contribution__success=True).values('grant_id')
_grants = _grants.filter(id__in=Subquery(contributions))

if keyword:
# 6. Filter grants having matching title & description
_grants = _grants.annotate(search=SearchVector('description'))
keyword_query = Q(title__icontains=keyword)
keyword_query |= Q(search=keyword)

_grants = _grants.filter(keyword_query)

if not idle_grants:
# 7. Filter grants which are stale (last update was > 3 months )
# 6. Filter grants which are stale (last update was > 3 months )
_grants = _grants.filter(last_update__gt=three_months_ago)

if state == 'active':
# 8. Filter grants which are active
# 7. Filter grants which are active
_grants = _grants.active()

if grant_types and grant_types not in ['all', 'collections']:
# 9. Fetch grants which have mentioned grant_types
# 8. Fetch grants which have mentioned grant_types
types= grant_types.split(',')
type_query= Q()
for grant_type in types:
Expand All @@ -706,21 +704,21 @@ def get_grants_by_filters(
_grants = _grants.filter(type_query)

if following and request.user.is_authenticated:
# 10. Filter grants having matching title & description
# 9. Filter grants having matching title & description
favorite_grants = Favorite.grants().filter(user=request.user).values('grant_id')
_grants = _grants.filter(id__in=Subquery(favorite_grants))

if grant_tags:
tags = grant_tags.split(',')
# 11. Fetch grants which have mentioned grant_tags
# 10. Fetch grants which have mentioned grant_tags
tag_query= Q()
for tag in tags:
tag_query |= Q(tags__name=tag)

_grants = _grants.filter(tag_query)

if tenants:
# 12. Fetch grants which have mentioned tenants
# 11. Fetch grants which have mentioned tenants
tenant_query = Q()
for tenant in tenants.split(','):
if tenant in tenants:
Expand All @@ -733,15 +731,22 @@ def get_grants_by_filters(

if grant_regions:
regions = grant_regions.split(',')
# 13. Fetch grants which have mentnioned regions
# 12. Fetch grants which have mentioned regions
region_query= Q()
for region in regions:
region_query |= Q(region=region)
_grants = _grants.filter(region_query)

if keyword:
# 13. Grant search by title & description
_grants = _grants.annotate(search=SearchVector('description'))
keyword_query = Q(title__icontains=keyword)
keyword_query |= Q(search=keyword)

_grants = _grants.filter(keyword_query)

chibie marked this conversation as resolved.
Show resolved Hide resolved
# 13. Sort filtered grants
if sort:
# 14. Sort filtered grants
if 'match_pledge_amount_' in sort:
order = '-' if sort[0] is '-' else ''
sort_by_clr_pledge_matching_amount = int(sort.split('amount_')[1])
Expand Down