Skip to content

Commit

Permalink
Merge pull request #318 from jwjacobson/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jwjacobson authored Sep 21, 2024
2 parents 75f0ea7 + 00c9ff4 commit f3092cf
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions tune/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from .models import Tune


def search_field(tune_set, search_term):
split_term = search_term.split(":")
field, term = split_term[0], split_term[1]

def search_field(tune_set, field, term):
"""
Search a specific field for a term.
"""
if field.lower() == "keys":
term_query = tune_set.filter(
Q(tune__key__icontains=term) | Q(tune__other_keys__icontains=term)
Expand All @@ -31,6 +31,9 @@ def search_field(tune_set, search_term):


def exclude_term(tune_set, search_term):
"""
Exclude a term from a search.
"""
excluded_term = search_term[1:]

term_query = tune_set.exclude(
Expand All @@ -50,6 +53,9 @@ def exclude_term(tune_set, search_term):


def nickname_search(tune_set, search_term):
"""
Search for a composer by their nickname.
"""
nickname_query = tune_set.filter(
Q(tune__composer__icontains=Tune.NICKNAMES[search_term])
)
Expand All @@ -63,16 +69,17 @@ def query_tunes(tune_set, search_terms, timespan=None):
searches = set()

for term in search_terms:
if term and term[0] == "-":
# If the term begins with "-", exclude it from the search
if term.startswith("-"):
term_query = exclude_term(tune_set, term)

elif (
term
and len(term.split(":")) > 1
and term.split(":")[0].lower() in Tune.field_names
):
term_query = search_field(tune_set, term)
# If the term contains a colon, attempt field-specific search
elif ":" in term:
field, term = term.split(":", 1)
if field.lower() in Tune.field_names:
term_query = search_field(tune_set, field, term)

# Default, search all fields for the term
else:
term_query = tune_set.filter(
Q(tune__title__icontains=term)
Expand All @@ -98,7 +105,7 @@ def query_tunes(tune_set, search_terms, timespan=None):
search_results = searches.pop()

while searches:
search_results = search_results & searches.pop()
search_results &= searches.pop()

return search_results

Expand Down

0 comments on commit f3092cf

Please sign in to comment.