Passed
Push — master ( 962bc8...27fb16 )
by William
03:18 queued 36s
created

app.files   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 25

2 Functions

Rating   Name   Duplication   Size   Complexity  
F upload() 0 35 21
A export() 0 17 4
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 dateutil.relativedelta import relativedelta
8
from pathlib import Path
9
from io import TextIOWrapper
10
import csv
11
from .models import Schedule
12
13
14
def export():
15
    try:
16
        engine = db.create_engine(os.environ.get('DATABASE_URL')).connect()
17
    except:
18
        engine = db.create_engine('sqlite:///db.sqlite').connect()
19
20
    # pull the schedule information
21
    df = pd.read_sql('SELECT * FROM schedule;', engine)
22
    df = df.sort_values(by="startdate",
23
                        key=lambda x: np.argsort(index_natsorted(df["startdate"]))).reset_index(drop=True)
24
25
    csv_data = "Name,Amount,Type,Frequency,Next Date\n"
26
    for i in range(len(df.index)):
27
        # Create a CSV string from the data
28
        csv_data += f"{df['name'][i]},{df['amount'][i]},{df['type'][i]},{df['frequency'][i]},{df['startdate'][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 %H:%M:%S'
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
48
            existing = Schedule.query.filter_by(name=name).first()
49
50
            if (not existing and (type == "Income" or type == "Expense")
51
                    and (frequency == "Monthly" or frequency == "Quarterly" or frequency == "Yearly" or
52
                         frequency == "Weekly" or frequency == "BiWeekly" or frequency == "Onetime")):
53
                schedule = Schedule(name=name, amount=amount, type=type, frequency=frequency, startdate=next_date)
54
                db.session.add(schedule)
55
                db.session.commit()
56
            elif (existing and (type == "Income" or type == "Expense")
57
                    and (frequency == "Monthly" or frequency == "Quarterly" or frequency == "Yearly" or
58
                         frequency == "Weekly" or frequency == "BiWeekly" or frequency == "Onetime")):
59
                existing.amount = amount
60
                existing.frequency = frequency
61
                existing.startdate = next_date
62
                existing.type = type
63
                db.session.commit()
64
        except:
65
            pass
66
67
    return 0
68