Skip to content

Commit

Permalink
Update: fixed and optimised keyword search
Browse files Browse the repository at this point in the history
  • Loading branch information
awf-dbca committed Feb 28, 2024
1 parent c71c0c6 commit dd8663d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
47 changes: 32 additions & 15 deletions wildlifecompliance/components/main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,33 +428,50 @@ def search_keywords(search_words, search_application, search_licence, search_ret
application_list = []
licence_list = []
return_list = []
if is_internal:
application_list = Application.objects.all()\
.computed_exclude(processing_status__in=[
Application.PROCESSING_STATUS_DISCARDED
])\
.order_by('lodgement_number', '-id')
licence_list = WildlifeLicence.objects.all()\
.order_by('licence_number', '-id')\
.distinct('licence_number')
return_list = Return.objects.all()\
.order_by('lodgement_number', '-id')
if search_words:
search_words_regex = "(?:"
for i in range(0,len(search_words)):
search_words_regex = search_words_regex + search_words[i]
if i == len(search_words)-1:
search_words_regex = search_words_regex + ")"
else:
search_words_regex = search_words_regex + "|"

#filter_regex = ".*\".*\":\s\"(\\\\\"|[^\"])*"+search_words_regex+"(\\\\\"|[^\"])*\".*"
if is_internal:
application_list = Application.objects.all().filter(form_data_records__value__iregex=search_words_regex)\
.order_by('lodgement_number', '-id')\
.distinct('lodgement_number')
#.computed_exclude(processing_status__in=[
# Application.PROCESSING_STATUS_DISCARDED
#])\
licence_list = WildlifeLicence.objects\
.filter(licence_number__iregex=search_words_regex)\
.order_by('licence_number', '-id')\
.distinct('licence_number')
return_list = Return.objects.all()\
.filter(lodgement_number__iregex=search_words_regex)\
.order_by('lodgement_number', '-id')\
.distinct('lodgement_number')

if search_application:
for app in application_list:
if app.data:
try:
app_data = {'data': []}
for record in app.data:
if 'thead' in record.value:
app_data.get('data').append({'value': ast.literal_eval(record.value).get('tbody')})
try:
app_data.get('data').append({'value': ast.literal_eval(record.value).get('tbody')})
except:
app_data.get('data').append({'value': record.value})
else:
app_data.get('data').append({'value': record.value})
results = search(app_data, search_words)
final_results = {}
if results:
for r in results:
for key, value in r.iteritems():
for key, value in r.items():
final_results.update({'key': key, 'value': value})
res = {
'number': app.lodgement_number,
Expand All @@ -479,7 +496,7 @@ def search_keywords(search_words, search_application, search_licence, search_ret
})
if results:
for r in results:
for key, value in r.iteritems():
for key, value in r.items():
final_results.update({'key': key, 'value': value})
res = {
'number': lic.licence_number,
Expand All @@ -504,7 +521,7 @@ def search_keywords(search_words, search_application, search_licence, search_ret
})
if results:
for r in results:
for key, value in r.iteritems():
for key, value in r.items():
final_results.update({'key': key, 'value': value})
res = {
'number': ret.lodgement_number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@
<div class="row">
<div class="col-lg-12">
<div>
<input :disabled="!hasSearchKeywords" type="button" @click.prevent="searchKeyword" class="btn btn-primary" style="margin-bottom: 5px"value="Search"/>
<input type="reset" @click.prevent="clearKeywordSearch" class="btn btn-primary" style="margin-bottom: 5px"value="Clear"/>
<button v-if="searching" type="button" class="btn btn-primary" style="margin-bottom: 5px" value="Search" disabled>
Search<i class="fa fa-circle-o-notch fa-spin fa-fw"></i></button>
<input v-else type="button" @click.prevent="searchKeyword" class="btn btn-primary" style="margin-bottom: 5px" value="Search" :disabled="!hasSearchKeywords"/>
<input type="reset" @click.prevent="clearKeywordSearch" class="btn btn-primary" style="margin-bottom: 5px" value="Clear"/>
</div>
</div>
</div>
Expand Down Expand Up @@ -191,6 +193,7 @@ export default {
searchReturn: false,
referenceWord: '',
keyWord: null,
searching:false,
results: [],
errors: false,
errorString: '',
Expand Down Expand Up @@ -400,6 +403,7 @@ export default {
let vm = this;
if(this.searchKeywords.length > 0)
{
vm.searching=true;
vm.$http.post('/api/search_keywords.json',{
searchKeywords: vm.searchKeywords,
searchApplication: vm.searchApplication,
Expand All @@ -411,9 +415,11 @@ export default {
vm.$refs.keyword_search_datatable.vmDataTable.clear()
vm.$refs.keyword_search_datatable.vmDataTable.rows.add(vm.results);
vm.$refs.keyword_search_datatable.vmDataTable.draw();
vm.searching=false;
},
err => {
console.log(err);
vm.searching=false;
});
}
},
Expand Down
8 changes: 4 additions & 4 deletions wildlifecompliance/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def serialize_export(app_obj):
ap = ActivityPurposeMap()
# cross-reference name_label_pairs with key_value_pairs, and combine
for key_value in key_value_pairs:
for k, v in key_value.iteritems():
for k, v in key_value.items():
name = k.split('.')[-1]
activity = k.split('.')[0]
try:
Expand Down Expand Up @@ -171,7 +171,7 @@ def search(dictionary, search_list=[''], delimiter='.'):
"""
result = []
flat_dict = flatten(dictionary, delimiter=delimiter)
for k, v in flat_dict.iteritems():
for k, v in flat_dict.items():
if any(x.lower() in str(v).lower() for x in search_list):
result.append({k: str(v)})

Expand All @@ -191,7 +191,7 @@ def search_keys(dictionary, search_list=['help_text', 'label']):
search_item2 = search_list[1]
result = []
flat_dict = flatten(dictionary)
for k, v in flat_dict.iteritems():
for k, v in flat_dict.items():
if any(x in k for x in search_list):
result.append({k: v})

Expand Down Expand Up @@ -242,7 +242,7 @@ def search_multiple_keys(
all_search_list = [primary_search] + search_list
result = []
flat_dict = flatten(dictionary)
for k, v in flat_dict.iteritems():
for k, v in flat_dict.items():
if any(x in k for x in all_search_list):
result.append({k: v})

Expand Down

0 comments on commit dd8663d

Please sign in to comment.