-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_transactions.py
45 lines (38 loc) · 1.46 KB
/
db_transactions.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
import csv
from pymongo import MongoClient
# connect to MongoDB atlas
client = MongoClient('mongodb+srv://Desis:Desis@cluster0.4qlek3x.mongodb.net/?retryWrites=true&w=majority')
db = client['Desis']
user_data_collection = db['Desis']
# dictionary to store user data
user_data = {
"user": 'user_1',
"Important": {},
"Non-important": {},
"Essential": {},
"Non-Essential": {},
"Global Balance": 0,
}
# read csv file and populate user_data dictionary
with open('dummydata1.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
transaction = {
"Txn Date": row['Txn Date'],
"Description": row['Description'],
"Transfer To/From": row['Transfer To/From'],
"Ref No./Cheque No.": row['Ref No./Cheque No.'],
"Debit": float(row['Debit']),
"Credit": float(row['Credit']),
"Balance": float(row['Balance'])
}
category = row['Class']
ref_no = row['Ref No./Cheque No.']
if ref_no != '':
user_data[category][ref_no] = transaction
# update category balance
user_data[category]["Balance"] = user_data[category].get("Balance", 0) - transaction["Debit"] + transaction["Credit"]
# update global balance
user_data["Global Balance"] += -transaction["Debit"] + transaction["Credit"]
# insert user data into MongoDB
user_data_collection.insert_one(user_data)