Commit c2796785 authored by fred's avatar fred

add possibility to search on nonstop zone

parent 8a5f167b
from django import forms from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import Track from .models import Track
...@@ -19,3 +20,10 @@ class TrackMetaForm(forms.ModelForm): ...@@ -19,3 +20,10 @@ class TrackMetaForm(forms.ModelForm):
class Meta: class Meta:
model = Track model = Track
fields = ['language', 'instru', 'sabam', 'cfwb', 'nonstop_zones'] fields = ['language', 'instru', 'sabam', 'cfwb', 'nonstop_zones']
class TrackSearchForm(forms.Form):
q = forms.CharField(label=_('Text'))
zone = forms.ChoiceField(label=_('Nonstop Zone'), choices=get_optional_nonstop_zones)
order_by = forms.ChoiceField(label=_('Order'),
choices=[('alpha', _('Alphabetically'))])
...@@ -6,26 +6,18 @@ ...@@ -6,26 +6,18 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<form><input name="q" type="search"><button>{% trans "Search" %}</button></form> <form id="track-search">
{{ form.as_p }}
<button>{% trans "Search" %}</button>
</form>
<hr> <hr>
{% if artists.exists %}
<h3>{% trans "Artists" %}</h3>
<ul>
{% for artist in artists %}
<li><a href="{% url 'artist-view' pk=artist.id %}">{{artist.name}}</a></li>
{% endfor %}
</ul>
{% endif %}
{% if tracks.exists %} {% if tracks.exists %}
<h3>{% trans "Tracks" %}</h3>
<ul> <ul>
{% for track in tracks %} {% for track in tracks %}
<li><a href="{% url 'track-view' pk=track.id %}">{{track.title}}</a> — {{track.artist.name}}</li> <li><a href="{% url 'track-view' pk=track.id %}">{{track.title}}</a> — {{track.artist.name}}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
...@@ -10,6 +10,7 @@ import mutagen ...@@ -10,6 +10,7 @@ import mutagen
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.contrib import messages from django.contrib import messages
from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic.base import RedirectView, TemplateView from django.views.generic.base import RedirectView, TemplateView
...@@ -18,7 +19,7 @@ from django.views.generic.detail import DetailView ...@@ -18,7 +19,7 @@ from django.views.generic.detail import DetailView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from .forms import UploadTracksForm, TrackMetaForm from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm
from .models import SomaLogLine, Track, Artist, NonstopFile from .models import SomaLogLine, Track, Artist, NonstopFile
from emissions.models import Nonstop from emissions.models import Nonstop
...@@ -245,8 +246,19 @@ class SearchView(TemplateView): ...@@ -245,8 +246,19 @@ class SearchView(TemplateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
ctx = super(SearchView, self).get_context_data(**kwargs) ctx = super(SearchView, self).get_context_data(**kwargs)
ctx['form'] = TrackSearchForm()
queryset = Track.objects.all()
q = self.request.GET.get('q') q = self.request.GET.get('q')
if q: if q:
ctx['artists'] = Artist.objects.filter(name__icontains=q.lower()) queryset = queryset.filter(Q(title__icontains=q.lower()) | Q(artist__name__icontains=q.lower()))
ctx['tracks'] = Track.objects.filter(title__icontains=q.lower()).order_by('title').select_related()
zone = self.request.GET.get('zone')
if zone:
from emissions.models import Nonstop
queryset = queryset.filter(nonstop_zones=zone)
if q or zone:
ctx['tracks'] = queryset.order_by('title').select_related()
return ctx return ctx
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment