musician saas create nextcloud users

This commit is contained in:
Jorge Pastor 2024-11-13 13:25:56 +01:00
parent 0e0a4d1e6b
commit 5ac858b24c
5 changed files with 66 additions and 3 deletions

View file

@ -30,7 +30,7 @@ API_PATHS = {
'mailbox-detail': 'mailboxes/{pk}/',
'mailbox-password': 'mailboxes/{pk}/set_password/',
'mailinglist-list': 'lists/',
'saas-list': 'saas/',
# 'saas-list': 'saas/',
'website-list': 'websites/',
# other

View file

@ -4,6 +4,7 @@ from orchestra.forms.widgets import SpanWidget
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from orchestra.utils.python import random_ascii
from django.core.exceptions import ValidationError
from django.forms.widgets import HiddenInput
@ -92,3 +93,50 @@ class NextcloudChangePasswordForm(ChangePasswordForm):
password = self.cleaned_data.get("password")
self.fields['password'] = password
self.instance.set_password(password)
class NextcloudCreateForm(forms.ModelForm):
error_messages = {
'password_mismatch': _('The two password fields didnt match.'),
}
password = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
class Meta:
fields = ("service", "name", "password", "password2", "account")
model = SaaS
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super().__init__(*args, **kwargs)
self.fields['account'].initial = user
self.fields['account'].widget = HiddenInput()
self.fields['service'].choices = [("nextcloud","nextCloud")]
self.fields['password'].help_text = _("Suggestion: %s") % random_ascii(20)
def clean_password2(self):
password = self.cleaned_data.get("password")
password2 = self.cleaned_data.get("password2")
if password and password2 and password != password2:
raise ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
return password
def clean_password(self):
password = self.cleaned_data.get("password")
self.fields['password'] = password
self.instance.set_password(password)

View file

@ -12,7 +12,7 @@ from orchestra.contrib.musician.mixins import (CustomContextMixin, ExtendedPagin
UserTokenRequiredMixin)
from .forms import ( NextcloudChangePasswordForm, SaasNextcloudUpdateForm,
SaasWordpressUpdateForm )
SaasWordpressUpdateForm, NextcloudCreateForm )
from orchestra.contrib.saas.models import SaaS
@ -88,10 +88,23 @@ class NextcloudChangePasswordView(CustomContextMixin, UserTokenRequiredMixin, Up
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)
return self.model.objects.filter(account=self.request.user)
class NextcloudCreateView(CustomContextMixin, UserTokenRequiredMixin, CreateView):
model = SaaS
template_name = "musician/saas_nextcloud_form.html"
form_class = NextcloudCreateForm
success_url = reverse_lazy("musician:saas-nextcloud-list")
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs

View file

@ -54,4 +54,5 @@
</table>
</div>
<a class="btn btn-primary mt-4 mb-4" href="{% url 'musician:saas-nextcloud-create' %}">{% trans "New user" %}</a>
{% endblock %}

View file

@ -53,6 +53,7 @@ urlpatterns = [
path('saas/nextcloud/', views.SaasNextcloudListView.as_view(), name='saas-nextcloud-list'),
path('saas/nextcloud/<int:pk>/', views.SaasNextcloudUpdateView.as_view(), name='saas-nextcloud-update'),
path('saas/<int:pk>/nextcloud-change-password/', views.NextcloudChangePasswordView.as_view(), name='nextcloud-password'),
path('saas/nextcloud/new/', views.NextcloudCreateView.as_view(), name='saas-nextcloud-create'),
path('saas/wordpress/', views.SaasWordpressListView.as_view(), name='saas-wordpress-list'),
path('saas/wordpress/<int:pk>/', views.SaasWordpressUpdateView.as_view(), name='saas-wordpress-update'),
path('saas/<int:pk>/delete/', views.SaasDeleteView.as_view(), name='saas-delete'),