From 30d2b2d630d1af15644c64d356a2a1459d31bc82 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Mon, 20 Dec 2021 15:55:33 +0100
Subject: [PATCH] add models for juridical person management (#58554)

---
 .../migrations/0028_juridicalentity.py        | 40 ++++++++++++
 src/authentic2/a2_rbac/models.py              | 15 +++++
 .../migrations/0028_usersubprofile.py         | 62 +++++++++++++++++++
 src/authentic2/custom_user/models.py          | 21 +++++++
 4 files changed, 138 insertions(+)
 create mode 100644 src/authentic2/a2_rbac/migrations/0028_juridicalentity.py
 create mode 100644 src/authentic2/custom_user/migrations/0028_usersubprofile.py

diff --git a/src/authentic2/a2_rbac/migrations/0028_juridicalentity.py b/src/authentic2/a2_rbac/migrations/0028_juridicalentity.py
new file mode 100644
index 00000000..e1a8e20e
--- /dev/null
+++ b/src/authentic2/a2_rbac/migrations/0028_juridicalentity.py
@@ -0,0 +1,40 @@
+# Generated by Django 2.2.19 on 2021-12-20 16:33
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('a2_rbac', '0027_auto_20211213_0949'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='JuridicalEntity',
+            fields=[
+                (
+                    'id',
+                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
+                ),
+                ('name', models.CharField(max_length=128, verbose_name='name')),
+                (
+                    'kind',
+                    models.CharField(
+                        choices=[('national', 'national'), ('local', 'local')],
+                        max_length=32,
+                        verbose_name='kind',
+                    ),
+                ),
+                ('registration_number', models.CharField(max_length=64, verbose_name='registration_number')),
+                (
+                    'status',
+                    models.CharField(
+                        choices=[('active', 'active'), ('deactivated', 'deactivated')],
+                        max_length=32,
+                        verbose_name='entity status',
+                    ),
+                ),
+            ],
+        ),
+    ]
diff --git a/src/authentic2/a2_rbac/models.py b/src/authentic2/a2_rbac/models.py
index 22fb1219..f229f515 100644
--- a/src/authentic2/a2_rbac/models.py
+++ b/src/authentic2/a2_rbac/models.py
@@ -417,6 +417,21 @@ class RoleAttribute(models.Model):
         return {'name': self.name, 'kind': self.kind, 'value': self.value}
 
 
+class JuridicalEntity(models.Model):
+    KINDS = (
+        ('national', _('national')),
+        ('local', _('local')),
+    )
+    STATUSES = (
+        ('active', _('active')),
+        ('deactivated', _('deactivated')),
+    )
+    name = models.CharField(max_length=128, verbose_name=_('name'))
+    kind = models.CharField(max_length=32, choices=KINDS, verbose_name=_('kind'))
+    registration_number = models.CharField(max_length=64, verbose_name=_('registration_number'))
+    status = models.CharField(max_length=32, choices=STATUSES, verbose_name=_('entity status'))
+
+
 GenericRelation(Permission, content_type_field='target_ct', object_id_field='target_id').contribute_to_class(
     ContentType, 'admin_perms'
 )
diff --git a/src/authentic2/custom_user/migrations/0028_usersubprofile.py b/src/authentic2/custom_user/migrations/0028_usersubprofile.py
new file mode 100644
index 00000000..9329a3c7
--- /dev/null
+++ b/src/authentic2/custom_user/migrations/0028_usersubprofile.py
@@ -0,0 +1,62 @@
+# Generated by Django 2.2.19 on 2021-12-20 16:33
+
+import django.db.models.deletion
+import django.utils.timezone
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('a2_rbac', '0028_juridicalentity'),
+        ('custom_user', '0027_user_deactivation_reason'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='UserSubprofile',
+            fields=[
+                (
+                    'id',
+                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
+                ),
+                (
+                    'kind',
+                    models.CharField(
+                        choices=[('administrator', 'administrator'), ('mandatory', 'mandatory')],
+                        max_length=32,
+                        verbose_name='kind',
+                    ),
+                ),
+                (
+                    'creation_timestamp',
+                    models.DateTimeField(
+                        default=django.utils.timezone.now, verbose_name='creation timestamp'
+                    ),
+                ),
+                (
+                    'validation_timestamp',
+                    models.DateTimeField(default=None, null=True, verbose_name='validation timestamp'),
+                ),
+                (
+                    'juridical_entity',
+                    models.ForeignKey(
+                        on_delete=django.db.models.deletion.CASCADE,
+                        related_name='managers',
+                        to='a2_rbac.JuridicalEntity',
+                        verbose_name='juridical entity',
+                    ),
+                ),
+                (
+                    'user',
+                    models.ForeignKey(
+                        on_delete=django.db.models.deletion.CASCADE,
+                        related_name='subprofiles',
+                        to=settings.AUTH_USER_MODEL,
+                        verbose_name='user',
+                    ),
+                ),
+            ],
+        ),
+    ]
diff --git a/src/authentic2/custom_user/models.py b/src/authentic2/custom_user/models.py
index 674e40d8..fc509bb0 100644
--- a/src/authentic2/custom_user/models.py
+++ b/src/authentic2/custom_user/models.py
@@ -460,3 +460,24 @@ class DeletedUser(models.Model):
         verbose_name = _('deleted user')
         verbose_name_plural = _('deleted users')
         ordering = ('deleted', 'id')
+
+
+class UserSubprofile(models.Model):
+    KINDS = (
+        ('administrator', _('administrator')),
+        ('mandatory', _('mandatory')),
+    )
+    user = models.ForeignKey(
+        to=User, verbose_name=_('user'), related_name='subprofiles', on_delete=models.CASCADE
+    )
+    kind = models.CharField(max_length=32, choices=KINDS, verbose_name=_('kind'))
+    juridical_entity = models.ForeignKey(
+        to='a2_rbac.JuridicalEntity',
+        verbose_name=_('juridical entity'),
+        related_name='managers',
+        on_delete=models.CASCADE,
+    )
+    creation_timestamp = models.DateTimeField(verbose_name=_('creation timestamp'), default=timezone.now)
+    validation_timestamp = models.DateTimeField(
+        verbose_name=_('validation timestamp'), null=True, default=None
+    )
-- 
2.34.1

