Skip to content

Commit

Permalink
feat: implement referenceonlist view
Browse files Browse the repository at this point in the history
This commit introduces a `referenceonlist` view. This view lists all
references on a specific model instance. It reuses the existing
`reference_list.html` template, but splits out the actual list into a
partial. The view by default returns the whole html page, but for
ajax/htmx requests returns only the partial.
  • Loading branch information
b1rger committed Nov 14, 2023
1 parent 2378172 commit 8f47ddb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul>
{% for reference in object_list %}
<li>
<a href="{{ reference.get_absolute_url }}">{{ reference }} ({{ reference.id }})</a>
</li>
{% empty %}
<li>No references yet.</li>
{% endfor %}
</ul>

10 changes: 1 addition & 9 deletions apis_bibsonomy/templates/apis_bibsonomy/reference_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
<div class="px-4 py-5 my-5 text-center">
<h1>References:</h1>
</div>
<ul>
{% for reference in object_list %}
<li>
<a href="{{ reference.get_absolute_url }}">{{ reference }} ({{ reference.id }})</a>
</li>
{% empty %}
<li>No references yet.</li>
{% endfor %}
</ul>
{% include "apis_bibsonomy/partials/reference_list.html" %}
</div>
{% endblock content %}
1 change: 1 addition & 0 deletions apis_bibsonomy/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
path('references/', views.ReferenceListView.as_view(), name='referencelist'),
path('references/<int:pk>', views.ReferenceDetailView.as_view(), name='referencedetail'),
path('references/<int:pk>/delete', views.ReferenceDeleteView.as_view(), name='referencedelete'),
path('referenceson/<int:contenttype>/<int:pk>', views.ReferenceOnListView.as_view(), name="referenceonlist"),
]
19 changes: 19 additions & 0 deletions apis_bibsonomy/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.contrib.contenttypes.models import ContentType
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
from django.http import Http404

from .models import Reference

Expand All @@ -27,3 +29,20 @@ def get_success_url(self):

class ReferenceListView(ListView):
model = Reference

class ReferenceOnListView(ReferenceListView):
def get_queryset(self):
pk = self.kwargs.get("pk")
contenttype = self.kwargs.get("contenttype")
try:
contenttype = ContentType.objects.get_for_id(contenttype)
except ContentType.DoesNotExist:
raise Http404
return self.model.objects.filter(content_type=contenttype, object_id=pk)

def get_template_names(self):
# return only a partial if the request is ajax or htmx
partial = "HX-Request" in self.request.headers or self.request.headers.get('x-requested-with') == 'XMLHttpRequest'
if partial:
return "apis_bibsonomy/partials/reference_list.html"
return super().get_template_names()

0 comments on commit 8f47ddb

Please sign in to comment.