diff --git a/dash/admin.py b/dash/admin.py index 8c38f3f..ec19ca5 100644 --- a/dash/admin.py +++ b/dash/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin -# Register your models here. +from .models import InstanceConfiguration + +admin.site.register(InstanceConfiguration) diff --git a/dash/migrations/0001_initial.py b/dash/migrations/0001_initial.py new file mode 100644 index 0000000..04a0f8a --- /dev/null +++ b/dash/migrations/0001_initial.py @@ -0,0 +1,52 @@ +# Generated by Django 4.0.3 on 2022-06-17 14:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [] + + operations = [ + migrations.CreateModel( + name="InstanceConfiguration", + fields=[ + ("ID", models.AutoField(primary_key=True, serialize=False)), + ( + "name", + models.CharField( + max_length=32, + unique=True, + verbose_name="Name of this configuration", + ), + ), + ( + "ram", + models.FloatField( + verbose_name="The amount of RAM an instance will have" + ), + ), + ( + "cpu", + models.IntegerField( + verbose_name="The amount of CPU an instance will have" + ), + ), + ( + "storage", + models.FloatField( + verbose_name="The amount of storage an instance will have" + ), + ), + ("created_at", models.DateTimeField(auto_now_add=True)), + ], + ), + migrations.AddConstraint( + model_name="instanceconfiguration", + constraint=models.UniqueConstraint( + fields=("ram", "cpu", "storage"), name="no_repeat_config" + ), + ), + ] diff --git a/dash/models.py b/dash/models.py index 71a8362..b400f2f 100644 --- a/dash/models.py +++ b/dash/models.py @@ -1,3 +1,52 @@ -from django.db import models +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . -# Create your models here. +from django.db import models +from django.contrib.auth.models import User +from django.utils.http import urlencode +from django.urls import reverse + + +class InstanceConfiguration(models.Model): + ID = models.AutoField(primary_key=True) + name = models.CharField( + "Name of this configuration", + unique=True, + max_length=32, + ) + ram = models.FloatField( + "The amount of RAM an instance will have", + null=False, + ) + cpu = models.IntegerField( + "The amount of CPU an instance will have", + null=False, + ) + storage = models.FloatField( + "The amount of storage an instance will have", + null=False, + ) + + created_at = models.DateTimeField(auto_now_add=True, blank=True) + + def __str__(self): + return f"{self.name}" + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["ram", "cpu", "storage"], name="no_repeat_config" + ) + ] diff --git a/dash/tests.py b/dash/tests.py index 542aa6c..7af6025 100644 --- a/dash/tests.py +++ b/dash/tests.py @@ -16,8 +16,10 @@ from django.contrib.auth import get_user_model from django.utils.http import urlencode from django.urls import reverse from django.test import TestCase, Client, override_settings -from django.contrib.auth import authenticate from django.conf import settings +from django.db.utils import IntegrityError + +from .models import InstanceConfiguration class DashHome(TestCase): @@ -72,3 +74,27 @@ class DashHome(TestCase): self.assertEqual(b"Billing" in resp.content, True) self.assertEqual(b"Support" in resp.content, True) self.assertEqual(b"Logout" in resp.content, True) + + +class InstancesConfig(TestCase): + """ + Tests InstancesConfig model + """ + + def test_unique_constraint(self): + """ + Test configuration uniqueness + """ + config1 = InstanceConfiguration( + name="test config 1", ram=0.5, cpu=1, storage=0.5 + ) + config1.save() + config2 = InstanceConfiguration( + name="test config 2", ram=0.5, cpu=2, storage=0.5 + ) + config2.save() + with self.assertRaises(IntegrityError): + config3 = InstanceConfiguration( + name="test config 3", ram=0.5, cpu=1, storage=0.5 + ) + config3.save()