Skip to content
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
31 changes: 2 additions & 29 deletions src/genlab_bestilling/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,6 @@ def clean_contact_email_results(self) -> str:
raise forms.ValidationError(msg) from ValidationError(msg)
return ", ".join(emails)

def clean_contact_person_results(self) -> str:
names_raw = self.cleaned_data.get("contact_person_results", "")
names = [n.strip() for n in names_raw.split(",") if n.strip()]

for name in names:
# Optionally allow hyphens and apostrophes in names
if not all(c.isalpha() or c.isspace() or c in "-'" for c in name):
msg = f"Invalid name: {name}"
raise forms.ValidationError(msg) from ValidationError(msg)
return ", ".join(names)

def save(self, commit: bool = True) -> Model:
if not commit:
msg = "This form is always committed"
Expand All @@ -386,26 +375,16 @@ def save(self, commit: bool = True) -> Model:
# Delete old entries first (in case of resubmission)
obj.results_contacts.all().delete()

names = [
strip_tags(n.strip())
for n in self.cleaned_data["contact_person_results"].split(",")
if n.strip()
]
emails = [
strip_tags(e.strip())
for e in self.cleaned_data["contact_email_results"].split(",")
if e.strip()
]

if names and emails:
if len(names) != len(emails):
msg = "The number of names must match the number of emails."
raise ValidationError(msg)

for name, email in zip(names, emails, strict=False):
if emails:
for email in emails:
AnalysisOrderResultsCommunication.objects.create(
analysis_order=obj,
contact_person_results=name,
contact_email_results=email,
)

Expand All @@ -418,12 +397,6 @@ def clean(self) -> None:
msg = "An extraction order must be selected"
raise ValidationError(msg)

contact_person_results = forms.CharField(
label="Contact person(s) for results",
help_text="Comma-separated list of names to contact with results",
required=True,
)

contact_email_results = forms.CharField(
label="Contact email(s) for results",
help_text="Comma-separated list of emails to contact with results (must match order of names)", # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 5.2.3 on 2025-07-29 12:58

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("genlab_bestilling", "0036_sample_markers"),
]

operations = [
migrations.RemoveField(
model_name="analysisorderresultscommunication",
name="contact_person_results",
),
]
7 changes: 1 addition & 6 deletions src/genlab_bestilling/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,19 +561,14 @@ class AnalysisOrderResultsCommunication(models.Model):
on_delete=models.CASCADE,
related_name="results_contacts",
)
contact_person_results = models.CharField(
null=True,
blank=False,
help_text="Person to contact for analysis resuls",
)
contact_email_results = models.EmailField(
null=True,
blank=False,
help_text="Email to send analysis results",
)

def __str__(self):
return f"{str(self.analysis_order)} {str(self.contact_person_results)} {str(self.contact_email_results)}" # noqa: E501
return f"{str(self.analysis_order)} {str(self.contact_email_results)}"


class AnalysisOrder(Order):
Expand Down
10 changes: 5 additions & 5 deletions src/staff/templatetags/order_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,21 +372,21 @@ def contact_detail_table(order: Order) -> dict:
if isinstance(order, AnalysisOrder):
result_contacts = (
AnalysisOrderResultsCommunication.objects.filter(analysis_order=order)
.values_list("contact_person_results", "contact_email_results")
.values_list("contact_email_results")
.distinct()
)
if result_contacts:
result_contacts_html = format_html_join(
"\n",
'<div>{} — <a href="mailto:{}" class="text-primary underline !text-primary">{}</a></div>', # noqa: E501
[(name, email, email) for name, email in result_contacts],
"; ",
'<a href="mailto:{}" class="text-primary underline !text-primary">{}</a>', # noqa: E501
[(email[0], email[0]) for email in result_contacts],
)

fields = {
"Samples owner of genetic project": order.genrequest.samples_owner,
"Responsible genetic researcher": order.contact_person,
"Responsible genetic researcher email": order.contact_email,
"Contact name and email for analysis results": result_contacts_html,
"Contact email for analysis results": result_contacts_html,
}

return {
Expand Down