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.
143 lines
4.7 KiB
143 lines
4.7 KiB
11 years ago
|
from django.contrib import admin
|
||
7 years ago
|
from cashonly.core.models import *
|
||
|
from cashonly.core.formfields import DigitField
|
||
|
from cashonly.core.services import AccountManager
|
||
11 years ago
|
from django import forms
|
||
11 years ago
|
from django.template.defaultfilters import escape
|
||
|
from django.core.urlresolvers import reverse
|
||
11 years ago
|
from django.utils.translation import ugettext as _
|
||
|
from django.utils.translation import ugettext_lazy
|
||
|
from django.utils.translation import ugettext_noop
|
||
11 years ago
|
|
||
7 years ago
|
|
||
11 years ago
|
class AccountForm(forms.ModelForm):
|
||
7 years ago
|
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'))
|
||
11 years ago
|
|
||
7 years ago
|
pin_change = DigitField(min_length=4, required=False,
|
||
|
label=ugettext_lazy('PIN'))
|
||
11 years ago
|
|
||
7 years ago
|
pin_empty = forms.BooleanField(required=False,
|
||
|
label=ugettext_lazy('clear PIN'))
|
||
11 years ago
|
|
||
7 years ago
|
class Meta:
|
||
|
model = Account
|
||
11 years ago
|
|
||
7 years ago
|
# Include all fields (omitting this causes a RemovedInDjango18Warning)
|
||
|
exclude = []
|
||
11 years ago
|
|
||
10 years ago
|
|
||
11 years ago
|
class AccountAdmin(admin.ModelAdmin):
|
||
7 years ago
|
list_display = ('user', 'card_number', 'credit', 'transaction_link')
|
||
|
form = AccountForm
|
||
|
readonly_fields = ('user', 'credit',)
|
||
|
fieldsets = (
|
||
|
(None, {
|
||
|
'fields': ('user', 'card_number', 'credit'),
|
||
|
}),
|
||
|
(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>' % \
|
||
7 years ago
|
(reverse("admin:cashonly_core_transaction_changelist"), account.id,
|
||
7 years ago
|
_('Transactions'))
|
||
|
|
||
|
transaction_link.short_description = ugettext_lazy('Transaction link')
|
||
|
|
||
|
transaction_link.allow_tags = True
|
||
|
|
||
|
def save_model(self, request, obj, form, change):
|
||
7 years ago
|
accmgr = AccountManager(obj)
|
||
|
|
||
7 years ago
|
pin = form.cleaned_data['pin_change']
|
||
|
pin_empty = form.cleaned_data['pin_empty']
|
||
|
if pin_empty:
|
||
7 years ago
|
accmgr.clear_pin()
|
||
|
elif pin is not None and len(pin) != 0:
|
||
|
accmgr.set_pin(pin)
|
||
7 years ago
|
|
||
|
amount = form.cleaned_data['credit_change']
|
||
|
comment = form.cleaned_data['credit_change_comment']
|
||
|
if amount is not None and amount != 0:
|
||
7 years ago
|
accmgr.change_credit(amount, request.user, comment)
|
||
7 years ago
|
|
||
7 years ago
|
# TODO: check whether this can be dropped
|
||
7 years ago
|
# Make sure the object is saved in any case
|
||
|
obj.save()
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
class ProductBarcodeInline(admin.TabularInline):
|
||
7 years ago
|
model = ProductBarcode
|
||
|
extra = 1
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
class ProductAdmin(admin.ModelAdmin):
|
||
7 years ago
|
list_display = ('name', 'category', 'price', 'active')
|
||
|
list_filter = ['category', 'active']
|
||
|
ordering = ('-active', 'name')
|
||
|
inlines = [ProductBarcodeInline]
|
||
|
fields = ('name', 'price', 'active', 'category', 'image')
|
||
|
|
||
11 years ago
|
|
||
|
class ProductCategoryAdmin(admin.ModelAdmin):
|
||
7 years ago
|
list_display = ('name', 'comment')
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
class SalesLogEntryAdmin(admin.ModelAdmin):
|
||
7 years ago
|
list_display = ('account', 'timestamp', 'product', 'count', 'unit_price')
|
||
|
list_filter = ['account', 'timestamp', 'product']
|
||
|
# Make sales log entries completely read-only
|
||
7 years ago
|
readonly_fields = list(map(lambda f: f.name, SalesLogEntry._meta.fields))
|
||
7 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
class TransactionAdmin(admin.ModelAdmin):
|
||
7 years ago
|
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,
|
||
|
)
|
||
11 years ago
|
|
||
11 years ago
|
admin.site.register(Account, AccountAdmin)
|
||
|
admin.site.register(Product, ProductAdmin)
|
||
|
admin.site.register(ProductCategory, ProductCategoryAdmin)
|
||
11 years ago
|
admin.site.register(Transaction, TransactionAdmin)
|
||
11 years ago
|
admin.site.register(SalesLogEntry, SalesLogEntryAdmin)
|