-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
184 lines (149 loc) · 4.22 KB
/
utils.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from operator import ge, lt
from datetime import datetime, timedelta
from flask import flash
from flowapp.constants import (
COMP_FUNCS,
TIME_YEAR,
TIME_US,
TIME_STMP,
TIME_FORMAT_ARG,
RULE_TYPES_DICT,
FORM_TIME_PATTERN,
)
def other_rtypes(rtype):
"""
get rtype and return list of remaining rtypes
for example get ipv4 and return [ipv6, rtbh]
"""
result = list(RULE_TYPES_DICT.keys())
try:
result.remove(rtype)
except ValueError:
pass
return result
def output_date_format(json_request_data, pref_format=TIME_YEAR):
"""
prefer user setting from parameter, if the parameter is not set
then use the prefered format computed from input date
"""
if not json_request_data:
return pref_format
if TIME_FORMAT_ARG in json_request_data and json_request_data[TIME_FORMAT_ARG]:
return json_request_data[TIME_FORMAT_ARG]
else:
return pref_format
def parse_api_time(apitime):
"""
check if the api time is in US, EU or timestamp format
:param apitime: string with date and time
:returns: datetime, prefered format
"""
apitime = str(apitime)
try:
return (
round_to_ten_minutes(datetime.strptime(apitime, FORM_TIME_PATTERN)),
TIME_US,
)
except ValueError:
mytime = False
try:
return round_to_ten_minutes(webpicker_to_datetime(apitime)), TIME_YEAR
except ValueError:
mytime = False
try:
return round_to_ten_minutes(webpicker_to_datetime(apitime, TIME_US)), TIME_US
except ValueError:
mytime = False
try:
return round_to_ten_minutes(datetime.fromtimestamp(int(apitime))), TIME_STMP
except OverflowError:
mytime = False
except ValueError:
mytime = False
return False
def quote_to_ent(comment):
"""
Convert all " to "
Used for comment sanitize / because of tooltip in dashboard break when quotes are unescaped
:param comment: string to be sanitized
:return: string
"""
if comment:
return comment.replace('"', """)
def webpicker_to_datetime(webtime, format=TIME_YEAR):
"""
convert 'YYYY/MM/DD HH:mm' to datetime
"""
if format == TIME_YEAR:
formating_string = "%Y/%m/%d %H:%M"
else:
formating_string = "%m/%d/%Y %H:%M"
return datetime.strptime(webtime, formating_string)
def datetime_to_webpicker(python_time, format=TIME_YEAR):
"""
convert datetime to 'YYYY/MM/DD HH:mm' string
"""
if format == TIME_YEAR:
formating_string = "%Y/%m/%d %H:%M"
else:
formating_string = "%m/%d/%Y %H:%M"
return datetime.strftime(python_time, formating_string)
def get_state_by_time(python_time):
"""
returns state for rule based on given time
if given time is in the past returns 2 (withdrawed rule)
else returns 1
:param python_time:
:return: integer rstate
"""
present = datetime.now()
if python_time <= present:
return 2
else:
return 1
def round_to_ten_minutes(python_time):
"""
Round given time to nearest ten minutes
:param python_time: datetime
:return: datetime
"""
python_time += timedelta(minutes=5)
python_time -= timedelta(
minutes=python_time.minute % 10,
seconds=python_time.second,
microseconds=python_time.microsecond,
)
return python_time
def flash_errors(form):
"""
Flash all error messages
:param form: WTForm object
:return: none
"""
for field, errors in form.errors.items():
for error in errors:
flash("Error in the %s field - %s" % (getattr(form, field).label.text, error))
def active_css_rstate(rtype, rstate):
"""
returns dict with rstates as keys and css class value
:param rstate: string
:return: dict
"""
return {
"active": "",
"expired": "",
"all": "",
"ipv4": "",
"ipv6": "",
"rtbh": "",
rtype: "active",
rstate: "active",
}
def get_comp_func(rstate="active"):
try:
comp_func = COMP_FUNCS[rstate]
except IndexError:
comp_func = None
except KeyError:
comp_func = None
return comp_func