1
|
|
|
from app import db |
2
|
|
|
from datetime import datetime, date |
3
|
|
|
import pandas as pd |
4
|
|
|
import os |
5
|
|
|
from natsort import index_natsorted |
6
|
|
|
import numpy as np |
7
|
|
|
from io import TextIOWrapper |
8
|
|
|
import csv |
9
|
|
|
from .models import Schedule |
10
|
|
|
import platform |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def export(): |
14
|
|
|
try: |
15
|
|
|
engine = db.create_engine(os.environ.get('DATABASE_URL')).connect() |
16
|
|
|
except: |
17
|
|
|
engine = db.create_engine('sqlite:///db.sqlite').connect() |
18
|
|
|
|
19
|
|
|
# pull the schedule information |
20
|
|
|
df = pd.read_sql('SELECT * FROM schedule;', engine) |
21
|
|
|
df = df.sort_values(by="startdate", |
22
|
|
|
key=lambda x: np.argsort(index_natsorted(df["startdate"]))).reset_index(drop=True) |
23
|
|
|
|
24
|
|
|
csv_data = "Name,Amount,Type,Frequency,Next Date,First Date\n" |
25
|
|
|
for i in range(len(df.index)): |
26
|
|
|
# Create a CSV string from the data |
27
|
|
|
csv_data += (f"{df['name'][i]},{df['amount'][i]},{df['type'][i]},{df['frequency'][i]},{df['startdate'][i]}" |
28
|
|
|
f",{df['firstdate'][i]}\n") |
29
|
|
|
|
30
|
|
|
return csv_data |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def upload(csv_file): |
34
|
|
|
csv_file = TextIOWrapper(csv_file, encoding='utf-8') |
35
|
|
|
csv_reader = csv.reader(csv_file, delimiter=',') |
36
|
|
|
next(csv_reader) |
37
|
|
|
|
38
|
|
|
format = '%Y-%m-%d' |
39
|
|
|
for row in csv_reader: |
40
|
|
|
try: |
41
|
|
|
name = row[0] |
42
|
|
|
amount = float(row[1]) |
43
|
|
|
type = row[2] |
44
|
|
|
frequency = row[3] |
45
|
|
|
next_date = row[4] |
46
|
|
|
next_date = datetime.strptime(next_date, format).date() |
47
|
|
|
first_date = row[5] |
48
|
|
|
first_date = datetime.strptime(first_date, format).date() |
49
|
|
|
|
50
|
|
|
existing = Schedule.query.filter_by(name=name).first() |
51
|
|
|
|
52
|
|
|
if (not existing and (type == "Income" or type == "Expense") |
53
|
|
|
and (frequency == "Monthly" or frequency == "Quarterly" or frequency == "Yearly" or |
54
|
|
|
frequency == "Weekly" or frequency == "BiWeekly" or frequency == "Onetime")): |
55
|
|
|
schedule = Schedule(name=name, amount=amount, type=type, frequency=frequency, startdate=next_date, |
56
|
|
|
firstdate=first_date) |
57
|
|
|
db.session.add(schedule) |
58
|
|
|
db.session.commit() |
59
|
|
|
elif (existing and (type == "Income" or type == "Expense") |
60
|
|
|
and (frequency == "Monthly" or frequency == "Quarterly" or frequency == "Yearly" or |
61
|
|
|
frequency == "Weekly" or frequency == "BiWeekly" or frequency == "Onetime")): |
62
|
|
|
existing.amount = amount |
63
|
|
|
existing.frequency = frequency |
64
|
|
|
existing.startdate = next_date |
65
|
|
|
existing.type = type |
66
|
|
|
existing.firstdate = first_date |
67
|
|
|
db.session.commit() |
68
|
|
|
except: |
69
|
|
|
pass |
70
|
|
|
|
71
|
|
|
return 0 |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def version(): |
75
|
|
|
# get current python version |
76
|
|
|
pyversion = platform.python_version() |
77
|
|
|
# check for docker env variable |
78
|
|
|
try: |
79
|
|
|
app_version = os.environ['APP_VERSION'] |
80
|
|
|
version = "pycashflow: " + app_version + " :: python: " + pyversion |
81
|
|
|
except KeyError: |
82
|
|
|
# read VERSION file and store string |
83
|
|
|
basedir = os.path.abspath(os.path.dirname(__file__)) |
84
|
|
|
app_version = open(os.path.join(basedir, 'VERSION'), 'r').read() |
85
|
|
|
version = "pycashflow: " + app_version + " :: python: " + pyversion |
86
|
|
|
|
87
|
|
|
return version |
88
|
|
|
|