django-orchestra-test/orchestra/apps/contacts/admin.py

113 lines
3.8 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django import forms
from django.contrib import admin
2015-04-04 18:10:39 +00:00
from django.utils.translation import ugettext_lazy as _
2014-05-08 16:59:35 +00:00
from orchestra.admin import AtLeastOneRequiredInlineFormSet, ExtendedModelAdmin
from orchestra.admin.actions import SendEmail
2015-04-04 18:10:39 +00:00
from orchestra.admin.utils import insertattr, change_url
2014-05-08 16:59:35 +00:00
from orchestra.apps.accounts.admin import AccountAdmin, AccountAdminMixin
from orchestra.forms.widgets import paddingCheckboxSelectMultiple
2014-10-17 10:04:47 +00:00
from .filters import EmailUsageListFilter
2014-10-17 10:04:47 +00:00
from .models import Contact
2014-05-08 16:59:35 +00:00
class ContactAdmin(AccountAdminMixin, ExtendedModelAdmin):
2014-05-08 16:59:35 +00:00
list_display = (
2014-11-17 14:17:33 +00:00
'dispaly_name', 'email', 'phone', 'phone2', 'country', 'account_link'
2014-05-08 16:59:35 +00:00
)
2014-11-20 15:34:59 +00:00
# TODO email usage custom filter contains
list_filter = (EmailUsageListFilter,)
2014-05-08 16:59:35 +00:00
search_fields = (
'account__username', 'account__full_name', 'short_name', 'full_name', 'phone', 'phone2',
2014-05-08 16:59:35 +00:00
'email'
)
fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('account_link', 'short_name', 'full_name')
}),
(_("Email"), {
'classes': ('wide',),
'fields': ('email', 'email_usage',)
}),
(_("Phone"), {
'classes': ('wide',),
'fields': ('phone', 'phone2'),
}),
(_("Postal address"), {
'classes': ('wide',),
'fields': ('address', ('zipcode', 'city'), 'country')
}),
)
# TODO don't repeat all only for account_link do it on accountadmin
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('account', 'short_name', 'full_name')
}),
(_("Email"), {
'classes': ('wide',),
'fields': ('email', 'email_usage',)
}),
(_("Phone"), {
'classes': ('wide',),
'fields': ('phone', 'phone2'),
}),
(_("Postal address"), {
'classes': ('wide',),
'fields': ('address', ('zipcode', 'city'), 'country')
}),
)
actions = [SendEmail(),]
2014-11-17 14:17:33 +00:00
def dispaly_name(self, contact):
2015-04-02 16:14:55 +00:00
return str(contact)
2014-11-17 14:17:33 +00:00
dispaly_name.short_description = _("Name")
dispaly_name.admin_order_field = 'short_name'
def formfield_for_dbfield(self, db_field, **kwargs):
""" Make value input widget bigger """
if db_field.name == 'address':
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2})
if db_field.name == 'email_usage':
kwargs['widget'] = paddingCheckboxSelectMultiple(130)
return super(ContactAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2014-05-08 16:59:35 +00:00
admin.site.register(Contact, ContactAdmin)
2014-10-17 10:04:47 +00:00
class ContactInline(admin.StackedInline):
2014-05-08 16:59:35 +00:00
model = Contact
formset = AtLeastOneRequiredInlineFormSet
extra = 0
fields = (
2014-10-23 15:38:46 +00:00
('short_name', 'full_name'), 'email', 'email_usage', ('phone', 'phone2'),
2014-05-08 16:59:35 +00:00
)
def get_extra(self, request, obj=None, **kwargs):
return 0 if obj and obj.contacts.exists() else 1
2014-10-17 10:04:47 +00:00
2014-10-23 15:38:46 +00:00
def get_view_on_site_url(self, obj=None):
if obj:
return change_url(obj)
2014-10-17 10:04:47 +00:00
def formfield_for_dbfield(self, db_field, **kwargs):
""" Make value input widget bigger """
2014-10-23 15:38:46 +00:00
if db_field.name == 'short_name':
kwargs['widget'] = forms.TextInput(attrs={'size':'15'})
2014-10-17 10:04:47 +00:00
if db_field.name == 'address':
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2})
if db_field.name == 'email_usage':
kwargs['widget'] = paddingCheckboxSelectMultiple(45)
return super(ContactInline, self).formfield_for_dbfield(db_field, **kwargs)
2014-05-08 16:59:35 +00:00
insertattr(AccountAdmin, 'inlines', ContactInline)
search_fields = (
'contacts__short_name', 'contacts__full_name',
)
for field in search_fields:
2014-05-08 16:59:35 +00:00
insertattr(AccountAdmin, 'search_fields', field)