2014-05-08 16:59:35 +00:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.admin import ExtendedModelAdmin
|
2014-09-18 15:07:39 +00:00
|
|
|
from orchestra.admin.utils import admin_link, change_url
|
2014-05-08 16:59:35 +00:00
|
|
|
from orchestra.apps.accounts.admin import AccountAdminMixin, SelectAccountAdminMixin
|
2014-11-11 12:05:47 +00:00
|
|
|
from orchestra.forms.widgets import DynamicHelpTextSelect
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-11-10 15:03:34 +00:00
|
|
|
from . import settings
|
2014-05-08 16:59:35 +00:00
|
|
|
from .models import Content, Website, WebsiteOption
|
|
|
|
|
|
|
|
|
|
|
|
class WebsiteOptionInline(admin.TabularInline):
|
|
|
|
model = WebsiteOption
|
|
|
|
extra = 1
|
|
|
|
|
2014-11-10 15:15:37 +00:00
|
|
|
OPTIONS_HELP_TEXT = {
|
2014-11-10 15:03:34 +00:00
|
|
|
k: str(unicode(v[1])) if len(v) == 3 else ''
|
|
|
|
for k, v in settings.WEBSITES_OPTIONS.iteritems()
|
2014-11-10 15:15:37 +00:00
|
|
|
}
|
2014-11-10 15:03:34 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
# class Media:
|
|
|
|
# css = {
|
|
|
|
# 'all': ('orchestra/css/hide-inline-id.css',)
|
|
|
|
# }
|
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
|
2014-11-11 12:05:47 +00:00
|
|
|
kwargs['widget'] = DynamicHelpTextSelect(
|
|
|
|
'this.id.replace("name", "value")', self.OPTIONS_HELP_TEXT
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
return super(WebsiteOptionInline, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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):
|
|
|
|
list_display = ('name', 'display_domains', 'display_webapps', 'account_link')
|
|
|
|
list_filter = ('port', 'is_active')
|
|
|
|
change_readonly_fields = ('name',)
|
|
|
|
inlines = [ContentInline, WebsiteOptionInline]
|
|
|
|
filter_horizontal = ['domains']
|
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('extrapretty',),
|
|
|
|
'fields': ('account_link', 'name', 'port', 'domains', 'is_active'),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
filter_by_account_fields = ['domains']
|
2014-11-20 16:48:50 +00:00
|
|
|
list_prefetch_related = ('domains', 'content_set__webapp')
|
2014-11-10 15:03:34 +00:00
|
|
|
search_fields = ('name', 'account__username', 'domains__name')
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def display_domains(self, website):
|
|
|
|
domains = []
|
|
|
|
for domain in website.domains.all():
|
|
|
|
url = '%s://%s' % (website.protocol, domain)
|
|
|
|
domains.append('<a href="%s">%s</a>' % (url, url))
|
|
|
|
return '<br>'.join(domains)
|
|
|
|
display_domains.short_description = _("domains")
|
|
|
|
display_domains.allow_tags = True
|
2014-11-10 15:40:51 +00:00
|
|
|
display_domains.admin_order_field = 'domains'
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def display_webapps(self, website):
|
|
|
|
webapps = []
|
2014-11-02 14:33:55 +00:00
|
|
|
for content in website.content_set.all():
|
2014-05-08 16:59:35 +00:00
|
|
|
webapp = content.webapp
|
2014-09-18 15:07:39 +00:00
|
|
|
url = change_url(webapp)
|
2014-05-08 16:59:35 +00:00
|
|
|
name = "%s on %s" % (webapp.get_type_display(), content.path)
|
|
|
|
webapps.append('<a href="%s">%s</a>' % (url, name))
|
|
|
|
return '<br>'.join(webapps)
|
|
|
|
display_webapps.allow_tags = True
|
|
|
|
display_webapps.short_description = _("Web apps")
|
|
|
|
|
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
|
|
|
if db_field.name == 'root':
|
|
|
|
kwargs['widget'] = forms.TextInput(attrs={'size':'100'})
|
|
|
|
return super(WebsiteAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Website, WebsiteAdmin)
|