-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
1360 lines (1199 loc) · 60.7 KB
/
database.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This file contains functions relevant to actually manipulating values in the database
"""
# Library imports
import sqlite3, datetime, platform, hashlib, secrets, calendar, copy
from datetime import datetime, timedelta
from datetime import date
from flask_login import UserMixin
from enum import Enum
# Custom Imports
from exceptions import *
from logging import *
app_platform = platform.system()
if app_platform == 'Windows':
database = 'C:\\Users\\norma\\Documents\\Github\\budgeteer\\database.sqlite'
else:
database = '/home/opc/database.sqlite'
conn = sqlite3.connect(database, check_same_thread=False)
c = conn.cursor()
class TType(Enum):
BASIC_TRANSACTION = (0, "Basic Transaction", "local_atm")
ENVELOPE_TRANSFER = (1, "Envelope Transfer", "swap_horiz")
ACCOUNT_TRANSFER = (2, "Account Transfer", "swap_vert")
INCOME = (3, "Income", "vertical_align_top")
SPLIT_TRANSACTION = (4, "Split Transaction", "call_split")
ENVELOPE_FILL = (5, "Envelope Fill", "input")
ENVELOPE_DELETE = (6, "Envelope Delete", "layers_clear")
ACCOUNT_DELETE = (7, "Account Delete", "lmoney_off")
ACCOUNT_ADJUST = (8, "Account Adjust", "build")
def __init__(self, id, desc, icon):
self.id = id # Integer value (used in database)
self.desc = desc # Human readable name (used in hover text)
self.icon = icon # Materialize icon name
@classmethod
def from_int(cls, value):
for t in cls:
if t.value[0] == value:
return t
raise ValueError(f"ERROR: {value} is not a valid TType value!")
# region ---------------CLASS DEFINITIONS---------------
class Transaction:
def __init__(self, type: 'TType', name, amt, date, envelope_id, account_id, grouping, note, schedule, status, user_id, pending):
self.id = None
self.type = type
self.name = name
self.amt = amt
self.date = date
self.envelope_id = envelope_id
self.account_id = account_id
self.grouping = grouping
self.note = note
self.schedule = schedule
self.status = status
self.user_id = user_id
self.pending = pending
def __repr__(self):
return "ID:{}, TYPE:{}, NAME:{}, AMT:{}, DATE:{}, E_ID:{}, A_ID:{}, GRP:{}, NOTE:{}, SCHED:{}, STATUS:{}, U_ID:{}, PENDING:{}".format(self.id,self.type,self.name,self.amt,self.date,self.envelope_id,self.account_id,self.grouping,self.note,self.schedule,self.status,self.user_id,self.pending)
def __str__(self):
return "ID:{}, TYPE:{}, NAME:{}, AMT:{}, DATE:{}, E_ID:{}, A_ID:{}, GRP:{}, NOTE:{}, SCHED:{}, STATUS:{}, U_ID:{}, PENDING:{}".format(self.id,self.type,self.name,self.amt,self.date,self.envelope_id,self.account_id,self.grouping,self.note,self.schedule,self.status,self.user_id,self.pending)
class Account:
def __init__(self, id, name, balance, deleted, user_id, display_order):
self.id = id
self.name = name
self.balance = balance
self.deleted = deleted
self.user_id = user_id
self.display_order = display_order
def __repr__(self):
return "ID:{}, NAME:{}, BAL:{}, DEL:{}, U_ID:{}, DISP:{}".format(self.id,self.name,self.balance,self.deleted,self.user_id,self.display_order)
def __str__(self):
return "ID:{}, NAME:{}, BAL:{}, DEL:{}, U_ID:{}, DISP:{}".format(self.id,self.name,self.balance,self.deleted,self.user_id,self.display_order)
class Envelope:
def __init__(self, id, name, balance, budget, deleted, user_id, display_order):
self.id = id
self.name = name
self.balance = balance
self.budget = budget
self.deleted = deleted
self.user_id = user_id
self.display_order = display_order
def __repr__(self):
return "ID:{}, NAME:{}, BAL:{}, BUDG:{}, DEL:{}, U_ID:{}, DISP:{}".format(self.id,self.name,self.balance,self.budget,self.deleted,self.user_id,self.display_order)
def __str__(self):
return "ID:{}, NAME:{}, BAL:{}, BUDG:{}, DEL:{}, U_ID:{}, DISP:{}".format(self.id,self.name,self.balance,self.budget,self.deleted,self.user_id,self.display_order)
class User(UserMixin):
def __init__(self, uuid, email, password_hash, password_salt, first_name, last_name, unallocated_e_id=None):
self.id = uuid
self.email = email
self.password_hash = password_hash
self.password_salt = password_salt
self.first_name = first_name
self.last_name = last_name
self.unallocated_e_id = unallocated_e_id
def check_password(self, password):
return hash_password(password, self.password_salt)[0] == self.password_hash
def __repr__(self):
return '<UUID: {}, EMAIL: {}, PWD_HASH: {}, SALT: {}, FIRST: {}, LAST: {}, U_EID: {}>'.format(self.id, self.email, self.password_hash, self.password_salt, self.first_name, self.last_name, self.unallocated_e_id)
def __str__(self):
return '<UUID: {}, EMAIL: {}, PWD_HASH: {}, SALT: {}, FIRST: {}, LAST: {}, U_EID: {}>'.format(self.id, self.email, self.password_hash, self.password_salt, self.first_name, self.last_name, self.unallocated_e_id)
# endregion CLASS DEFINITIONS
# region ---------------HELPER FUNCTIONS---------------
def unpack(list_of_tuples):
unpacked_array = []
for item in list_of_tuples:
unpacked_array.append(item[0])
return unpacked_array
def hash_password(password, salt=None):
if salt is None:
salt = secrets.token_hex(16)
hashed_password = hashlib.sha256((password + salt).encode()).hexdigest()
return hashed_password, salt
def balanceformat(number):
"""
Formats amount numbers into a string with a "$" and "-" if necessary for display
"""
if number is None:
string = "NAN"
else:
string = '$%.2f' % abs(number)
if number < 0:
string = '-' + string
return string
def date_parse(date_str):
"""
Converts string to datetime object
"""
if len(date_str) == 10:
date = datetime.strptime(date_str, "%Y-%m-%d")
elif len(date_str)==19:
date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
elif len(date_str)==26:
date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S.%f")
else:
raise TimestampParseError(f"ERROR: Timestamp {date_str} could not be parsed!")
return date
def add_months(sourcedate, months):
"""
Adds an integer amount of months to a given date.
Returns the new date.
"""
month = sourcedate.month - 1 + months
year = sourcedate.year + month // 12
month = month % 12 + 1
day = min(sourcedate.day, calendar.monthrange(year,month)[1])
return date(year, month, day)
def schedule_date_calc(tdate, schedule, timestamp, should_consider_timestamp):
"""
Calculates the upcoming transaction date for a scheduled transaction based on its frequency and the user's timestamp.
Returns the date of the next transaction.
If should_consider_timestamp is true, the next date will be the first valid date after the timestamp. Otherwise, it will be the first valid date after the transaction date.
"""
date_is_pending = False
while not date_is_pending:
if (schedule=="daily"):
nextdate = tdate + timedelta(days=1)
elif (schedule=="weekly"):
nextdate = tdate + timedelta(days=7)
elif (schedule=="biweekly"):
nextdate = tdate + timedelta(days=14)
elif (schedule=="monthly"):
nextdate = add_months(tdate,1)
elif (schedule=="endofmonth"):
lastmonthday = calendar.monthrange(tdate.year, tdate.month)[1]
if (tdate.day == lastmonthday):
nextdate = add_months(tdate,1)
else:
nextdate = date(tdate.year, tdate.month, lastmonthday)
elif (schedule=="semianually"):
nextdate = add_months(tdate,6)
elif (schedule=="anually"):
nextdate = add_months(tdate,12)
else:
raise InvalidFormDataError("ERROR: Invalid schedule option!")
nextdate = datetime.combine(nextdate, datetime.min.time())
if nextdate > timestamp:
date_is_pending = True
else:
tdate = nextdate
if not should_consider_timestamp:
break
return nextdate
def is_pending(date, timestamp):
if date > timestamp:
return True
else:
return False
# endregion HELPER FUNCTIONS
# region ---------------TRANSACTION FUNCTIONS---------------
def insert_transaction(t):
"""
Inserts transaction into database, then applies it to the envelope/account totals if it is not pending
"""
with conn:
# ---1. INSERT THE TRANSACTION INTO THE TABLE---
c.execute("INSERT INTO transactions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, ?)",
(t.id, t.type.id, t.name, t.amt, t.date, t.envelope_id, t.account_id, t.grouping, t.note, t.schedule, t.status, t.user_id, t.pending))
# ---2. UPDATE THE ACCOUNT/ENVELOPE BALANCES---
if not t.pending: #Only update balances if transaction is not pending
apply_transaction(t.account_id, t.envelope_id, t.amt)
log_write('T INSERT: ' + str(t))
def apply_transaction(a_id, e_id, amt):
"""
Subtract the amount of the transaction from the envelope and account it is associated with
"""
if a_id is not None:
newaccountbalance = get_account_balance(a_id) - amt
update_account_balance(a_id, newaccountbalance)
if e_id is not None:
newenvelopebalance = get_envelope_balance(e_id) - amt
update_envelope_balance(e_id, newenvelopebalance)
def unapply_transaction(a_id, e_id, amt):
"""
Add the amount of the transaction from the envelope and account it is associated with
"""
if a_id is not None:
newaccountbalance = get_account_balance(a_id) + amt
update_account_balance(a_id, newaccountbalance)
if e_id is not None:
newenvelopebalance = get_envelope_balance(e_id) + amt
update_envelope_balance(e_id, newenvelopebalance)
def get_transaction(id):
"""
Retrieves transaction object from database given its ID
"""
c.execute("SELECT * FROM transactions WHERE id=?", (id,))
tdata = c.fetchone()
if tdata is None:
raise TransactionNotFoundError(f"Transaction with ID {id} not found!")
t = Transaction(TType.from_int(tdata[1]),tdata[2],tdata[3],tdata[4],tdata[5],tdata[6],tdata[7],tdata[8],tdata[9],tdata[10],tdata[11],tdata[14]) #Last is 14 because e_reconcile and a_reconcile columns exist in table
t.date = date_parse(t.date) # Convert string to datetime object
t.id = tdata[0]
return t
# TODO: When start or amount isn't a valid integer, this should throw an error and not crash because of a bad sqlite3.IntegrityError: datatype mismatch
def get_home_transactions(uuid, start, amount):
"""
For displaying transactions on the HOME PAGE ONLY
Given a start number and an amount of transactions to fetch, returns:
1. A list of transaction objects for display
2. An offset signifying how many transactions are currently displayed, or where the next start should be.
3. limit - If this is true, there are no more transactions to fetch
Note: Grouped transactions (Split transactions and envelope fills) displays as a single transaction
with its summed total
"""
u = get_user_by_uuid(uuid)
c.execute("SELECT id from TRANSACTIONS WHERE user_id=? GROUP BY grouping ORDER BY day DESC, id DESC LIMIT ? OFFSET ?", (uuid, amount, start))
groupings = c.fetchall()
tlist = []
for id in groupings:
t = get_transaction(id[0])
# Set the amount for display
if t.type == TType.BASIC_TRANSACTION or t.type == TType.INCOME or t.type == TType.ACCOUNT_ADJUST or t.type == TType.ACCOUNT_DELETE:
t.amt = -1 * t.amt
elif t.type == TType.ENVELOPE_TRANSFER:
# Get the id for the other envelope and put it in the t.account_id field for special display in the .transaction-details div
t_ids = get_ids_from_grouping(t.grouping)
if (t.amt > 0):
from_envelope_id = get_transaction(t_ids[0]).envelope_id
to_envelope_id = get_transaction(t_ids[1]).envelope_id
else:
from_envelope_id = get_transaction(t_ids[1]).envelope_id
to_envelope_id = get_transaction(t_ids[0]).envelope_id
# Display format (eID -> aID) or for envelope transfers, (fromEnvelope -> toEnvelope)
t.envelope_id = from_envelope_id # When displaying transactions, envelope ID is always displayed first.
t.account_id = to_envelope_id # When displaying transactions, account ID is always displayed second
t.amt = abs(t.amt)
elif t.type == TType.ACCOUNT_TRANSFER:
# Get the id for the other account and put it in the t.account_id field for special display in the .transaction-details div
t_ids = get_ids_from_grouping(t.grouping)
if (t.amt > 0):
from_account_id = get_transaction(t_ids[0]).account_id
to_account_id = get_transaction(t_ids[1]).account_id
else:
from_account_id = get_transaction(t_ids[1]).account_id
to_account_id = get_transaction(t_ids[0]).account_id
# Display format (eID -> aID) or for account transfers, (fromAccount -> toAccount)
t.envelope_id = from_account_id # When displaying transactions, envelope ID is always displayed first.
t.account_id = to_account_id # When displaying transactions, account ID is always displayed second
t.amt = abs(t.amt)
elif t.type == TType.SPLIT_TRANSACTION:
c.execute("SELECT SUM(amount) FROM transactions WHERE grouping=?", (t.grouping,))
t.amt = -1 * c.fetchone()[0] # Total the amount for all the constituent transactions
elif t.type == TType.ENVELOPE_FILL:
c.execute("SELECT SUM(amount) FROM transactions WHERE grouping=? AND envelope_id=?", (t.grouping,u.unallocated_e_id))
t.amt = c.fetchone()[0] # Total the amount for the envelope fill
t.amt = t.amt/100
tlist.append(t)
# offset specifies where to start from on the next call
offset = start + len(tlist)
# if limit is True you're at the end of the list
if len(tlist) < amount:
limit = True
else:
limit = False
return tlist, offset, limit
def get_envelope_transactions(uuid, envelope_id, start, amount):
"""
For displaying transactions on the ENVELOPE PAGE ONLY
Given a start number and an amount of transactions to fetch, returns for a particular envelope:
1. A list of transaction objects for display
2. An offset signifying how many transactions are currently displayed, or where the next start should be.
3. limit - If this is true, there are no more transactions to fetch
Note: Grouped transactions (Split transactions and envelope fills) displays as a single transaction
with its summed total
"""
# Make sure envelope exists and uuid matches envelope uuid
c.execute("SELECT user_id FROM envelopes WHERE id=?",(envelope_id,))
db_uuid = c.fetchone()
if not db_uuid:
raise EnvelopeNotFoundError(f"Envelope with ID {envelope_id} not found!")
if uuid != db_uuid[0]:
raise UnauthorizedAccessError(f"Provided uuid '{uuid}' does not match requested envelope uuid '{db_uuid}'!")
c.execute("SELECT id FROM transactions WHERE envelope_id=? ORDER by day DESC, id DESC LIMIT ? OFFSET ?", (envelope_id, amount, start))
ids = unpack(c.fetchall())
tlist = []
for id in ids:
t = get_transaction(id)
# For display purposes, the envelope ID displays first, then the account ID in the .transaction-details div
if t.type == TType.ENVELOPE_TRANSFER:
t_ids = get_ids_from_grouping(t.grouping)
if (t.amt == 0): # If amt is 0: Display the envelope order based on the order they come out of the database
from_envelope_id = get_transaction(t_ids[1]).envelope_id
to_envelope_id = get_transaction(t_ids[0]).envelope_id
elif (t.amt > 0): # If amt is positive: Actual amt is negative, which means the envelope is being drained, which means it's FROM
t_ids.remove(t.id) #Remove the transaction with the positive balance
from_envelope_id = t.envelope_id
to_envelope_id = get_transaction(t_ids[0]).envelope_id
elif (t.amt < 0): # If amt is negative: Actual amt is positive, which means the envelope is being filled, which means it's TO
t_ids.remove(t.id) #Remove the transaction with the positive balance
to_envelope_id = t.envelope_id
from_envelope_id = get_transaction(t_ids[0]).envelope_id
# Display format (eID -> aID) or for envelope transfers, (fromEnvelope -> toEnvelope)
t.envelope_id = from_envelope_id # When displaying transactions, envelope ID is always displayed first.
t.account_id = to_envelope_id # When displaying transactions, account ID is always displayed second
t.amt = t.amt * -1 / 100
tlist.append(t)
# offset specifies where to start from on the next call
offset = start + len(tlist)
# if limit is True you're at the end of the list
if len(tlist) < amount:
limit = True
else:
limit = False
return tlist, offset, limit
def get_account_transactions(uuid, account_id, start, amount):
"""
For displaying transactions on the ACCOUNTS PAGE ONLY
Given a start number and an amount of transactions to fetch, returns for a particular account:
1. A list of transaction objects for display
2. An offset signifying how many transactions are currently displayed, or where the next start should be.
3. limit - If this is true, there are no more transactions to fetch
Note: Grouped transactions (Split transactions and envelope fills) displays as a single transaction
with its summed total
"""
# Make sure account exists and uuid matches account uuid
c.execute("SELECT user_id FROM accounts WHERE id=?",(account_id,))
db_uuid = c.fetchone()
if not db_uuid:
raise AccountNotFoundError(f"Account with ID {account_id} not found!")
if uuid != db_uuid[0]:
raise UnauthorizedAccessError(f"Provided uuid '{uuid}' does not match requested account uuid '{db_uuid}'!")
c.execute("SELECT id, SUM(amount) FROM transactions WHERE account_id=? GROUP BY grouping ORDER by day DESC, id DESC LIMIT ? OFFSET ?", (account_id,amount,start))
ids = c.fetchall()
tlist = []
for thing in ids:
t = get_transaction(thing[0])
if t.type == TType.ACCOUNT_TRANSFER:
# Get the id for the other account and put it in the t.account_id field for special display in the .transaction-details div
t_ids = get_ids_from_grouping(t.grouping)
# Display format (eID -> aID) or for account transfers, (fromAccount -> toAccount)
t_ids = get_ids_from_grouping(t.grouping)
if (t.amt == 0): # If amt is 0: Display the account order based on the order they come out of the database
from_account_id = get_transaction(t_ids[1]).account_id
to_account_id = get_transaction(t_ids[0]).account_id
elif (t.amt > 0): # If amt is positive: Actual amt is negative, which means the account is being drained, which means it's FROM
t_ids.remove(t.id) #Remove the transaction with the positive balance
from_account_id = t.account_id
to_account_id = get_transaction(t_ids[0]).account_id
elif (t.amt < 0): # If amt is negative: Actual amt is positive, which means the account is being filled, which means it's TO
t_ids.remove(t.id) #Remove the transaction with the positive balance
to_account_id = t.account_id
from_account_id = get_transaction(t_ids[0]).account_id
# Display format (eID -> aID) or for account transfers, (fromaccount -> toaccount)
t.envelope_id = from_account_id # When displaying transactions, envelope ID is always displayed first.
t.account_id = to_account_id # When displaying transactions, account ID is always displayed second
t.amt = thing[1] * -1 / 100
tlist.append(t)
# offset specifies where to start from on the next call
offset = start + len(tlist)
# if limit is True you're at the end of the list
if len(tlist) < amount:
limit = True
else:
limit = False
return tlist, offset, limit
def delete_transaction(uuid, t_id):
"""
Deletes transaction and associated grouped transactions and updates appropriate envelope/account balances
"""
u = get_user_by_uuid(uuid)
with conn:
if get_transaction(t_id).user_id != uuid:
raise UnauthorizedAccessError(f"User with uuid {uuid} does not have permission to delete transaction with id {t_id}")
# 1. Get all the grouped transactions to delete
ids = get_grouped_ids_from_id(t_id)
for id in ids:
t = get_transaction(id)
# 2a. If you're deleting an "ENVELOPE_DELETE" transaction, restore the envelope
if t.type == TType.ENVELOPE_DELETE:
if t.envelope_id is not u.unallocated_e_id: # Don't try to restore the unallocated envelope since it can't be deleted
if (get_envelope(t.envelope_id).deleted is not False): # Don't try to restore an envelope that is not deleted
restore_envelope(t.envelope_id)
# 2b. If you're deleting an "ACCOUNT_DELETE" transaction, restore the account
if t.type == TType.ACCOUNT_DELETE:
if (get_account(t.account_id).deleted is not False): # Don't try to restore an account that is not deleted
restore_account(t.account_id)
# 4. If the transaction is not pending, update envelope/account balances
if not t.pending:
if t.envelope_id is not None:
if bool(get_envelope(t.envelope_id).deleted) is not True:
update_envelope_balance(t.envelope_id, get_envelope_balance(t.envelope_id) + t.amt)
else:
update_envelope_balance(u.unallocated_e_id, get_envelope_balance(u.unallocated_e_id) + t.amt)
# When deleting a transaction referencing a deleted envelope, you must also update the amount of the
# ENVELOPE_DELETE transactions so the deleted envelope maintains a balance of $0.
# i.e. If you delete a $1 transaction in a deleted envelope, the money does not go back into the envelope since it's deleted
# instead, it goes to the unallocated envelope, but now the sum of transactions in your deleted envelopes will not be $0
# unless you change the amount ENVELOPE_DELETE transaction(s) (which drains the deleted envelope balance down to 0).
# Step 1. Get both the ID's for the ENVELOPE_DELETE transactions of the deleted envelope referenced by the transaction
c.execute("""SELECT id FROM transactions WHERE grouping = (
SELECT grouping FROM transactions WHERE (type=? AND envelope_id=?)
)""", (TType.ENVELOPE_DELETE, t.envelope_id)
)
e_delete_ids = unpack(c.fetchall())
# Step 2. Get the ENVELOPE_DELETE transactions
for e_delete_id in e_delete_ids:
e_delete_t = get_transaction(e_delete_id)
# Step 3. Change the amount by which you will adjust the transaction amount depending on whether
# it was filling or draining the envelopes in the ENVELOPE_DELETE transaction.
if e_delete_t.envelope_id == u.unallocated_e_id:
amt = t.amt*-1
else:
amt = t.amt
# Step 4. Update the ENVELOPE_DELETE transaction amount
c.execute("UPDATE transactions SET amount=? WHERE id=?",(e_delete_t.amt + amt, e_delete_id))
log_write('T UPDATE: ' + str(e_delete_t))
if t.account_id is not None:
if bool(get_account(t.account_id).deleted) is not True:
update_account_balance(t.account_id, get_account_balance(t.account_id) + t.amt)
else:
update_envelope_balance(u.unallocated_e_id, get_envelope_balance(u.unallocated_e_id) - t.amt)
# When deleting a transaction referencing a deleted account, you must also update the amount of the
# ACCOUNT_DELETE transactions so the deleted account maintains a balance of $0.
# i.e. If you delete a $1 transaction in a deleted account, the money does not go back into the account since it's deleted
# instead, it goes to the unallocated envelope, but now the sum of transactions in your deleted account will not be $0
# unless you change the amount ACCOUNT_DELETE transaction (which drains the deleted account balance down to 0).
# Step 1. Get the id of the ACCOUNT_DELETE transaction
c.execute("""SELECT id FROM transactions WHERE (type=? AND account_id=?)""", (TType.ACCOUNT_DELETE, t.account_id))
a_delete_id = c.fetchone()[0]
a_delete_t = get_transaction(a_delete_id)
# Step 2. Update the ACCOUNT_DELETE transaction amount
c.execute("UPDATE transactions SET amount=? WHERE id=?",(a_delete_t.amt + t.amt, a_delete_id))
log_write('T UPDATE: ' + str(a_delete_t))
# 6. Delete the actual transaction from the database
c.execute("DELETE FROM transactions WHERE id=?", (id,))
log_write('T DELETE: ' + str(t))
def new_split_transaction(t):
"""
Takes a transaction with arrays of amt and envelope_id and creates individual transactions
This is a grouped transaction with multiple envelopes!
"""
# 1. Combine/sum subtransactions from the same envelope
compressed_e_ids = []
compressed_amts = []
for i in range(len(t.envelope_id)):
if t.envelope_id[i] not in compressed_e_ids:
compressed_e_ids.append(t.envelope_id[i])
compressed_amts.append(t.amt[i])
else:
n = compressed_e_ids.index(t.envelope_id[i])
compressed_amts[n] = compressed_amts[n] + t.amt[i]
# 2. Generate shared grouping number
grouping = gen_grouping_num()
# 3. Insert the transaction
if len(compressed_e_ids) == 1: #If every part of the split transaction went to the same envelope, it's basically a normal transaction
insert_transaction(Transaction(TType.BASIC_TRANSACTION, t.name, compressed_amts[0], t.date, compressed_e_ids[0], t.account_id, grouping, t.note, t.schedule, t.status, t.user_id, t.pending))
else:
for i in range(len(compressed_e_ids)):
insert_transaction(Transaction(TType.SPLIT_TRANSACTION, t.name, compressed_amts[i], t.date, compressed_e_ids[i], t.account_id, grouping, t.note, t.schedule, t.status, t.user_id, t.pending))
# TODO: When implementing unit testing, test what happens if the timestamp is not in the correct format
def check_pending_transactions(uuid, timestamp):
"""
Given a timestamp passed in from the user's browser, apply or unapply the pending transactions in the database and set the pending flag accordingly
"""
with conn:
# 1. Apply pending transactions that are before the timestamp
c.execute("SELECT id FROM transactions WHERE pending=1 AND user_id=? AND date(day) <= date(?)",(uuid, timestamp[0:10],))
t_ids = c.fetchall()
if len(t_ids) != 0:
for id in t_ids:
t = get_transaction(id[0])
apply_transaction(t.account_id, t.envelope_id, t.amt)
c.execute("UPDATE transactions SET pending=0,schedule=null WHERE id=?",(t.id,))
# 1.1 Create scheduled transactions if the transaction is scheduled
if (t.schedule is not None):
isPending = False
next_t = t
# Keep creating scheduled transactions until you create one with a date after the timestamp
while isPending == False:
scheduled_t = create_scheduled_transaction(next_t, date_parse(timestamp), False)
isPending = scheduled_t.pending
next_t = copy.deepcopy(scheduled_t)
if isPending == False:
scheduled_t.schedule = None # Clear out the schedule field if it's not a pending transaction
insert_transaction(scheduled_t)
# 2. Unapply pending transactions that are after the timestamp
c.execute("SELECT id,account_id,envelope_id,amount FROM transactions WHERE pending=0 AND user_id=? AND date(day) > date(?)",(uuid, timestamp[0:10],))
t_data_list = c.fetchall()
if len(t_data_list) != 0:
for (id, a_id, e_id, amt) in t_data_list:
unapply_transaction(a_id, e_id, amt)
c.execute("UPDATE transactions SET pending=1 WHERE id=?",(id,))
return None
def create_scheduled_transaction(t, timestamp, should_consider_timestamp):
"""
Given a transaction, create a new transaction with the same parameters, but with a new date
"""
nextdate = schedule_date_calc(t.date, t.schedule, timestamp, should_consider_timestamp)
return Transaction(t.type, t.name, t.amt, nextdate, t.envelope_id, t.account_id, gen_grouping_num(), t.note, t.schedule, t.status, t.user_id, is_pending(nextdate, timestamp))
# endregion TRANSACTION FUNCTIONS
# region ---------------ACCOUNT FUNCTIONS---------------
def insert_account(a):
"""
Inserts new account and creates "initial account balance" transaction
"""
u = get_user_by_uuid(a.user_id)
with conn:
c.execute("INSERT INTO accounts (name, balance, user_id, display_order) VALUES (?, ?, ?, ?)", (a.name, 0, a.user_id, a.display_order))
account_id = c.lastrowid
income_name = 'Initial Account Balance: ' + a.name
log_write('A INSERT: ' + str(get_account(account_id)))
insert_transaction(Transaction(TType.INCOME, income_name, -1 * a.balance, datetime.combine(date.today(), datetime.min.time()), u.unallocated_e_id, account_id, gen_grouping_num(), '', None, False, a.user_id, False))
def get_account(id):
"""
Returns account associated with the given id
"""
c.execute("SELECT * FROM accounts WHERE id=?", (id,))
adata = c.fetchone()
if adata is None:
raise AccountNotFoundError(f"Account with ID {id} not found!")
a = Account(*adata)
return a
def get_user_account_dict(uuid):
"""
Used for displaying the accounts in the side panel and selects.
Returns:
1. A boolean that says whether there are accounts to render
2. A tuple with a dictionary with keys of account_id and values of account objects.
"""
c.execute("SELECT id FROM accounts where user_id=? ORDER by display_order ASC, id ASC", (uuid,))
ids = unpack(c.fetchall())
a_dict = {}
active_accounts = False
for id in ids:
a = get_account(id)
a.balance = a.balance/100
a_dict[id] = a
if a.deleted == 0:
active_accounts = True
return (active_accounts, a_dict)
def get_user_account_order(uuid):
"""
Used for determining when the account order has changed within the account editor.
Returns:
1. A dictionary with keys of account id and values of display order
"""
with conn:
c.execute("SELECT id,display_order FROM accounts WHERE (deleted=0 AND user_id=?) ORDER BY id ASC",(uuid,))
tuple_array = c.fetchall()
if tuple_array is None:
raise OtherError(f"ERROR: Account order unable to be retrieved for given uuid: {uuid}")
display_dict = dict(tuple_array)
return display_dict
def delete_account(account_id):
"""
Deletes an account:
1. Creates a transaction that zeros account balance (basically a negative income)
2. Sets account "deleted" flag to true
3. Sets display_order to NULL
"""
with conn:
a = get_account(account_id)
u = get_user_by_uuid(a.user_id)
# 1. Empty the deleted account
insert_transaction(Transaction(TType.ACCOUNT_DELETE, f"Deleted account: {a.name}", a.balance, datetime.combine(date.today(), datetime.min.time()), u.unallocated_e_id, a.id, gen_grouping_num(), "", None, 0, a.user_id, False))
# 2. Mark the account as deleted
c.execute("UPDATE accounts SET deleted=1 WHERE id=?", (account_id,))
log_write('A DELETE: ' + str(get_account(account_id)))
#3. Set the displaly_order to NULL
c.execute("UPDATE accounts SET display_order=NULL WHERE id=?", (account_id,))
def restore_account(account_id):
"""
Restores an account by setting its deleted flag to false
* Does NOT adjust the account balances, since this is done at the transaction level
"""
with conn:
a = get_account(account_id)
c.execute("SELECT MAX(display_order) FROM accounts WHERE user_id=?", (a.user_id,))
max_disp_order = c.fetchone()[0]
if max_disp_order is not None:
new_disp_order = max_disp_order + 1
else:
new_disp_order = 0
c.execute("UPDATE accounts SET deleted=0, display_order=? WHERE id=?",(new_disp_order, account_id))
log_write('A RESTORE: ' + str(get_account(account_id)))
def get_account_balance(id):
"""
Returns account balance
"""
c.execute("SELECT balance FROM accounts WHERE id=?", (id,))
balance = c.fetchone()
if balance is not None:
return balance[0]
else:
raise AccountNotFoundError(f"Account with ID {id} not found!")
def update_account_balance(id, balance):
"""
Updates balance of account with given id
NOTE: This shouldn't be possible as a stand alone action. It should always follow a transaction adjusting the balance.
"""
with conn:
c.execute("UPDATE accounts SET balance=? WHERE id=?",(balance, id))
def edit_account(id, new_name, new_balance, new_order, timestamp):
"""
Updates account balalnce and name, then subtracts the change in balance from the unallocated envelope
"""
with conn:
balance_diff = get_account_balance(id) - new_balance
if balance_diff != 0:
adjust_account_balance(id, balance_diff, new_name, timestamp)
c.execute("UPDATE accounts SET name=?, display_order=? WHERE id=?", (new_name, new_order, id))
log_write('A EDIT: ' + str(get_account(id)))
def edit_accounts(uuid, accounts_to_edit, new_accounts, present_ids, timestamp):
"""
Compares an old and new list of accounts, then:
1. Updates accounts in DB if their fields are different from those in old_accounts
2. Adds new accounts not present in old list
3. Deletes old accounts not present in new list
"""
done_something = False
c.execute("SELECT id FROM accounts WHERE deleted=0 AND user_id=?",(uuid,))
original_account_ids = unpack(c.fetchall())
# 1. Updates info for accounts that exist in both lists
for a in accounts_to_edit:
edit_account(int(a.id), a.name, a.balance, a.display_order, timestamp)
done_something = True
# 2. Adds new accounts not present in old list
for a in new_accounts:
insert_account(a)
done_something = True
# 3. Deletes old accounts not present in new list
for id in original_account_ids:
if not (id in present_ids):
delete_account(id)
done_something = True
if done_something:
return "Accounts updated!"
else:
return "No changes were made"
def account_transfer(name, amount, date, to_account, from_account, note, schedule, user_id, pending):
"""
Creates one transaction draining an account, and one transaction filling another
"""
grouping = gen_grouping_num()
insert_transaction(Transaction(TType.ACCOUNT_TRANSFER, name, -1*amount, date, None, to_account, grouping, note, schedule, False, user_id, pending)) #Fill
insert_transaction(Transaction(TType.ACCOUNT_TRANSFER, name, amount, date, None, from_account, grouping, note, schedule, False, user_id, pending)) #Empty
def adjust_account_balance(a_id, balance_diff, name, date):
"""
Creates ACCOUNT_ADJUST transactions for when the user manually sets the account balance in the account editor
This transfers the difference into the unallocated envelope.
The name input exists so that your ACCOUNT_ADJUST transaction will match the name of the account if you renamed it at the same time
as adjusting the balance.
"""
a = get_account(a_id)
u = get_user_by_uuid(a.user_id)
# 1. Create the transaction note
if balance_diff > 0:
note = f"{balanceformat(balance_diff/100)} was deducted from this account AND the Unallocated envelope."
else:
note = f"{balanceformat(-1*balance_diff/100)} was added to this account AND the Unallocated envelope."
# 2. Add a transaction with an amount that will make the account balance equal to the specied balance
insert_transaction(Transaction(TType.ACCOUNT_ADJUST, f"{name}: Balance Adjustment", balance_diff, date, u.unallocated_e_id, a_id, gen_grouping_num(), note, None, False, a.user_id, False))
# endregion ACCOUNT FUNCTIONS
# region ---------------ENVELOPE FUNCTIONS---------------
def insert_envelope(e):
"""
Inserts an envelope into the database with given name and budget and the default balance of 0
"""
with conn:
c.execute("INSERT INTO envelopes (name, budget, user_id, display_order) VALUES (?, ?, ?, ?)", (e.name, e.budget, e.user_id, e.display_order))
log_write('E INSERT: ' + str(get_envelope(c.lastrowid)))
def get_envelope(id):
"""
Returns an envelope object given an envelope_id
"""
c.execute("SELECT * FROM envelopes WHERE id=?", (id,))
edata = c.fetchone()
if edata is None:
raise EnvelopeNotFoundError(f"Envelope with ID {id} not found!")
e = Envelope(*edata)
return e
def get_user_envelope_dict(uuid):
"""
Used for displaying the envelopes in the side panel and selects.
Returns:
1. A boolean that says whether there are envelopes to render
2. A tuple with a dictionary with keys of envelope_id and values of envelope objects.
3. A string displaying the total budget for all envelopes
"""
u = get_user_by_uuid(uuid)
c.execute("SELECT id FROM envelopes WHERE user_id=? ORDER by display_order ASC, id ASC", (uuid,))
ids = unpack(c.fetchall())
e_dict = {}
active_envelopes = False
budget_total = 0
for id in ids:
e = get_envelope(id)
e.balance = e.balance/100
e.budget = e.budget/100
e_dict[id] = e
if e.deleted == 0 and e.id != u.unallocated_e_id:
active_envelopes = True
budget_total = budget_total + e.budget
return (active_envelopes, e_dict, budget_total)
def get_user_envelope_order(uuid):
"""
Used for determining when the envelope order has changed within the envelope editor.
Returns:
1. A dictionary with keys of envelope id and values of display order
"""
with conn:
u = get_user_by_uuid(uuid)
c.execute("SELECT id,display_order FROM envelopes WHERE (deleted=0 AND user_id=? AND id!=?) ORDER BY id ASC", (uuid, u.unallocated_e_id,))
tuple_array = c.fetchall()
if tuple_array is None:
raise OtherError(f"ERROR: Envelope order unable to be retrieved for given uuid: {uuid}")
display_dict = dict(tuple_array)
return display_dict
def delete_envelope(envelope_id):
"""
Deletes an envelope:
1. Creates a transaction that zeros envelope balance
2. Creates a transaction that adds envelope balance to unallocated envelope
3. Sets envelope "deleted" flag to true
4. Sets display_order of deleted envelope to NULL
"""
with conn:
e = get_envelope(envelope_id)
u = get_user_by_uuid(e.user_id)
if (envelope_id != u.unallocated_e_id): #You can't delete the unallocated envelope
grouping = gen_grouping_num()
# 1. Empty the deleted envelope
insert_transaction(Transaction(TType.ENVELOPE_DELETE,f"Deleted envelope: {e.name}", e.balance, datetime.combine(date.today(), datetime.min.time()), envelope_id, None, grouping, "", None, 0, e.user_id, False))
# 2. Fill the unallocated envelope
insert_transaction(Transaction(TType.ENVELOPE_DELETE,f"Deleted envelope: {e.name}", -1*e.balance, datetime.combine(date.today(), datetime.min.time()), u.unallocated_e_id, None, grouping, "", None, 0, e.user_id, False))
# 3. Mark the envelope as deleted
c.execute("UPDATE envelopes SET deleted=1 WHERE id=?", (envelope_id,))
log_write('E DELETE: ' + str(get_envelope(envelope_id)))
# 4. Set display_order of deleted envelope to NULL
c.execute("UPDATE envelopes SET display_order=NULL WHERE id=?", (envelope_id,))
else:
raise OtherError(f"ERROR: User {u.uuid} tried to delete unallocated envelope with id {envelope_id}")
def restore_envelope(envelope_id):
"""
Restores an envelope by setting its deleted flag to false
* Does NOT adjust the envelope balances, since this is done at the transaction level
"""
e = get_envelope(envelope_id)
with conn:
c.execute("SELECT MAX(display_order) FROM envelopes WHERE user_id=?", (e.user_id,))
max_disp_order = c.fetchone()[0]
if max_disp_order is not None:
new_disp_order = max_disp_order + 1
else:
new_disp_order = 0
c.execute("UPDATE envelopes SET deleted=0, display_order=? WHERE id=?",(new_disp_order, envelope_id))
log_write('E RESTORE: ' + str(get_envelope(envelope_id)))
def get_envelope_balance(id):
"""
Returns envelope balance given envelope id
"""
c.execute("SELECT balance FROM envelopes WHERE id=?", (id,))
balance = c.fetchone()
if balance is not None:
return balance[0]
else:
raise EnvelopeNotFoundError(f"Envelope with ID {id} not found!")
def update_envelope_balance(id, balance):
"""
Updates envelope balance for given id.
NOTE: This shouldn't be possible as a stand alone action. It should always follow a transaction adjusting the balance.
"""
with conn:
c.execute("UPDATE envelopes SET balance=? WHERE id=?",(balance, id))
def edit_envelope(id, new_name, new_budget, new_order):
"""
Updates the name, budget, and order for given envelope id
"""
with conn:
c.execute("UPDATE envelopes SET name=?, budget=?, display_order=? WHERE id=?",(new_name, new_budget, new_order, id))
log_write('E EDIT: ' + str(get_envelope(id)))
def edit_envelopes(uuid, envelopes_to_edit, new_envelopes, present_ids):
"""
Inputs:
1. envelopes_to_edit: A list of envelope objects that have different names, budgets, or orders than what is stored in the database
2. new_envelopes: A lit of brand new envelope objects to be inserted into the database
3. present_ids: A list of all id's present in the envelope editor modal (notably missing any that were deleted by the user)
"""
u = get_user_by_uuid(uuid)
done_something = False
c.execute("SELECT id FROM envelopes WHERE deleted=0 AND user_id=?",(uuid,))
original_envelope_ids = unpack(c.fetchall())
# 1. Updates info for envelopes
for e in envelopes_to_edit:
edit_envelope(int(e.id), e.name, e.budget, e.display_order)
done_something = True
# 2. Adds new envelopes
for e in new_envelopes:
insert_envelope(e)
done_something = True
# 3. Deletes any envelopes in the database that are no longer in the present_ids submitted from the form
for id in original_envelope_ids:
if not id in present_ids and id != u.unallocated_e_id:
delete_envelope(id)
done_something = True
if done_something:
return "Envelopes updated!"
else:
return "No changes were made"
def envelope_transfer(name, amt, date, to_envelope, from_envelope, note, schedule, user_id, pending):
"""
Creates a transaction to fill one envelope and another to empty the other
"""
grouping = gen_grouping_num()
insert_transaction(Transaction(TType.ENVELOPE_TRANSFER, name, -1*amt, date, to_envelope, None, grouping, note, schedule, False, user_id, pending)) #Fill
insert_transaction(Transaction(TType.ENVELOPE_TRANSFER, name, amt, date, from_envelope, None, grouping, note, schedule, False, user_id, pending)) #Empty
def envelope_fill(t):
"""
Takes an ENVELOPE_FILL transaction with an array of envelope ids and amounts and creates sub-transactions to fill the envelopes
"""
u = get_user_by_uuid(t.user_id)
if t.type == TType.ENVELOPE_FILL:
grouping = gen_grouping_num()
amts = t.amt
envelopes = t.envelope_id
for i in range(len(amts)):
# Empty the unallocated envelope
insert_transaction(Transaction(TType.ENVELOPE_FILL, t.name, amts[i], t.date, u.unallocated_e_id, None, grouping, t.note, t.schedule, False, t.user_id, t.pending))
# Fill the other envelopes
insert_transaction(Transaction(TType.ENVELOPE_FILL, t.name, amts[i] * -1, t.date, envelopes[i], None, grouping, t.note, t.schedule, False, t.user_id, t.pending))
else:
raise OtherError(f"ERROR: Transaction type mismatch. Expected TType.ENVELOPE_FILL, got {t.type}")
# endregion ENVELOPE FUNCTIONS
# region USER FUNCTIONS ------ #
def get_user_by_email(email):
"""
Given an email address, return a User object if the email is in the database, or return none if not
"""
with conn:
c.execute("SELECT * FROM users WHERE email=?",(email,))
u = c.fetchone()
if u is not None:
user = User(*u)
return user
else:
return None
def get_user_for_flask(uuid):
"""
Given a uuid, return a User object if the uuid is in the database, or return None if not
Note: Must return None, and not throw an exception
"""
conn = sqlite3.connect(database, check_same_thread=False)
c = conn.cursor()
c.execute("SELECT * FROM users WHERE uuid=?",(uuid,))
u = c.fetchone()
c.close()