-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
361 lines (289 loc) · 10.7 KB
/
main.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
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog,\
QMessageBox, QHeaderView
from PyQt5.QtCore import QModelIndex, Qt
from PyQt5.QtWidgets import QLabel
from ui.QBar import QBar
from models import TreeModel, TreeItem
from ui.mainWindow import Ui_MainWindow
import os
import sys
import logging
from collections import OrderedDict
import datetime
import config
from enums import ACCOUNT_TYPES
from utils import Account, ORM
from accountsManager import AccountsManager
from categoriesManager import CategoriesManager
from transactionsRoll import TransactionsRoll
from budgetManager import BudgetManager
from budgetReport import BudgetReport
from balanceReport import BalanceReport
# Define working directory for app
if "APPDATA" in os.environ: # We are on Windows
APP_DATA_PATH = os.path.join(os.environ["APPDATA"], config.APPNAME)
elif "HOME" in os.environ: # We are on Linux
APP_DATA_PATH = os.path.join(os.environ["HOME"], "." + config.APPNAME)
else: # Fallback to our working dir
APP_DATA_PATH = os.getcwd()
def update_recent(name=''):
"""
Updates the value of most recent opened file.
:param name: file name
"""
with open(os.path.join(APP_DATA_PATH, config.RECENT), 'w') as recent:
recent.write(name)
def order_accounts(accounts):
"""
Takes a list of accounts and turns it into dictionary with account type keys.
:param accounts: list of Accounts
:return: OrderedDict of Accounts
"""
account_dict = OrderedDict((i, []) for i in ACCOUNT_TYPES)
for acc in accounts:
account_dict[acc.type].append(acc)
return OrderedDict((k, v) for k, v in account_dict.items() if len(v) > 0)
class AccountsTree(TreeModel):
def __init__(self, orm):
super().__init__(('Account', 'Balance'))
self.orm = orm
self._update_accounts()
def _update_accounts(self):
"""
Fetches account list from DB and builds a tree model of accounts.
Adds subtotal and grand total to that model.
"""
accounts = self.orm.fetch_accounts_summary()
account_dict = order_accounts(accounts)
for key, accs in account_dict.items():
item = TreeItem((key, ''), self.rootItem)
self.rootItem.appendChild(item)
for acc in accs:
acc_item = TreeItem(acc, item)
item.appendChild(acc_item)
# Add type total
if len(accs) > 1:
sub_balance = sum(acc.balance for acc in accs if not acc.exbudget)
subtotal = TreeItem(('Total', sub_balance), item)
item.appendChild(subtotal)
# Add grand total
total_balance = sum([acc.balance for acc in accounts if not acc.exbudget])
total = TreeItem(('Grand Total', total_balance), self.rootItem)
self.rootItem.appendChild(total)
class MainWindow(Ui_MainWindow, QMainWindow):
def __init__(self):
super().__init__()
self.orm = None
self.accounts = None
# Set up the user interface
self.setupUi(self)
# Set up some visual tweaks
self.accountsTree.header().setSectionResizeMode(
QHeaderView.ResizeToContents)
# Connect signals and slots
self.actionOpenFile.triggered.connect(self.choose_file)
self.actionNewFile.triggered.connect(self.create_file)
self.actionCloseFile.triggered.connect(self.close_file)
self.actionQuit.triggered.connect(self.exit_action_triggered)
self.actionManageAccounts.triggered.connect(self.manage_accounts)
self.actionManageCategories.triggered.connect(self.manage_categories)
self.actionBudget.triggered.connect(self.manage_budget)
self.actionAbout.triggered.connect(self.show_about)
self.actionBudgetReport.triggered.connect(self.report_budget)
self.actionBalance.triggered.connect(self.report_balance)
self.accountsTree.doubleClicked.connect(self.account_clicked)
# Try to open the most recent file
self.load_recent_file()
def show_accounts(self):
"""
Reloads the account model into view.
"""
self.accounts = AccountsTree(self.orm)
self.accountsTree.setModel(self.accounts)
self.accountsTree.expandAll()
def show_budget_report(self):
"""
Loads the budget report from DB for current month and year and puts
it into GUI.
"""
self.clear_bars()
year = datetime.date.today().year
month = datetime.date.today().month
for budget_bar in self.orm.fetch_budget_report_bars(month, year):
self.add_bar(budget_bar)
self.barsLayout.setColumnStretch(1, 1)
def clear_bars(self):
"""
Clears the widget for budget report.
"""
for i in reversed(range(self.barsLayout.count())):
widget = self.barsLayout.itemAt(i).widget()
# Detach from layout
self.barsLayout.removeWidget(widget)
# Remove from GUI
widget.setParent(None)
def add_bar(self, budget_bar):
"""
Adds single bar to the budget report at selected position
"""
category = budget_bar.category
category_text = category.parent + '::' + category.name
position = self.barsLayout.rowCount() + 1
self.barsLayout.addWidget(QLabel(category_text), position, 0)
bar = QBar(budget_bar)
self.barsLayout.addWidget(bar, position, 1)
self.barsLayout.addWidget(QLabel(budget_bar.expectation), position, 2)
def load_recent_file(self):
"""
Tries to find the most recent opened file.
"""
file_name = ''
file_path = os.path.join(APP_DATA_PATH, config.RECENT)
if os.path.exists(file_path):
with open(file_path, 'r') as recent:
file_name = recent.readline()
if file_name and os.path.exists(file_name):
self.open_file(file_name)
def choose_file(self):
"""
Fires up user dialog letting him choose file to be opened.
"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.ExistingFile)
dialog.setNameFilter(config.FILE_TYPE)
if dialog.exec():
file_name = dialog.selectedFiles()[0]
self.open_file(file_name)
def create_file(self):
"""
Fires up user dialog letting him create new file.
"""
file_name, _ = QFileDialog.getSaveFileName(
self, caption='Save as...', filter=config.FILE_TYPE)
if file_name:
# if file is new file_name comes w/o extension, have to add it
ext = config.FILE_TYPE.split('*')[1]
if not file_name.endswith(ext):
file_name += ext
self.open_file(file_name)
def open_file(self, name):
"""
Opens the DB file.
"""
# read dbfile and load data
self.orm = ORM(name)
self.show_accounts()
self.show_budget_report()
# update recent filename
update_recent(name)
# update window title
self.setWindowTitle(' -- '.join((config.APPNAME, name)))
def close_file(self):
"""
Closes the DB file and cleans up GUI.
"""
# Clear the budget report
self.clear_bars()
# Clear the accounts tree view
self.accounts = None
self.accountsTree.setModel(None)
# Release DB
self.orm = None
# Forget recent filename
update_recent()
# update window title
self.setWindowTitle(config.APPNAME)
def exit_action_triggered(self):
"""
Exits the program.
"""
self.close()
def manage_accounts(self):
"""
Fires up the widget to manage accounts
"""
if self.orm and self.accounts:
acc_manager = AccountsManager(self.orm)
self.menuBar.setEnabled(False)
acc_manager.exec()
self.menuBar.setEnabled(True)
# Update if there was changes
self.show_accounts()
self.show_budget_report()
def manage_categories(self):
"""
Fires up the widget to manage accounts
"""
if self.orm and self.accounts:
cat_manager = CategoriesManager(self.orm)
self.menuBar.setEnabled(False)
cat_manager.exec()
self.menuBar.setEnabled(True)
def manage_budget(self):
"""
Fires up the widget to manage budget
"""
if self.orm and self.accounts:
budget_manager = BudgetManager(self.orm)
self.menuBar.setEnabled(False)
budget_manager.exec()
self.menuBar.setEnabled(True)
# Update budget report
self.show_budget_report()
def report_budget(self):
"""
Fires up the widget with budget report.
"""
if self.orm and self.accounts:
report = BudgetReport(self.orm)
self.menuBar.setEnabled(False)
report.exec()
self.menuBar.setEnabled(True)
def report_balance(self):
"""
Fires up the widget with balance report.
"""
if self.orm and self.accounts:
report = BalanceReport(self.orm)
self.menuBar.setEnabled(False)
report.exec()
self.menuBar.setEnabled(True)
def show_about(self):
message = " ".join([config.APPNAME, config.VERSION, '\n'])
message += "Icons are designed by Freepik."
QMessageBox.information(self, "About", message)
def account_clicked(self, index: QModelIndex):
"""
Opens the list of transactions for given index in the account
tree model.
"""
account = index.data(role=Qt.UserRole)
if not isinstance(account, Account):
return
transaction_manager = TransactionsRoll(self.orm, account)
self.menuBar.setEnabled(False)
transaction_manager.exec()
self.menuBar.setEnabled(True)
# Update if there was changes
self.show_accounts()
self.show_budget_report()
if __name__ == '__main__':
# Ensure consistency of data dir
if not os.path.exists(APP_DATA_PATH):
os.makedirs(APP_DATA_PATH)
if not os.path.exists(os.path.join(APP_DATA_PATH, config.RECENT)):
update_recent()
log_name = os.path.join(APP_DATA_PATH, config.LOG)
logging.basicConfig(
filename=log_name,
format='%(asctime)s %(message)s',
level=logging.ERROR)
# level=logging.INFO)
logging.info("app_data_path:" + APP_DATA_PATH)
try:
app = QApplication(sys.argv)
form = MainWindow()
form.show()
sys.exit(app.exec())
except Exception as e:
logging.exception(e)