Skip to content

Commit

Permalink
fix(models): create wrapper around bibtex lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Nov 27, 2024
1 parent 0de7cd2 commit 66ac860
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apis_bibsonomy/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs):
self.helper = FormHelper(self)
if "instance" in kwargs:
self.fields["bibs_url"].widget.choices = [
(self.initial.get("bibs_url"), self.instance.bibtex.get("title"))
(self.initial.get("bibs_url"), self.instance.get_bibtex.get("title"))
]
self.helper.layout = Layout(
Row(
Expand Down
9 changes: 8 additions & 1 deletion apis_bibsonomy/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -30,11 +31,17 @@ class Reference(models.Model):
referenced_object = GenericForeignKey()

def __str__(self):
title = self.bibtex.get("title")
title = self.get_bibtex.get("title")
desc = [title, self.pages_start, self.pages_end, self.folio, self.notes]
desc = ", ".join(map(str, filter(None, desc)))
return desc

@property
def get_bibtex(self):
if isinstance(self.bibtex, str):
return json.loads(self.bibtex)
return self.bibtex

def get_absolute_url(self):
return reverse("apis_bibsonomy:referencedetail", kwargs={"pk": self.pk})

Expand Down
4 changes: 3 additions & 1 deletion apis_bibsonomy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def form_valid(self, form):
)
args["object_id"] = self.request.resolver_match.kwargs["pk"]
ref = Reference.objects.create(**args)
self.request.session["last_bibsonomy_reference_title"] = ref.bibtex.get("title")
self.request.session["last_bibsonomy_reference_title"] = ref.get_bibtex.get(
"title"
)
return super().form_valid(form)


Expand Down

0 comments on commit 66ac860

Please sign in to comment.