diff --git a/panel/src/panel/forms.py b/panel/src/panel/forms.py index 6f1518df..09c6587b 100644 --- a/panel/src/panel/forms.py +++ b/panel/src/panel/forms.py @@ -1,4 +1,5 @@ from django import forms +# from django.forms import inlineformset_factory from panel.models import ( Configuration, PeertubeConfig, @@ -42,3 +43,5 @@ class PeertubeConfigForm(forms.ModelForm): class Meta: model = PeertubeConfig fields = ['enable'] + +# BookInlineFormSet = inlineformset_factory(Deployment, MastodonConfigForm, extra=1, can_delete=False) diff --git a/panel/src/panel/models.py b/panel/src/panel/models.py index c83dd457..aff30d4f 100644 --- a/panel/src/panel/models.py +++ b/panel/src/panel/models.py @@ -24,6 +24,38 @@ class PeertubeConfig(models.Model): class Configuration(models.Model): + class ConfigurationManager(models.Manager): + # Define which related fields should be auto-created + auto_create_related = [ + 'mastodon', + 'pixelfed', + 'peertube', + ] + + def create(self, **kwargs): + # Create the model instance but don't save yet + instance = self.model(**kwargs) + + # Auto-create any missing related objects + for field_name in self.auto_create_related: + # Skip if the field was provided + if field_name in kwargs: + continue + + # Get the related model class + field = self.model._meta.get_field(field_name) + related_model = field.related_model + + # Create the related object + related_obj = related_model.objects.create() + + # Set the relation + setattr(instance, field_name, related_obj) + + # Save and return the instance + instance.save() + return instance + operator = models.ForeignKey( User, on_delete=models.SET_NULL, @@ -48,3 +80,16 @@ class Configuration(models.Model): mastodon = models.OneToOneField(MastodonConfig, on_delete=models.CASCADE, related_name='config', null=True) pixelfed = models.OneToOneField(PixelfedConfig, on_delete=models.CASCADE, related_name='config', null=True) peertube = models.OneToOneField(PeertubeConfig, on_delete=models.CASCADE, related_name='config', null=True) + + # Use the custom manager + # objects = ConfigurationManager() + + # def save(self, *args, **kwargs): + # # Create the related items if they don't exist + # if not hasattr(self, 'mastodon'): + # MastodonConfig.objects.create(config=self) + # if not hasattr(self, 'mastodon'): + # MastodonConfig.objects.create(config=self) + # if not hasattr(self, 'mastodon'): + # MastodonConfig.objects.create(config=self) + # super().save(*args, **kwargs)