2014-05-08 16:59:35 +00:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import admin
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls import reverse
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.encoding import force_str
|
2021-05-17 12:15:12 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext, gettext_lazy as _
|
2023-07-25 20:16:38 +00:00
|
|
|
from django.shortcuts import resolve_url
|
|
|
|
from django.contrib.admin.templatetags.admin_urls import admin_urlname
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from orchestra.admin import ExtendedModelAdmin
|
2016-02-19 10:11:28 +00:00
|
|
|
from orchestra.admin.utils import admin_link, get_modeladmin
|
2015-07-21 10:44:32 +00:00
|
|
|
from orchestra.contrib.accounts.actions import list_accounts
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.admin import AccountAdminMixin
|
2023-07-25 20:16:38 +00:00
|
|
|
from orchestra.contrib.systemusers.models import WebappUsers
|
2014-11-11 12:05:47 +00:00
|
|
|
from orchestra.forms.widgets import DynamicHelpTextSelect
|
2015-10-05 12:09:11 +00:00
|
|
|
from orchestra.plugins.admin import SelectPluginAdminMixin, display_plugin_field
|
2015-10-01 16:02:26 +00:00
|
|
|
from orchestra.utils.html import get_on_site_link
|
2023-08-24 10:23:08 +00:00
|
|
|
from orchestra.settings import NEW_SERVERS
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2016-05-13 10:07:04 +00:00
|
|
|
from .filters import HasWebsiteListFilter, DetailListFilter
|
2015-04-09 14:32:10 +00:00
|
|
|
from .models import WebApp, WebAppOption
|
2015-03-10 11:46:48 +00:00
|
|
|
from .options import AppOption
|
|
|
|
from .types import AppType
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebAppOptionInline(admin.TabularInline):
|
|
|
|
model = WebAppOption
|
|
|
|
extra = 1
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-11-10 15:15:37 +00:00
|
|
|
OPTIONS_HELP_TEXT = {
|
2023-10-24 16:59:02 +00:00
|
|
|
op.name: force_str(op.help_text) for op in AppOption.get_plugins()
|
2014-11-10 15:15:37 +00:00
|
|
|
}
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Media:
|
|
|
|
css = {
|
|
|
|
'all': ('orchestra/css/hide-inline-id.css',)
|
|
|
|
}
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
|
|
|
if db_field.name == 'value':
|
|
|
|
kwargs['widget'] = forms.TextInput(attrs={'size':'100'})
|
2014-11-10 15:03:34 +00:00
|
|
|
if db_field.name == 'name':
|
2015-03-04 21:06:16 +00:00
|
|
|
if self.parent_object:
|
|
|
|
plugin = self.parent_object.type_class
|
|
|
|
else:
|
|
|
|
request = kwargs['request']
|
2015-04-09 14:32:10 +00:00
|
|
|
webapp_modeladmin = get_modeladmin(self.parent_model)
|
|
|
|
plugin_value = webapp_modeladmin.get_plugin_value(request)
|
|
|
|
plugin = AppType.get(plugin_value)
|
2015-06-05 09:57:06 +00:00
|
|
|
kwargs['choices'] = plugin.get_group_options_choices()
|
2014-11-10 15:15:37 +00:00
|
|
|
# Help text based on select widget
|
2015-03-27 19:50:54 +00:00
|
|
|
target = 'this.id.replace("name", "value")'
|
|
|
|
kwargs['widget'] = DynamicHelpTextSelect(target, self.OPTIONS_HELP_TEXT)
|
2014-05-08 16:59:35 +00:00
|
|
|
return super(WebAppOptionInline, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
|
|
|
|
|
2015-03-04 21:06:16 +00:00
|
|
|
class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin):
|
2016-02-17 12:32:30 +00:00
|
|
|
list_display = (
|
2023-07-25 20:16:38 +00:00
|
|
|
'name', 'display_type', 'display_detail', 'display_websites', 'account_link', 'target_server',
|
2016-02-17 12:32:30 +00:00
|
|
|
)
|
2016-05-13 10:07:04 +00:00
|
|
|
list_filter = ('type', HasWebsiteListFilter, DetailListFilter)
|
2014-05-08 16:59:35 +00:00
|
|
|
inlines = [WebAppOptionInline]
|
2016-05-07 10:32:51 +00:00
|
|
|
readonly_fields = ('account_link',)
|
2023-07-25 20:16:38 +00:00
|
|
|
change_readonly_fields = ('name', 'type', 'display_websites', 'display_sftpuser', 'target_server',)
|
2015-03-23 15:36:51 +00:00
|
|
|
search_fields = ('name', 'account__username', 'data', 'website__domains__name')
|
2016-02-17 12:32:30 +00:00
|
|
|
list_prefetch_related = ('content_set__website', 'content_set__website__domains')
|
2015-03-10 11:46:48 +00:00
|
|
|
plugin = AppType
|
2015-03-04 21:06:16 +00:00
|
|
|
plugin_field = 'type'
|
|
|
|
plugin_title = _("Web application type")
|
2015-07-21 10:44:32 +00:00
|
|
|
actions = (list_accounts,)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-10-05 12:09:11 +00:00
|
|
|
display_type = display_plugin_field('type')
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2023-07-25 20:16:38 +00:00
|
|
|
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")
|
2023-07-25 14:59:58 +00:00
|
|
|
|
2021-05-17 12:15:12 +00:00
|
|
|
@mark_safe
|
2014-05-08 16:59:35 +00:00
|
|
|
def display_websites(self, webapp):
|
|
|
|
websites = []
|
2015-03-10 16:57:23 +00:00
|
|
|
for content in webapp.content_set.all():
|
2016-02-17 12:32:30 +00:00
|
|
|
site_url = content.get_absolute_url()
|
|
|
|
site_link = get_on_site_link(site_url)
|
2014-05-08 16:59:35 +00:00
|
|
|
website = content.website
|
2023-10-24 16:59:02 +00:00
|
|
|
#name = "%s on %s %s" % (website.name, content.path, site_link)
|
|
|
|
name = "%s on %s" % (website.name, content.path)
|
2016-02-19 10:11:28 +00:00
|
|
|
link = admin_link(display=name)(website)
|
2015-09-29 08:45:47 +00:00
|
|
|
websites.append(link)
|
2015-03-11 16:32:33 +00:00
|
|
|
if not websites:
|
|
|
|
add_url = reverse('admin:websites_website_add')
|
|
|
|
add_url += '?account=%s' % webapp.account_id
|
|
|
|
plus = '<strong style="color:green; font-size:12px">+</strong>'
|
2023-10-24 16:59:02 +00:00
|
|
|
websites.append('<a href="%s">%s%s</a>' % (add_url, plus, gettext("Add website")))
|
2014-05-08 16:59:35 +00:00
|
|
|
return '<br>'.join(websites)
|
|
|
|
display_websites.short_description = _("web sites")
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-03-20 15:13:08 +00:00
|
|
|
def display_detail(self, webapp):
|
2015-10-05 12:09:11 +00:00
|
|
|
try:
|
|
|
|
return webapp.type_instance.get_detail()
|
|
|
|
except KeyError:
|
2021-05-21 09:17:06 +00:00
|
|
|
return mark_safe("<span style='color:red;'>Not available</span>")
|
2015-03-20 15:13:08 +00:00
|
|
|
display_detail.short_description = _("detail")
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2023-07-25 20:16:38 +00:00
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
if not change:
|
2023-08-01 14:41:03 +00:00
|
|
|
user = form.cleaned_data.get('username')
|
2023-08-24 10:23:08 +00:00
|
|
|
server = form.cleaned_data.get('target_server')
|
|
|
|
if user and server.name in NEW_SERVERS:
|
2023-07-25 20:16:38 +00:00
|
|
|
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)
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
admin.site.register(WebApp, WebAppAdmin)
|