-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledgerweb.py
143 lines (110 loc) · 4.25 KB
/
ledgerweb.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
from flask import Flask, render_template, request, redirect, url_for
import subprocess
import os.path
import yaml
import time
import fileinput
if os.path.exists('config.yaml'):
configfile = 'config.yaml'
else:
configfile = 'defaultconfig.yaml'
with open(configfile, 'r') as f:
try:
config = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)
sync_down = "rclone sync {rname}:{rpath}/finance.ledger data/".format(rname=config['remotename'], rpath=config['remotepath'])
sync_up = "rclone sync data/finance.ledger {rname}:{rpath}".format(rname=config['remotename'], rpath=config['remotepath'])
app = Flask(__name__)
@app.route('/input')
def input():
return render_template('input.html', ac=config['accounts'])
@app.route('/submit', methods = ['GET'])
def submit():
transdate = request.args.get('transdate')
transclear = request.args.get('transclear')
transrep = request.args.get('transrep')
fromacc = request.args.get('fromacc')
fromamount = request.args.get('fromamount')
toacc = request.args.get('toacc')
toamount = request.args.get('toamount')
r = subprocess.call(sync_down, shell = True)
if transclear == "on":
c = " * "
else:
c = " "
if fromamount:
fromamount = str(fromamount) + " EUR"
else:
fromamount = ""
if toamount:
toamount = str(toamount) + " EUR"
else:
toamount = ""
transdate = transdate.replace("-","/")
ledgerfile = open("./data/finance.ledger","a")
ledgerfile.write(transdate + c + transrep + "\n")
ledgerfile.write(" ; generated by ledgerweb\n")
ledgerfile.write(" " + fromacc + " " + fromamount + "\n")
ledgerfile.write(" " + toacc + " " + toamount + "\n\n")
ledgerfile.close()
r = subprocess.call(sync_up, shell = True)
return redirect(url_for('input'))
@app.route('/reports')
def reports():
r = subprocess.call(sync_down, shell = True)
reps = []
c = 1
for r in config['reports']:
reps.append((c,r['name'],subprocess.check_output(r['cmd'],shell=True).decode("utf-8")))
c += 1
return render_template('reports.html', reps=reps)
@app.route('/')
def index():
fa = []
for f in config['favs']:
fa.append(f['name'])
return render_template('index.html', favs=fa)
@app.route('/fsubmit', methods = ['GET'])
def fsubmit():
amount = request.args.get('famount')
rep = request.args.get('fselect')
r = subprocess.call(sync_down, shell = True)
fav = [x for x in config['favs'] if x['name'] == rep][0]
ledgerfile = open('data/finance.ledger', 'a')
ledgerfile.write("{date} {rep}\n".format(date=time.strftime("%Y/%m/%d"), rep=rep))
ledgerfile.write(" ; generated by ledgerweb\n")
ledgerfile.write(" {f}\n".format(f=fav['from']))
ledgerfile.write(" {to} {amount} EUR\n\n".format(to=fav['to'], amount=amount))
ledgerfile.close()
r = subprocess.call(sync_up, shell = True)
return redirect(url_for('index'))
@app.route('/clear')
def clear():
r = subprocess.call(sync_down, shell=True)
uc_cmd = "ledger reg -U -f data/finance.ledger Assets --register-format \"- rec: %P\n date: %D\n amount: %t\n\""
ledger_out = subprocess.check_output(uc_cmd, shell=True).decode('utf-8')
uncleared = yaml.safe_load(ledger_out)
return render_template('clear.html', uncleared=uncleared)
@app.route('/csubmit', methods = ['GET'])
def csubmit():
to_clear = [x.strip() for x in request.args.get('cselect').split("|")]
fs = "{date} {rep}".format(date=to_clear[0],rep=to_clear[1])
ts = "{date} * {rep}".format(date=to_clear[0],rep=to_clear[1])
with open('data/finance.ledger', 'r') as file:
data = file.read()
data = data.replace(fs,ts)
with open('data/finance.ledger', 'w') as file:
file.write(data)
r = subprocess.call(sync_up, shell=True)
return redirect(url_for('clear'))
@app.route('/reloadconfig')
def reloadconfig():
with open(configfile, 'r') as f:
try:
config = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=False, host=config['hostip'], port=config['hostport'])