2014-05-08 16:59:35 +00:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import admin
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-11-21 15:39:41 +00:00
|
|
|
from orchestra.admin import AtLeastOneRequiredInlineFormSet, ExtendedModelAdmin
|
2014-11-24 14:39:41 +00:00
|
|
|
from orchestra.admin.actions import SendEmail
|
2015-04-04 18:10:39 +00:00
|
|
|
from orchestra.admin.utils import insertattr, change_url
|
2015-07-21 10:44:32 +00:00
|
|
|
from orchestra.contrib.accounts.actions import list_accounts
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.admin import AccountAdmin, AccountAdminMixin
|
2021-05-13 10:37:17 +00:00
|
|
|
from orchestra.forms.widgets import PaddingCheckboxSelectMultiple
|
2014-10-17 10:04:47 +00:00
|
|
|
|
2014-11-24 14:39:41 +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
|
|
|
|
|
|
|
|
2014-11-21 15:39:41 +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
|
2014-11-24 14:39:41 +00:00
|
|
|
list_filter = (EmailUsageListFilter,)
|
2014-05-08 16:59:35 +00:00
|
|
|
search_fields = (
|
2014-11-21 15:39:41 +00:00
|
|
|
'account__username', 'account__full_name', 'short_name', 'full_name', 'phone', 'phone2',
|
2014-05-08 16:59:35 +00:00
|
|
|
'email'
|
|
|
|
)
|
2014-07-29 14:29:59 +00:00
|
|
|
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')
|
|
|
|
}),
|
|
|
|
)
|
2014-11-21 15:39:41 +00:00
|
|
|
# TODO don't repeat all only for account_link do it on accountadmin
|
2014-07-29 14:29:59 +00:00
|
|
|
add_fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': ('account', 'short_name', 'full_name')
|
|
|
|
}),
|
|
|
|
(_("Email"), {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': ('email', 'email_usage',)
|
|
|
|
}),
|
|
|
|
(_("Phone"), {
|
|
|
|
'classes': ('wide',),
|
2014-11-21 15:39:41 +00:00
|
|
|
'fields': ('phone', 'phone2'),
|
2014-07-29 14:29:59 +00:00
|
|
|
}),
|
|
|
|
(_("Postal address"), {
|
|
|
|
'classes': ('wide',),
|
2014-11-21 15:39:41 +00:00
|
|
|
'fields': ('address', ('zipcode', 'city'), 'country')
|
2014-07-29 14:29:59 +00:00
|
|
|
}),
|
|
|
|
)
|
2015-07-21 10:44:32 +00:00
|
|
|
actions = (SendEmail(), list_accounts)
|
2021-05-13 10:37:17 +00:00
|
|
|
|
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'
|
2021-05-13 10:37:17 +00:00
|
|
|
|
2014-07-29 14:29:59 +00:00
|
|
|
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':
|
2021-05-13 10:37:17 +00:00
|
|
|
kwargs['widget'] = PaddingCheckboxSelectMultiple(130)
|
2014-07-29 14:29:59 +00:00
|
|
|
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
|
|
|
)
|
2021-05-13 10:37:17 +00:00
|
|
|
|
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
|
2021-05-13 10:37:17 +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)
|
2021-05-13 10:37:17 +00:00
|
|
|
|
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':
|
2021-05-13 10:37:17 +00:00
|
|
|
kwargs['widget'] = PaddingCheckboxSelectMultiple(45)
|
2014-10-17 10:04:47 +00:00
|
|
|
return super(ContactInline, self).formfield_for_dbfield(db_field, **kwargs)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
insertattr(AccountAdmin, 'inlines', ContactInline)
|
2014-07-29 14:29:59 +00:00
|
|
|
search_fields = (
|
2015-03-10 11:46:48 +00:00
|
|
|
'contacts__short_name', 'contacts__full_name',
|
2014-07-29 14:29:59 +00:00
|
|
|
)
|
|
|
|
for field in search_fields:
|
2014-05-08 16:59:35 +00:00
|
|
|
insertattr(AccountAdmin, 'search_fields', field)
|