Aravinth Manivannan 2022-06-18 13:52:52 +05:30
parent 99a7533a79
commit 39e54df5ef
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
3 changed files with 66 additions and 0 deletions

View File

@ -1,6 +1,25 @@
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# 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 <http://www.gnu.org/licenses/>.
from django.apps import AppConfig from django.apps import AppConfig
class DashConfig(AppConfig): class DashConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField" default_auto_field = "django.db.models.BigAutoField"
name = "dash" name = "dash"
def ready(self):
from .models import InstanceConfiguration
InstanceConfiguration.create_default_configs()

View File

@ -16,6 +16,7 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.http import urlencode from django.utils.http import urlencode
from django.db.utils import IntegrityError
from django.urls import reverse from django.urls import reverse
@ -42,6 +43,24 @@ class InstanceConfiguration(models.Model):
created_at = models.DateTimeField(auto_now_add=True, blank=True) 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): def __str__(self):
return f"{self.name}" return f"{self.name}"

View File

@ -127,6 +127,34 @@ class InstancesConfig(TestCase):
) )
config3.save() 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): class CreateInstance(TestCase):
def setUp(self): def setUp(self):