Coverage for src/ptf_tools/forms.py: 40%
360 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-10 14:28 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-10 14:28 +0000
1import glob
2import os
3from operator import itemgetter
5from allauth.account.forms import SignupForm as BaseSignupForm
6from ckeditor_uploader.fields import RichTextUploadingFormField
7from crispy_forms.helper import FormHelper
8from django import forms
9from django.conf import settings
10from django.contrib.auth import get_user_model
11from invitations.forms import CleanEmailMixin
12from mersenne_cms.models import News, Page
13from ptf.model_helpers import get_collection_id, is_site_en_only, is_site_fr_only
14from ptf.models import (
15 Collection,
16 ExtId,
17 ExtLink,
18 GraphicalAbstract,
19 RelatedArticles,
20 ResourceId,
21)
23from .models import Invitation, InvitationExtraData
25TYPE_CHOICES = (
26 ("doi", "doi"),
27 ("mr-item-id", "mr"),
28 ("zbl-item-id", "zbl"),
29 ("numdam-id", "numdam"),
30 ("pmid", "pubmed"),
31)
33RESOURCE_ID_CHOICES = (
34 ("issn", "p-issn"),
35 ("e-issn", "e-issn"),
36)
38REL_CHOICES = (
39 ("small_icon", "small_icon"),
40 ("icon", "icon"),
41 ("test_website", "test_website"),
42 ("website", "website"),
43)
45IMPORT_CHOICES = (
46 ("1", "Préserver des métadonnées existantes dans ptf-tools (equal-contrib, coi_statement)"),
47 ("2", "Remplacer tout par le fichier XML"),
48)
51class PtfFormHelper(FormHelper):
52 def __init__(self, *args, **kwargs):
53 super().__init__(*args, **kwargs)
54 self.label_class = "col-4 col-sm-2"
55 self.field_class = "col-8 col-sm-6"
56 self.form_tag = False
59class PtfModalFormHelper(FormHelper):
60 def __init__(self, *args, **kwargs):
61 super().__init__(*args, **kwargs)
62 self.label_class = "col-3"
63 self.field_class = "col-8"
64 self.form_tag = False
67class PtfLargeModalFormHelper(FormHelper):
68 def __init__(self, *args, **kwargs):
69 super().__init__(*args, **kwargs)
70 self.label_class = "col-6"
71 self.field_class = "col-6"
72 self.form_tag = False
75class FormSetHelper(FormHelper):
76 def __init__(self, *args, **kwargs):
77 super().__init__(*args, **kwargs)
78 self.form_tag = False
79 self.template = "bootstrap5/whole_uni_formset.html"
82class ExtIdForm(forms.ModelForm):
83 class Meta:
84 model = ExtId
85 fields = ["resource", "id_type", "id_value"]
87 def __init__(self, *args, **kwargs):
88 super().__init__(*args, **kwargs)
89 self.fields["id_type"].widget = forms.Select(choices=TYPE_CHOICES)
90 self.fields["resource"].widget = forms.HiddenInput()
93class ExtLinkForm(forms.ModelForm):
94 class Meta:
95 model = ExtLink
96 fields = ["rel", "location"]
97 widgets = {
98 "rel": forms.Select(choices=REL_CHOICES),
99 }
102class ResourceIdForm(forms.ModelForm):
103 class Meta:
104 model = ResourceId
105 fields = ["id_type", "id_value"]
106 widgets = {
107 "id_type": forms.Select(choices=RESOURCE_ID_CHOICES),
108 }
111class CollectionForm(forms.ModelForm):
112 class Meta:
113 model = Collection
114 fields = [
115 "pid",
116 "provider",
117 "coltype",
118 "title_tex",
119 "abbrev",
120 "doi",
121 "wall",
122 "alive",
123 "sites",
124 ]
125 widgets = {
126 "title_tex": forms.TextInput(),
127 }
129 def __init__(self, *args, **kwargs):
130 # Add extra fields before the base class __init__
131 self.base_fields["description_en"] = RichTextUploadingFormField(
132 required=False, label="Description (EN)"
133 )
134 self.base_fields["description_fr"] = RichTextUploadingFormField(
135 required=False, label="Description (FR)"
136 )
138 super().__init__(*args, **kwargs)
140 # self.instance is now set, specify initial values
141 qs = self.instance.abstract_set.filter(tag="description")
142 for abstract in qs:
143 if abstract.lang == "fr":
144 self.initial["description_fr"] = abstract.value_html
145 elif abstract.lang == "en":
146 self.initial["description_en"] = abstract.value_html
149class ContainerForm(forms.Form):
150 pid = forms.CharField(required=True, initial="")
151 title = forms.CharField(required=False, initial="")
152 trans_title = forms.CharField(required=False, initial="")
153 publisher = forms.CharField(required=True, initial="")
154 year = forms.CharField(required=False, initial="")
155 volume = forms.CharField(required=False, initial="")
156 number = forms.CharField(required=False, initial="")
157 icon = forms.FileField(required=False)
159 def __init__(self, container, *args, **kwargs):
160 super().__init__(*args, **kwargs)
161 self.container = container
163 if "data" in kwargs:
164 # form_invalid: preserve input values
165 self.fields["pid"].initial = kwargs["data"]["pid"]
166 self.fields["publisher"].initial = kwargs["data"]["publisher"]
167 self.fields["year"].initial = kwargs["data"]["year"]
168 self.fields["volume"].initial = kwargs["data"]["volume"]
169 self.fields["number"].initial = kwargs["data"]["number"]
170 self.fields["title"].initial = kwargs["data"]["title"]
171 self.fields["trans_title"].initial = kwargs["data"]["trans_title"]
172 elif container:
173 self.fields["pid"].initial = container.pid
174 self.fields["title"].initial = container.title_tex
175 # We arbitrarily take the first translated title
176 trans_title = self.container.title_set.filter(type="main").first()
177 if trans_title:
178 self.fields["trans_title"].initial = trans_title.title_html
179 else:
180 self.fields["trans_title"].initial = ""
182 if container.my_publisher:
183 self.fields["publisher"].initial = container.my_publisher.pub_name
184 self.fields["year"].initial = container.fyear
185 self.fields["volume"].initial = container.volume
186 self.fields["number"].initial = container.number
188 for extlink in container.extlink_set.all():
189 if extlink.rel == "icon":
190 self.fields["icon"].initial = os.path.basename(extlink.location)
192 def clean(self):
193 cleaned_data = super().clean()
194 return cleaned_data
197class ArticleForm(forms.Form):
198 pid = forms.CharField(required=True, initial="")
199 title = forms.CharField(required=False, initial="")
200 fpage = forms.CharField(required=False, initial="")
201 lpage = forms.CharField(required=False, initial="")
202 page_count = forms.CharField(required=False, initial="")
203 page_range = forms.CharField(required=False, initial="")
204 icon = forms.FileField(required=False)
205 pdf = forms.FileField(required=False)
206 coi_statement = forms.CharField(required=False, initial="")
207 show_body = forms.BooleanField(required=False, initial=True)
208 do_not_publish = forms.BooleanField(required=False, initial=True)
210 def __init__(self, article, *args, **kwargs):
211 super().__init__(*args, **kwargs)
212 self.article = article
214 if "data" in kwargs:
215 data = kwargs["data"]
216 # form_invalid: preserve input values
217 self.fields["pid"].initial = data["pid"]
218 if "title" in data:
219 self.fields["title"].initial = data["title"]
220 if "fpage" in data:
221 self.fields["fpage"].initial = data["fpage"]
222 if "lpage" in data:
223 self.fields["lpage"].initial = data["lpage"]
224 if "page_range" in data:
225 self.fields["page_range"].initial = data["page_range"]
226 if "page_count" in data:
227 self.fields["page_count"].initial = data["page_count"]
228 if "coi_statement" in data:
229 self.fields["coi_statement"].initial = data["coi_statement"]
230 if "show_body" in data:
231 self.fields["show_body"].initial = data["show_body"]
232 if "do_not_publish" in data:
233 self.fields["do_not_publish"].initial = data["do_not_publish"]
234 elif article:
235 # self.fields['pid'].initial = article.pid
236 self.fields["title"].initial = article.title_tex
237 self.fields["fpage"].initial = article.fpage
238 self.fields["lpage"].initial = article.lpage
239 self.fields["page_range"].initial = article.page_range
240 self.fields["coi_statement"].initial = (
241 article.coi_statement if article.coi_statement else ""
242 )
243 self.fields["show_body"].initial = article.show_body
244 self.fields["do_not_publish"].initial = article.do_not_publish
246 for count in article.resourcecount_set.all():
247 if count.name == "page-count":
248 self.fields["page_count"].initial = count.value
250 for extlink in article.extlink_set.all():
251 if extlink.rel == "icon":
252 self.fields["icon"].initial = os.path.basename(extlink.location)
254 qs = article.datastream_set.filter(rel="full-text", mimetype="application/pdf")
255 if qs.exists():
256 datastream = qs.first()
257 self.fields["pdf"].initial = datastream.location
259 def clean(self):
260 cleaned_data = super().clean()
261 return cleaned_data
264def cast_volume(element):
265 # Permet le classement des volumes dans le cas où :
266 # - un numero de volume est de la forme "11-12" (cf crchim)
267 # - un volume est de la forme "S5" (cf smai)
268 if not element:
269 return "", ""
270 try:
271 casted = int(element.split("-")[0])
272 extra = ""
273 except ValueError as _:
274 casted = int(element.split("-")[0][1:])
275 extra = element
276 return extra, casted
279def unpack_pid(filename):
280 # retourne un tableau pour chaque filename de la forme :
281 # [filename, collection, year, vseries, volume_extra, volume, issue_extra, issue]
282 # Permet un tri efficace par la suite
283 collection, year, vseries, volume, issue = filename.split("/")[-1].split(".")[0].split("_")
284 extra_volume, casted_volume = cast_volume(volume)
285 extra_issue, casted_issue = cast_volume(issue)
286 return (
287 filename,
288 collection,
289 year,
290 vseries,
291 extra_volume,
292 casted_volume,
293 extra_issue,
294 casted_issue,
295 )
298def get_volume_choices(colid, to_appear=False):
299 if settings.IMPORT_CEDRICS_DIRECTLY:
300 collection_folder = os.path.join(settings.CEDRAM_TEX_FOLDER, colid)
302 if to_appear:
303 issue_folders = [
304 volume for volume in os.listdir(collection_folder) if f"{colid}_0" in volume
305 ]
307 else:
308 issue_folders = [
309 d
310 for d in os.listdir(collection_folder)
311 if os.path.isdir(os.path.join(collection_folder, d))
312 ]
313 issue_folders = sorted(issue_folders, reverse=True)
315 files = [
316 (os.path.join(collection_folder, d, d + "-cdrxml.xml"), d)
317 for d in issue_folders
318 if os.path.isfile(os.path.join(collection_folder, d, d + "-cdrxml.xml"))
319 ]
320 else:
321 if to_appear:
322 volumes_path = os.path.join(
323 settings.CEDRAM_XML_FOLDER, colid, "metadata", f"{colid}_0*.xml"
324 )
325 else:
326 volumes_path = os.path.join(settings.CEDRAM_XML_FOLDER, colid, "metadata", "*.xml")
328 files = [unpack_pid(filename) for filename in glob.glob(volumes_path)]
329 sort = sorted(files, key=itemgetter(1, 2, 3, 4, 5, 6, 7), reverse=True)
330 files = [(item[0], item[0].split("/")[-1]) for item in sort]
331 return files
334def get_article_choices(colid, issue_name):
335 issue_folder = os.path.join(settings.CEDRAM_TEX_FOLDER, colid, issue_name)
336 article_choices = [
337 (d, os.path.basename(d))
338 for d in os.listdir(issue_folder)
339 if (
340 os.path.isdir(os.path.join(issue_folder, d))
341 and os.path.isfile(os.path.join(issue_folder, d, d + "-cdrxml.xml"))
342 )
343 ]
344 article_choices = sorted(article_choices, reverse=True)
346 return article_choices
349# Collections whose Cedrics articles can carry TeX formulas in their title
350# and keywords metadata. The Import Article modal (import_article.html) shows
351# the "Convert TeX metadata to unicode" option only for these collections.
352TEX_METADATA_COLLECTIONS = ("CRCHIM", "CRGEOS", "CRBIOL")
355class ImportArticleForm(forms.Form):
356 issue = forms.ChoiceField(
357 label="Numéro",
358 )
359 article = forms.ChoiceField(
360 label="Article",
361 )
363 def __init__(self, *args, **kwargs):
364 # we need to pop this extra colid kwarg if not, the call to super.__init__ won't work
365 colid = kwargs.pop("colid")
366 super().__init__(*args, **kwargs)
367 volumes = get_volume_choices(colid)
368 self.fields["issue"].choices = volumes
369 articles = []
370 if volumes: 370 ↛ 371line 370 didn't jump to line 371 because the condition on line 370 was never true
371 articles = get_article_choices(colid, volumes[0][1])
372 self.fields["article"].choices = articles
374 # Several Cedrics collections carry TeX formulas in their metadata
375 # (title and keywords): offer to convert them to unicode on import.
376 if colid in TEX_METADATA_COLLECTIONS:
377 self.fields["convert_tex_to_unicode"] = forms.BooleanField(
378 label="Convert TeX metadata to unicode",
379 required=False,
380 )
383class ImportContainerForm(forms.Form):
384 filename = forms.ChoiceField(
385 label="Numéro",
386 )
387 remove_email = forms.BooleanField(
388 label="Delete emails from CEDRAM contribs?",
389 initial=True,
390 required=False,
391 )
392 remove_date_prod = forms.BooleanField(
393 label="Delete published dates from CEDRAM?",
394 initial=True,
395 required=False,
396 )
398 def __init__(self, *args, **kwargs):
399 # we need to pop this extra colid kwarg if not, the call to super.__init__ won't work
400 colid = kwargs.pop("colid")
401 to_appear = kwargs.pop("to_appear")
402 super().__init__(*args, **kwargs)
403 self.fields["filename"].choices = get_volume_choices(colid, to_appear)
406class DiffContainerForm(forms.Form):
407 import_choice = forms.ChoiceField(
408 choices=IMPORT_CHOICES, label="Que faire des différences ?", widget=forms.RadioSelect()
409 )
411 def __init__(self, *args, **kwargs):
412 # we need to pop this extra full_path kwarg if not, the call to super.__init__ won't work
413 kwargs.pop("colid")
414 # filename = kwargs.pop('filename')
415 # to_appear = kwargs.pop('to_appear')
416 super().__init__(*args, **kwargs)
418 self.fields["import_choice"].initial = IMPORT_CHOICES[0][0]
421class ImportEditflowArticleForm(forms.Form):
422 def validate_xml_file(value):
423 if not value.name.lower().endswith(".xml"):
424 raise forms.ValidationError("Only .xml files are allowed.")
426 editflow_xml_file = forms.FileField(
427 label="Import an article from an XML file provided by Editflow.",
428 required=True,
429 validators=[validate_xml_file],
430 widget=forms.ClearableFileInput(attrs={"accept": ".xml"}),
431 help_text="Only .xml files are accepted.",
432 )
434 def __init__(self, *args, **kwargs):
435 # we need to pop this extra full_path kwarg if not, the call to super.__init__ won't work
436 kwargs.pop("colid", None)
437 super().__init__(*args, **kwargs)
440class RegisterPubmedForm(forms.Form):
441 CHOICES = [
442 ("off", "Yes"),
443 ("on", "No, update the article in PubMed"),
444 ]
445 update_article = forms.ChoiceField(
446 label="Are you registering the article for the first time ? Note: If you are updating the article, consider that only AuthorList (Author, Affiliation, Identifier), InvestigatorList (Investigator, Affiliation, Identifier), Pagination, ELocationID, OtherAbstract, PII and DOI fields can be updated. All other edits must be made using the PubMed Data Management system: https://www.ncbi.nlm.nih.gov/pubmed/management/",
447 widget=forms.RadioSelect,
448 choices=CHOICES,
449 required=False,
450 initial="on",
451 )
454class CreateFrontpageForm(forms.Form):
455 create_frontpage = forms.BooleanField(
456 label="Update des frontpages des articles avec date de mise en ligne ?",
457 initial=False,
458 required=False,
459 )
462class RelatedForm(forms.ModelForm):
463 doi_list = forms.CharField(
464 required=False,
465 widget=forms.Textarea(attrs={"rows": "10", "placeholder": "doi_1\ndoi_2\ndoi_3\n"}),
466 )
468 exclusion_list = forms.CharField(
469 required=False,
470 widget=forms.Textarea(attrs={"rows": "10"}),
471 )
473 class Meta:
474 model = RelatedArticles
475 fields = ["doi_list", "exclusion_list", "automatic_list"]
478class GraphicalAbstractForm(forms.ModelForm):
479 """Form for the Graphical Abstract model"""
481 class Meta:
482 model = GraphicalAbstract
483 fields = ("graphical_abstract", "illustration")
486class PageForm(forms.ModelForm):
487 class Meta:
488 model = Page
489 fields = [
490 "menu_title_en",
491 "menu_title_fr",
492 "parent_page",
493 "content_en",
494 "content_fr",
495 "state",
496 "slug_en",
497 "slug_fr",
498 "menu_order",
499 "position",
500 "mersenne_id",
501 "site_id",
502 ]
504 def __init__(self, *args, **kwargs):
505 site_id = kwargs.pop("site_id")
506 user = kwargs.pop("user")
507 super().__init__(*args, **kwargs)
509 self.fields["site_id"].initial = site_id
511 if not user.is_staff: 511 ↛ 518line 511 didn't jump to line 518 because the condition on line 511 was always true
512 for field_name in ["mersenne_id", "site_id"]:
513 field = self.fields[field_name]
514 # Hide the field is not enough, otherwise BaseForm._clean_fields will not get the value
515 field.disabled = True
516 field.widget = field.hidden_widget()
518 colid = get_collection_id(int(site_id))
520 # By default, CKEditor stores files in 1 folder
521 # We want to store the files in a @colid folder
522 for field_name in ["content_en", "content_fr"]:
523 field = self.fields[field_name]
524 widget = field.widget
525 widget.config["filebrowserUploadUrl"] = "/ckeditor/upload/" + colid
526 widget.config["filebrowserBrowseUrl"] = "/ckeditor/browse/" + colid
528 pages = Page.objects.filter(site_id=site_id, parent_page=None)
529 if self.instance: 529 ↛ 532line 529 didn't jump to line 532 because the condition on line 529 was always true
530 pages = pages.exclude(id=self.instance.id)
532 choices = [(p.id, p.menu_title_en) for p in pages if p.menu_title_en]
533 self.fields["parent_page"].choices = sorted(
534 choices + [(None, "---------")], key=lambda x: x[1]
535 )
537 self.fields["menu_title_en"].widget.attrs.update({"class": "menu_title"})
538 self.fields["menu_title_fr"].widget.attrs.update({"class": "menu_title"})
540 if is_site_en_only(site_id): 540 ↛ 541line 540 didn't jump to line 541 because the condition on line 540 was never true
541 self.fields.pop("content_fr")
542 self.fields.pop("menu_title_fr")
543 self.fields.pop("slug_fr")
544 elif is_site_fr_only(site_id): 544 ↛ 545line 544 didn't jump to line 545 because the condition on line 544 was never true
545 self.fields.pop("content_en")
546 self.fields.pop("menu_title_en")
547 self.fields.pop("slug_en")
549 def save_model(self, request, obj, form, change):
550 obj.site_id = form.cleaned_data["site_id"]
551 super().save_model(request, obj, form, change)
554class NewsForm(forms.ModelForm):
555 class Meta:
556 model = News
557 fields = [
558 "title_en",
559 "title_fr",
560 "content_en",
561 "content_fr",
562 "site_id",
563 ]
565 def __init__(self, *args, **kwargs):
566 site_id = kwargs.pop("site_id")
567 user = kwargs.pop("user")
568 super().__init__(*args, **kwargs)
570 self.fields["site_id"].initial = site_id
572 if not user.is_staff:
573 for field_name in ["site_id"]:
574 field = self.fields[field_name]
575 # Hide the field is not enough, otherwise BaseForm._clean_fields will not get the value
576 field.disabled = True
577 field.widget = field.hidden_widget()
579 colid = get_collection_id(int(site_id))
581 # By default, CKEditor stores files in 1 folder
582 # We want to store the files in a @colid folder
583 for field_name in ["content_en", "content_fr"]:
584 field = self.fields[field_name]
585 widget = field.widget
586 widget.config["filebrowserUploadUrl"] = "/ckeditor/upload/" + colid
587 widget.config["filebrowserBrowseUrl"] = "/ckeditor/browse/" + colid
589 if is_site_en_only(site_id):
590 self.fields.pop("content_fr")
591 self.fields.pop("title_fr")
592 elif is_site_fr_only(site_id):
593 self.fields.pop("content_en")
594 self.fields.pop("title_en")
596 def save_model(self, request, obj, form, change):
597 obj.site_id = form.cleaned_data["site_id"]
598 super().save_model(request, obj, form, change)
601class InviteUserForm(forms.Form):
602 """Base form to invite user."""
604 required_css_class = "required"
606 first_name = forms.CharField(label="First name", max_length=150, required=True)
607 last_name = forms.CharField(label="Last name", max_length=150, required=True)
608 email = forms.EmailField(label="E-mail address", required=True)
611class InvitationAdminChangeForm(forms.ModelForm):
612 class Meta:
613 model = Invitation
614 fields = "__all__"
616 def clean_extra_data(self):
617 """
618 Enforce the JSON structure with the InvitationExtraData dataclass interface.
619 """
620 try:
621 InvitationExtraData(**self.cleaned_data["extra_data"])
622 except Exception as e:
623 raise forms.ValidationError(e)
625 return self.cleaned_data["extra_data"]
628class InvitationAdminAddForm(InvitationAdminChangeForm, CleanEmailMixin):
629 class Meta:
630 fields = ("email", "first_name", "last_name", "extra_data")
632 def save(self, *args, **kwargs):
633 """
634 Populate the invitation data, save in DB and send the invitation e-mail.
635 """
636 cleaned_data = self.clean()
637 email = cleaned_data["email"]
638 params = {"email": email}
639 if cleaned_data.get("inviter"):
640 params["inviter"] = cleaned_data["inviter"]
641 else:
642 user = getattr(self, "user", None)
643 if isinstance(user, get_user_model()):
644 params["inviter"] = user
645 instance = Invitation.create(**params)
646 instance.first_name = cleaned_data["first_name"]
647 instance.last_name = cleaned_data["last_name"]
648 instance.extra_data = cleaned_data.get("extra_data", {})
649 instance.save()
650 full_name = f"{instance.first_name} {instance.last_name}"
651 instance.send_invitation(self.request, full_name=full_name)
652 super().save(*args, **kwargs)
653 return instance
656class SignupForm(BaseSignupForm):
657 email = forms.EmailField(widget=forms.HiddenInput())