Skip to content

Commit

Permalink
Merge pull request #320 from jwjacobson/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jwjacobson authored Sep 23, 2024
2 parents e37452f + 3445239 commit bb0a50a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 55 deletions.
20 changes: 4 additions & 16 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@

from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(dsn=os.environ.get("SENTRY_DSN"), integrations=[DjangoIntegration()])
sentry_sdk.init(
dsn=os.environ.get("SENTRY_DSN"), integrations=[DjangoIntegration()]
)

EMAIL_HOST = "smtp.sendgrid.net"
EMAIL_HOST_USER = "apikey"
Expand Down Expand Up @@ -131,20 +133,7 @@
# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", # noqa: E501
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", # noqa: E501
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", # noqa: E501
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", # noqa: E501
},
]
AUTH_PASSWORD_VALIDATORS = []

INTERNAL_IPS = [
"127.0.0.1",
Expand Down Expand Up @@ -184,7 +173,6 @@

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# sentry_sdk.init(
# dsn=os.environ.get("SENTRY_DSN"),
# # Set traces_sample_rate to 1.0 to capture 100%
Expand Down
43 changes: 33 additions & 10 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ def test_exclude_term(tune_set):
def test_search_field_title(tune_set):
field = "title"
term = "you"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_titles = {"All the Things You Are", "I Remember You"}

assert result.count() == 2
Expand All @@ -260,7 +261,22 @@ def test_search_field_title(tune_set):
def test_search_field_composer(tune_set):
field = "composer"
term = "parker"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_composer = "Parker"
expected_titles = {"Confirmation", "Dewey Square"}

assert result.count() == 2
for tune in result:
assert tune.tune.composer == expected_composer
assert tune.tune.title in expected_titles


def test_search_field_composer_nickname(tune_set):
field = "composer"
term = "bird"
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_composer = "Parker"
expected_titles = {"Confirmation", "Dewey Square"}

Expand All @@ -272,8 +288,9 @@ def test_search_field_composer(tune_set):

def test_search_field_key(tune_set):
field = "key"
term = "f"
result = search_field(tune_set["tunes"], field, term)
term = "F"
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_key = "F"
expected_titles = {"Confirmation", "Long Ago and Far Away", "I Remember You"}

Expand All @@ -286,7 +303,8 @@ def test_search_field_key(tune_set):
def test_search_field_keys(tune_set):
field = "keys"
term = "eb"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_key = "Eb"
expected_titles = {
"Dewey Square",
Expand All @@ -303,7 +321,8 @@ def test_search_field_keys(tune_set):
def test_search_field_form(tune_set):
field = "form"
term = "abac"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_form = "ABAC"
expected_titles = {
"Dearly Beloved",
Expand All @@ -320,7 +339,8 @@ def test_search_field_form(tune_set):
def test_search_field_style(tune_set):
field = "style"
term = "jazz"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_style = "jazz"
expected_titles = {
"Confirmation",
Expand All @@ -339,7 +359,8 @@ def test_search_field_style(tune_set):
def test_search_field_meter(tune_set):
field = "meter"
term = "3"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_meter = 3
expected_titles = {"Someday My Prince Will Come"}

Expand All @@ -352,7 +373,8 @@ def test_search_field_meter(tune_set):
def test_search_field_year(tune_set):
field = "year"
term = "1941"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_year = 1941
expected_titles = {"I Remember You", "A Flower is a Lovesome Thing"}

Expand All @@ -365,7 +387,8 @@ def test_search_field_year(tune_set):
def test_search_field_year_partial(tune_set):
field = "year"
term = "195"
result = search_field(tune_set["tunes"], field, term)
query = search_field(tune_set["tunes"], field, term)
result = tune_set["tunes"].filter(query)
expected_years = {1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959}
expected_titles = {"Kary's Trance", "Coming on the Hudson"}

Expand Down
61 changes: 32 additions & 29 deletions tune/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ 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)
)
if field.lower() == "key":
return Q(tune__key__exact=term)

elif field.lower() == "keys":
return Q(tune__key__icontains=term) | Q(tune__other_keys__icontains=term)

elif field.lower() == "form":
if term.lower() == "blues" or term.lower() == "irregular":
term_query = tune_set.filter(Q(tune__song_form=term))
return Q(tune__song_form=term)
else:
term_query = tune_set.filter(Q(tune__song_form=term.upper()))
return Q(tune__song_form=term.upper())

elif field.lower() == "tags":
term_query = tune_set.filter(Q(tags__name__icontains=term))
return Q(tags__name__icontains=term)

else:
term_query = tune_set.filter(Q(**{f"tune__{field}__icontains": term}))
elif field.lower() == "composer" and term in Tune.NICKNAMES:
return nickname_search(tune_set, term)

return term_query
else:
return Q(**{f"tune__{field}__icontains": term})


def exclude_term(tune_set, search_term):
Expand Down Expand Up @@ -56,32 +58,32 @@ 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])
)
nickname_query = Q(tune__composer__icontains=Tune.NICKNAMES[search_term])
return nickname_query


def query_tunes(tune_set, search_terms, timespan=None):
"""
Run a search of the user's repertoire and return the results.
"""
searches = set()
combined_query = Q()

for term in search_terms:
# If the term begins with "-", exclude it from the search
negate = False

if term.startswith("-"):
term_query = exclude_term(tune_set, term)
negate = True
term = term[1:]

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

# Default, search all fields for the term
# Default, search across all fields
else:
term_query = tune_set.filter(
term_query = (
Q(tune__title__icontains=term)
| Q(tune__composer__icontains=term)
| Q(tune__key__icontains=term)
Expand All @@ -94,20 +96,21 @@ def query_tunes(tune_set, search_terms, timespan=None):
| Q(tags__name__icontains=term)
)

# If the term is a nickname, add in the nickname search
if term in Tune.NICKNAMES:
term_query |= nickname_search(tune_set, term)

if timespan is not None:
term_query = term_query.exclude(last_played__gte=timespan)
if negate:
term_query = ~term_query

searches.add(term_query)
combined_query &= term_query

search_results = searches.pop()
tune_set = tune_set.filter(combined_query)

while searches:
search_results &= searches.pop()
if timespan is not None:
tune_set = tune_set.exclude(last_played__gte=timespan)

return search_results
return tune_set


def return_search_results(request, search_terms, tunes, search_form, timespan=None):
Expand Down

0 comments on commit bb0a50a

Please sign in to comment.