This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.py
107 lines (80 loc) · 2.22 KB
/
helpers.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
#!/usr/bin/python3
from datetime import date, datetime
from hashlib import sha256
from dateutil.relativedelta import relativedelta
def empty_strings_to_none(dictionary):
new_dictionary = {}
for key, value in dictionary.items():
if value == "":
new_dictionary[key] = None
else:
new_dictionary[key] = value
return new_dictionary
def current_month_num():
"""Returns the current month number as an integer."""
return int(datetime.now().strftime("%m"))
def next_month(current_month):
if current_month == 12:
return 1
else:
return current_month + 1
def previous_month(current_month):
if current_month == 1:
return 12
else:
return current_month - 1
def last_day_of_month(input_date):
next_month = input_date + relativedelta(months=1)
first_day_of_next_month = date(next_month.year, next_month.month, 1)
return first_day_of_next_month - relativedelta(days=1)
def month_input_to_date(month_input, set_to_last_day=False):
if month_input is None:
return None
elif set_to_last_day is False:
return datetime.strptime(month_input, "%Y-%m").date()
else:
return last_day_of_month(
datetime.strptime(month_input, "%Y-%m").date()
)
def date_to_month_input(date_obj):
if date_obj is None:
return None
else:
return date_obj.strftime("%Y-%m")
def month_count(start_date, end_date):
count = 0
one_month = relativedelta(months=1)
if start_date > end_date:
return 0
while True:
count += 1
if (
start_date.year == end_date.year
and start_date.month == end_date.month
):
break
else:
start_date += one_month
return count
def checkbox_to_boolean(value):
if value == "on":
return True
else:
return False
def hash(value, salt=""):
hashed_value = sha256((value + salt).encode()).hexdigest()
return hashed_value
months = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December",
}