From cdd4a0152113137ad2bf1eb8d308ff83d306d9fd Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Wed, 28 Feb 2024 21:26:34 +0100 Subject: [PATCH] Added models for Cafeteria, Location, Institution --- api_v1/admin.py | 10 +++++-- api_v1/cafeteria/__init__.py | 0 api_v1/cafeteria/model.py | 19 ++++++++++++ api_v1/institution/__init__.py | 0 api_v1/institution/model.py | 22 ++++++++++++++ api_v1/location/__init__.py | 0 api_v1/location/model.py | 22 ++++++++++++++ .../migrations/0003_createdLocationModel.py | 28 +++++++++++++++++ .../migrations/0004_createdCafeteriaModel.py | 24 +++++++++++++++ .../migrations/0005_addedInstitutionModel.py | 30 +++++++++++++++++++ api_v1/models.py | 5 +++- api_v1/profile/{profile.py => model.py} | 3 ++ api_v1/profile/profile_services.py | 2 +- 13 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 api_v1/cafeteria/__init__.py create mode 100644 api_v1/cafeteria/model.py create mode 100644 api_v1/institution/__init__.py create mode 100644 api_v1/institution/model.py create mode 100644 api_v1/location/__init__.py create mode 100644 api_v1/location/model.py create mode 100644 api_v1/migrations/0003_createdLocationModel.py create mode 100644 api_v1/migrations/0004_createdCafeteriaModel.py create mode 100644 api_v1/migrations/0005_addedInstitutionModel.py rename api_v1/profile/{profile.py => model.py} (74%) diff --git a/api_v1/admin.py b/api_v1/admin.py index 465ffc4..8718f5b 100644 --- a/api_v1/admin.py +++ b/api_v1/admin.py @@ -1,6 +1,12 @@ from django.contrib import admin -from api_v1.profile.profile import Profile + +from api_v1.cafeteria.model import Cafeteria +from api_v1.institution.model import Institution +from api_v1.location.model import Location +from api_v1.profile.model import Profile # Register your models here. admin.site.register(Profile) - +admin.site.register(Location) +admin.site.register(Cafeteria) +admin.site.register(Institution) diff --git a/api_v1/cafeteria/__init__.py b/api_v1/cafeteria/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api_v1/cafeteria/model.py b/api_v1/cafeteria/model.py new file mode 100644 index 0000000..51f3029 --- /dev/null +++ b/api_v1/cafeteria/model.py @@ -0,0 +1,19 @@ +import uuid + +from django.db import models + +from ..institution.model import Institution +from ..location.model import Location + + +class Cafeteria(models.Model): + cafeteria_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + + name = models.CharField(max_length=50, blank=False, null=False) + location = models.OneToOneField(Location, on_delete=models.DO_NOTHING) + + website = models.URLField() + institutes = models.ManyToManyField(Institution) + + def __str__(self): + return '{} ({})'.format(self.name, self.location) diff --git a/api_v1/institution/__init__.py b/api_v1/institution/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api_v1/institution/model.py b/api_v1/institution/model.py new file mode 100644 index 0000000..06136e2 --- /dev/null +++ b/api_v1/institution/model.py @@ -0,0 +1,22 @@ +import uuid + +from django.db import models + +from api_v1.location.model import Location + + +class Institution(models.Model): + institution_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + + name = models.CharField(max_length=150, blank=False, null=False) + description = models.TextField(null=True) + + location = models.OneToOneField(Location, on_delete=models.DO_NOTHING, null=True, blank=True) + + website = models.URLField(null=True) + + def __str__(self): + result = self.name + if self.description: + result += f' - {self.description}' + return result diff --git a/api_v1/location/__init__.py b/api_v1/location/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api_v1/location/model.py b/api_v1/location/model.py new file mode 100644 index 0000000..f69b849 --- /dev/null +++ b/api_v1/location/model.py @@ -0,0 +1,22 @@ +import uuid + +from django.db import models + + +class Location(models.Model): + location_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + + longitude = models.DecimalField(max_digits=9, decimal_places=6) + latitude = models.DecimalField(max_digits=9, decimal_places=6) + + country_code = models.CharField(max_length=2) # Following ISO-3166-1 + country_name = models.CharField(max_length=50) + + city = models.CharField(max_length=50) + postal_code = models.CharField(max_length=20) + street = models.CharField(max_length=50) + number = models.CharField(max_length=20) + + def __str__(self): + return '{} {}, {} {}, {} {}'.format(self.street, self.number, self.postal_code, self.city, self.country_code, + self.country_name) diff --git a/api_v1/migrations/0003_createdLocationModel.py b/api_v1/migrations/0003_createdLocationModel.py new file mode 100644 index 0000000..2b898fd --- /dev/null +++ b/api_v1/migrations/0003_createdLocationModel.py @@ -0,0 +1,28 @@ +# Generated by Django 5.0.2 on 2024-02-28 20:03 + +import uuid +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api_v1', '0002_rebrandUserInformationToProfile'), + ] + + operations = [ + migrations.CreateModel( + name='Location', + fields=[ + ('location_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('longitude', models.DecimalField(decimal_places=6, max_digits=9)), + ('latitude', models.DecimalField(decimal_places=6, max_digits=9)), + ('country_code', models.CharField(max_length=2)), + ('country_name', models.CharField(max_length=50)), + ('city', models.CharField(max_length=50)), + ('postal_code', models.CharField(max_length=20)), + ('street', models.CharField(max_length=50)), + ('number', models.CharField(max_length=20)), + ], + ), + ] diff --git a/api_v1/migrations/0004_createdCafeteriaModel.py b/api_v1/migrations/0004_createdCafeteriaModel.py new file mode 100644 index 0000000..1d2dab1 --- /dev/null +++ b/api_v1/migrations/0004_createdCafeteriaModel.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.2 on 2024-02-28 20:03 + +import django.db.models.deletion +import uuid +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api_v1', '0003_createdLocationModel'), + ] + + operations = [ + migrations.CreateModel( + name='Cafeteria', + fields=[ + ('cafeteria_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=50)), + ('website', models.URLField()), + ('location', models.OneToOneField(on_delete=django.db.models.deletion.DO_NOTHING, to='api_v1.location')), + ], + ), + ] diff --git a/api_v1/migrations/0005_addedInstitutionModel.py b/api_v1/migrations/0005_addedInstitutionModel.py new file mode 100644 index 0000000..87585cd --- /dev/null +++ b/api_v1/migrations/0005_addedInstitutionModel.py @@ -0,0 +1,30 @@ +# Generated by Django 5.0.2 on 2024-02-28 20:23 + +import django.db.models.deletion +import uuid +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api_v1', '0004_createdCafeteriaModel'), + ] + + operations = [ + migrations.CreateModel( + name='Institution', + fields=[ + ('institution_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=150)), + ('description', models.TextField(null=True)), + ('website', models.URLField(null=True)), + ('location', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='api_v1.location')), + ], + ), + migrations.AddField( + model_name='cafeteria', + name='institutes', + field=models.ManyToManyField(to='api_v1.institution'), + ), + ] diff --git a/api_v1/models.py b/api_v1/models.py index 69f6446..1cb81d8 100644 --- a/api_v1/models.py +++ b/api_v1/models.py @@ -1 +1,4 @@ -from api_v1.profile.profile import Profile +from api_v1.profile.model import Profile +from api_v1.location.model import Location +from api_v1.cafeteria.model import Cafeteria +from api_v1.institution.model import Institution diff --git a/api_v1/profile/profile.py b/api_v1/profile/model.py similarity index 74% rename from api_v1/profile/profile.py rename to api_v1/profile/model.py index 7229c28..3bb920d 100644 --- a/api_v1/profile/profile.py +++ b/api_v1/profile/model.py @@ -6,3 +6,6 @@ class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) display_name = models.CharField(max_length=150, null=False) + + def __str__(self): + return '{} ({})'.format(self.display_name, self.user) diff --git a/api_v1/profile/profile_services.py b/api_v1/profile/profile_services.py index c84b59b..3395f9d 100644 --- a/api_v1/profile/profile_services.py +++ b/api_v1/profile/profile_services.py @@ -1,4 +1,4 @@ -from api_v1.profile.profile import Profile +from api_v1.profile.model import Profile from django.contrib.auth.models import User from typing import Optional