2024-11-07 16:15:47 +00:00
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
2024-11-19 12:21:02 +00:00
from django . views . generic . list import ListView
2024-11-07 16:15:47 +00:00
from django . views . generic . edit import ( CreateView , DeleteView , FormView ,
UpdateView )
2024-11-19 12:21:02 +00:00
from orchestra . contrib . saas . models import SaaS
from orchestra . contrib . resources . models import Resource , ResourceData
from orchestra . contrib . musician . settings import ALLOWED_RESOURCES
from orchestra . contrib . musician . utils import get_bootstraped_percent_exact
2024-11-07 16:15:47 +00:00
from orchestra . contrib . musician . mixins import ( CustomContextMixin , ExtendedPaginationMixin ,
UserTokenRequiredMixin )
2024-11-07 17:05:47 +00:00
from . forms import ( NextcloudChangePasswordForm , SaasNextcloudUpdateForm ,
2024-12-03 11:06:43 +00:00
SaasWordpressUpdateForm , NextcloudCreateForm )
2024-11-18 11:04:49 +00:00
2024-11-07 16:15:47 +00:00
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 ' ) ,
2024-11-08 11:38:26 +00:00
' verbose_name ' : _ ( ' Nextcloud Service ' ) ,
' description ' : _ ( " Members can manage their Nextcloud users in this section. " ) ,
2024-11-07 16:15:47 +00:00
}
def get_queryset ( self ) :
2024-11-19 12:21:02 +00:00
qs = self . model . objects . filter ( account = self . request . user , service = ' nextcloud ' )
disk_resource = Resource . objects . get ( name = ' nextcloud-disk ' )
2024-12-03 11:06:43 +00:00
name = self . request . GET . get ( ' name ' )
if name :
qs = qs . filter ( name__icontains = name )
2024-11-19 12:21:02 +00:00
for Nuser in qs :
try :
Nuser . usage = Nuser . resource_set . get ( resource = disk_resource )
except ResourceData . DoesNotExist :
Nuser . usage = ResourceData ( resource = disk_resource )
Nuser . percent = get_bootstraped_percent_exact ( Nuser . usage . used , Nuser . usage . allocated )
return qs
2024-12-03 11:06:43 +00:00
2024-11-07 16:15:47 +00:00
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 ' ) ,
2024-11-08 11:38:26 +00:00
' verbose_name ' : _ ( ' Community WordPress ' ) ,
' description ' : _ ( " Pangea ' s multisite WordPress service. Members can have their WordPress on this community WordPress hosted and maintained by Pangea. " ) ,
2024-11-07 16:15:47 +00:00
}
def get_queryset ( self ) :
2024-12-03 11:06:43 +00:00
qs = self . model . objects . filter ( account = self . request . user , service = ' wordpress ' )
name = self . request . GET . get ( ' name ' )
if name :
qs = qs . filter ( name__icontains = name )
return qs
2024-11-07 16:15:47 +00:00
2024-11-07 17:05:47 +00:00
class SaasWordpressUpdateView ( CustomContextMixin , UserTokenRequiredMixin , UpdateView ) :
model = SaaS
form_class = SaasWordpressUpdateForm
template_name = " musician/saas_wordpress_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-wordpress-list " )
def get_form_kwargs ( self ) :
kwargs = super ( ) . get_form_kwargs ( )
kwargs [ " user " ] = self . request . user
return kwargs
class SaasNextcloudUpdateView ( CustomContextMixin , UserTokenRequiredMixin , UpdateView ) :
2024-11-07 16:15:47 +00:00
model = SaaS
2024-11-07 17:05:47 +00:00
form_class = SaasNextcloudUpdateForm
template_name = " musician/saas_nextcloud_form.html "
2024-11-07 16:15:47 +00:00
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 )
2024-11-13 12:25:56 +00:00
2024-11-07 16:15:47 +00:00
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 ) :
2024-11-13 12:25:56 +00:00
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
2024-11-18 11:04:49 +00:00
def get_context_data ( self , * * kwargs ) :
context = super ( ) . get_context_data ( * * kwargs )
context . update ( {
' extra_user ' : self . is_extra_user ( context [ ' profile ' ] ) ,
} )
return context
def is_extra_user ( self , profile ) :
qs = SaaS . objects . filter ( account = profile , service = " nextcloud " )
number_of_users = len ( qs )
allowed_users = ALLOWED_RESOURCES [ profile . type ] [ ' nextcloud ' ]
return number_of_users > = allowed_users