- with open(lock_file, "w+"): pass
- with open(db_file, "w") as f:
- json.dump(db, f)
- os.remove(lock_file)
- else:
- self.send_response(400)
- self.end_headers()
- self.wfile.write(bytes("Sorry, lock file!", "utf-8"))
- return
- day_of_year = today.toordinal() - datetime.date(today.year, 1, 1).toordinal() + 1
- year_length = 365 + calendar.isleap(today.year)
- workday_goal = db["year_goal"] / 12 / db["workdays_per_month"]
- workdays_per_week = (db["workdays_per_month"] * 12) / (year_length / 7)
- month_goal = db["year_goal"] / 12
- week_goal = db["year_goal"] / (year_length / 7)
- def success_color(success):
- if success < 0.5:
- return "red";
- elif success < 1:
- return "yellow";
- else:
- return "green"
- def progressbar(title, earned, goal, time_progress=-1):
- time_progress_indicator = ""
- success_income = earned / goal
- success_income_cut = min(success_income, 1.0)
- success_income_bonus = max(success_income - 1.0, 0)
- success = success_income + 0
- diff_goal = "%.2f€" % (earned - goal)
- if title != "workday":
- diff_goal += "(%.2f€)" % (earned - (goal * time_progress))
- if time_progress >= 0:
- success = 1
- if time_progress > 0:
- success = success_income / time_progress
- time_progress_indicator = "<div class=\"time_progress\" style=\"margin-left: %spx\"></div>" % int(time_progress * 100)
- return "<tr><th>%s</th>" \
- "<td class=\"countable\">%.2f€</td>" \
- "<td class=\"progressbar\">%s<div class=\"progress\" style=\"background-color: %s; width: %s\"></div></td>" \
- "<td class=\"progressbar surplusbar\"><div class=\"diff_goal\">%s</div><div class=\"progress surplus\" style=\"width: %s\"></div></td></tr>" % (
- title, earned, time_progress_indicator, success_color(success), int(success_income_cut * 100), diff_goal, int(success_income_bonus * 100))
- day_income = (db["workday_minutes_worked_1"] / 60.0) * db["workday_hourly_rate_1"]
- day_income += (db["workday_minutes_worked_2"] / 60.0) * db["workday_hourly_rate_2"]
- day_income += (db["workday_minutes_worked_3"] / 60.0) * db["workday_hourly_rate_3"]
- year_plus = db["year_income"] + day_income
- month_plus = db["month_income"] + day_income
- week_plus = db["week_income"] + day_income
- progress_time_year = day_of_year / year_length
- progress_time_month = today.day / calendar.monthrange(today.year, today.month)[1]
- progress_time_week = today.weekday() / 7
- year_line = progressbar("year", year_plus, db["year_goal"], progress_time_year)
- month_line = progressbar("month", month_plus, month_goal, progress_time_month)
- week_line = progressbar("week", week_plus, week_goal, progress_time_week)
- day_line = progressbar("workday", day_income, workday_goal)
- body = year_line + "\n" + month_line + "\n" + week_line + "\n" + day_line
- page = header + body + footer % (
- db["workday_hourly_rate_1"], db["workday_minutes_worked_1"],
- db["workday_hourly_rate_2"], db["workday_minutes_worked_2"],
- db["workday_hourly_rate_3"], db["workday_minutes_worked_3"],
- db["year_goal"],
- month_goal,
- week_goal,
- db["workdays_per_month"],
- workday_goal,
- workdays_per_week,
- )
- self.wfile.write(bytes(page, "utf-8"))
+ db.write_db()
+ day_of_year = today.toordinal() - datetime.date(today.year, 1, 1).toordinal() + 1
+ year_length = 365 + calendar.isleap(today.year)
+ workday_goal = db.year_goal / 12 / db.workdays_per_month
+ workdays_per_week = (db.workdays_per_month * 12) / (year_length / 7)
+ month_goal = db.year_goal / 12
+ week_goal = db.year_goal / (year_length / 7)
+ day_income = (db.workday_minutes_worked_1 / 60.0) * db.workday_hourly_rate_1
+ day_income += (db.workday_minutes_worked_2 / 60.0) * db.workday_hourly_rate_2
+ day_income += (db.workday_minutes_worked_3 / 60.0) * db.workday_hourly_rate_3
+ year_plus = db.year_income + day_income
+ month_plus = db.month_income + day_income
+ week_plus = db.week_income + day_income
+ progress_time_year = day_of_year / year_length
+ progress_time_month = today.day / calendar.monthrange(today.year, today.month)[1]
+ progress_time_week = today.weekday() / 7
+ progress_bars = [ProgressBar("year", year_plus, db.year_goal, progress_time_year),
+ ProgressBar("month", month_plus, month_goal, progress_time_month),
+ ProgressBar("week", week_plus, week_goal, progress_time_week),
+ ProgressBar("workday", day_income, workday_goal)]
+ homepage = self.apps['income_progress'] if hasattr(self, 'apps') else self.homepage
+ page = jinja2.Template(tmpl).render(
+ homepage = homepage,
+ progress_bars = progress_bars,
+ workday_hourly_rate_1 = db.workday_hourly_rate_1,
+ workday_minutes_worked_1 = db.workday_minutes_worked_1,
+ workday_hourly_rate_2 = db.workday_hourly_rate_2,
+ workday_minutes_worked_2 = db.workday_minutes_worked_2,
+ workday_hourly_rate_3 = db.workday_hourly_rate_3,
+ workday_minutes_worked_3 = db.workday_minutes_worked_3,
+ year_goal = db.year_goal,
+ month_goal = month_goal,
+ week_goal = week_goal,
+ workdays_per_month = db.workdays_per_month,
+ workday_goal = workday_goal,
+ workdays_per_week = workdays_per_week,
+ )
+ self.send_HTML(page)
+ except PlomException as e:
+ self.fail_400(e)
+