-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
200 lines (140 loc) · 6.25 KB
/
crud.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
"""CRUD operations."""
from model import (db, User, Account, EntryLog, connect_to_db)
import datetime
import copy
import sys
# from flask_sqlalchemy import SQLAlchemy
##### class User #####
def create_user(first_name, last_name, email, password):
"""Create and return a new user."""
user = User(first_name=first_name,
last_name=last_name,
email=email,
password=password)
db.session.add(user)
db.session.commit()
return user
def get_user_by_email(email):
"""Return a user by email."""
return User.query.filter(User.email == email).first()
# def edit_user_by_user_id(user_id,
# new_first_name,
# new_last_name,
# new_email,
# new_password):
# """Edit user's information by user_id."""
# user = User.query.filter(User.user_id == user_id)
# user.update({User.first_name: new_first_name})
# user.update({User.last_name: new_last_name})
# user.update({User.email: new_email})
# user.update({User.password: new_password})
##### class Account #####
def create_account(user_id, account_type, account_nickname):
"""Create and return an account."""
account = Account(user_id=user_id,
account_type=account_type,
account_nickname=account_nickname)
db.session.add(account)
db.session.commit()
return account
def get_accounts_by_user_id(user_id):
"""Return a list of accounts by searching with user_id."""
return Account.query.filter(Account.user_id == user_id).all()
def get_account_by_account_id(account_id):
"""Return a specific account info with account_id."""
return Account.query.filter(Account.account_id == account_id).first()
# def edit_account_by_account_id(account_id, new_account_type, new_account_nickname):
# """Edit an account by account_id."""
# account = Account.query.filter(Account.account_id == account_id)
# account.update({Account.account_type: new_account_type})
def remove_account_by_account_id(account_id):
"""Remove an account by account_id.
Note: this action will remove all entries in the account,
followed by removing the account."""
EntryLog.query.filter(EntryLog.account_id == account_id).delete()
db.session.commit()
Account.query.filter(Account.account_id == account_id).delete()
db.session.commit()
##### class EntryLog #####
def create_entry_log(account_id, date, category, description, amount, stop_date=None, frequency=None):
"""Create and return an entry."""
entry_log = EntryLog(account_id=account_id,
date=date,
category=category,
description=description,
amount=amount,
stop_date=stop_date,
frequency=frequency) # ! stop_date and frequency added to consolidate tables
db.session.add(entry_log)
db.session.commit()
return entry_log
def get_entry_logs_by_account_id(account_id):
"""Return all the entry logs associated with a particular account. Limit to
maximum of one year for display."""
all_entries = EntryLog.query.filter(
EntryLog.account_id == account_id).all()
entries_within_a_year = []
today = datetime.date.today()
a_year_later = today + datetime.timedelta(365)
for entry in all_entries:
if entry.date <= a_year_later:
entries_within_a_year.append(entry)
return entries_within_a_year
def edit_entry_amount_by_entry_id(entry_id,
new_amount):
"""Edit an entry by entry_id."""
entry = EntryLog.query.filter(EntryLog.entry_id == entry_id)
entry.update({EntryLog.amount: new_amount})
db.session.commit()
def remove_entry_by_entry_id(entry_id):
"""Remove an entry by entry_id."""
EntryLog.query.filter(EntryLog.entry_id == entry_id).delete()
db.session.commit()
#* recurrent entries *#
def convert_frequency_to_num_of_day(frequency_int, frequency_unit):
"""Return # of days from # of weeks for or just # of days in timedelta
for recurrent entries."""
if frequency_unit == "weeks":
num_of_days = 7 * frequency_int
elif frequency_unit == "days":
num_of_days = frequency_int
return datetime.timedelta(num_of_days)
def retrieve_recurrent_entries_by_account_id(account_id):
""" Find all the recurrent entries and return in a list of object."""
# Objects are mutable. We need to make a copy of the instances
copy_of_recurrent_entries = EntryLog.query.filter(
EntryLog.account_id == account_id, EntryLog.frequency != None).all()
return copy_of_recurrent_entries
def find_all_dates(start_date, stop_date, frequency):
""" Return a list of dates based on one single recurrent entry's dates and frequency.
Frequency is in datetime.timedelta. Limited to one year max."""
list_of_dates = []
today = datetime.date.today()
a_year_later = today + datetime.timedelta(365)
if stop_date > a_year_later or stop_date == None:
stop_date = a_year_later
while start_date <= stop_date:
date = start_date + frequency
if date <= stop_date:
list_of_dates.append(date)
start_date = date
else:
break
return list_of_dates
def list_of_recurrent_entries_with_all_dates(list_of_recurrent_entries):
""" Return a list of recurrent entries associated with all dates based on frequency."""
updated_entry_list_with_new_dates = []
for entry in list_of_recurrent_entries:
date = entry.date
stop_date = entry.stop_date
frequency = entry.frequency
list_of_dates = find_all_dates(date, stop_date, frequency)
for date in list_of_dates:
entry_copy = copy.deepcopy(entry)
entry_copy.date = date
updated_entry_list_with_new_dates.append(entry_copy)
return updated_entry_list_with_new_dates
if __name__ == '__main__':
from server import app
local = "-local" in sys.argv
connect_to_db(app, local=local)