]> git.0d.be Git - django-panik-nonstop.git/commitdiff
add button to add tracks "en masse" to zone
authorFrédéric Péters <fpeters@0d.be>
Wed, 14 Feb 2024 09:37:47 +0000 (10:37 +0100)
committerFrédéric Péters <fpeters@0d.be>
Wed, 14 Feb 2024 09:38:07 +0000 (10:38 +0100)
nonstop/forms.py
nonstop/templates/nonstop/search-add-to-zone.html [new file with mode: 0644]
nonstop/templates/nonstop/search.html
nonstop/urls.py
nonstop/views.py

index dfc1d6d748d1de7d0b529965e3e8d6783a0a8964..412507ce9b70cbaa44290c6186c5e3251a36a424 100644 (file)
@@ -265,3 +265,7 @@ class RecurringStreamForm(forms.Form):
 class ArtistSearchForm(forms.Form):
     q = forms.CharField(label=_('Name'), required=False)
     zone = forms.ChoiceField(label=_('Nonstop Zone'), choices=get_search_nonstop_zones, required=False)
+
+
+class TracksAddToZoneForm(forms.Form):
+    zone = forms.ChoiceField(label=_('Nonstop Zone'), choices=get_nonstop_zones, required=True)
diff --git a/nonstop/templates/nonstop/search-add-to-zone.html b/nonstop/templates/nonstop/search-add-to-zone.html
new file mode 100644 (file)
index 0000000..4e5304e
--- /dev/null
@@ -0,0 +1,18 @@
+{% extends "base.html" %}
+{% load i18n %}
+
+{% block appbar %}
+  <h2>{% trans "Add to zone" %}</h2>
+{% endblock %}
+
+{% block content %}
+  <form method="post" enctype="multipart/form-data">
+    {% csrf_token %}
+    {{ form.as_p }}
+    <input type="hidden" name="qs" value="{{ qs }}">
+    <div class="buttons">
+      <button class="submit-button">{% trans "Add" %}</button>
+      <a class="cancel" href=".">{% trans "Cancel" %}</a>
+    </div>
+  </form>
+{% endblock %}
index 970d51aaa69c43ffb87fe759003baaf56372a5c2..3d7490e4ad67f88fc5767245d58ef925dfc16585 100644 (file)
         {% endif %}
       </span>
       <span class="download">
-        <a class="button" href="csv?{{ qs }}" download="nonstop.csv">CSV</a>
+        <a class="button" href="csv?{{ qs }}" download="nonstop.csv">Export to CSV</a>
+        {% if qs and perms.nonstop.add_track %}
+          -
+          <a class="button" href="add-to-zone?{{ qs }}" data-popup>{% trans "Add tracks to zone" %}</a>
+        {% endif %}
       </span>
     </div>
   {% endif %}
index c68e8f2b81cb147b6b934ebee53d36ae518a2c36..16c66c1815903e665a46c0a85dce0eba60813df9 100644 (file)
@@ -35,6 +35,7 @@ from .views import (
     StreamsListView,
     TrackDetailView,
     TrackEditView,
+    TracksAddToZoneView,
     UploadTracksView,
     ZoneAddView,
     ZoneDeleteView,
@@ -94,6 +95,7 @@ urlpatterns = [
     ),
     path('search/', SearchView.as_view(), name='nonstop-search'),
     path('search/csv', SearchCsvView.as_view(), name='nonstop-search-csv'),
+    path('search/add-to-zone', TracksAddToZoneView.as_view(), name='nonstop-search-add-to-zone'),
     path('quick-links/', RedirectView.as_view(url=reverse_lazy('nonstop-quick-links'))),
     path('cleanup/', CleanupView.as_view(), name='nonstop-cleanup'),
     path('force-switch/', force_switch_view, name='nonstop-force-switch'),
index 4248d27fd3fc58b9bb5ebd7561db18def71bca9d..bdfe00da6213efb76dd2e81c035c06f8ddc109c8 100644 (file)
@@ -6,6 +6,7 @@ import logging
 import os
 import subprocess
 import tempfile
+import urllib.parse
 from io import StringIO
 
 import mutagen
@@ -40,6 +41,7 @@ from .forms import (
     RecurringStreamForm,
     TrackArtistTitleForm,
     TrackMetaForm,
+    TracksAddToZoneForm,
     TrackSearchForm,
     UploadTracksForm,
     ZoneSettingsForm,
@@ -719,14 +721,13 @@ class QuickLinksView(TemplateView):
 class SearchView(TemplateView):
     template_name = 'nonstop/search.html'
 
-    def get_queryset(self):
+    @classmethod
+    def get_search_queryset(cls, q=None, zone=None):
         queryset = Track.objects.all()
 
-        q = self.request.GET.get('q')
         if q:
             queryset = queryset.filter(Q(title__icontains=q.lower()) | Q(artist__name__icontains=q.lower()))
 
-        zone = self.request.GET.get('zone')
         if zone:
             from emissions.models import Nonstop
 
@@ -746,6 +747,10 @@ class SearchView(TemplateView):
             else:
                 queryset = queryset.filter(nonstop_zones=zone).distinct()
 
+        return queryset
+
+    def get_queryset(self):
+        queryset = self.get_search_queryset(q=self.request.GET.get('q'), zone=self.request.GET.get('zone'))
         order = self.request.GET.get('order_by') or 'title'
         if order:
             if 'added_to_nonstop_timestamp' in order:
@@ -796,6 +801,30 @@ class SearchCsvView(SearchView):
         return HttpResponse(out.getvalue(), content_type='text/csv; charset=utf-8')
 
 
+class TracksAddToZoneView(FormView):
+    form_class = TracksAddToZoneForm
+    template_name = 'nonstop/search-add-to-zone.html'
+
+    def get_context_data(self, **kwargs):
+        ctx = super().get_context_data(**kwargs)
+        qs = self.request.GET.copy()
+        ctx['qs'] = qs.urlencode()
+        return ctx
+
+    def form_valid(self, form):
+        assert self.request.user.has_perm('nonstop.add_track')
+        add_to_zone = Nonstop.objects.get(id=form.cleaned_data['zone'])
+        query_string = dict(urllib.parse.parse_qs(self.request.POST['qs']))
+        q = query_string['q'][0] if query_string.get('q') else None
+        zone = query_string['zone'][0] if query_string.get('zone') else None
+        for track in SearchView.get_search_queryset(q=q, zone=zone):
+            track.nonstop_zones.add(add_to_zone)
+        return super().form_valid(form)
+
+    def get_success_url(self):
+        return reverse('nonstop-search') + '?' + self.request.POST['qs']
+
+
 class CleanupView(TemplateView):
     template_name = 'nonstop/cleanup.html'