webapps static complete

This commit is contained in:
jorgepastorr 2023-07-25 22:16:38 +02:00 committed by pedro
parent 24baf1bb70
commit 50b83c31bd
6 changed files with 61 additions and 26 deletions

View file

@ -20,7 +20,7 @@ def save_selected(modeladmin, request, queryset):
save_selected.short_description = "Re-save selected objects" save_selected.short_description = "Re-save selected objects"
class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
list_display = ('name', 'type', 'display_users', 'account_link') list_display = ('name', 'type', 'target_server', 'display_users', 'account_link')
list_filter = ('type', HasUserListFilter) list_filter = ('type', HasUserListFilter)
search_fields = ('name', 'account__username') search_fields = ('name', 'account__username')
change_readonly_fields = ('name', 'type', 'target_server') change_readonly_fields = ('name', 'type', 'target_server')

View file

@ -766,6 +766,7 @@ class WebappUserController(ServiceController):
elif [[ $useradd_code -ne 0 ]]; then elif [[ $useradd_code -ne 0 ]]; then
exit $useradd_code exit $useradd_code
fi fi
usermod -aG %(user)s www-data
fi fi
usermod -aG %(user)s %(parent)s usermod -aG %(user)s %(parent)s
@ -828,5 +829,5 @@ class WebappUserController(ServiceController):
'webapp_path': os.path.normpath(user.get_base_home() + "/webapps/" + user.home), 'webapp_path': os.path.normpath(user.get_base_home() + "/webapps/" + user.home),
'parent': user.get_parent(), 'parent': user.get_parent(),
} }
context['deleted_home'] = context['webapp_path'] + ".delete" context['deleted_home'] = context['webapp_path'] + ".deleted"
return replace(context, "'", '"') return replace(context, "'", '"')

View file

@ -4,11 +4,14 @@ from django.urls import reverse
from django.utils.encoding import force_str from django.utils.encoding import force_str
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import gettext, gettext_lazy as _ from django.utils.translation import gettext, gettext_lazy as _
from django.shortcuts import resolve_url
from django.contrib.admin.templatetags.admin_urls import admin_urlname
from orchestra.admin import ExtendedModelAdmin from orchestra.admin import ExtendedModelAdmin
from orchestra.admin.utils import admin_link, get_modeladmin from orchestra.admin.utils import admin_link, get_modeladmin
from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.actions import list_accounts
from orchestra.contrib.accounts.admin import AccountAdminMixin from orchestra.contrib.accounts.admin import AccountAdminMixin
from orchestra.contrib.systemusers.models import WebappUsers
from orchestra.forms.widgets import DynamicHelpTextSelect from orchestra.forms.widgets import DynamicHelpTextSelect
from orchestra.plugins.admin import SelectPluginAdminMixin, display_plugin_field from orchestra.plugins.admin import SelectPluginAdminMixin, display_plugin_field
from orchestra.utils.html import get_on_site_link from orchestra.utils.html import get_on_site_link
@ -52,12 +55,12 @@ class WebAppOptionInline(admin.TabularInline):
class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin): class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin):
list_display = ( list_display = (
'name', 'display_type', 'display_detail', 'display_websites', 'account_link' 'name', 'display_type', 'display_detail', 'display_websites', 'account_link', 'target_server',
) )
list_filter = ('type', HasWebsiteListFilter, DetailListFilter) list_filter = ('type', HasWebsiteListFilter, DetailListFilter)
inlines = [WebAppOptionInline] inlines = [WebAppOptionInline]
readonly_fields = ('account_link',) readonly_fields = ('account_link',)
change_readonly_fields = ('name', 'type', 'display_websites', 'sftpuser', 'target_server') change_readonly_fields = ('name', 'type', 'display_websites', 'display_sftpuser', 'target_server',)
search_fields = ('name', 'account__username', 'data', 'website__domains__name') search_fields = ('name', 'account__username', 'data', 'website__domains__name')
list_prefetch_related = ('content_set__website', 'content_set__website__domains') list_prefetch_related = ('content_set__website', 'content_set__website__domains')
plugin = AppType plugin = AppType
@ -67,6 +70,15 @@ class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin)
display_type = display_plugin_field('type') display_type = display_plugin_field('type')
def display_sftpuser(self, obj):
salida = ""
if obj.sftpuser is None:
salida = None
else:
url = resolve_url(admin_urlname(WebappUsers._meta, 'change'), obj.sftpuser.id)
salida += f'<a href="{url}">{obj.sftpuser}</a> <br />'
return mark_safe(salida)
display_sftpuser.short_description = _("user sftp")
@mark_safe @mark_safe
def display_websites(self, webapp): def display_websites(self, webapp):
@ -95,4 +107,19 @@ class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin)
display_detail.short_description = _("detail") display_detail.short_description = _("detail")
def save_model(self, request, obj, form, change):
if not change:
user = form.cleaned_data['username']
if user:
user = WebappUsers(
username=form.cleaned_data['username'],
account_id=obj.account.pk,
target_server=form.cleaned_data['target_server'],
home=form.cleaned_data['name']
)
user.set_password(form.cleaned_data["password1"])
user.save()
obj.sftpuser = user
super(WebAppAdmin, self).save_model(request, obj, form, change)
admin.site.register(WebApp, WebAppAdmin) admin.site.register(WebApp, WebAppAdmin)

View file

@ -13,7 +13,18 @@ class WebAppServiceMixin(object):
doc_settings = (settings, doc_settings = (settings,
('WEBAPPS_UNDER_CONSTRUCTION_PATH', 'WEBAPPS_MOVE_ON_DELETE_PATH',) ('WEBAPPS_UNDER_CONSTRUCTION_PATH', 'WEBAPPS_MOVE_ON_DELETE_PATH',)
) )
def check_webapp_dir(self, context):
self.append(textwrap.dedent("""
# Create webapp dir
CREATED=0
if [[ ! -e %(app_path)s ]]; then
mkdir -p %(app_path)s
CREATED=1
elif [[ -z $( ls -A %(app_path)s ) ]]; then
CREATED=1
fi""") % context
)
def create_webapp_dir(self, context): def create_webapp_dir(self, context):
self.append(textwrap.dedent(""" self.append(textwrap.dedent("""
# Create webapp dir # Create webapp dir
@ -57,14 +68,15 @@ class WebAppServiceMixin(object):
def get_context(self, webapp): def get_context(self, webapp):
context = webapp.type_instance.get_directive_context() context = webapp.type_instance.get_directive_context()
context.update({ context.update({
'user': webapp.get_username(), 'user': webapp.sftpuser.username if webapp.target_server.name in settings.WEBAPP_NEW_SERVERS else webapp.get_username(),
'group': webapp.get_groupname(), 'group': webapp.sftpuser.username if webapp.target_server.name in settings.WEBAPP_NEW_SERVERS else webapp.get_groupname(),
'app_name': webapp.name, 'app_name': webapp.name,
'app_type': webapp.type, 'app_type': webapp.type,
'app_path': webapp.get_path(), 'app_path': webapp.get_path(),
'banner': self.get_banner(), 'banner': self.get_banner(),
'under_construction_path': settings.WEBAPPS_UNDER_CONSTRUCTION_PATH, 'under_construction_path': settings.WEBAPPS_UNDER_CONSTRUCTION_PATH,
'is_mounted': webapp.content_set.exists(), 'is_mounted': webapp.content_set.exists(),
'target_server': webapp.target_server,
}) })
context['deleted_app_path'] = settings.WEBAPPS_MOVE_ON_DELETE_PATH % context context['deleted_app_path'] = settings.WEBAPPS_MOVE_ON_DELETE_PATH % context
return context return context

View file

@ -18,18 +18,13 @@ class StaticController(WebAppServiceMixin, ServiceController):
if context.get('target_server').name in WEBAPP_NEW_SERVERS: if context.get('target_server').name in WEBAPP_NEW_SERVERS:
self.check_webapp_dir(context) self.check_webapp_dir(context)
self.set_under_construction(context) self.set_under_construction(context)
# TODO: crea el usuario sftp
# webapp.name = webapp.sftpuser.directory.replace("webapps/", "")
# webapp.save()
else: else:
self.create_webapp_dir(context) self.create_webapp_dir(context)
self.set_under_construction(context) self.set_under_construction(context)
def delete(self, webapp): def delete(self, webapp):
context = self.get_context(webapp) context = self.get_context(webapp)
if context.get('target_server').name not in WEBAPP_NEW_SERVERS: if context.get('target_server').name in WEBAPP_NEW_SERVERS:
self.delete_webapp_dir(context) webapp.sftpuser.delete()
else: else:
# TODO: elimina el usuario sftp self.delete_webapp_dir(context)
pass

View file

@ -35,21 +35,21 @@ class StaticForm(PluginDataForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(StaticForm, self).__init__(*args, **kwargs) super(StaticForm, self).__init__(*args, **kwargs)
if self.instance.id is None: self.fields['sftpuser'].widget = forms.HiddenInput()
self.fields['sftpuser'].widget = forms.HiddenInput() if self.instance.id is not None:
else:
self.fields['username'].widget = forms.HiddenInput() self.fields['username'].widget = forms.HiddenInput()
self.fields['password1'].widget = forms.HiddenInput() self.fields['password1'].widget = forms.HiddenInput()
self.fields['password2'].widget = forms.HiddenInput() self.fields['password2'].widget = forms.HiddenInput()
def clean(self): def clean(self):
webapp_server = self.cleaned_data.get("target_server") if not self.instance.id:
sftpuser = self.cleaned_data.get('sftpuser') webapp_server = self.cleaned_data.get("target_server")
if webapp_server is None: username = self.cleaned_data.get('username')
self.add_error("target_server", _("choice some target_server")) if webapp_server is None:
else: self.add_error("target_server", _("choice some target_server"))
if webapp_server.name in WEBAPP_NEW_SERVERS and sftpuser == None: else:
self.add_error("sftpuser", _("SFTP user is required by new webservers")) if webapp_server.name in WEBAPP_NEW_SERVERS and username == '':
self.add_error("username", _("SFTP user is required by new webservers"))
def clean_password2(self): def clean_password2(self):
password1 = self.cleaned_data.get("password1") password1 = self.cleaned_data.get("password1")