Implemented "Balance change reason" field.

This commit is contained in:
s3lph 2018-09-01 23:48:10 +02:00
parent 3bdc9417fd
commit a3fa86fb25
4 changed files with 19 additions and 7 deletions

View file

@ -266,9 +266,10 @@ class MatematDatabase(object):
# Resolve the values to change # Resolve the values to change
name: str = kwargs['name'] if 'name' in kwargs else user.name name: str = kwargs['name'] if 'name' in kwargs else user.name
email: str = kwargs['email'] if 'email' in kwargs else user.email email: str = kwargs['email'] if 'email' in kwargs else user.email
balance: int = kwargs['balance'] if 'balance' in kwargs else user.balance
is_admin: bool = kwargs['is_admin'] if 'is_admin' in kwargs else user.is_admin is_admin: bool = kwargs['is_admin'] if 'is_admin' in kwargs else user.is_admin
is_member: bool = kwargs['is_member'] if 'is_member' in kwargs else user.is_member is_member: bool = kwargs['is_member'] if 'is_member' in kwargs else user.is_member
balance: int = kwargs['balance'] if 'balance' in kwargs else user.balance
balance_reason: Optional[str] = kwargs['balance_reason'] if 'balance_reason' in kwargs else None
receipt_pref: ReceiptPreference = kwargs['receipt_pref'] if 'receipt_pref' in kwargs else user.receipt_pref receipt_pref: ReceiptPreference = kwargs['receipt_pref'] if 'receipt_pref' in kwargs else user.receipt_pref
with self.db.transaction() as c: with self.db.transaction() as c:
c.execute('SELECT balance FROM users WHERE user_id = :user_id', {'user_id': user.id}) c.execute('SELECT balance FROM users WHERE user_id = :user_id', {'user_id': user.id})
@ -287,11 +288,13 @@ class MatematDatabase(object):
'value': balance - oldbalance, 'value': balance - oldbalance,
'old_balance': oldbalance 'old_balance': oldbalance
}) })
# TODO: Implement reason field
c.execute(''' c.execute('''
INSERT INTO modifications (ta_id, agent, reason) INSERT INTO modifications (ta_id, agent, reason)
VALUES (last_insert_rowid(), :agent, NULL) VALUES (last_insert_rowid(), :agent, :reason)
''', {'agent': agent.name}) ''', {
'agent': agent.name,
'reason': balance_reason
})
c.execute(''' c.execute('''
UPDATE users SET UPDATE users SET
username = :username, username = :username,

View file

@ -174,7 +174,7 @@ class DatabaseTest(unittest.TestCase):
agent = db.create_user('admin', 'supersecurepassword', 'admin@example.com', True, True) agent = db.create_user('admin', 'supersecurepassword', 'admin@example.com', True, True)
user = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com', True, True) user = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com', True, True)
db.change_user(user, agent, email='newaddress@example.com', is_admin=False, is_member=False, balance=4200, db.change_user(user, agent, email='newaddress@example.com', is_admin=False, is_member=False, balance=4200,
receipt_pref=ReceiptPreference.MONTHLY) balance_reason='This is a reason!', receipt_pref=ReceiptPreference.MONTHLY)
# Changes must be reflected in the passed user object # Changes must be reflected in the passed user object
self.assertEqual('newaddress@example.com', user.email) self.assertEqual('newaddress@example.com', user.email)
self.assertFalse(user.is_admin) self.assertFalse(user.is_admin)
@ -188,6 +188,9 @@ class DatabaseTest(unittest.TestCase):
self.assertFalse(checkuser.is_member) self.assertFalse(checkuser.is_member)
self.assertEqual(4200, checkuser.balance) self.assertEqual(4200, checkuser.balance)
self.assertEqual(ReceiptPreference.MONTHLY, checkuser.receipt_pref) self.assertEqual(ReceiptPreference.MONTHLY, checkuser.receipt_pref)
with db.transaction(exclusive=False) as c:
c.execute('SELECT reason FROM modifications LIMIT 1')
self.assertEqual('This is a reason!', c.fetchone()[0])
# Balance change without an agent must fail # Balance change without an agent must fail
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
db.change_user(user, None, balance=0) db.change_user(user, None, balance=0)

View file

@ -1,4 +1,4 @@
from typing import Any, Dict, Union from typing import Any, Dict, Optional, Union
import os import os
import magic import magic
@ -103,6 +103,9 @@ def handle_change(args: RequestArguments, user: User, authuser: User, db: Matema
return return
password = str(args.password) password = str(args.password)
balance = parse_chf(str(args.balance)) balance = parse_chf(str(args.balance))
balance_reason: Optional[str] = str(args.reason)
if balance_reason == '':
balance_reason = None
is_member = 'ismember' in args is_member = 'ismember' in args
is_admin = 'isadmin' in args is_admin = 'isadmin' in args
# An empty e-mail field should be interpreted as NULL # An empty e-mail field should be interpreted as NULL
@ -115,7 +118,7 @@ def handle_change(args: RequestArguments, user: User, authuser: User, db: Matema
db.change_password(user, '', password, verify_password=False) db.change_password(user, '', password, verify_password=False)
# Write the user detail changes # Write the user detail changes
db.change_user(user, agent=authuser, name=username, email=email, is_member=is_member, is_admin=is_admin, db.change_user(user, agent=authuser, name=username, email=email, is_member=is_member, is_admin=is_admin,
balance=balance, receipt_pref=receipt_pref) balance=balance, balance_reason=balance_reason, receipt_pref=receipt_pref)
except DatabaseConsistencyError: except DatabaseConsistencyError:
return return
# If a new avatar was uploaded, process it # If a new avatar was uploaded, process it

View file

@ -37,6 +37,9 @@
<label for="moduser-account-balance">Balance: </label> <label for="moduser-account-balance">Balance: </label>
CHF <input id="moduser-account-balance" name="balance" type="number" step="0.01" value="{{ user.balance|chf(False) }}" /><br/> CHF <input id="moduser-account-balance" name="balance" type="number" step="0.01" value="{{ user.balance|chf(False) }}" /><br/>
<label for="moduser-account-balance-reason">Reason for balance modification: </label>
<input id="moduser-account-balance-reason" type="text" name="reason" placeholder="Shows up on receipt" /><br/>
<label for="moduser-account-avatar"> <label for="moduser-account-avatar">
<img height="150" src="/upload/thumbnails/users/{{ user.id }}.png" alt="Avatar of {{ user.name }}" /> <img height="150" src="/upload/thumbnails/users/{{ user.id }}.png" alt="Avatar of {{ user.name }}" />
</label><br/> </label><br/>