Commit 100c21f0 authored by Richard Glosner's avatar Richard Glosner
Browse files

Merge branch '455-create-api-for-platform-configuration' into 'main'

Resolve "Create API for platform configuration"

See merge request inject/backend!405
parents b86b0fa6 3c67e807
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@ Boolean type variables use consider `true` and `yes` as truthy, and `false` and
- `INJECT_MAX_UPLOAD_SIZE`: _int, default=10MB_ - Specifies the maximum body size of requests, including file uploads.
- `INJECT_REDICT_HOST`: _string, default=None_ - If set, enables Redict. Host address where Redict is running.
- `INJECT_REDICT_PORT`: _int, default=6379_ - Port where Redict is running.
- `INJECT_UPDATE_INTERVAL`: _int, default=10_ - Number of seconds that specifies the frequency at which automatic actions of exercise are checked and executed (minimum value is 2 seconds).
The lower the value, the more responsive the exercise may seem, but it requires higher performance.
- `INJECT_MAX_TEAMS`: _int, default=50_ - The number of maximum allowed teams in an exercise.


### Running the Application with Poetry:
+12 −0
Original line number Diff line number Diff line
from django.apps import AppConfig
from django.conf import settings


class CommonLibConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "common_lib"

    def ready(self):
        if settings.TESTING_MODE:
            return

        from common_lib.models import PlatformConfig

        if not PlatformConfig.objects.exists():
            raise Exception(
                "Initial PlatformConfig not found. Please run the migrations to create the necessary database tables."
            )
+29 −0
Original line number Diff line number Diff line
from common_lib.models import PlatformConfig
from common_lib.schema.inputs import PlatformConfigInput


class PlatformConfigManager:
    @staticmethod
    def update_config(
        platform_config_input: PlatformConfigInput,
    ) -> PlatformConfig:
        config = PlatformConfigManager.get_config()
        max_teams = platform_config_input.max_teams
        update_interval = platform_config_input.update_interval

        if max_teams is not None:
            if max_teams < 1:
                raise ValueError("'max_teams' must be at least 1")
            config.max_teams = max_teams

        if update_interval is not None:
            if update_interval < 2:
                raise ValueError("'update_interval' must be at least 2 seconds")
            config.update_interval = update_interval

        config.save()
        return config

    @staticmethod
    def get_config() -> PlatformConfig:
        return PlatformConfig.objects.first()
+30 −0
Original line number Diff line number Diff line
# Generated by Django 3.2.25 on 2025-07-01 16:52

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='PlatformConfig',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('max_teams', models.IntegerField()),
                ('update_interval', models.IntegerField()),
            ],
        ),
        migrations.AddConstraint(
            model_name='platformconfig',
            constraint=models.CheckConstraint(check=models.Q(('max_teams__gte', 1)), name='max_teams_gte_1'),
        ),
        migrations.AddConstraint(
            model_name='platformconfig',
            constraint=models.CheckConstraint(check=models.Q(('update_interval__gte', 2)), name='update_interval_gte_2'),
        ),
    ]
+26 −0
Original line number Diff line number Diff line
# Generated by Django 3.2.25 on 2025-07-03 15:54
import os

from django.db import migrations

def create_platform_config(apps, schema_editor):
    PlatformConfig = apps.get_model('common_lib', 'PlatformConfig')
    PlatformConfig.objects.create(
        max_teams=int(os.environ.get("INJECT_MAX_TEAMS", 50)),
        update_interval=int(os.environ.get("INJECT_UPDATE_INTERVAL", 10))
    )

def reverse_create_platform_config(apps, schema_editor):
    PlatformConfig = apps.get_model('common_lib', 'PlatformConfig')
    PlatformConfig.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('common_lib', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_platform_config, reverse_create_platform_config),
    ]
Loading