musician saas wordpress initial

This commit is contained in:
Jorge Pastor 2024-10-30 13:29:34 +01:00
parent b346d5403b
commit ecd273bcc3
2 changed files with 82 additions and 5 deletions

View File

@ -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.<br>"
"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': '&lt;site_name&gt;',
'name': '&lt;site_name&gt;',
}
site_domain = site_domain % context
if '&lt;site_name&gt;' in site_domain:
site_link = site_domain
else:
site_link = '<a href="http://%s">%s</a>' % (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: <a href="{0}">{0}</a>'.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
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)

View File

@ -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):