-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from basxsoftwareassociation/feature/defaults…
…earchforbrowseviews Added default generic search functionality to BrowseView
- Loading branch information
Showing
4 changed files
with
154 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import django_countries | ||
from django.db import models | ||
from django.db.models import Q | ||
from django_countries.fields import CountryField | ||
|
||
|
||
def get_char_text_qset(fields, searchquery, prefix): | ||
char_text_fields = { | ||
f | ||
for f in fields | ||
if isinstance(f, models.fields.CharField) | ||
or isinstance(f, models.fields.TextField) | ||
} | ||
|
||
return { | ||
Q(**{prefix + "_".join((f.name, "_contains")): searchquery}) | ||
for f in char_text_fields | ||
} | ||
|
||
|
||
def get_country_qset(fields, searchquery, prefix): | ||
countries = {name.lower(): code for code, name in django_countries.countries} | ||
country_fields = {f for f in fields if isinstance(f, CountryField)} | ||
|
||
if not country_fields: | ||
return set() | ||
|
||
match_countries = { | ||
country_name for country_name in countries if searchquery in country_name | ||
} | ||
|
||
return { | ||
Q(**{prefix + f.name: countries[key]}) | ||
for f in country_fields | ||
for key in match_countries | ||
} | ||
|
||
|
||
def get_field_queryset(fields, searchquery, prefix="", follow_relationships=1): | ||
queryset = { | ||
*get_char_text_qset(fields, searchquery, prefix), | ||
*get_country_qset(fields, searchquery, prefix), | ||
} | ||
|
||
qs = Q() | ||
for query in queryset: | ||
qs |= query | ||
|
||
if follow_relationships > 0: | ||
foreignkey_fields = { | ||
f | ||
for f in fields | ||
if isinstance(f, models.fields.related.ForeignKey) | ||
or isinstance(f, models.fields.related.ManyToManyField) | ||
} | ||
for foreignkey_field in foreignkey_fields: | ||
# skip fields with a name beginning with '_' | ||
if foreignkey_field.name[0] == "_": | ||
continue | ||
|
||
if foreignkey_field.related_model: | ||
foreign_fields = foreignkey_field.related_model._meta.fields | ||
else: | ||
foreign_fields = foreignkey_field._meta.fields | ||
|
||
new_prefix = prefix + "__".join([foreignkey_field.name, ""]) | ||
qs |= get_field_queryset( | ||
foreign_fields, searchquery, new_prefix, follow_relationships - 1 | ||
) | ||
|
||
return qs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters