forked from MEDVEDx64/FunkyStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoney.py
44 lines (38 loc) · 1.63 KB
/
money.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from datetime import datetime
def transfer(db, login_src, login_dest, amount = 0.0):
"Transfer some $$$ from account to account, return True on success, False on failure (not enough money, etc.)"
if amount < 0:
return (False, 'Invalid amount')
if login_src == login_dest:
return (False, 'Destination and source are the same account')
acc = db['accounts'].find_one({'login': login_src})
if not acc:
return (False, 'Source account does not exist')
if not 'money' in acc:
return (False, 'Source account have no money')
if not 'flags' in acc or not 'money_send' in acc['flags']:
return (False, 'Source account have no permissions to send money')
acc_dst = db['accounts'].find_one({'login': login_dest})
if not acc_dst:
return (False, 'Destination account does not exist')
if not 'flags' in acc_dst or not 'money_recv' in acc_dst['flags']:
return (False, 'Destination account have no permissions to receive money')
if amount > acc['money']:
return (False, 'Not enough money')
db['accounts'].update({'login': login_src}, {'$set': {'money': acc['money'] - amount}})
if 'money' in acc_dst:
db['accounts'].update({'login': login_dest}, {'$set': {'money': acc_dst['money'] + amount}})
else:
db['accounts'].update({'login': login_dest}, {'$set': {'money': amount}})
db['transactions'].insert({'source': login_src, 'destination': login_dest, 'amount': amount, 'timestamp': datetime.now()})
return (True, 'Success')
def get_balance(db, login):
"Return None when requested account does not exist"
acc = db['accounts'].find_one({'login': login})
if acc:
if 'money' in acc:
return acc['money']
else:
return 0.0
else:
return None