From ecd273bcc38b35b0cef50e598ff8c73cc7ab4759 Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Wed, 30 Oct 2024 13:29:34 +0100 Subject: [PATCH] musician saas wordpress initial --- orchestra/contrib/musician/forms.py | 85 +++++++++++++++++++- orchestra/contrib/saas/backends/nextcloud.py | 2 +- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/orchestra/contrib/musician/forms.py b/orchestra/contrib/musician/forms.py index 6fb90f7e..2bd07a03 100644 --- a/orchestra/contrib/musician/forms.py +++ b/orchestra/contrib/musician/forms.py @@ -2,6 +2,7 @@ from django import forms from django.contrib.auth.forms import AuthenticationForm from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ +from orchestra.utils.python import random_ascii from django.forms.widgets import HiddenInput from django.contrib.auth.hashers import make_password @@ -204,8 +205,20 @@ class SystemUsersChangePasswordForm(ChangePasswordForm): fields = ("password",) model = SystemUser - +from orchestra.forms.widgets import SpanWidget +from orchestra.forms import widgets +from django.utils.safestring import mark_safe +from rest_framework import serializers class SaasUpdateForm(forms.ModelForm): + site_url = forms.CharField(label=_("Site URL"), widget=SpanWidget(), required=False) + + # dos campos para wordpress + blog_id = forms.IntegerField(label=("Blog ID"), widget=widgets.SpanWidget, required=False, + help_text=_("ID of this blog used by WordPress, the only attribute that doesn't change.")) + email = forms.EmailField(label=_("Email"), + help_text=_("A new user will be created if the above email address is not in the database.
" + "The username and password will be mailed to this email address.")) + class Meta: model = SaaS fields = ("is_active", "service", "name", "data", "custom_url") @@ -214,12 +227,76 @@ class SaasUpdateForm(forms.ModelForm): self.user = kwargs.pop('user') super().__init__(*args, **kwargs) self.fields['name'].widget.attrs['readonly'] = True - self.fields['service'].widget.attrs['disabled'] = 'disabled' + self.fields['site_url'].widget.attrs['readonly'] = True + self.fields['email'].widget.attrs['readonly'] = True + self.fields['blog_id'].widget.attrs['readonly'] = True + self.fields['service'].widget = HiddenInput() self.fields['data'].widget = HiddenInput() self.fields["custom_url"].widget = HiddenInput() + + # asignar valor al field site_url + site_domain = self.instance.get_site_domain() + context = { + 'site_name': '<site_name>', + 'name': '<site_name>', + } + site_domain = site_domain % context + if '<site_name>' in site_domain: + site_link = site_domain + else: + site_link = '%s' % (site_domain, site_domain) + self.fields['site_url'].widget.display = site_link + + if self.instance: + if self.instance.pk: + self.fields['data'].required = False + + if self.instance.service == 'nextcloud': + self.fields["email"].widget = HiddenInput() + self.fields["blog_id"].widget = HiddenInput() + self.fields["email"].required = False + + + if self.instance.service == 'wordpress': + admin_url = 'http://%s/wp-admin/' % self.instance.get_site_domain() + help_text = 'Admin URL: {0}'.format(admin_url) + self.fields['site_url'].help_text = mark_safe(help_text) + + if self.instance: + for field in self.declared_fields: + initial = self.fields[field].initial + self.fields[field].initial = self.instance.data.get(field, initial) + + + def clean(self): + super().clean() + data = {} + # Update data fields + for field in self.declared_fields: + try: + data[field] = self.cleaned_data[field] + except KeyError: + data[field] = self.data[field] + # Keep old data fields + for field, value in self.instance.data.items(): + if field not in data: + try: + data[field] = self.cleaned_data[field] + except KeyError: + data[field] = value + self.cleaned_data['data'] = data + class NextcloudChangePasswordForm(ChangePasswordForm): - class Meta: fields = ("password",) - model = SaaS \ No newline at end of file + model = SaaS + + def __init__(self, *args, **kwargs): + super(NextcloudChangePasswordForm, self).__init__(*args, **kwargs) + self.fields['password'].help_text = _("Suggestion: %s") % random_ascii(20) + + def clean_password(self): + password = self.cleaned_data.get("password") + self.fields['password'] = password + self.instance.set_password(password) diff --git a/orchestra/contrib/saas/backends/nextcloud.py b/orchestra/contrib/saas/backends/nextcloud.py index b0f3d33a..f2064800 100644 --- a/orchestra/contrib/saas/backends/nextcloud.py +++ b/orchestra/contrib/saas/backends/nextcloud.py @@ -43,7 +43,7 @@ class NextCloudController(NextCloudAPIMixin, ServiceController): def remove(self, saas, server): self.api_delete('users/%s' % saas.name) - def save(self, saas): + def save(self, saas): self.append(self.update_or_create, saas) def delete(self, saas):