2014-10-11 16:21:51 +00:00
|
|
|
from django.contrib import messages
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls import reverse
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.encoding import force_str
|
2016-05-20 08:29:25 +00:00
|
|
|
from django.utils.html import format_html
|
2014-10-11 16:21:51 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2016-05-20 08:29:25 +00:00
|
|
|
from django.utils.text import capfirst
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-10-11 16:21:51 +00:00
|
|
|
|
2016-05-20 08:29:25 +00:00
|
|
|
from orchestra.admin.utils import change_url
|
|
|
|
|
2014-10-11 16:21:51 +00:00
|
|
|
|
|
|
|
def validate_contact(request, bill, error=True):
|
|
|
|
""" checks if all the preconditions for bill generation are met """
|
|
|
|
msg = _('{relation} account "{account}" does not have a declared invoice contact. '
|
|
|
|
'You should <a href="{url}#invoicecontact-group">provide one</a>')
|
|
|
|
valid = True
|
|
|
|
send = messages.error if error else messages.warning
|
2014-10-20 15:51:24 +00:00
|
|
|
if not hasattr(bill.account, 'billcontact'):
|
2023-10-24 16:59:02 +00:00
|
|
|
account = force_str(bill.account)
|
2014-10-11 16:21:51 +00:00
|
|
|
url = reverse('admin:accounts_account_change', args=(bill.account_id,))
|
|
|
|
message = msg.format(relation=_("Related"), account=account, url=url)
|
|
|
|
send(request, mark_safe(message))
|
|
|
|
valid = False
|
2021-05-12 12:38:17 +00:00
|
|
|
main = type(bill).account.field.related_model.objects.get_main()
|
2014-10-20 15:51:24 +00:00
|
|
|
if not hasattr(main, 'billcontact'):
|
2023-10-24 16:59:02 +00:00
|
|
|
account = force_str(main)
|
2014-10-11 16:21:51 +00:00
|
|
|
url = reverse('admin:accounts_account_change', args=(main.id,))
|
|
|
|
message = msg.format(relation=_("Main"), account=account, url=url)
|
|
|
|
send(request, mark_safe(message))
|
|
|
|
valid = False
|
|
|
|
return valid
|
2016-05-20 08:29:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_context_emails(modeladmin, request, queryset):
|
|
|
|
opts = modeladmin.model._meta
|
|
|
|
bills = []
|
|
|
|
for bill in queryset:
|
|
|
|
emails = ', '.join(bill.get_billing_contact_emails())
|
|
|
|
bills.append(format_html('{0}: <a href="{1}">{2}</a> <i>{3}</i>',
|
|
|
|
capfirst(opts.verbose_name), change_url(bill), bill, emails)
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
'display_objects': bills
|
|
|
|
}
|