From 772a17eaeefb859febe98f856d41ea619f80f3df Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 8 Oct 2024 15:03:36 +0200
Subject: [PATCH] add status fields to contact person

---
 scipost_django/organizations/forms.py         | 21 +++++++++++++
 ...on_date_deprecated_contactperson_status.py | 30 +++++++++++++++++++
 scipost_django/organizations/models.py        | 13 ++++++++
 .../organizations/_organization_card.html     | 15 ++++++++++
 4 files changed, 79 insertions(+)
 create mode 100644 scipost_django/organizations/migrations/0022_contactperson_date_deprecated_contactperson_status.py

diff --git a/scipost_django/organizations/forms.py b/scipost_django/organizations/forms.py
index 24761f39d..cd4681405 100644
--- a/scipost_django/organizations/forms.py
+++ b/scipost_django/organizations/forms.py
@@ -165,6 +165,21 @@ class ContactPersonForm(forms.ModelForm):
     class Meta:
         model = ContactPerson
         fields = "__all__"
+        widgets = {
+            "date_deprecated": forms.DateInput(attrs={"type": "date"}),
+        }
+
+    def clean(self):
+        cleaned_data = super().clean()
+        date_deprecated = cleaned_data.get("date_deprecated")
+        status = cleaned_data.get("status")
+
+        if status == ContactPerson.STATUS_DEPRECATED and not date_deprecated:
+            self.add_error("date_deprecated", "A deprecation date is required.")
+        elif status != ContactPerson.STATUS_DEPRECATED and date_deprecated:
+            self.add_error("status", "Status must be set to 'Deprecated'.")
+
+        return cleaned_data
 
     def clean_email(self):
         """
@@ -219,6 +234,12 @@ class NewContactForm(ContactForm):
     first_name = forms.CharField()
     last_name = forms.CharField()
     email = forms.CharField()
+    status = forms.ChoiceField(
+        choices=ContactPerson.STATUS_CHOICES, initial=ContactPerson.STATUS_UNKNOWN
+    )
+    date_deprecated = forms.DateField(
+        required=False, widget=forms.DateInput(attrs={"type": "date"})
+    )
     existing_user = None
 
     def __init__(self, *args, **kwargs):
diff --git a/scipost_django/organizations/migrations/0022_contactperson_date_deprecated_contactperson_status.py b/scipost_django/organizations/migrations/0022_contactperson_date_deprecated_contactperson_status.py
new file mode 100644
index 000000000..90fb98636
--- /dev/null
+++ b/scipost_django/organizations/migrations/0022_contactperson_date_deprecated_contactperson_status.py
@@ -0,0 +1,30 @@
+# Generated by Django 4.2.15 on 2024-10-08 12:43
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("organizations", "0021_enable_unaccent"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="contactperson",
+            name="date_deprecated",
+            field=models.DateField(blank=True, null=True),
+        ),
+        migrations.AddField(
+            model_name="contactperson",
+            name="status",
+            field=models.CharField(
+                choices=[
+                    ("unknown", "Unknown"),
+                    ("active", "Active"),
+                    ("deprecated", "Deprecated"),
+                ],
+                default="unknown",
+                max_length=16,
+            ),
+        ),
+    ]
diff --git a/scipost_django/organizations/models.py b/scipost_django/organizations/models.py
index fc5f3be24..78d7971b6 100644
--- a/scipost_django/organizations/models.py
+++ b/scipost_django/organizations/models.py
@@ -693,6 +693,15 @@ class ContactPerson(models.Model):
     Instances can be promoted to Contact instances, which possess login credentials.
     """
 
+    STATUS_UNKNOWN = "unknown"
+    STATUS_ACTIVE = "active"
+    STATUS_DEPRECATED = "deprecated"
+    STATUS_CHOICES = (
+        (STATUS_UNKNOWN, "Unknown"),
+        (STATUS_ACTIVE, "Active"),
+        (STATUS_DEPRECATED, "Deprecated"),
+    )
+
     organization = models.ForeignKey(
         "organizations.Organization", on_delete=models.CASCADE
     )
@@ -701,6 +710,10 @@ class ContactPerson(models.Model):
     last_name = models.CharField(max_length=64)
     email = models.EmailField()
     role = models.CharField(max_length=128)
+    status = models.CharField(
+        max_length=16, choices=STATUS_CHOICES, default=STATUS_UNKNOWN
+    )
+    date_deprecated = models.DateField(blank=True, null=True)
 
     class Meta:
         ordering = ["last_name", "first_name", "organization"]
diff --git a/scipost_django/organizations/templates/organizations/_organization_card.html b/scipost_django/organizations/templates/organizations/_organization_card.html
index e1715d7c7..00f9598b7 100644
--- a/scipost_django/organizations/templates/organizations/_organization_card.html
+++ b/scipost_django/organizations/templates/organizations/_organization_card.html
@@ -769,12 +769,27 @@
           {% endif %}
 
           <table class="table">
+            <tr>
+              <th>Name</th>
+              <th>Email</th>
+              <th>Role</th>
+              <th>Status</th>
+
+              {% if perms.scipost.can_manage_organizations or "can_view_org_contacts" in user_org_perms %}
+                <th>Actions</th>
+              {% endif %}
+            </tr>
 
             {% for contactperson in org.contactperson_set.all %}
               <tr>
                 <td>{{ contactperson }}</td>
                 <td>{{ contactperson.email }}</td>
                 <td>{{ contactperson.role }}</td>
+                <td>{{ contactperson.get_status_display }}
+                  {% if contactperson.status == 'deprecated' %}
+                    <div class="text-muted">{{ contactperson.date_deprecated|date:"Y-m-d" }}</div>
+                  {% endif %}
+                </td>
 
                 {% if perms.scipost.can_manage_organizations or "can_view_org_contacts" in user_org_perms %}
                   <td>
-- 
GitLab