musician reorder code saas

This commit is contained in:
Jorge Pastor 2024-11-07 17:15:47 +01:00
parent e5c046c3af
commit 9f8e6ce9b8
6 changed files with 175 additions and 146 deletions

View file

@ -209,79 +209,3 @@ class SystemUsersChangePasswordForm(ChangePasswordForm):
fields = ("password",)
model = SystemUser
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=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")
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
self.fields['name'].widget.attrs['readonly'] = True
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()
# 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["blog_id"].widget = HiddenInput()
self.fields["custom_url"].widget = HiddenInput()
self.fields["email"].widget = HiddenInput()
self.fields["email"].required = False
if self.instance.service == 'wordpress':
self.fields["is_active"].widget = HiddenInput()
self.fields["custom_url"].widget.attrs['readonly'] = True
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)
class NextcloudChangePasswordForm(ChangePasswordForm):
class Meta:
fields = ("password",)
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

@ -0,0 +1,90 @@
from django import forms
from orchestra.forms.widgets import SpanWidget
from orchestra.forms import widgets
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from orchestra.utils.python import random_ascii
from django.forms.widgets import HiddenInput
from orchestra.contrib.saas.models import SaaS
from orchestra.contrib.musician.forms import ChangePasswordForm
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=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")
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
self.fields['name'].widget.attrs['readonly'] = True
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()
# 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["blog_id"].widget = HiddenInput()
self.fields["custom_url"].widget = HiddenInput()
self.fields["email"].widget = HiddenInput()
self.fields["email"].required = False
if self.instance.service == 'wordpress':
self.fields["is_active"].widget = HiddenInput()
self.fields["custom_url"].widget.attrs['readonly'] = True
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)
class NextcloudChangePasswordForm(ChangePasswordForm):
class Meta:
fields = ("password",)
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

@ -0,0 +1,82 @@
from django.utils.translation import gettext_lazy as _
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.edit import (CreateView, DeleteView, FormView,
UpdateView)
from django.views.generic.list import ListView
from orchestra.contrib.musician.mixins import (CustomContextMixin, ExtendedPaginationMixin,
UserTokenRequiredMixin)
from .forms import ( SaasUpdateForm, NextcloudChangePasswordForm )
from orchestra.contrib.saas.models import SaaS
# class SaasListView(ServiceListView):
# service_class = SaasService
# model = SaaS
# template_name = "musician/saas_list.html"
# extra_context = {
# # Translators: This message appears on the page title
# 'title': _('Software as a Service'),
# }
class SaasNextcloudListView(CustomContextMixin, UserTokenRequiredMixin, ListView):
model = SaaS
template_name = "musician/saas_nextcloud_list.html"
extra_context = {
# Translators: This message appears on the page title
'title': _('Software as a Service'),
}
def get_queryset(self):
return self.model.objects.filter(account=self.request.user, service='nextcloud')
class SaasWordpressListView(CustomContextMixin, UserTokenRequiredMixin, ListView):
model = SaaS
template_name = "musician/saas_wordpress_list.html"
extra_context = {
# Translators: This message appears on the page title
'title': _('Software as a Service'),
}
def get_queryset(self):
return self.model.objects.filter(account=self.request.user, service='wordpress')
class SaasUpdateView(CustomContextMixin, UserTokenRequiredMixin, UpdateView):
model = SaaS
form_class = SaasUpdateForm
template_name = "musician/saas_form.html"
def get_queryset(self):
qs = SaaS.objects.filter(account=self.request.user)
return qs
def get_success_url(self):
return reverse_lazy("musician:saas-nextcloud-list")
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["user"] = self.request.user
return kwargs
class NextcloudChangePasswordView(CustomContextMixin, UserTokenRequiredMixin, UpdateView):
template_name = "musician/nextcloud_change_password.html"
model = SaaS
form_class = NextcloudChangePasswordForm
success_url = reverse_lazy("musician:saas-nextcloud-list")
def get_queryset(self):
return self.model.objects.filter(account=self.request.user)
class SaasDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView):
template_name = "musician/saas_check_delete.html"
model = SaaS
success_url = reverse_lazy("musician:saas-nextcloud-list")
def get_queryset(self):
return self.model.objects.filter(account=self.request.user)

View file

@ -6,7 +6,7 @@
{% csrf_token %}
<p>{% blocktrans %}Are you sure that you want remove the SaaS:{% endblocktrans %} {{ saas }} ?</p>
<p class="alert alert-warning"><strong>{% trans 'WARNING: This action cannot be undone.' %}</strong></p>
<a class="btn btn-light mr-2" href="{% url 'musician:saas-list' %}">{% trans 'Cancel' %}</a>
<a class="btn btn-light mr-2" href="{% url 'musician:saas-nextcloud-list' %}">{% trans 'Cancel' %}</a>
<input class="btn btn-danger" type="submit" value="{% trans 'Delete' %}">
</form>
{% endblock %}

View file

@ -34,7 +34,6 @@ from orchestra.contrib.domains.models import Domain, Record
from orchestra.contrib.lists.models import List
from orchestra.contrib.mailboxes.models import Address, Mailbox
from orchestra.contrib.resources.models import Resource, ResourceData
from orchestra.contrib.saas.models import SaaS
from orchestra.contrib.systemusers.models import WebappUsers, SystemUser
from orchestra.utils.html import html_to_pdf
@ -42,7 +41,7 @@ from .auth import logout as auth_logout
from .forms import (LoginForm, MailboxChangePasswordForm, MailboxCreateForm,
MailboxSearchForm, MailboxUpdateForm, MailForm,
RecordCreateForm, RecordUpdateForm, WebappUsersChangePasswordForm,
SystemUsersChangePasswordForm, SaasUpdateForm, NextcloudChangePasswordForm)
SystemUsersChangePasswordForm)
from .mixins import (CustomContextMixin, ExtendedPaginationMixin,
UserTokenRequiredMixin)
from .models import Address as AddressService
@ -56,6 +55,7 @@ from .utils import get_bootstraped_percent, get_bootstraped_percent_exact
from .webapps.views import *
from .websites.views import *
from .lists.views import *
from .saas.views import *
logger = logging.getLogger(__name__)
@ -614,73 +614,6 @@ class DatabaseListView(ServiceListView):
return qs
# class SaasListView(ServiceListView):
# service_class = SaasService
# model = SaaS
# template_name = "musician/saas_list.html"
# extra_context = {
# # Translators: This message appears on the page title
# 'title': _('Software as a Service'),
# }
class SaasNextcloudListView(CustomContextMixin, UserTokenRequiredMixin, ListView):
model = SaaS
template_name = "musician/saas_nextcloud_list.html"
extra_context = {
# Translators: This message appears on the page title
'title': _('Software as a Service'),
}
def get_queryset(self):
return self.model.objects.filter(account=self.request.user, service='nextcloud')
class SaasWordpressListView(CustomContextMixin, UserTokenRequiredMixin, ListView):
model = SaaS
template_name = "musician/saas_wordpress_list.html"
extra_context = {
# Translators: This message appears on the page title
'title': _('Software as a Service'),
}
def get_queryset(self):
return self.model.objects.filter(account=self.request.user, service='wordpress')
class SaasUpdateView(CustomContextMixin, UserTokenRequiredMixin, UpdateView):
model = SaaS
form_class = SaasUpdateForm
template_name = "musician/saas_form.html"
def get_queryset(self):
qs = SaaS.objects.filter(account=self.request.user)
return qs
def get_success_url(self):
return reverse_lazy("musician:saas-nextcloud-list")
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["user"] = self.request.user
return kwargs
class NextcloudChangePasswordView(CustomContextMixin, UserTokenRequiredMixin, UpdateView):
template_name = "musician/nextcloud_change_password.html"
model = SaaS
form_class = NextcloudChangePasswordForm
success_url = reverse_lazy("musician:saas-nextcloud-list")
def get_queryset(self):
return self.model.objects.filter(account=self.request.user)
class SaasDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView):
template_name = "musician/saas_check_delete.html"
model = SaaS
success_url = reverse_lazy("musician:saas-nextcloud-list")
def get_queryset(self):
return self.model.objects.filter(account=self.request.user)
class DomainDetailView(CustomContextMixin, UserTokenRequiredMixin, DetailView):
template_name = "musician/domain_detail.html"
extra_context = {