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
|
|
|
|
from django.utils.translation import ugettext, ugettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from orchestra.admin import ExtendedModelAdmin
|
2014-09-18 15:07:39 +00:00
|
|
|
from orchestra.admin.utils import change_url
|
2014-10-20 15:51:24 +00:00
|
|
|
from orchestra.apps.accounts.admin import AccountAdminMixin
|
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 WebApp, WebAppOption
|
|
|
|
|
|
|
|
|
|
|
|
class WebAppOptionInline(admin.TabularInline):
|
|
|
|
model = WebAppOption
|
|
|
|
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.WEBAPPS_OPTIONS.iteritems()
|
2014-11-10 15:15:37 +00:00
|
|
|
}
|
2014-11-10 15:03:34 +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'})
|
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(WebAppOptionInline, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
|
|
|
|
|
2014-10-20 15:51:24 +00:00
|
|
|
class WebAppAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
2014-11-17 14:17:33 +00:00
|
|
|
list_display = ('display_name', 'type', 'display_websites', 'account_link')
|
2014-05-08 16:59:35 +00:00
|
|
|
list_filter = ('type',)
|
2014-10-20 15:51:24 +00:00
|
|
|
add_fields = ('account', 'name', 'type')
|
|
|
|
fields = ('account_link', 'name', 'type')
|
2014-05-08 16:59:35 +00:00
|
|
|
inlines = [WebAppOptionInline]
|
|
|
|
readonly_fields = ('account_link',)
|
|
|
|
change_readonly_fields = ('name', 'type')
|
2014-11-02 14:33:55 +00:00
|
|
|
prefetch_related = ('content_set__website',)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-11-10 15:15:37 +00:00
|
|
|
TYPE_HELP_TEXT = {
|
|
|
|
k: str(unicode(v.get('help_text', '')))
|
|
|
|
for k, v in settings.WEBAPPS_TYPES.iteritems()
|
|
|
|
}
|
|
|
|
|
2014-11-17 14:17:33 +00:00
|
|
|
def display_name(self, webapp):
|
|
|
|
return webapp.get_name()
|
|
|
|
display_name.short_description = _("Name")
|
|
|
|
display_name.admin_order_field = 'name'
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def display_websites(self, webapp):
|
|
|
|
websites = []
|
2014-11-02 14:33:55 +00:00
|
|
|
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))
|
2014-10-24 14:19:34 +00:00
|
|
|
add_url = reverse('admin:websites_website_add')
|
2014-11-11 12:05:47 +00:00
|
|
|
# TODO support for preselecting related web app on website
|
2014-10-24 12:26:24 +00:00
|
|
|
add_url += '?account=%s' % webapp.account_id
|
2014-10-24 14:19:34 +00:00
|
|
|
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 formfield_for_dbfield(self, db_field, **kwargs):
|
|
|
|
""" Make value input widget bigger """
|
|
|
|
if db_field.name == 'type':
|
|
|
|
# Help text based on select widget
|
2014-11-13 17:47:45 +00:00
|
|
|
kwargs['widget'] = DynamicHelpTextSelect(
|
|
|
|
'this.id.replace("name", "value")', self.TYPE_HELP_TEXT
|
|
|
|
)
|
2014-11-10 15:15:37 +00:00
|
|
|
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)
|