From 735b90cac61d3b47b985b15299da487940a52acd Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Mon, 14 Sep 2020 17:01:31 +0300 Subject: [PATCH 1/2] Remove the "fac_set" property from InternetExchange As far as I can tell, this doesn't work. self.ixfac_set is a manager object, which is not iterable (one would have to do self.ixfac_set.all() to iterate through it). --- django_peeringdb/models/concrete.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/django_peeringdb/models/concrete.py b/django_peeringdb/models/concrete.py index 5a4abdf..24df9f8 100644 --- a/django_peeringdb/models/concrete.py +++ b/django_peeringdb/models/concrete.py @@ -69,10 +69,6 @@ class InternetExchange(InternetExchangeBase): on_delete=models.CASCADE, ) - @property - def fac_set(self): - return [ixfac.fac for ixfac in self.ixfac_set] - @expose_model class InternetExchangeFacility(InternetExchangeFacilityBase): From 740bb9936bcd00489d2310d6e8944b940479f60a Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Mon, 14 Sep 2020 17:03:06 +0300 Subject: [PATCH 2/2] Add ManyToManyFields on concrete models In the concrete models, add ManyToManyFields between Network and Facility/IXLan, and InternetExchange and Facility. Intermediate models already exist (NetworkFacility, NetworkIXLan and InternetExchangeFacility respectively) and these are being used as "through" models with Django's many-to-many field. Effectively, this does not really change the schema, but provides forward and reverse managers to go from one of these objects to the next. So for example, one can use ixp.fac_set.all() to list all facilities for an exchange, or fac.ix_set.all() to do the inverse. Note that this change really makes the definitions of the InternetExchangeFacility and InternetExchangeFacilityBase models moot, as they're "through" models with no additional fields, and if they were absent, Django would create the intermediate tables in the database itself. However, avoid not being removed in this change to minimize the amount of changes and risk involved here. Finally, although a more descriptive "facilities" or "networks" would be preferrable in the author's opinion, the new fields are named "_set" (fac_set, ix_set, ixlan_set) to match existing conventions. --- .../migrations/0012_manytomany.py | 33 ++++++++++++++++ django_peeringdb/models/concrete.py | 38 ++++++++++++++----- 2 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 django_peeringdb/migrations/0012_manytomany.py diff --git a/django_peeringdb/migrations/0012_manytomany.py b/django_peeringdb/migrations/0012_manytomany.py new file mode 100644 index 0000000..9b87841 --- /dev/null +++ b/django_peeringdb/migrations/0012_manytomany.py @@ -0,0 +1,33 @@ +# Generated by Django 2.2.12 on 2020-09-13 13:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("django_peeringdb", "0011_ixlan_ixf_fields"), + ] + + operations = [ + migrations.AddField( + model_name="network", + name="fac_set", + field=models.ManyToManyField( + related_name="net_set", + through="django_peeringdb.NetworkFacility", + to="django_peeringdb.Facility", + verbose_name="Facility", + ), + ), + migrations.AddField( + model_name="network", + name="ixlan_set", + field=models.ManyToManyField( + related_name="net_set", + through="django_peeringdb.NetworkIXLan", + to="django_peeringdb.IXLan", + verbose_name="Internet Exchange LAN", + ), + ), + ] diff --git a/django_peeringdb/models/concrete.py b/django_peeringdb/models/concrete.py index 24df9f8..85ce671 100644 --- a/django_peeringdb/models/concrete.py +++ b/django_peeringdb/models/concrete.py @@ -50,16 +50,6 @@ class Facility(FacilityBase): ) -@expose_model -class Network(NetworkBase): - org = models.ForeignKey( - Organization, - related_name="net_set", - verbose_name=_("Organization"), - on_delete=models.CASCADE, - ) - - @expose_model class InternetExchange(InternetExchangeBase): org = models.ForeignKey( @@ -68,6 +58,12 @@ class InternetExchange(InternetExchangeBase): verbose_name=_("Organization"), on_delete=models.CASCADE, ) + fac_set = models.ManyToManyField( + Facility, + related_name="ix_set", + verbose_name=_("Facility"), + through="InternetExchangeFacility", + ) @expose_model @@ -113,6 +109,28 @@ class IXLanPrefix(IXLanPrefixBase): ) +@expose_model +class Network(NetworkBase): + org = models.ForeignKey( + Organization, + related_name="net_set", + verbose_name=_("Organization"), + on_delete=models.CASCADE, + ) + fac_set = models.ManyToManyField( + Facility, + related_name="net_set", + verbose_name=_("Facility"), + through="NetworkFacility", + ) + ixlan_set = models.ManyToManyField( + IXLan, + related_name="net_set", + verbose_name=_("Internet Exchange LAN"), + through="NetworkIXLan", + ) + + @expose_model class NetworkContact(ContactBase): net = models.ForeignKey(