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 Jun 28, 2023
1 parent 9b68d0a commit 59648a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% 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);
$("#id_{{ field }}").append(newOption).trigger("change");
{% endif %}
{% endfor %}
{% endspaceless %}
}
document.body.onload = 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>
6 changes: 6 additions & 0 deletions apis_bibsonomy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +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()
ref = Reference.objects.filter(bibs_url=form.cleaned_data['bibs_url']).order_by("last_update").first()
self.request.session["last_bibsonomy_reference_title"] = ref.bibtexjson.get("title")

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)
Expand Down

0 comments on commit 59648a2

Please sign in to comment.