Skip to content

Commit

Permalink
Merge pull request #264 from jwjacobson/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jwjacobson authored Apr 14, 2024
2 parents 5f82c87 + 593fdc3 commit 9512afc
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 78 deletions.
13 changes: 7 additions & 6 deletions project/templates/account/signup.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{% extends "base.html" %}
{% load allauth i18n %}
{% block head_title %}
{% trans "Signup" %}
{% endblock head_title %}
<!-- {% load allauth i18n %} -->
<!-- {% block head_title %} -->
<!-- {% trans "Signup" %} -->
<!-- {% endblock head_title %} -->
{% block content %}
<div class="row my-4">
<div class="col-6">
<div class="card bg-success-subtle border-primary"style=" border-radius: 0;">
<div class="card-body">
<h3 class = "card-title text-info-emphasis">Sign up</h3>
<p class="card-text">
{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}
Already have an account? <a href="{{ login_url }}">Sign in here</a>.
</p>
{% url 'account_signup' as action_url %}
{% element form form=form method="post" action=action_url tags="entrance,signup" %}

{{ form.non_field_errors | safe }}
<input type="hidden" name="remember" value="true" />

Expand All @@ -35,4 +36,4 @@ <h3 class = "card-title text-info-emphasis">Sign up</h3>
</div>
</div>
</div>
{% endblock content %}
{% endblock content %}
2 changes: 1 addition & 1 deletion tune/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def clean_key(self):
data = self.cleaned_data["key"]
if data and data.lower() not in Tune.KEYS:
raise ValidationError(
{_('Invalid key (all normal keys accepted plus "none" and "atonal").')}
'Invalid key (all normal keys accepted plus "none" and "atonal").'
)
return data.title()

Expand Down
4 changes: 1 addition & 3 deletions tune/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ class Tune(models.Model):
}

title = models.CharField(max_length=90, help_text="The only required field")
composer = models.CharField(max_length=30, blank=True, help_text="Last names only for now")
composer = models.CharField(max_length=30, blank=True)
key = models.CharField(
max_length=10,
blank=True,
help_text="Letters A-G, one only, add - for minor",
)
other_keys = models.CharField(
max_length=20,
blank=True,
help_text="Letters A-G, as many as you want, separated by a space",
)
song_form = models.CharField(choices=FORMS, max_length=15, blank=True)
style = models.CharField(choices=STYLES, max_length=15, blank=True, default="standard")
Expand Down
2 changes: 1 addition & 1 deletion tune/templates/tune/_another_button.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="playTuneWrapper">
<div class="card bg-danger border-primary" style="height: 200px; max-width: 22rem; border-radius: 0;">
<div class="card bg-danger border-primary" style="max-width: 22rem; border-radius: 0;">
<div class="card-body">
{% if selected_tune %}
<h5 class = "card-title">You should play</h5>
Expand Down
8 changes: 4 additions & 4 deletions tune/templates/tune/browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ <h4>
</table>
</div>
<script>
$(document).ready( function () {
$(document).ready(function () {
$('#public-table').DataTable({
"searching": false,
"info": false,
"lengthChange": false,
"paging": false,
"language": {emptyTable: ' '}
);
"language": { "emptyTable": "No data available" }
});
});
</script>
</script>
{% endblock %}
7 changes: 4 additions & 3 deletions tune/templates/tune/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

{% block content %}
<div class="row mt-4">
<div class ="col">
<div class ="col">
<p>
Here you can browse and manage your repertoire.
</p>
</div>
</div>
</div>

<div class="card bg-success-subtle border-primary" style="max-width: 18rem; border-radius: 0;">
<div class="card-body">
<h5 class = "card-title">Tune Search</h5>
<h5 class="card-title">Tune Search</h5>
<div>
<form method="post" action="{% url 'tune:tune_list' %}">
{% csrf_token %}
Expand Down
57 changes: 23 additions & 34 deletions tune/templates/tune/play.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,31 @@
</div>
</div>


<div class="row d-flex align-items-stretch">
<div class ="col-md-6">
<div class="card bg-success-subtle border-primary" style="height: 200px; max-width: 18rem; border-radius: 0;">
<div class="card-body">
<h5 class = "card-title">Tune Search</h5>
<div class="col-4 my-1">
<input class="ms-0 ps-0"
style = "border-radius: 0; width: fit-content;"
type="search"
name="search"
id="searchField"
hx-trigger="keyup[keyCode==13]"
hx-get="{% url 'tune:get_random_tune' %}"
hx-target="#tunes"
hx-indicator=".htmx-indicator">
</div>
<div class="col-12 my-2">
<div class="col-md-6">
<div class="card bg-success-subtle border-primary" style="max-width: 18rem; border-radius: 0;">
<div class="card-body">
<h5 class="card-title">Tune Search</h5>
<div>
<form method="post" hx-post="{% url 'tune:get_random_tune' %}" hx-target="#tunes" hx-trigger="submit" hx-indicator=".htmx-indicator">
{% csrf_token %}
<label for="searchField"></label>
<input type="text" name="search_term" class="ms-0 ps-0" id="searchField">
</div>
<div class="col-12 my-2">
<label for="timespan">Haven't played in:</label>
<select id="timespan" name="timespan">
<option value="anytime">n/a</option>
<option value="day">a day</option>
<option value="week">a week</option>
<option value="month">a month</option>
<option value="two_months">two months</option>
<option value="three_months">three months</option>
</select>
</div>
<div class="col-1 my-1">
<button class="btn btn-primary"
hx-trigger="click"
hx-get="{% url 'tune:get_random_tune' %}"
hx-include="#searchField"
hx-target="#tunes"
hx-indicator=".htmx-indicator"
>Search</button>
</div>
<select id="timespan" name="timespan">
<option value="anytime">n/a</option>
<option value="day">a day</option>
<option value="week">a week</option>
<option value="month">a month</option>
<option value="two_months">2 months</option>
<option value="three_months">3 months</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
</div>
</div>
</div>
Expand Down
54 changes: 28 additions & 26 deletions tune/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.utils import timezone
from django.conf import settings
from django.http import HttpResponse
from django.template.loader import render_to_string

from .models import Tune, RepertoireTune
from .forms import TuneForm, RepertoireTuneForm, SearchForm
Expand Down Expand Up @@ -205,30 +206,36 @@ def recount(request):

@login_required
def get_random_tune(request):
"""
Function for getting a random tune from the search results on the play page.
"""
original_search_string = request.GET.get("search", "")
search_terms = original_search_string.split(" ")
user = request.user
tunes = (
RepertoireTune.objects.select_related("tune")
.filter(player=request.user)
.filter(player=user)
.exclude(knowledge="don't know")
)
search_form = SearchForm(request.POST or None)

rep_tunes = query_tunes(tunes, search_terms)
if not search_form.is_valid():
return HttpResponse("Invalid search", status=400)

if not rep_tunes:
return render(request, "tune/_play_card.html", {"selected_tune": None})
search_terms = search_form.cleaned_data["search_term"].split(" ")
timespan = search_form.cleaned_data.get("timespan", None)
result_dict = return_search_results(request, search_terms, tunes, search_form, timespan)

rep_tunes = list(rep_tunes)
selected_tune = choice(rep_tunes)
if "error" in result_dict:
return render(request, result_dict["template"], result_dict)

rep_tunes.remove(selected_tune)
request.session["rep_tunes"] = [rt.id for rt in rep_tunes]
tunes = result_dict.get("tunes")

if not tunes:
return HttpResponse("No tunes found", status=404)

selected_tune = choice(tunes)
remaining_rep_tunes_ids = [tune.id for tune in tunes if tune != selected_tune]
request.session["rep_tunes"] = remaining_rep_tunes_ids
request.session.save()

return render(request, "tune/_play_card.html", {"selected_tune": selected_tune})
html = render_to_string("tune/_play_card.html", {"selected_tune": selected_tune}, request)
return HttpResponse(html)


@login_required
Expand Down Expand Up @@ -290,29 +297,24 @@ def tune_browse(request):
"""
View for loading the public page, where users can browse public tunes and take them into their repertoire
"""
user = request.user
admin = User.objects.get(id=settings.ADMIN_USER_ID)

user_tunes = RepertoireTune.objects.select_related("tune").filter(player=user)
user_tune_titles = {
tune.tune.title for tune in user_tunes
} # using title since the user's and the admin's id for the same tune are distinct
user_tunes = RepertoireTune.objects.select_related("tune").filter(player=request.user)
user_tune_titles = {tune.tune.title for tune in user_tunes}

tunes = RepertoireTune.objects.select_related("tune").filter(player=admin)
tunes = RepertoireTune.objects.select_related("tune").filter(
player=User.objects.get(id=settings.ADMIN_USER_ID)
)
tune_count = len(tunes)

if request.method == "POST":
search_form = SearchForm(request.POST)
if search_form.is_valid():
search_terms = search_form.cleaned_data["search_term"].split(" ")
results = return_search_results(request, search_terms, tunes, search_form)
tunes = results.get("tunes")
tune_count = results.get("tune_count", 0)

tunes = return_search_results(request, search_terms, tunes, search_form)["tunes"]
tune_count = len(tunes)
else:
search_form = SearchForm()

request.session["tune_count"] = tune_count
return render(
request,
"tune/browse.html",
Expand Down

0 comments on commit 9512afc

Please sign in to comment.