-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspp_manager.py
160 lines (123 loc) · 5.18 KB
/
spp_manager.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
import coloredlogs, logging, datetime, json, traceback, sys
from spp import SPP
from users import Users
from flask_table import Table, Col
logger = logging.getLogger(__name__)
coloredlogs.install(level='INFO')
class SppManager:
def __init__(self):
self.__service_protection_periods = []
self.__load()
def get_spp_total(self):
return len(self.__service_protection_periods)
def get_spp_active(self):
active_spp = 0
for spp in self.__service_protection_periods:
if spp.is_spp():
active_spp += 1
return active_spp
def get_spp_unactive(self):
unactive_spp = 0
for spp in self.__service_protection_periods:
if not spp.is_spp():
unactive_spp += 1
return unactive_spp
def is_spp(self, users=None, api_key=None):
if users == None:
for spp in self.__service_protection_periods:
if spp.is_spp():
return True
else:
for spp in self.__service_protection_periods:
if spp.is_spp() and users.get_level(api_key) >= spp.get_priority():
return True
return False
def add_spp(self, spp_dict, users=None):
if users == None:
for spp_json in spp_dict.get("spp"):
spp = SPP()
load_errs = spp.load_spp(spp_json)
if load_errs:
return load_errs
self.__service_protection_periods.append(spp)
else:
for spp_json in spp_dict.get("spp"):
spp = SPP()
print(users.get_user(spp_dict.get("api_key")))
load_errs = spp.load_spp(spp_json, users.get_user(spp_dict.get("api_key")))
if load_errs:
return load_errs
if spp_json.get("priority") >= users.get_level(spp_dict.get("api_key")):
self.__service_protection_periods.append(spp)
else:
return "User [" + users.get_user(spp_dict.get("api_key")) + "] level [" + users.get_level(spp_dict.get("api_key")) + "] is not authorised to create an SPP for that Priority Level [" + spp_json.get("priority") + "]"
self.save()
return ""
def set_spp(self, api_key, uuid):
logger.info("Inverting the enabled status for spp [" + uuid + "] with user [" + Users().get_user(api_key) + "]")
for spp in self.__service_protection_periods:
if spp.get_uuid() == uuid and Users().get_level(api_key) <= spp.get_priority():
spp.invert_status()
return True
return False
def remove_spp(self):
return "todo"
def export(self):
exports = []
for spp in self.__service_protection_periods:
export_json = {}
export_json["username"] = spp.get_username()
export_json["enabled"] = spp.get_enabled()
export_json["priority"] = spp.get_priority()
export_json["start_time"] = spp.get_start_time().isoformat()
export_json["end_time"] = spp.get_end_time().isoformat()
export_json["uuid"] = spp.get_uuid()
exports.append(export_json)
spp_export = {}
spp_export["spp"] = exports
return spp_export
def save(self):
try:
with open('json/spp.json', 'w') as path:
json.dump(self.export(), path, sort_keys=True, indent=4)
except:
logger.warning("Couldn't save SPP json. Continuing...")
traceback.print_exc(file=sys.stdout)
def __load(self):
try:
with open('json/spp.json', 'r') as path:
imports = json.load(path)
errs = self.add_spp(imports)
if errs:
logger.warning(errs)
except:
logger.warning("Couldn't load SPP json. Continuing...")
traceback.print_exc(file=sys.stdout)
def get_spp_table(self):
items = []
for spp in self.__service_protection_periods:
items.append(Item(spp.get_username(), str(spp.get_priority()), str(spp.get_start_time()), str(spp.get_end_time()), str(spp.get_enabled()), str(spp.is_spp())))
table = ItemTable(items)
return table.__html__()
def get_active_button(self):
if self.is_spp():
return '<button type="button" class="btn btn-danger">SPP Enabled</button>'
return '<button type="button" class="btn btn-success">SPP Disabled</button>'
# Declare your table
class ItemTable(Table):
classes = ['table table-dark']
name = Col('Username')
level = Col('Level')
start_time = Col('Start Time')
end_time = Col('End Time')
enabled = Col('Enabled')
active = Col('Active')
# Get some objects
class Item(object):
def __init__(self, name, level, start_time, end_time, enabled, active):
self.name = name
self.level = level
self.start_time = start_time
self.end_time = end_time
self.enabled = enabled
self.active = active