django-orchestra/orchestra/apps/domains/admin.py

117 lines
4.0 KiB
Python
Raw Normal View History

import re
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 ChangeListDefaultFilter, ExtendedModelAdmin
2014-09-26 15:05:20 +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
from orchestra.utils import apps
2014-09-26 15:05:20 +00:00
from .actions import view_zone
2014-05-08 16:59:35 +00:00
from .forms import RecordInlineFormSet, DomainAdminForm
from .filters import TopDomainListFilter
from .models import Domain, Record
class RecordInline(admin.TabularInline):
model = Record
formset = RecordInlineFormSet
verbose_name_plural = _("Extra records")
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):
""" Make value input widget bigger """
if db_field.name == 'value':
kwargs['widget'] = forms.TextInput(attrs={'size':'100'})
2014-10-23 15:38:46 +00:00
if db_field.name == 'ttl':
kwargs['widget'] = forms.TextInput(attrs={'size':'10'})
2014-05-08 16:59:35 +00:00
return super(RecordInline, self).formfield_for_dbfield(db_field, **kwargs)
class DomainInline(admin.TabularInline):
model = Domain
2014-10-24 11:10:30 +00:00
fields = ('domain_link', 'display_records', 'account_link')
readonly_fields = ('domain_link', 'display_records', 'account_link')
2014-05-08 16:59:35 +00:00
extra = 0
verbose_name_plural = _("Subdomains")
2014-10-17 13:14:31 +00:00
domain_link = admin_link('__unicode__')
2014-05-08 16:59:35 +00:00
domain_link.short_description = _("Name")
2014-10-24 11:10:30 +00:00
account_link = admin_link('account')
def display_records(self, domain):
return ', '.join(domain.records.values_list('type', flat=True))
display_records.short_description = _("Declared records")
2014-05-08 16:59:35 +00:00
def has_add_permission(self, *args, **kwargs):
return False
class DomainAdmin(ChangeListDefaultFilter, AccountAdminMixin, ExtendedModelAdmin):
2014-10-23 15:38:46 +00:00
# TODO name link
2014-05-08 16:59:35 +00:00
fields = ('name', 'account')
2014-07-17 16:09:24 +00:00
list_display = (
'structured_name', 'display_is_top', 'websites', 'account_link'
)
2014-05-08 16:59:35 +00:00
inlines = [RecordInline, DomainInline]
list_filter = [TopDomainListFilter]
change_readonly_fields = ('name',)
search_fields = ['name',]
2014-09-24 20:09:41 +00:00
default_changelist_filters = (
('top_domain', 'True'),
)
2014-05-08 16:59:35 +00:00
form = DomainAdminForm
2014-09-26 15:05:20 +00:00
change_view_actions = [view_zone]
2014-05-08 16:59:35 +00:00
def structured_name(self, domain):
2014-07-17 16:09:24 +00:00
if not domain.is_top:
2014-05-08 16:59:35 +00:00
return ' '*4 + domain.name
return domain.name
structured_name.short_description = _("name")
structured_name.allow_tags = True
structured_name.admin_order_field = 'structured_name'
2014-07-17 16:09:24 +00:00
def display_is_top(self, domain):
return domain.is_top
display_is_top.boolean = True
display_is_top.admin_order_field = 'top'
2014-05-08 16:59:35 +00:00
def websites(self, domain):
if apps.isinstalled('orchestra.apps.websites'):
webs = domain.websites.all()
if webs:
links = []
for web in webs:
2014-09-18 15:07:39 +00:00
url = change_url(web)
2014-05-08 16:59:35 +00:00
links.append('<a href="%s">%s</a>' % (url, web.name))
return '<br>'.join(links)
return _("No website")
websites.admin_order_field = 'websites__name'
websites.short_description = _("Websites")
websites.allow_tags = True
2014-07-08 15:19:15 +00:00
def get_queryset(self, request):
2014-05-08 16:59:35 +00:00
""" Order by structured name and imporve performance """
2014-07-08 15:19:15 +00:00
qs = super(DomainAdmin, self).get_queryset(request)
2014-09-08 15:10:16 +00:00
qs = qs.select_related('top')
2014-05-08 16:59:35 +00:00
# For some reason if we do this we know for sure that join table will be called T4
query = str(qs.query)
table = re.findall(r'(T\d+)\."account_id"', query)[0]
2014-05-08 16:59:35 +00:00
qs = qs.extra(
select={
'structured_name': 'CONCAT({table}.name, domains_domain.name)'.format(table=table)
},
2014-05-08 16:59:35 +00:00
).order_by('structured_name')
if apps.isinstalled('orchestra.apps.websites'):
qs = qs.prefetch_related('websites')
return qs
admin.site.register(Domain, DomainAdmin)