feat: add instance configuration form visible only to admins
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
parent
3be96ed131
commit
3705c64616
|
@ -1,3 +1,5 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
# Register your models here.
|
from .models import InstanceConfiguration
|
||||||
|
|
||||||
|
admin.site.register(InstanceConfiguration)
|
||||||
|
|
|
@ -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"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,3 +1,52 @@
|
||||||
from django.db import models
|
# 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/>.
|
||||||
|
|
||||||
# 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"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
|
@ -16,8 +16,10 @@ from django.contrib.auth import get_user_model
|
||||||
from django.utils.http import urlencode
|
from django.utils.http import urlencode
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.test import TestCase, Client, override_settings
|
from django.test import TestCase, Client, override_settings
|
||||||
from django.contrib.auth import authenticate
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.db.utils import IntegrityError
|
||||||
|
|
||||||
|
from .models import InstanceConfiguration
|
||||||
|
|
||||||
|
|
||||||
class DashHome(TestCase):
|
class DashHome(TestCase):
|
||||||
|
@ -72,3 +74,27 @@ class DashHome(TestCase):
|
||||||
self.assertEqual(b"Billing" in resp.content, True)
|
self.assertEqual(b"Billing" in resp.content, True)
|
||||||
self.assertEqual(b"Support" in resp.content, True)
|
self.assertEqual(b"Support" in resp.content, True)
|
||||||
self.assertEqual(b"Logout" 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()
|
||||||
|
|
Loading…
Reference in New Issue