Skip to content

Commit

Permalink
feat(ReferenceOnListView): load last added bibsononmy title in form
Browse files Browse the repository at this point in the history
This commit adds a small javascript snipplet to the
`reference_list.html` template which autofills the bibsonomy forms
`bibs_url` field with the data from the last added bibsonomy.
  • Loading branch information
b1rger committed Nov 14, 2023
1 parent 3fff43c commit 04519be
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% comment %}
iterate through the `last_bibsonomy_reference` that is set in
the session and use it to fill out the `bibs_url` form field
{% endcomment %}
<script>
function autoFillBibsonomyForm() {
{% spaceless %}
{% for field, value in request.session.last_bibsonomy_reference.items %}
{% if field == "bibs_url" %}
var newOption = new Option("{{ request.session.last_bibsonomy_reference_title }}", "{{ value }}", true, true);
$('select.listselect2').each(function() {
$(this).append(newOption).trigger("change");
});
{% endif %}
{% endfor %}
{% endspaceless %}
}
document.body.onload = autoFillBibsonomyForm();
htmx.on("htmx:afterSettle", function(evt) {
autoFillBibsonomyForm();
});
</script>

Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
</script>
{% include "apis_bibsonomy/partials/reinit_select2.html" %}
{% include "apis_bibsonomy/partials/fix_select2_bootstrap_focus.html" %}
{% include "apis_bibsonomy/partials/autofill_bibsonomy.html" %}
</div>
5 changes: 5 additions & 0 deletions apis_bibsonomy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ def get_success_url(self):

def form_valid(self, form):
args = form.cleaned_data
# we store the data about the last entered entry in the session
# so we can automatically fill the form with the last reference
self.request.session["last_bibsonomy_reference"] = form.cleaned_data.copy()

args['content_type'] = ContentType.objects.get_for_id(self.request.resolver_match.kwargs['contenttype'])
args['object_id'] = self.request.resolver_match.kwargs['pk']
ref = Reference.objects.create(**args)
self.request.session["last_bibsonomy_reference_title"] = ref.bibtexjson.get("title")
return super().form_valid(form)

0 comments on commit 04519be

Please sign in to comment.