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

110 lines
3.6 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext, ugettext_lazy as _
2014-05-08 16:59:35 +00:00
from orchestra.admin import AtLeastOneRequiredInlineFormSet
2014-10-23 15:38:46 +00:00
from orchestra.admin.utils import insertattr, admin_link, 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 .models import Contact
2014-05-08 16:59:35 +00:00
class ContactAdmin(AccountAdminMixin, admin.ModelAdmin):
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
2014-05-08 16:59:35 +00:00
list_filter = ('email_usage',)
search_fields = (
'contact__account__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')
}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('account', 'short_name', 'full_name')
}),
(_("Email"), {
'classes': ('wide',),
'fields': ('email', 'email_usage',)
}),
(_("Phone"), {
'classes': ('wide',),
'fields': ('phone', 'phone_alternative'),
}),
(_("Postal address"), {
'classes': ('wide',),
'fields': ('address', ('zip_code', 'city'), 'country')
}),
)
2014-11-17 14:17:33 +00:00
def dispaly_name(self, contact):
return unicode(contact)
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', 'contacts__phone',
'contacts__phone2', 'contacts__email'
)
for field in search_fields:
2014-05-08 16:59:35 +00:00
insertattr(AccountAdmin, 'search_fields', field)