diff --git a/journals/forms.py b/journals/forms.py index d173a344773c53a27ea61e613631c63cdf7b8edc..eb54e4deb587df7ccbc6bbb533d2eb6450988883 100644 --- a/journals/forms.py +++ b/journals/forms.py @@ -1,3 +1,5 @@ +import re + from django import forms from django.utils import timezone @@ -45,6 +47,20 @@ class CitationListBibitemsForm(forms.Form): self.fields['latex_bibitems'].widget.attrs.update( {'rows': 30, 'cols': 50, 'placeholder': 'Paste the .tex bibitems here'}) + def extract_dois(self): + entries_list = self.cleaned_data['latex_bibitems'] + entries_list = re.sub(r'(?m)^\%.*\n?', '', entries_list) + entries_list = entries_list.split('\doi{') + dois = [] + nentries = 1 + for entry in entries_list[1:]: # drop first bit before first \doi{ + dois.append( + {'key': 'ref' + str(nentries), + 'doi': entry.partition('}')[0], } + ) + nentries += 1 + return dois + class FundingInfoForm(forms.Form): funding_statement = forms.CharField(widget=forms.Textarea()) diff --git a/journals/views.py b/journals/views.py index 9ed655acb42b1b6bbcf314725c38a4ac00b18fd1..c901e579b419c757e395de4d9054f3b1ef467a33 100644 --- a/journals/views.py +++ b/journals/views.py @@ -372,15 +372,7 @@ def create_citation_list_metadata(request, doi_label): if request.method == 'POST': bibitems_form = CitationListBibitemsForm(request.POST, request.FILES) if bibitems_form.is_valid(): - publication.metadata['citation_list'] = [] - entries_list = bibitems_form.cleaned_data['latex_bibitems'].split('\doi{') - nentries = 1 - for entry in entries_list[1:]: # drop first bit before first \doi{ - publication.metadata['citation_list'].append( - {'key': 'ref' + str(nentries), - 'doi': entry.partition('}')[0], } - ) - nentries += 1 + publication.metadata['citation_list'] = bibitems_form.extract_dois() publication.save() bibitems_form = CitationListBibitemsForm() context = {