diff --git a/dash/apps.py b/dash/apps.py index efbebea..bf29acb 100644 --- a/dash/apps.py +++ b/dash/apps.py @@ -1,6 +1,25 @@ +# 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 . from django.apps import AppConfig class DashConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "dash" + + def ready(self): + from .models import InstanceConfiguration + + InstanceConfiguration.create_default_configs() diff --git a/dash/models.py b/dash/models.py index 382b9b9..f486a34 100644 --- a/dash/models.py +++ b/dash/models.py @@ -16,6 +16,7 @@ from django.db import models from django.contrib.auth.models import User from django.utils.http import urlencode +from django.db.utils import IntegrityError from django.urls import reverse @@ -42,6 +43,24 @@ class InstanceConfiguration(models.Model): created_at = models.DateTimeField(auto_now_add=True, blank=True) + @classmethod + def create_default_configs(cls: "InstanceConfiguration"): + configs = [ + cls(name="s1-2", rent=10, ram=2, cpu=1, storage=10), + cls(name="s1-4", rent=20, ram=4, cpu=1, storage=20), + cls(name="s1-8", rent=40, ram=8, cpu=2, storage=40), + ] + for config in configs: + print(f"[*] Saving configuration {config.name}") + try: + config.save() + except IntegrityError as db_error: + print(f"[!] Configuration {config.name} exists") + continue + except Exception as e: + print(f"[ERROR] {e}") + raise e + def __str__(self): return f"{self.name}" diff --git a/dash/tests.py b/dash/tests.py index e2edcd8..f22131a 100644 --- a/dash/tests.py +++ b/dash/tests.py @@ -127,6 +127,34 @@ class InstancesConfig(TestCase): ) config3.save() + def test_default_configuration_is_loaded(self): + """ + Expects InstancesConfig titled "s1-2", "s1-4" and "s1-8" + + ref: https://gitea.hostea.org/Hostea/july-mvp/issues/10#issuecomment-639 + """ + + InstanceConfiguration.create_default_configs() + + self.assertEqual( + InstanceConfiguration.objects.filter( + name="s1-2", rent=10, ram=2, cpu=1, storage=10 + ).exists(), + True, + ) + self.assertEqual( + InstanceConfiguration.objects.filter( + name="s1-4", rent=20, ram=4, cpu=1, storage=20 + ).exists(), + True, + ) + self.assertEqual( + InstanceConfiguration.objects.filter( + name="s1-8", rent=40, ram=8, cpu=2, storage=40 + ).exists(), + True, + ) + class CreateInstance(TestCase): def setUp(self):