-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
executable file
·156 lines (116 loc) · 4.22 KB
/
server.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
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
"""
klog - Binary Kitchen's log tool
Copyright (c) Binary Kitchen e.V., 2018
Author:
Ralf Ramsauer <[email protected]>
This work is licensed under the terms of the GNU GPL, version 2. See
the LICENSE file in the top-level directory.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
"""
import os
from datetime import datetime
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired
from pyklog.LogEntry import LogEntry
from pyklog.KitchenLog import Config, KitchenLog
from locale import setlocale, LC_ALL
from flask import Flask, render_template, request
setlocale(LC_ALL, 'de_DE.UTF-8')
f_config = os.path.join(os.environ['HOME'], '.config', 'klogrc')
cfg = Config(f_config, needs_email=False, sync=True)
klog = KitchenLog(cfg.namespace, cfg.repo)
app = Flask('klog')
class EntryForm(FlaskForm):
begin = StringField('begin', validators=[DataRequired()])
end = StringField('end')
topic = StringField('topic', validators=[DataRequired()])
appendix = StringField('appendix')
content = StringField('content', validators=[DataRequired()])
def __init__(self, *args, **kwargs):
FlaskForm.__init__(self, *args, **kwargs)
def validate(self):
if not FlaskForm.validate(self):
return False
if not self.end.data or self.end.data == self.begin.data:
self.end.data = 'None'
return True
def convert(self):
entry_raw = \
"""BEGIN: %s
END: %s
TOPIC: %s
APPENDIX: %s
%s
""" % (self.begin.data,
self.end.data,
self.topic.data,
self.appendix.data,
self.content.data)
return LogEntry.sanitise_entry(entry_raw)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/modify', methods=['POST', 'GET'])
def modify():
id = request.args.get('id')
info = None
if id is None:
return list()
try:
id = int(id)
except ValueError:
return list()
entry = klog.get_no(id)
if not entry:
return list()
removals = [x.replace('remove_', '') for x in request.form.keys() if x.startswith('remove_')]
try:
removals = [int(x) for x in removals]
removals = [x for x in removals if x < len(entry.media)]
except ValueError:
removals = list()
entry_form = EntryForm(request.form, csrf_enabled=False)
if entry_form.validate():
entry_raw = entry_form.convert()
if 'remove' in request.form:
entry.remove()
klog.commit('Removed %s' % entry.shortlog)
info = 'Entry successfully removed', 'success'
return render_template('list.html', info=info, content=klog.years_dict())
elif entry_raw == str(entry) and len(removals) == 0:
info = 'Nothing changed', 'warning'
else:
try:
for removal in removals:
entry.remove_media(removal)
entry.reload(entry_raw, True)
info = 'success', 'success'
klog.commit('Modified %s ' % entry.shortlog)
except ValueError as e:
info = str(e), 'danger'
return render_template('modify.html', id=id, entry=entry, info=info)
@app.route('/list')
def list():
return render_template('list.html', content=klog.years_dict())
@app.route('/new', methods=['POST', 'GET'])
def new():
entry_form = EntryForm(request.form, csrf_enabled=False)
info = None
if entry_form.validate():
entry = klog.new_entry(datetime.today())
entry_raw = entry_form.convert()
try:
entry.reload(entry_raw, True)
info = 'success', 'success'
klog.commit('Modified %s ' % entry.shortlog)
return render_template('list.html', info=info, content=klog.years_dict())
except ValueError as e:
info = str(e), 'danger'
template = LogEntry.new(cfg.d_repo, datetime.today())
return render_template('new.html', info=info, template=template)
app.run(debug=True, host='127.0.0.1', port=5000)