From 745b0b3007ff167c850d4e6e4df388a1f40d4379 Mon Sep 17 00:00:00 2001 From: Dana Lambert Date: Thu, 4 Nov 2021 11:18:04 +1300 Subject: [PATCH] Add Habitat, Zone, HabitatImage and ZoneImageSegment models --- backend/right_tree/api/admin.py | 20 +++++--- ...itat_habitatimage_zone_zoneimagesegment.py | 50 +++++++++++++++++++ backend/right_tree/api/models.py | 42 ++++++++++++++++ 3 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 backend/right_tree/api/migrations/0002_habitat_habitatimage_zone_zoneimagesegment.py diff --git a/backend/right_tree/api/admin.py b/backend/right_tree/api/admin.py index 36da8b8..cf5b649 100644 --- a/backend/right_tree/api/admin.py +++ b/backend/right_tree/api/admin.py @@ -1,10 +1,14 @@ from django.contrib import admin -from right_tree.api.models import Plant, SoilOrder, SoilLayer, SoilVariant, EcologicalRegion, EcologicalDistrictLayer, ToleranceLevel +import right_tree.api.models as models -admin.site.register(Plant) -admin.site.register(SoilOrder) -admin.site.register(SoilLayer) -admin.site.register(SoilVariant) -admin.site.register(EcologicalRegion) -admin.site.register(EcologicalDistrictLayer) -admin.site.register(ToleranceLevel) +admin.site.register(models.Plant) +admin.site.register(models.SoilOrder) +admin.site.register(models.SoilLayer) +admin.site.register(models.SoilVariant) +admin.site.register(models.EcologicalRegion) +admin.site.register(models.EcologicalDistrictLayer) +admin.site.register(models.ToleranceLevel) +admin.site.register(models.HabitatImage) +admin.site.register(models.Habitat) +admin.site.register(models.Zone) +admin.site.register(models.ZoneImageSegment) diff --git a/backend/right_tree/api/migrations/0002_habitat_habitatimage_zone_zoneimagesegment.py b/backend/right_tree/api/migrations/0002_habitat_habitatimage_zone_zoneimagesegment.py new file mode 100644 index 0000000..713fa01 --- /dev/null +++ b/backend/right_tree/api/migrations/0002_habitat_habitatimage_zone_zoneimagesegment.py @@ -0,0 +1,50 @@ +# Generated by Django 3.2.8 on 2021-11-03 21:38 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Habitat', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50)), + ], + ), + migrations.CreateModel( + name='HabitatImage', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50)), + ('image_filename', models.CharField(default='-', max_length=50)), + ('habitat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='habitat', to='api.habitat')), + ], + ), + migrations.CreateModel( + name='Zone', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50)), + ('variant', models.CharField(blank=True, max_length=50, null=True)), + ('refined_variant', models.CharField(blank=True, max_length=100, null=True)), + ('redirect_habitat', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='redirect_habitat', to='api.habitat')), + ], + ), + migrations.CreateModel( + name='ZoneImageSegment', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('segment_order', models.PositiveIntegerField(default=0)), + ('segment_percentage_width', models.PositiveIntegerField(default=0)), + ('habitat_image', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='habitat_image', to='api.habitatimage')), + ('zone', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='zone', to='api.zone')), + ], + ), + ] diff --git a/backend/right_tree/api/models.py b/backend/right_tree/api/models.py index 33e54fc..9dcc5d4 100644 --- a/backend/right_tree/api/models.py +++ b/backend/right_tree/api/models.py @@ -76,3 +76,45 @@ class Plant(models.Model): def __str__(self): return self.name + + +class Habitat(models.Model): + name = models.CharField(max_length=50) + + def __str__(self): + return self.name + + +class HabitatImage(models.Model): + habitat = models.ForeignKey( + Habitat, related_name='habitat', on_delete=models.CASCADE) + name = models.CharField(max_length=50) + image_filename = models.CharField(max_length=50, default='-') + + def __str__(self): + return self.name + + +class Zone(models.Model): + name = models.CharField(max_length=50) + variant = models.CharField(null=True, blank=True, max_length=50) + refined_variant = models.CharField(null=True, blank=True, max_length=100) + redirect_habitat = models.ForeignKey( + Habitat, blank=True, null=True, on_delete=models.CASCADE, related_name='redirect_habitat') + + def __str__(self): + refined_variant_str = f", {self.refined_variant}" if self.refined_variant is not None else "" + variant_str = f"({self.variant}{refined_variant_str})" if self.variant is not None else "" + return f"{self.name} {variant_str}" + + +class ZoneImageSegment(models.Model): + zone = models.ForeignKey( + Zone, on_delete=models.CASCADE, related_name='zone') + habitat_image = models.ForeignKey( + HabitatImage, on_delete=models.CASCADE, related_name='habitat_image') + segment_order = models.PositiveIntegerField(default=0) + segment_percentage_width = models.PositiveIntegerField(default=0) + + def __str__(self): + return f"{self.habitat_image.name}, {self.zone.name}"