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

128 lines
4.6 KiB
Python
Raw Normal View History

2014-08-22 11:28:46 +00:00
from django import forms
2014-07-23 16:24:56 +00:00
from django.contrib import admin
from django.core.urlresolvers import reverse
2014-09-03 13:56:02 +00:00
from django.db import models
2014-07-23 16:24:56 +00:00
from django.utils.translation import ugettext_lazy as _
2014-08-19 18:59:23 +00:00
from orchestra.admin import ExtendedModelAdmin
2014-07-23 16:24:56 +00:00
from orchestra.admin.utils import admin_link, admin_date
from orchestra.apps.accounts.admin import AccountAdminMixin
2014-09-03 13:56:02 +00:00
from . import settings
2014-08-19 18:59:23 +00:00
from .actions import generate_bill
2014-07-23 16:24:56 +00:00
from .filters import BillTypeListFilter
from .models import (Bill, Invoice, AmendmentInvoice, Fee, AmendmentFee, Budget,
BillLine, BudgetLine)
class BillLineInline(admin.TabularInline):
model = BillLine
2014-09-03 13:56:02 +00:00
fields = ('description', 'rate', 'amount', 'tax', 'total', 'subtotal')
readonly_fields = ('subtotal',)
def subtotal(self, line):
if line.total:
subtotal = 0
for subline in line.sublines.all():
subtotal += subline.total
return line.total - subtotal
return ''
2014-08-22 11:28:46 +00:00
def get_readonly_fields(self, request, obj=None):
if obj and obj.status != Bill.OPEN:
return self.fields
return super(BillLineInline, self).get_readonly_fields(request, obj=obj)
def has_add_permission(self, request):
if request.__bill__ and request.__bill__.status != Bill.OPEN:
return False
return super(BillLineInline, self).has_add_permission(request)
def has_delete_permission(self, request, obj=None):
if obj and obj.status != Bill.OPEN:
return False
return super(BillLineInline, self).has_delete_permission(request, obj=obj)
2014-07-23 16:24:56 +00:00
class BudgetLineInline(admin.TabularInline):
model = Budget
2014-09-03 13:56:02 +00:00
fields = ('description', 'rate', 'amount', 'tax', 'total')
2014-07-23 16:24:56 +00:00
2014-08-19 18:59:23 +00:00
class BillAdmin(AccountAdminMixin, ExtendedModelAdmin):
2014-07-23 16:24:56 +00:00
list_display = (
2014-09-03 13:56:02 +00:00
'number', 'status', 'type_link', 'account_link', 'created_on_display',
'num_lines', 'display_total'
2014-07-23 16:24:56 +00:00
)
list_filter = (BillTypeListFilter, 'status',)
2014-08-22 11:28:46 +00:00
add_fields = ('account', 'type', 'status', 'due_on', 'comments')
fieldsets = (
(None, {
2014-09-03 13:56:02 +00:00
'fields': ('number', 'display_total', 'account_link', 'type',
'status', 'due_on', 'comments'),
2014-08-22 11:28:46 +00:00
}),
(_("Raw"), {
'classes': ('collapse',),
'fields': ('html',),
}),
)
2014-08-19 18:59:23 +00:00
change_view_actions = [generate_bill]
2014-08-22 11:28:46 +00:00
change_readonly_fields = ('account_link', 'type', 'status')
2014-09-03 13:56:02 +00:00
readonly_fields = ('number', 'display_total')
2014-07-23 16:24:56 +00:00
inlines = [BillLineInline]
created_on_display = admin_date('created_on')
2014-09-03 13:56:02 +00:00
def num_lines(self, bill):
return bill.billlines__count
num_lines.admin_order_field = 'billlines__count'
num_lines.short_description = _("lines")
def display_total(self, bill):
return "%i &%s;" % (bill.get_total(), settings.BILLS_CURRENCY.lower())
display_total.allow_tags = True
display_total.short_description = _("total")
2014-08-19 18:59:23 +00:00
def type_link(self, bill):
bill_type = bill.type.lower()
2014-07-23 16:24:56 +00:00
url = reverse('admin:bills_%s_changelist' % bill_type)
2014-08-19 18:59:23 +00:00
return '<a href="%s">%s</a>' % (url, bill.get_type_display())
type_link.allow_tags = True
type_link.short_description = _("type")
type_link.admin_order_field = 'type'
2014-07-23 16:24:56 +00:00
2014-08-22 11:28:46 +00:00
def get_readonly_fields(self, request, obj=None):
fields = super(BillAdmin, self).get_readonly_fields(request, obj=obj)
if obj and obj.status != Bill.OPEN:
fields += self.add_fields
return fields
2014-07-23 16:24:56 +00:00
def get_inline_instances(self, request, obj=None):
if self.model is Budget:
self.inlines = [BudgetLineInline]
2014-08-22 11:28:46 +00:00
# Make parent object available for inline.has_add_permission()
request.__bill__ = obj
2014-07-23 16:24:56 +00:00
return super(BillAdmin, self).get_inline_instances(request, obj=obj)
2014-08-22 11:28:46 +00:00
def formfield_for_dbfield(self, db_field, **kwargs):
""" Make value input widget bigger """
if db_field.name == 'comments':
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 4})
if db_field.name == 'html':
kwargs['widget'] = forms.Textarea(attrs={'cols': 150, 'rows': 20})
return super(BillAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2014-09-03 13:56:02 +00:00
def queryset(self, request):
qs = super(BillAdmin, self).queryset(request)
qs = qs.annotate(models.Count('billlines'))
qs = qs.prefetch_related('billlines', 'billlines__sublines')
return qs
2014-07-23 16:24:56 +00:00
admin.site.register(Bill, BillAdmin)
admin.site.register(Invoice, BillAdmin)
admin.site.register(AmendmentInvoice, BillAdmin)
admin.site.register(Fee, BillAdmin)
admin.site.register(AmendmentFee, BillAdmin)
admin.site.register(Budget, BillAdmin)