From 9f8e6ce9b84565dca2f5ba6e42a3e66dbb51f15f Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Thu, 7 Nov 2024 17:15:47 +0100 Subject: [PATCH] musician reorder code saas --- orchestra/contrib/musician/forms.py | 76 ---------------- orchestra/contrib/musician/saas/__init__.py | 0 orchestra/contrib/musician/saas/forms.py | 90 +++++++++++++++++++ orchestra/contrib/musician/saas/views.py | 82 +++++++++++++++++ .../templates/musician/saas_check_delete.html | 2 +- orchestra/contrib/musician/views.py | 71 +-------------- 6 files changed, 175 insertions(+), 146 deletions(-) create mode 100644 orchestra/contrib/musician/saas/__init__.py create mode 100644 orchestra/contrib/musician/saas/forms.py create mode 100644 orchestra/contrib/musician/saas/views.py diff --git a/orchestra/contrib/musician/forms.py b/orchestra/contrib/musician/forms.py index 2769367f..fca6d1ff 100644 --- a/orchestra/contrib/musician/forms.py +++ b/orchestra/contrib/musician/forms.py @@ -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.
" - "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': '<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["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: {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) - - -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) diff --git a/orchestra/contrib/musician/saas/__init__.py b/orchestra/contrib/musician/saas/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/orchestra/contrib/musician/saas/forms.py b/orchestra/contrib/musician/saas/forms.py new file mode 100644 index 00000000..cb014ca6 --- /dev/null +++ b/orchestra/contrib/musician/saas/forms.py @@ -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.
" + "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': '<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["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: {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) + + +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) diff --git a/orchestra/contrib/musician/saas/views.py b/orchestra/contrib/musician/saas/views.py new file mode 100644 index 00000000..da89bdf0 --- /dev/null +++ b/orchestra/contrib/musician/saas/views.py @@ -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) \ No newline at end of file diff --git a/orchestra/contrib/musician/templates/musician/saas_check_delete.html b/orchestra/contrib/musician/templates/musician/saas_check_delete.html index 07e4f292..8cb33dd6 100644 --- a/orchestra/contrib/musician/templates/musician/saas_check_delete.html +++ b/orchestra/contrib/musician/templates/musician/saas_check_delete.html @@ -6,7 +6,7 @@ {% csrf_token %}

{% blocktrans %}Are you sure that you want remove the SaaS:{% endblocktrans %} {{ saas }} ?

{% trans 'WARNING: This action cannot be undone.' %}

- {% trans 'Cancel' %} + {% trans 'Cancel' %} {% endblock %} diff --git a/orchestra/contrib/musician/views.py b/orchestra/contrib/musician/views.py index cc9d6055..a2c48d7e 100644 --- a/orchestra/contrib/musician/views.py +++ b/orchestra/contrib/musician/views.py @@ -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 = {