Passed
Push — master ( 696e95...c8f1df )
by William
01:26
created

app.files.version()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
11
12
def export():
13
    try:
14
        engine = db.create_engine(os.environ.get('DATABASE_URL')).connect()
15
    except:
16
        engine = db.create_engine('sqlite:///db.sqlite').connect()
17
18
    # pull the schedule information
19
    df = pd.read_sql('SELECT * FROM schedule;', engine)
20
    df = df.sort_values(by="startdate",
21
                        key=lambda x: np.argsort(index_natsorted(df["startdate"]))).reset_index(drop=True)
22
23
    csv_data = "Name,Amount,Type,Frequency,Next Date\n"
24
    for i in range(len(df.index)):
25
        # Create a CSV string from the data
26
        csv_data += f"{df['name'][i]},{df['amount'][i]},{df['type'][i]},{df['frequency'][i]},{df['startdate'][i]}\n"
27
28
    return csv_data
29
30
31
def upload(csv_file):
32
    csv_file = TextIOWrapper(csv_file, encoding='utf-8')
33
    csv_reader = csv.reader(csv_file, delimiter=',')
34
    next(csv_reader)
35
36
    format = '%Y-%m-%d %H:%M:%S'
37
    for row in csv_reader:
38
        try:
39
            name = row[0]
40
            amount = float(row[1])
41
            type = row[2]
42
            frequency = row[3]
43
            next_date = row[4]
44
            next_date = datetime.strptime(next_date, format).date()
45
46
            existing = Schedule.query.filter_by(name=name).first()
47
48
            if (not existing and (type == "Income" or type == "Expense")
49
                    and (frequency == "Monthly" or frequency == "Quarterly" or frequency == "Yearly" or
50
                         frequency == "Weekly" or frequency == "BiWeekly" or frequency == "Onetime")):
51
                schedule = Schedule(name=name, amount=amount, type=type, frequency=frequency, startdate=next_date)
52
                db.session.add(schedule)
53
                db.session.commit()
54
            elif (existing and (type == "Income" or type == "Expense")
55
                    and (frequency == "Monthly" or frequency == "Quarterly" or frequency == "Yearly" or
56
                         frequency == "Weekly" or frequency == "BiWeekly" or frequency == "Onetime")):
57
                existing.amount = amount
58
                existing.frequency = frequency
59
                existing.startdate = next_date
60
                existing.type = type
61
                db.session.commit()
62
        except:
63
            pass
64
65
    return 0
66
67
68
def version():
69
    # read VERSION file and store string
70
    basedir = os.path.abspath(os.path.dirname(__file__))
71
    version = open(os.path.join(basedir, 'VERSION'), 'r').read()
72
73
    return version
74