aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2020-02-20 16:34:57 +0000
committerMatthew Lemon <lemon@matthewlemon.com>2020-02-20 16:34:57 +0000
commit8d2487262dfcffc2d6d20666dd99308b02165e11 (patch)
tree7e6e00d55ad96a1e0595d9ec0b90a02682876b44 /ctrack
parent0d9162aee0b28f5117a453064bd86577df35658a (diff)
changed database rules so that deleting Users does not delete Organisation and Person objects
Diffstat (limited to '')
-rw-r--r--ctrack/organisations/migrations/0003_auto_20200220_1625.py20
-rw-r--r--ctrack/organisations/migrations/0004_auto_20200220_1634.py20
-rw-r--r--ctrack/organisations/models.py22
3 files changed, 59 insertions, 3 deletions
diff --git a/ctrack/organisations/migrations/0003_auto_20200220_1625.py b/ctrack/organisations/migrations/0003_auto_20200220_1625.py
new file mode 100644
index 0000000..65f4478
--- /dev/null
+++ b/ctrack/organisations/migrations/0003_auto_20200220_1625.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.2.9 on 2020-02-20 16:25
+
+import ctrack.organisations.models
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('organisations', '0002_auto_20200220_1434'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='organisation',
+ name='updated_by',
+ field=models.ForeignKey(on_delete=models.SET(ctrack.organisations.models.Organisation.get_sentinel_user), to=settings.AUTH_USER_MODEL),
+ ),
+ ]
diff --git a/ctrack/organisations/migrations/0004_auto_20200220_1634.py b/ctrack/organisations/migrations/0004_auto_20200220_1634.py
new file mode 100644
index 0000000..297931e
--- /dev/null
+++ b/ctrack/organisations/migrations/0004_auto_20200220_1634.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.2.9 on 2020-02-20 16:34
+
+import ctrack.organisations.models
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('organisations', '0003_auto_20200220_1625'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='person',
+ name='updated_by',
+ field=models.ForeignKey(on_delete=models.SET(ctrack.organisations.models.Person.get_sentinel_user), to=settings.AUTH_USER_MODEL),
+ ),
+ ]
diff --git a/ctrack/organisations/models.py b/ctrack/organisations/models.py
index a04fa32..13023a0 100644
--- a/ctrack/organisations/models.py
+++ b/ctrack/organisations/models.py
@@ -43,6 +43,15 @@ class Person(models.Model):
(5, "DV"),
(6, "Other"),
]
+
+ def get_sentinel_user():
+ """
+ We need this so that we can ensure models.SET() is applied with a callable
+ to handle when Users are deleted from the system, preventing the Person objects
+ related to them being deleted also.
+ """
+ return get_user_model().objects.get_or_create(username='DELETED USER')[0]
+
primary_nis_contact = models.BooleanField(
default=False, verbose_name="Primary NIS contact"
)
@@ -59,7 +68,7 @@ class Person(models.Model):
mobile = models.CharField(max_length=20, blank=True)
landline = models.CharField(max_length=20, blank=True)
date_updated = models.DateField(auto_now=True)
- updated_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
+ updated_by = models.ForeignKey(get_user_model(), on_delete=models.SET(get_sentinel_user))
clearance = models.IntegerField(choices=CLEARANCE_LEVEL, default=1)
clearance_sponsor = models.CharField(max_length=100, blank=True)
clearance_start_date = models.DateField(blank=True, null=True)
@@ -98,12 +107,19 @@ class Submode(models.Model):
class Organisation(models.Model):
-
DESIGNATION_TYPE = [
(1, "Automatic"),
(2, "Reserve Power"),
]
+ def get_sentinel_user():
+ """
+ We need this so that we can ensure models.SET() is applied with a callable
+ to handle when Users are deleted from the system, preventing the Organisations
+ related to them being deleted also.
+ """
+ return get_user_model().objects.get_or_create(username='DELETED USER')[0]
+
name = models.CharField(max_length=255)
slug = AutoSlugField(populate_from=["name"])
submode = models.ForeignKey(
@@ -113,7 +129,7 @@ class Organisation(models.Model):
registered_company_name = models.CharField(max_length=255, blank=True)
registered_company_number = models.CharField(max_length=100, blank=True)
date_updated = models.DateField(auto_now=True)
- updated_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
+ updated_by = models.ForeignKey(get_user_model(), on_delete=models.SET(get_sentinel_user))
comments = models.TextField(max_length=500)
active = models.BooleanField(default=True)