Added search bar to collection list

This commit is contained in:
Tris Forster 2023-03-04 17:02:18 +11:00
parent 220163bca2
commit 1f0a336ed4
4 changed files with 37 additions and 16 deletions

View File

@ -3,6 +3,19 @@
{% block page %}
<h3 class="title">Library collections for {% firstof request.user.first_name request.user.username %}</h3>
<div class="block">
<form method="GET" action="{% url 'work_list' %}">
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" name="filter" type="text" placeholder="Find a work" value="{{ request.GET.filter }}"/>
</div>
<div class="control">
<a class="button" href="?"><i class="fas fa-times"></i></a>
</div>
</div>
</form>
</div>
<div class="columns is-multiline">
{% for collection in object_list %}
<div class="column is-half">

View File

@ -29,7 +29,7 @@
<th>Work</th>
<th>Composer</th>
<th class="is-hidden-mobile">Edition</th>
<th class="is-hidden-touch">Collection</th>
{% if not collection %}<th class="is-hidden-touch">Collection</th>{% endif %}
{% if request.is_admin %}
<th class="is-hidden-mobile">Copies</th>
{% endif %}
@ -41,7 +41,7 @@
<td><a href="{% url 'work_detail' collection=work.collection.pk pk=work.pk %}">{{ work.name }}</a></td>
<td title="{{ work.composer }}">{{ work.composer|truncatewords:3 }}</td>
<td class="is-hidden-mobile" title="{{ work.edition }}">{{ work.edition|truncatewords:2 }}</td>
<td class="is-hidden-touch">{{ work.collection.name }}</td>
{% if not collection %}<td class="is-hidden-touch">{{ work.collection.name }}</td>{% endif %}
{% if request.is_admin %}
<td class="is-hidden-mobile {{ work.is_available|yesno:'has-text-success,has-text-danger' }}">{{ work.available }}</td>
{% endif %}

View File

@ -16,7 +16,7 @@ urlpatterns = [
path('projects/<int:project>/items/manage', views.ProjectItemManageView.as_view(), name="item_list_manage"),
path('projects/<int:project>/items/append', views.ProjectItemAddView.as_view(), name="item_list_append"),
path('library', views.WorkListView.as_view(), name="work_list"),
path('library', views.LibraryWorkListView.as_view(), name="work_list"),
path('collections', views.CollectionListView.as_view(), name="collection_list"),
path('collections/<int:collection>', views.CollectionWorkListView.as_view(), name="collection_work_list"),

View File

@ -113,6 +113,9 @@ class ProjectItemAddView(ProjectMixin, UpdateView):
""" COLLECTION VIEWS """
class CollectionMixin(AuthorizedResourceMixin):
collection = None
def is_authorized(self):
collection_id = self.kwargs['collection']
self.collection = get_object_or_404(models.Collection, pk=collection_id)
@ -155,18 +158,6 @@ class CollectionListView(ListView):
class WorkListView(CollectionMixin, ListView):
paginate_by = 20
def request_denied(self):
if 'auth' in self.request.GET:
if self.request.GET['auth'] != self.collection.auth():
raise SuspiciousOperation("Bad collection link")
self.add_authorized_key('collection', self.collection.pk, self.collection.nonce)
return HttpResponseRedirect(self.request.path)
return super().request_denied()
def get_works(self):
collections = CollectionMixin.get_queryset(self)
return Work.objects.filter(collection__in=collections).select_related('collection')
def get_context_data(self, *args, **kwargs):
data = super(WorkListView, self).get_context_data(*args, **kwargs)
@ -185,10 +176,27 @@ class WorkListView(CollectionMixin, ListView):
else:
works = works.filter(Q(name__contains=q) | Q(composer__contains=q) | Q(meta_info__value__contains=q))
return works.order_by('name', 'composer', 'edition', 'pk')
return works.order_by('name', 'composer', 'edition', 'pk').distinct()
class LibraryWorkListView(WorkListView):
def is_authorized(self):
return True
def get_works(self):
collections = models.Collection.objects.filter(administrators=self.request.user)
return Work.objects.filter(collection__in=collections).select_related('collection')
class CollectionWorkListView(WorkListView):
def request_denied(self):
if 'auth' in self.request.GET:
if self.request.GET['auth'] != self.collection.auth():
raise SuspiciousOperation("Bad collection link")
self.add_authorized_key('collection', self.collection.pk, self.collection.nonce)
return HttpResponseRedirect(self.request.path)
return super().request_denied()
def get_works(self):
works = Work.objects.filter(collection=self.kwargs['collection'])