django-orchestra-test/orchestra/contrib/bills/helpers.py

45 lines
1.8 KiB
Python
Raw Permalink Normal View History

2014-10-11 16:21:51 +00:00
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.utils.encoding import force_text
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
2014-10-11 16:21:51 +00:00
from django.utils.translation import ugettext_lazy as _
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'):
2014-10-11 16:21:51 +00:00
account = force_text(bill.account)
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
main = type(bill).account.field.rel.to.objects.get_main()
2014-10-20 15:51:24 +00:00
if not hasattr(main, 'billcontact'):
2014-10-11 16:21:51 +00:00
account = force_text(main)
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
}