From da2e7d93a9a407664374d3a5bc03ca3c385a30b9 Mon Sep 17 00:00:00 2001 From: Valentin Gagarin Date: Mon, 24 Feb 2025 11:01:57 +0100 Subject: [PATCH] add enable toggle, let operators have many configuraitons this commit is a bit of a jumble, but it allows us to disable a configuration so the associated deployment can in principle be garbage-collected, and allows operators to have multiple configurations. it also (as a temporary hack) ties the deployment subdomain to the username so it's clear to operators where we're deploying to. --- panel/src/panel/forms.py | 9 +++++ ...tion_user_configuration_enable_and_more.py | 35 +++++++++++++++++++ panel/src/panel/models.py | 28 +++++++++++---- panel/src/panel/views.py | 2 +- 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 panel/src/panel/migrations/0004_remove_configuration_user_configuration_enable_and_more.py diff --git a/panel/src/panel/forms.py b/panel/src/panel/forms.py index 4f66229b..83cc2e65 100644 --- a/panel/src/panel/forms.py +++ b/panel/src/panel/forms.py @@ -11,3 +11,12 @@ class Deployment(forms.ModelForm): 'pixelfed', 'peertube', ] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + operator = self.instance.operator.username + self.fields['domain'].choices = [ + (code, f"{operator}.{label}") + for code, label in self.instance._meta.get_field('domain').choices + ] diff --git a/panel/src/panel/migrations/0004_remove_configuration_user_configuration_enable_and_more.py b/panel/src/panel/migrations/0004_remove_configuration_user_configuration_enable_and_more.py new file mode 100644 index 00000000..ab5ad013 --- /dev/null +++ b/panel/src/panel/migrations/0004_remove_configuration_user_configuration_enable_and_more.py @@ -0,0 +1,35 @@ +# Generated by Django 4.2.16 on 2025-02-24 09:49 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('panel', '0003_renamemodel'), + ] + + operations = [ + migrations.RemoveField( + model_name='configuration', + name='user', + ), + migrations.AddField( + model_name='configuration', + name='enable', + field=models.BooleanField(default=False, help_text='Enable the configuration'), + ), + migrations.AddField( + model_name='configuration', + name='operator', + field=models.ForeignKey(help_text='Operator who owns the configuration', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='configurations', to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='configuration', + name='domain', + field=models.CharField(choices=[('fediversity_eu', 'fediversity.eu'), ('fediversity_net', 'fediversity.net')], max_length=255), + ), + ] diff --git a/panel/src/panel/models.py b/panel/src/panel/models.py index f1be7272..92dae04c 100644 --- a/panel/src/panel/models.py +++ b/panel/src/panel/models.py @@ -1,15 +1,31 @@ from django.db import models from django.contrib.auth.models import User -DOMAIN_LIST = [("fediversity_eu", "fediversity.eu"), ("fediversity_net", "fediversity.net")] class Configuration(models.Model): - user = models.ForeignKey(User, on_delete=models.CASCADE, default=1) - domain = models.CharField(choices=DOMAIN_LIST, max_length=255) + operator = models.ForeignKey( + User, + on_delete=models.SET_NULL, + null=True, + related_name="configurations", + help_text="Operator who owns the configuration", + ) + enable = models.BooleanField( + default=False, + help_text="Enable the configuration", + ) + domain = models.CharField( + # XXX: hard-code available apex domains for now, + # they will be prefixed by the user name + # TODO: map to user's registered domains + choices=[ + ("fediversity_eu", "fediversity.eu"), + ("fediversity_net", "fediversity.net") + ], + max_length=255, + ) + # TODO: map to configuration model per service mastodon = models.BooleanField(default=False) pixelfed = models.BooleanField(default=False) peertube = models.BooleanField(default=False) - - def __str__(self): - return f"User: {self.user.username}, Domain: {self.domain}, Mastodon: {self.mastodon}, Pixelfed: {self.pixelfed}, Peertube: {self.peertube}" diff --git a/panel/src/panel/views.py b/panel/src/panel/views.py index f36ea383..740ecc7c 100644 --- a/panel/src/panel/views.py +++ b/panel/src/panel/views.py @@ -33,5 +33,5 @@ class ConfigurationForm(LoginRequiredMixin, UpdateView): def get_object(self, queryset=None): obj, created = Configuration.objects.get_or_create( - user=self.request.user) + operator=self.request.user) return obj