Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Bibtex Error Messages with Field Indication #421

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,37 @@ class AddBibtexPublicationForm(forms.Form):
required=True,
widget=forms.Textarea(attrs={"placeholder": "@article{..."}),
)
bibtex_error = ""

mandatory_fields = ["title", "year", "author"]

def parse_bibtex(self, bibtex_string):
try:
return bibtexparser.loads(bibtex_string)
except bibtexparser.bibdatabase.UndefinedString as e:
self.bibtex_error = f"{e} not a valid string"
return None

def is_valid(self):
if not super(AddBibtexPublicationForm, self).is_valid():
if not super().is_valid():
return False

bib_database = bibtexparser.loads(self.cleaned_data["bibtex_string"])
logger.debug(bib_database.entries)

if not bib_database.entries:
self.add_error(
"bibtex_string",
(
"Invalid formatting or missing one of required fields, "
"publication/journal/booktitle, title, year, author in BibTeX "
"entry"
),
)
bib_database = self.parse_bibtex(self.cleaned_data["bibtex_string"])
if bib_database and bib_database.entries:
missing_fields = []
for entry in bib_database.entries:
missing = [
field for field in self.mandatory_fields if field not in entry
]
missing_fields.extend(missing)

if missing_fields:
self.bibtex_error = (
f"Missing mandatory fields: {', '.join(missing_fields)}"
)
return False
else:
return False

return True


Expand Down
4 changes: 3 additions & 1 deletion projects/pub_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def add_publications(request, project_id):
if any(v for v in duplicate_pubs.values()):
_send_duplicate_pubs_notification(project.chargeCode, duplicate_pubs)
else:
messages.error(request, "Error adding publication(s)")
messages.error(
request, f"Error adding publication(s). {pubs_form.bibtex_error}"
)
pubs_form = AddBibtexPublicationForm(initial={"project_id": project.id})

return render(
Expand Down