Skip to content

Commit

Permalink
feat(ReferenceOnListView): split view into multiple views
Browse files Browse the repository at this point in the history
This commit adds the ReferenceOnListViewModal and
ReferenceOnListViewPartial views. The three views do the same thing, but
with different templates and success_urls. The main view shows the list
of references with the form on a full blown html page.
The partialf view shows the list and the form without the html
surrounding it (this is also the template that gets included into the
main and the modal view).
The modal view shows the information inside a modal.
  • Loading branch information
b1rger committed Nov 14, 2023
1 parent 04519be commit f14ae9e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script src="https://unpkg.com/htmx.org@1.9.0"></script>
<div id="referencelist">
{% include "apis_bibsonomy/partials/fix_select2_bootstrap_overflow.html" %}
<ul class="list-group list-group-flush">
{% for reference in object_list %}
Expand All @@ -18,7 +17,11 @@

{% if form %}
{% load crispy_forms_tags %}
<form method="post" hx-post="{{ request.path }}" hx-target="#referencelist" hx-swap="outerHTML" class="mt-4">
<form method="post"
hx-post="{{ hxpost|default:request.path }}"
hx-target="{{ hxtarget|default:"#referencelist" }}"
hx-swap="innerHTML"
class="mt-4">
{% crispy form %}
</form>
{% endif %}
Expand All @@ -31,4 +34,3 @@
{% include "apis_bibsonomy/partials/reinit_select2.html" %}
{% include "apis_bibsonomy/partials/fix_select2_bootstrap_focus.html" %}
{% include "apis_bibsonomy/partials/autofill_bibsonomy.html" %}
</div>
7 changes: 5 additions & 2 deletions apis_bibsonomy/templates/apis_bibsonomy/reference_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
{% block content %}
<div class="container">
<div class="px-4 py-5 my-5 text-center">
<h1>References:</h1>
<h1>References on {{ object }}:</h1>
</div>
<div id="referencelist">
{% url "apis_bibsonomy:referenceonlistpartial" contenttype.id object.id as hxpost %}
{% include "apis_bibsonomy/partials/reference_list.html" with hxpost=hxpost %}
</div>
{% include "apis_bibsonomy/partials/reference_list.html" %}
</div>
{% endblock content %}
12 changes: 12 additions & 0 deletions apis_bibsonomy/templates/apis_bibsonomy/reference_list_modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="modal-header">
<div class="modal-title" id="referenceModalLabel">References on: {{ object }}</div>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
{% include "apis_bibsonomy/partials/reference_list.html" with hxtarget="#modal-here" %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
2 changes: 2 additions & 0 deletions apis_bibsonomy/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
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"),
path('referenceson/<int:contenttype>/<int:pk>/modal', views.ReferenceOnListViewModal.as_view(), name="referenceonlistmodal"),
path('referenceson/<int:contenttype>/<int:pk>/partial', views.ReferenceOnListViewPartial.as_view(), name="referenceonlistpartial"),
]
22 changes: 15 additions & 7 deletions apis_bibsonomy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,10 @@ def get_queryset(self):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["contenttype"] = self.contenttype
context["object"] = self.contenttype.get_object_for_this_type(id=self.pk)
return context

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()

def get_success_url(self):
return reverse('apis_bibsonomy:referenceonlist', kwargs=self.request.resolver_match.kwargs)

Expand All @@ -72,3 +66,17 @@ def form_valid(self, form):
ref = Reference.objects.create(**args)
self.request.session["last_bibsonomy_reference_title"] = ref.bibtexjson.get("title")
return super().form_valid(form)


class ReferenceOnListViewModal(ReferenceOnListView):
template_name = "apis_bibsonomy/reference_list_modal.html"

def get_success_url(self):
return reverse('apis_bibsonomy:referenceonlistmodal', kwargs=self.request.resolver_match.kwargs)


class ReferenceOnListViewPartial(ReferenceOnListView):
template_name = "apis_bibsonomy/partials/reference_list.html"

def get_success_url(self):
return reverse('apis_bibsonomy:referenceonlistpartial', kwargs=self.request.resolver_match.kwargs)

0 comments on commit f14ae9e

Please sign in to comment.