1 # from http.server import BaseHTTPRequestHandler, HTTPServer
5 from plomlib import PlomDB, PlomException, run_server, run_server, PlomServer
9 tmpl = jinja2.Template("""<html>
10 <meta charset="UTF-8">
13 font-family: monospace;
14 background-color: #e0e0ff;
17 font-family: monospace;
21 border: 1px solid black;
26 background-color: black;
35 background-color: white;
37 border-left: 1px solid black;
38 border-right: 1px solid black;
46 background-color: green;
49 font-family: monospace;
81 <tr><th /><th>earned</th><th>progress</th><th>surplus</th></tr >
82 {% for p in progress_bars %}
83 <tr><th>{{p.title}}</th>
84 <td class="countable">{{p.earned|round(2)}}</td>
85 <td class="progressbar">{% if p.time_progress >= 0 %}<div class="time_progress" style="margin-left: {{p.time_progress}}px"></div>{% endif %}<div class="progress" style="background-color: {% if p.success < 0.5 %}red{% elif p.success < 1 %}yellow{% else %}green{% endif %}; width: {{p.success_income_cut}}"></div></td>
86 <td class="progressbar surplusbar"><div class="diff_goal">{{p.diff_goal}}</div><div class="progressbar surplus" style="width: {{p.success_income_bonus}}" /></div></td></tr>
89 <form action="/" method="POST">
91 <tr><th>hourly rate</th><th>worked today</th></tr>
92 <tr><td class="input_container"><input type="number" min="1" class="rate" name="workday_hourly_rate_1" value="{{workday_hourly_rate_1}}"/>€</td><td><input type="number" min="0" class="minutes" name="workday_minutes_worked_1" value="{{workday_minutes_worked_1}}" step="5" /> minutes</td>
93 <tr><td class="input_container"><input type="number" min="1" class="rate" name="workday_hourly_rate_2" value="{{workday_hourly_rate_2}}"/>€</td><td><input type="number" min="0" class="minutes" name="workday_minutes_worked_2" value="{{workday_minutes_worked_2}}" step="5" /> minutes</td>
94 <tr><td class="input_container"><input type="number" min="1" class="rate" name="workday_hourly_rate_3" value="{{workday_hourly_rate_3}}"/>€</td><td><input type="number" min="0" class="minutes" name="workday_minutes_worked_3" value="{{workday_minutes_worked_3}}" step="5" /> minutes</td>
96 <tr><th>yearly income goal</th><td><input type="number" class="year_goal" min="1" name="year_goal" value="{{year_goal}}" />€</td></tr>
97 <tr><th>monthly income goal</th><td class="countable">{{month_goal|round(2)}}€</td></tr>
98 <tr><th>weekly income goal</th><td class="countable">{{week_goal|round(2)}}€</td></tr>
99 <tr><th>workdays per month</th><td class="input_container"><input type="number" class="workdays" min="1" max="28" name="workdays_per_month" value="{{workdays_per_month}}" /></td></tr>
100 <tr><th>workday income goal</th><td class="countable">{{workday_goal|round(2)}}€</td></tr>
101 <tr><th>workdays per week</th><td class="countable">{{workdays_per_week|round(2)}}</td></tr>
103 <input type="submit" name="update" value="update inputs" />
104 <input type="submit" name="finish" value="finish day" />
109 class IncomeDB(PlomDB):
113 self.timestamp_year = 0,
114 self.timestamp_month = 0,
115 self.timestamp_week = 0,
116 self.year_income = 0,
117 self.month_income = 0,
118 self.week_income = 0,
119 self.workday_hourly_rate_1 = 10,
120 self.workday_hourly_rate_2 = 25,
121 self.workday_hourly_rate_3 = 50,
122 self.workday_minutes_worked_1 = 0,
123 self.workday_minutes_worked_2 = 0,
124 self.workday_minutes_worked_3 = 0,
125 self.year_goal = 20000,
126 self.workdays_per_month = 16
127 super().__init__('_income')
129 def read_db_file(self, f):
131 for k, v in d.items():
132 if not hasattr(self, k):
133 raise PlomException("bad key in db: " + k)
137 keys = [k for k in dir(self) if (not k.startswith('_')) and (not callable(getattr(self, k)))]
140 d[k] = getattr(self, k)
144 self.write_text_to_db(json.dumps(self.to_dict()))
147 def __init__(self, title, earned, goal, time_progress=-1):
150 self.time_progress = int(time_progress * 100)
151 success_income = self.earned / goal
152 self.success_income_cut = int(min(success_income, 1.0) * 100)
153 self.success_income_bonus = int(max(success_income - 1.0, 0) * 100)
154 self.success = success_income + 0
155 self.diff_goal = "%.2f€" % (self.earned - goal)
156 if title != "workday":
157 self.diff_goal += "(%.2f€)" % (self.earned - (goal * time_progress))
158 if time_progress >= 0:
160 if time_progress > 0:
161 self.success = success_income / time_progress
163 # class MyServer(BaseHTTPRequestHandler):
164 class IncomeServer(PlomServer):
167 from urllib.parse import parse_qs
169 length = int(self.headers['content-length'])
170 postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
172 db.workday_minutes_worked_1 = int(postvars['workday_minutes_worked_1'][0])
173 db.workday_minutes_worked_2 = int(postvars['workday_minutes_worked_2'][0])
174 db.workday_minutes_worked_3 = int(postvars['workday_minutes_worked_3'][0])
175 db.workday_hourly_rate_1 = int(postvars['workday_hourly_rate_1'][0])
176 db.workday_hourly_rate_2 = int(postvars['workday_hourly_rate_2'][0])
177 db.workday_hourly_rate_3 = int(postvars['workday_hourly_rate_3'][0])
178 db.year_goal = int(postvars['year_goal'][0])
179 db.workdays_per_month = int(postvars['workdays_per_month'][0])
180 if 'finish' in postvars.keys():
181 day_income = (db.workday_minutes_worked_1 / 60.0) * db.workday_hourly_rate_1
182 day_income += (db.workday_minutes_worked_2 / 60.0) * db.workday_hourly_rate_2
183 day_income += (db.workday_minutes_worked_3 / 60.0) * db.workday_hourly_rate_3
184 db.year_income += day_income
185 db.month_income += day_income
186 db.week_income += day_income
187 db.workday_minutes_worked_1 = 0
188 db.workday_minutes_worked_2 = 0
189 db.workday_minutes_worked_3 = 0
191 self.send_response(302)
192 self.send_header('Location', '/')
194 except PlomException as e:
202 today = datetime.datetime.now()
204 if today.year != db.timestamp_year:
205 db.timestamp_year = today.year
206 db.timestamp_month = today.month
210 if today.month != db.timestamp_month:
211 db.timestamp_month = today.month
214 if today.isocalendar()[1] != db.timestamp_week:
215 db.timestamp_week = today.isocalendar()[1]
219 print("Resetting timestamp")
221 day_of_year = today.toordinal() - datetime.date(today.year, 1, 1).toordinal() + 1
222 year_length = 365 + calendar.isleap(today.year)
223 workday_goal = db.year_goal / 12 / db.workdays_per_month
224 workdays_per_week = (db.workdays_per_month * 12) / (year_length / 7)
225 month_goal = db.year_goal / 12
226 week_goal = db.year_goal / (year_length / 7)
227 day_income = (db.workday_minutes_worked_1 / 60.0) * db.workday_hourly_rate_1
228 day_income += (db.workday_minutes_worked_2 / 60.0) * db.workday_hourly_rate_2
229 day_income += (db.workday_minutes_worked_3 / 60.0) * db.workday_hourly_rate_3
230 year_plus = db.year_income + day_income
231 month_plus = db.month_income + day_income
232 week_plus = db.week_income + day_income
233 progress_time_year = day_of_year / year_length
234 progress_time_month = today.day / calendar.monthrange(today.year, today.month)[1]
235 progress_time_week = today.weekday() / 7
236 progress_bars = [ProgressBar("year", year_plus, db.year_goal, progress_time_year),
237 ProgressBar("month", month_plus, month_goal, progress_time_month),
238 ProgressBar("week", week_plus, week_goal, progress_time_week),
239 ProgressBar("workday", day_income, workday_goal)]
241 progress_bars = progress_bars,
242 workday_hourly_rate_1 = db.workday_hourly_rate_1,
243 workday_minutes_worked_1 = db.workday_minutes_worked_1,
244 workday_hourly_rate_2 = db.workday_hourly_rate_2,
245 workday_minutes_worked_2 = db.workday_minutes_worked_2,
246 workday_hourly_rate_3 = db.workday_hourly_rate_3,
247 workday_minutes_worked_3 = db.workday_minutes_worked_3,
248 year_goal = db.year_goal,
249 month_goal = month_goal,
250 week_goal = week_goal,
251 workdays_per_month = db.workdays_per_month,
252 workday_goal = workday_goal,
253 workdays_per_week = workdays_per_week,
256 except PlomException as e:
259 if __name__ == "__main__":
260 run_server(server_port, IncomeServer)