aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/organisations
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2020-01-21 18:03:43 +0000
committerMatthew Lemon <lemon@matthewlemon.com>2020-01-21 18:04:15 +0000
commit5f06e5559c00db759574467c3884a4645346b224 (patch)
tree8fcd68579d7441823da3e90d913a0694fc65855e /ctrack/organisations
parent490fc024506c9ed9f9c0b5a4cfbcdecc95127dbe (diff)
reconfigured Address and Organisation relationship
Diffstat (limited to 'ctrack/organisations')
-rw-r--r--ctrack/organisations/admin.py19
-rw-r--r--ctrack/organisations/migrations/0006_auto_20200121_1732.py24
-rw-r--r--ctrack/organisations/models.py24
-rw-r--r--ctrack/organisations/tests/test_models.py12
4 files changed, 57 insertions, 22 deletions
diff --git a/ctrack/organisations/admin.py b/ctrack/organisations/admin.py
index 8890f15..1f36699 100644
--- a/ctrack/organisations/admin.py
+++ b/ctrack/organisations/admin.py
@@ -1,6 +1,21 @@
from django.contrib import admin
-from .models import Organisation
+from .models import Organisation, Address, AddressType
+
+
+class OrganisationAdmin(admin.ModelAdmin):
+ pass
+
+
+class AddressTypeAdmin(admin.ModelAdmin):
+ pass
+
+
+class AddressAdmin(admin.ModelAdmin):
+ pass
+
# Register your models here.
-admin.site.register(Organisation)
+admin.site.register(Organisation, OrganisationAdmin)
+admin.site.register(AddressType, AddressTypeAdmin)
+admin.site.register(Address, AddressAdmin)
diff --git a/ctrack/organisations/migrations/0006_auto_20200121_1732.py b/ctrack/organisations/migrations/0006_auto_20200121_1732.py
new file mode 100644
index 0000000..48a0636
--- /dev/null
+++ b/ctrack/organisations/migrations/0006_auto_20200121_1732.py
@@ -0,0 +1,24 @@
+# Generated by Django 2.2.9 on 2020-01-21 17:32
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('organisations', '0005_auto_20200121_1028'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='organisation',
+ name='address',
+ ),
+ migrations.AddField(
+ model_name='address',
+ name='organisation',
+ field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='organisations.Organisation'),
+ preserve_default=False,
+ ),
+ ]
diff --git a/ctrack/organisations/models.py b/ctrack/organisations/models.py
index cf8e7de..6bd1249 100644
--- a/ctrack/organisations/models.py
+++ b/ctrack/organisations/models.py
@@ -9,7 +9,20 @@ class AddressType(models.Model):
descriptor = models.CharField(max_length=50)
+class Organisation(models.Model):
+ name = models.CharField(max_length=255, blank=False)
+
+ def get_absolute_url(self):
+ return reverse("organisations:detail", kwargs={"name": self.slugify_name()})
+
+ def slugify_name(self):
+ return slugify(self.name)
+
+
class Address(models.Model):
+ organisation = models.ForeignKey(
+ Organisation, related_name="addresses", on_delete=models.CASCADE, blank=False
+ )
type = models.ForeignKey(AddressType, on_delete=models.CASCADE, default=1)
line1 = models.CharField(max_length=255)
line2 = models.CharField(max_length=255)
@@ -19,14 +32,3 @@ class Address(models.Model):
postcode = models.CharField(max_length=10)
country = models.CharField(max_length=100)
other_details = models.CharField(max_length=255)
-
-
-class Organisation(models.Model):
- name = models.CharField(max_length=255, blank=False)
- address = models.ForeignKey(Address, on_delete=models.CASCADE, blank=True, null=True)
-
- def get_absolute_url(self):
- return reverse("organisations:detail", kwargs={"name": self.slugify_name()})
-
- def slugify_name(self):
- return slugify(self.name)
diff --git a/ctrack/organisations/tests/test_models.py b/ctrack/organisations/tests/test_models.py
index d070a84..946fda5 100644
--- a/ctrack/organisations/tests/test_models.py
+++ b/ctrack/organisations/tests/test_models.py
@@ -1,7 +1,7 @@
import pytest
from slugify import slugify
-from ..models import Organisation, Address
+from ..models import Organisation
pytestmark = pytest.mark.django_db
@@ -11,12 +11,10 @@ def test_organisation_get_absolute_url(org: Organisation):
assert org.get_absolute_url() == f"/organisations/{slug}/"
-def test_create_organisation(addr: Address):
- Organisation(name="Big Bad OES Corporation", address=addr).save()
+def test_create_organisation():
+ Organisation(name="Big Bad OES Corporation").save()
# The organisation is saved in the db
assert Organisation.objects.get(name="Big Bad OES Corporation")
- # The organisation has the correct address
- assert Organisation.objects.get(name="Big Bad OES Corporation").address.type.descriptor == "Primary Address"
def test_delete_organisation(org: Organisation):
@@ -30,9 +28,5 @@ def test_delete_organisation(org: Organisation):
def test_update_organisation(org: Organisation):
# Change the name of the organisation
org.name = "Tonkers Ltd"
- # Get current value of line1 of the address
- addr_line1 = org.address.line1
org.save()
assert org.name == "Tonkers Ltd"
- # Assert that the address hasn't changed
- assert addr_line1 == org.address.line1