From 478a3246adc53f94304257fbf15b89630e9bf4bc Mon Sep 17 00:00:00 2001 From: Niklas Brachmann Date: Thu, 29 Aug 2013 23:20:56 +0200 Subject: [PATCH 1/6] Added seperate method to change user's credit and create a transaction --- cash/models.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cash/models.py b/cash/models.py index 6694dab..1729c11 100644 --- a/cash/models.py +++ b/cash/models.py @@ -27,6 +27,16 @@ class Account(models.Model): instance.account.card_number = instance.ldap_user.attrs['employeenumber'][0] instance.account.save() + def change_credit(amount, subject, desc): + self.credit -= amount + self.save() + + transaction = Transaction(account=self, subject=subject, + amount=(-amount), description=desc) + transaction.save() + + + def buy_products(self, products): # TODO place it somewhere else MAX_DEBIT = -3500 @@ -37,10 +47,7 @@ class Account(models.Model): total_value = sum(map(lambda p: p.price * products[p], products.keys())) if self.credit - total_value >= MAX_DEBIT: - self.credit -= total_value - self.save() - - descr = '' + desc = '' for product in products.keys(): amount = products[product] @@ -48,12 +55,9 @@ class Account(models.Model): count=amount, unit_price=product.price) logentry.save() - descr += '%dx %s\n' % (amount, product.name) - - transaction = Transaction(account=self, subject=BUY_SUBJECT, - amount=(-total_value), description=descr) - transaction.save() + desc += '%dx %s\n' % (amount, product.name) + self.change_credit(total_value, BUY_SUBJECT, desc) return True else: return False From 143db437bc15174f5c7922f18b2f9d90c8ee0d42 Mon Sep 17 00:00:00 2001 From: Niklas Brachmann Date: Thu, 29 Aug 2013 23:22:34 +0200 Subject: [PATCH 2/6] Added missing self-parameter to change_credit --- cash/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cash/models.py b/cash/models.py index 1729c11..091fcef 100644 --- a/cash/models.py +++ b/cash/models.py @@ -27,7 +27,7 @@ class Account(models.Model): instance.account.card_number = instance.ldap_user.attrs['employeenumber'][0] instance.account.save() - def change_credit(amount, subject, desc): + def change_credit(self, amount, subject, desc): self.credit -= amount self.save() From 5f0ada404a18bb29fd2e3d33bae9d89775280531 Mon Sep 17 00:00:00 2001 From: Niklas Brachmann Date: Thu, 29 Aug 2013 23:38:01 +0200 Subject: [PATCH 3/6] change_credit should add positive credit, not substract it --- cash/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cash/models.py b/cash/models.py index 091fcef..2bcf642 100644 --- a/cash/models.py +++ b/cash/models.py @@ -28,7 +28,7 @@ class Account(models.Model): instance.account.save() def change_credit(self, amount, subject, desc): - self.credit -= amount + self.credit += amount self.save() transaction = Transaction(account=self, subject=subject, @@ -57,7 +57,7 @@ class Account(models.Model): desc += '%dx %s\n' % (amount, product.name) - self.change_credit(total_value, BUY_SUBJECT, desc) + self.change_credit(-total_value, BUY_SUBJECT, desc) return True else: return False From b04d3f64d5715da357f4335a9ad70f2326b3d241 Mon Sep 17 00:00:00 2001 From: Niklas Brachmann Date: Thu, 29 Aug 2013 23:41:57 +0200 Subject: [PATCH 4/6] Added possibilty for admin to changer user's credit --- cash/admin.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cash/admin.py b/cash/admin.py index a2aeda5..543bdcb 100644 --- a/cash/admin.py +++ b/cash/admin.py @@ -1,16 +1,41 @@ from django.contrib import admin from cash.models import * +from django import forms +class AccountForm(forms.ModelForm): + credit_change = forms.DecimalField(max_digits = 5, decimal_places = 2, required = False) + + class Meta: + model = Account class AccountAdmin(admin.ModelAdmin): list_display = ('user', 'card_number', 'credit') + form = AccountForm + readonly_fields = ('credit',) + + def save_model(self, request, obj, form, change): + # TODO: Auslagern + PAYOUT_SUBJECT = "Auszahlung" + DEPOSIT_SUBJECT = "Einzahlung" + DESC = "Autorisiert von %s %s" + + amount = form.cleaned_data['credit_change'] + + print amount + + if amount is not None and amount != 0: + if amount > 0: + subject = DEPOSIT_SUBJECT + else: + subject = PAYOUT_SUBJECT + + obj.change_credit(amount, subject, DESC % (request.user.first_name, request.user.last_name)) class ProductBarcodeInline(admin.TabularInline): model = ProductBarcode extra = 1 - class ProductAdmin(admin.ModelAdmin): list_display = ('name', 'category', 'price') list_filter = ['category'] From 65df5e051904da0b2880026d98e4b785248282eb Mon Sep 17 00:00:00 2001 From: Niklas Brachmann Date: Thu, 29 Aug 2013 23:44:35 +0200 Subject: [PATCH 5/6] change_credit now also loggs with correct signum --- cash/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cash/models.py b/cash/models.py index 2bcf642..6bb67ec 100644 --- a/cash/models.py +++ b/cash/models.py @@ -32,7 +32,7 @@ class Account(models.Model): self.save() transaction = Transaction(account=self, subject=subject, - amount=(-amount), description=desc) + amount=amount, description=desc) transaction.save() From 267bb7ed21792cead4ae2a435dd063943bd437fb Mon Sep 17 00:00:00 2001 From: Niklas Brachmann Date: Fri, 30 Aug 2013 01:04:31 +0200 Subject: [PATCH 6/6] Made transaction list nice and beautyful and added a link to it for every account in the account list --- cash/admin.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/cash/admin.py b/cash/admin.py index 543bdcb..13ea26d 100644 --- a/cash/admin.py +++ b/cash/admin.py @@ -1,6 +1,8 @@ from django.contrib import admin from cash.models import * from django import forms +from django.template.defaultfilters import escape +from django.core.urlresolvers import reverse class AccountForm(forms.ModelForm): credit_change = forms.DecimalField(max_digits = 5, decimal_places = 2, required = False) @@ -9,10 +11,15 @@ class AccountForm(forms.ModelForm): model = Account class AccountAdmin(admin.ModelAdmin): - list_display = ('user', 'card_number', 'credit') + list_display = ('user', 'card_number', 'credit', 'transaction_link') form = AccountForm readonly_fields = ('credit',) + def transaction_link(self, account): + return 'Transaktionen' % (reverse("admin:cash_transaction_changelist"), account.id) + + transaction_link.allow_tags = True + def save_model(self, request, obj, form, change): # TODO: Auslagern PAYOUT_SUBJECT = "Auszahlung" @@ -41,20 +48,39 @@ class ProductAdmin(admin.ModelAdmin): list_filter = ['category'] inlines= [ProductBarcodeInline] - 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'] +class TransactionAdmin(admin.ModelAdmin): + list_display = ('account', 'timestamp', 'subject', 'description', 'amount') + list_filter = ['account', 'timestamp', 'subject'] + 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(ProductBarcode) admin.site.register(ProductCategory, ProductCategoryAdmin) -admin.site.register(Transaction) +admin.site.register(Transaction, TransactionAdmin) admin.site.register(SalesLogEntry, SalesLogEntryAdmin)