django-orchestra/orchestra/contrib/webapps/admin.py

98 lines
4.0 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django import forms
from django.contrib import admin
2014-10-24 14:19:34 +00:00
from django.core.urlresolvers import reverse
2015-04-02 16:14:55 +00:00
from django.utils.encoding import force_text
2014-10-24 14:19:34 +00:00
from django.utils.translation import ugettext, ugettext_lazy as _
2014-05-08 16:59:35 +00:00
from orchestra.admin import ExtendedModelAdmin
2015-04-09 14:32:10 +00:00
from orchestra.admin.utils import change_url, get_modeladmin
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.admin import AccountAdminMixin
from orchestra.forms.widgets import DynamicHelpTextSelect
2015-03-04 21:06:16 +00:00
from orchestra.plugins.admin import SelectPluginAdminMixin
2014-05-08 16:59:35 +00:00
2015-04-09 14:32:10 +00:00
from .filters import HasWebsiteListFilter
from .models import WebApp, WebAppOption
from .options import AppOption
from .types import AppType
2014-05-08 16:59:35 +00:00
class WebAppOptionInline(admin.TabularInline):
model = WebAppOption
extra = 1
2014-11-10 15:15:37 +00:00
OPTIONS_HELP_TEXT = {
2015-04-02 16:14:55 +00:00
op.name: force_text(op.help_text) for op in AppOption.get_plugins()
2014-11-10 15:15:37 +00:00
}
2014-05-08 16:59:35 +00:00
class Media:
css = {
'all': ('orchestra/css/hide-inline-id.css',)
}
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'value':
kwargs['widget'] = forms.TextInput(attrs={'size':'100'})
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):
list_display = ('name', 'type', 'display_detail', 'display_websites', 'account_link')
2015-04-09 14:32:10 +00:00
list_filter = ('type', HasWebsiteListFilter)
2014-05-08 16:59:35 +00:00
inlines = [WebAppOptionInline]
2015-03-23 15:36:51 +00:00
readonly_fields = ('account_link', )
change_readonly_fields = ('name', 'type', 'display_websites')
search_fields = ('name', 'account__username', 'data', 'website__domains__name')
list_prefetch_related = ('content_set__website',)
plugin = AppType
2015-03-04 21:06:16 +00:00
plugin_field = 'type'
plugin_title = _("Web application type")
2014-05-08 16:59:35 +00:00
def display_websites(self, webapp):
websites = []
for content in webapp.content_set.all():
2014-05-08 16:59:35 +00:00
website = content.website
2014-09-18 15:07:39 +00:00
url = change_url(website)
2014-05-08 16:59:35 +00:00
name = "%s on %s" % (website.name, content.path)
websites.append('<a href="%s">%s</a>' % (url, name))
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>'
websites.append('<a href="%s">%s%s</a>' % (add_url, plus, ugettext("Add website")))
2014-05-08 16:59:35 +00:00
return '<br>'.join(websites)
display_websites.short_description = _("web sites")
display_websites.allow_tags = True
2014-11-10 15:15:37 +00:00
def display_detail(self, webapp):
return webapp.type_instance.get_detail()
display_detail.short_description = _("detail")
2015-04-16 13:15:21 +00:00
# def get_form(self, request, obj=None, **kwargs):
# form = super(WebAppAdmin, self).get_form(request, obj, **kwargs)
# if obj:
#
2015-03-04 21:06:16 +00:00
# def formfield_for_dbfield(self, db_field, **kwargs):
# """ Make value input widget bigger """
# if db_field.name == 'type':
# # Help text based on select widget
# kwargs['widget'] = DynamicHelpTextSelect(
# 'this.id.replace("name", "value")', self.TYPE_HELP_TEXT
# )
# kwargs['help_text'] = self.TYPE_HELP_TEXT.get(db_field.default, '')
# return super(WebAppAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2014-05-08 16:59:35 +00:00
admin.site.register(WebApp, WebAppAdmin)