You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

144 lines
4.8 KiB

from django.contrib import admin
from cashonly.core.models import *
from cashonly.core.formfields import DigitField
from cashonly.core.services import AccountManager
from django import forms
from django.template.defaultfilters import escape
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
from django.utils.translation import ugettext_noop
class AccountForm(forms.ModelForm):
credit_change = forms.DecimalField(max_digits=5, decimal_places=2,
required=False,
label=ugettext_lazy('amount'))
credit_change_comment = forms.CharField(max_length=64, required=False,
label=ugettext_lazy('comment'))
pin_change = DigitField(min_length=4, required=False,
label=ugettext_lazy('PIN'))
pin_empty = forms.BooleanField(required=False,
label=ugettext_lazy('clear PIN'))
class Meta:
model = Account
# Include all fields (omitting this causes a RemovedInDjango18Warning)
exclude = []
class AccountAdmin(admin.ModelAdmin):
list_display = ('user', 'card_number', 'credit', 'debit_limit',
'transaction_link')
form = AccountForm
readonly_fields = ('user', 'credit',)
fieldsets = (
(None, {
'fields': ('user', 'card_number', 'credit', 'debit_limit',
'avatar'),
}),
(ugettext_lazy('credit change'), {
'fields': ('credit_change', 'credit_change_comment'),
}),
(ugettext_lazy('change PIN'), {
'fields': ('pin_change', 'pin_empty'),
}),
)
# Disable manual creation of accounts.
# Accounts are create after user creation automatically.
def has_add_permission(self, request):
return False
def transaction_link(self, account):
return '<a href="%s?account__id__exact=%d">%s</a>' % \
(reverse("admin:cashonly_core_transaction_changelist"), account.id,
_('Transactions'))
transaction_link.short_description = ugettext_lazy('Transaction link')
transaction_link.allow_tags = True
def save_model(self, request, obj, form, change):
accmgr = AccountManager(obj)
pin = form.cleaned_data['pin_change']
pin_empty = form.cleaned_data['pin_empty']
if pin_empty:
accmgr.clear_pin()
elif pin is not None and len(pin) != 0:
accmgr.set_pin(pin)
amount = form.cleaned_data['credit_change']
comment = form.cleaned_data['credit_change_comment']
if amount is not None and amount != 0:
accmgr.change_credit(amount, request.user, comment)
# TODO: check whether this can be dropped
# Make sure the object is saved in any case
obj.save()
class ProductBarcodeInline(admin.TabularInline):
model = ProductBarcode
extra = 1
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'category', 'price', 'active')
list_filter = ['category', 'active']
ordering = ('-active', 'name')
inlines = [ProductBarcodeInline]
fields = ('name', 'price', 'active', 'category', 'image')
class ProductCategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'comment')
class SalesLogEntryAdmin(admin.ModelAdmin):
list_display = ('account', 'timestamp', 'product', 'count', 'unit_price')
list_filter = ['account', 'timestamp', 'product']
# Make sales log entries completely read-only
readonly_fields = list(map(lambda f: f.name, SalesLogEntry._meta.fields))
class TransactionAdmin(admin.ModelAdmin):
list_display = ('account', 'timestamp', 'subject', 'description', 'amount')
list_filter = ['account', 'timestamp', 'subject']
# Disable mass deletion in the overview page
actions = None
date_hierarchy = 'timestamp'
# Disable tampering with the transactions completely.
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
if obj is None:
return True
return False
def has_delete_permission(self, request, obj=None):
return False
# Needed to not trigger an ImproperlyConfigured exception.
# FIXME: a bit too hacky
def changelist_view(self, request, extra_context=None):
self.list_display_links = (None, )
return super(TransactionAdmin, self).changelist_view(
request,
extra_context=None,
)
admin.site.register(Account, AccountAdmin)
admin.site.register(Product, ProductAdmin)
admin.site.register(ProductCategory, ProductCategoryAdmin)
admin.site.register(Transaction, TransactionAdmin)
admin.site.register(SalesLogEntry, SalesLogEntryAdmin)