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 resolve
|
2015-02-25 17:29:39 +00:00
|
|
|
from django.db.models import Q
|
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.html import format_html
|
2021-05-12 12:16:28 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from orchestra.admin import ExtendedModelAdmin
|
2016-03-31 16:02:50 +00:00
|
|
|
from orchestra.admin.actions import disable, enable
|
2014-09-18 15:07:39 +00:00
|
|
|
from orchestra.admin.utils import admin_link, change_url
|
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, SelectAccountAdminMixin
|
2016-03-14 10:40:11 +00:00
|
|
|
from orchestra.contrib.accounts.filters import IsActiveListFilter
|
2014-11-11 12:05:47 +00:00
|
|
|
from orchestra.forms.widgets import DynamicHelpTextSelect
|
2016-02-17 12:32:30 +00:00
|
|
|
from orchestra.utils.html import get_on_site_link
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
from .directives import SiteDirective
|
2016-03-14 10:40:11 +00:00
|
|
|
from .filters import HasWebAppsListFilter, HasDomainsFilter
|
2015-03-25 15:45:04 +00:00
|
|
|
from .forms import WebsiteAdminForm, WebsiteDirectiveInlineFormSet
|
2015-03-18 21:51:12 +00:00
|
|
|
from .models import Content, Website, WebsiteDirective
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2015-03-18 21:51:12 +00:00
|
|
|
class WebsiteDirectiveInline(admin.TabularInline):
|
|
|
|
model = WebsiteDirective
|
2015-03-25 15:45:04 +00:00
|
|
|
formset = WebsiteDirectiveInlineFormSet
|
2014-05-08 16:59:35 +00:00
|
|
|
extra = 1
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
DIRECTIVES_HELP_TEXT = {
|
2023-10-24 16:59:02 +00:00
|
|
|
op.name: force_str(op.help_text) for op in SiteDirective.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
|
|
|
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':
|
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.DIRECTIVES_HELP_TEXT)
|
2015-03-18 21:51:12 +00:00
|
|
|
return super(WebsiteDirectiveInline, self).formfield_for_dbfield(db_field, **kwargs)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContentInline(AccountAdminMixin, admin.TabularInline):
|
|
|
|
model = Content
|
|
|
|
extra = 1
|
|
|
|
fields = ('webapp', 'webapp_link', 'webapp_type', 'path')
|
|
|
|
readonly_fields = ('webapp_link', 'webapp_type')
|
|
|
|
filter_by_account_fields = ['webapp']
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-07-21 15:43:36 +00:00
|
|
|
webapp_link = admin_link('webapp', popup=True)
|
2014-05-08 16:59:35 +00:00
|
|
|
webapp_link.short_description = _("Web App")
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def webapp_type(self, content):
|
|
|
|
if not content.pk:
|
|
|
|
return ''
|
|
|
|
return content.webapp.get_type_display()
|
|
|
|
webapp_type.short_description = _("Web App type")
|
|
|
|
|
|
|
|
|
|
|
|
class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
2016-03-14 10:40:11 +00:00
|
|
|
list_display = (
|
|
|
|
'name', 'display_domains', 'display_webapps', 'account_link', 'display_active'
|
|
|
|
)
|
|
|
|
list_filter = (
|
|
|
|
'protocol', IsActiveListFilter, HasWebAppsListFilter, HasDomainsFilter
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
change_readonly_fields = ('name',)
|
2016-04-06 19:00:16 +00:00
|
|
|
inlines = (ContentInline, WebsiteDirectiveInline)
|
2014-05-08 16:59:35 +00:00
|
|
|
filter_horizontal = ['domains']
|
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('extrapretty',),
|
2021-01-13 15:41:36 +00:00
|
|
|
'fields': ('account_link', 'name', 'protocol', 'target_server', 'domains', 'is_active', 'comments'),
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
|
|
|
)
|
2015-02-25 17:29:39 +00:00
|
|
|
form = WebsiteAdminForm
|
2014-05-08 16:59:35 +00:00
|
|
|
filter_by_account_fields = ['domains']
|
2015-03-10 16:57:23 +00:00
|
|
|
list_prefetch_related = ('domains', 'content_set__webapp')
|
2015-04-29 21:35:56 +00:00
|
|
|
search_fields = ('name', 'account__username', 'domains__name', 'content__webapp__name')
|
2016-03-31 16:02:50 +00:00
|
|
|
actions = (disable, enable, list_accounts)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2021-05-12 12:16:28 +00:00
|
|
|
@mark_safe
|
2014-05-08 16:59:35 +00:00
|
|
|
def display_domains(self, website):
|
|
|
|
domains = []
|
|
|
|
for domain in website.domains.all():
|
2015-03-10 21:51:10 +00:00
|
|
|
url = '%s://%s' % (website.get_protocol(), domain)
|
2014-05-08 16:59:35 +00:00
|
|
|
domains.append('<a href="%s">%s</a>' % (url, url))
|
|
|
|
return '<br>'.join(domains)
|
|
|
|
display_domains.short_description = _("domains")
|
2014-11-10 15:40:51 +00:00
|
|
|
display_domains.admin_order_field = 'domains'
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2021-05-17 12:15:12 +00:00
|
|
|
@mark_safe
|
2014-05-08 16:59:35 +00:00
|
|
|
def display_webapps(self, website):
|
|
|
|
webapps = []
|
2015-03-10 16:57:23 +00:00
|
|
|
for content in website.content_set.all():
|
2016-02-17 12:32:30 +00:00
|
|
|
site_link = get_on_site_link(content.get_absolute_url())
|
2014-05-08 16:59:35 +00:00
|
|
|
webapp = content.webapp
|
2016-02-19 10:11:28 +00:00
|
|
|
detail = _("Edit Webapp") + ' ' + webapp.get_type_display()
|
2016-02-17 12:32:30 +00:00
|
|
|
try:
|
|
|
|
detail += ' ' + webapp.type_instance.get_detail()
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-09-18 15:07:39 +00:00
|
|
|
url = change_url(webapp)
|
2015-04-29 21:35:56 +00:00
|
|
|
name = "%s on %s" % (webapp.name, content.path or '/')
|
2021-05-17 12:15:12 +00:00
|
|
|
webapp_info = format_html('<a href="{}" title="{}">{}</a> {}', url, detail, name, site_link)
|
|
|
|
webapps.append(webapp_info)
|
2014-05-08 16:59:35 +00:00
|
|
|
return '<br>'.join(webapps)
|
|
|
|
display_webapps.short_description = _("Web apps")
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
2015-02-25 17:29:39 +00:00
|
|
|
"""
|
|
|
|
Exclude domains with exhausted ports
|
|
|
|
has to be done here, on the form doesn't work because of filter_by_account_fields
|
|
|
|
"""
|
|
|
|
formfield = super(WebsiteAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
if db_field.name == 'domains':
|
2015-03-10 21:51:10 +00:00
|
|
|
qset = Q(
|
|
|
|
Q(websites__protocol=Website.HTTPS_ONLY) |
|
|
|
|
Q(websites__protocol=Website.HTTP_AND_HTTPS) | Q(
|
|
|
|
Q(websites__protocol=Website.HTTP) & Q(websites__protocol=Website.HTTPS)
|
|
|
|
)
|
|
|
|
)
|
2023-10-24 16:59:02 +00:00
|
|
|
object_id = kwargs['request'].resolver_match.kwargs.get('object_id')
|
|
|
|
if object_id:
|
2015-02-25 17:29:39 +00:00
|
|
|
qset = Q(qset & ~Q(websites__pk=object_id))
|
|
|
|
formfield.queryset = formfield.queryset.exclude(qset)
|
|
|
|
return formfield
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-18 15:21:42 +00:00
|
|
|
def _create_formsets(self, request, obj, change):
|
|
|
|
""" bind contents formset to directive formset for unique location cross-validation """
|
|
|
|
formsets, inline_instances = super(WebsiteAdmin, self)._create_formsets(request, obj, change)
|
|
|
|
if request.method == 'POST':
|
|
|
|
contents, directives = formsets
|
|
|
|
directives.content_formset = contents
|
|
|
|
return formsets, inline_instances
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Website, WebsiteAdmin)
|