2023-02-01 14:49:26 +01:00
|
|
|
from base64 import b64encode
|
2022-11-07 17:56:41 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2023-02-01 14:49:26 +01:00
|
|
|
def _convert_to_textstring(lst):
|
|
|
|
return '\n'.join(lst)
|
|
|
|
|
|
|
|
|
|
|
|
def get_html_table_title(parameters):
|
2022-11-07 17:56:41 +01:00
|
|
|
title = get_print_title_str(parameters)
|
2023-02-01 14:49:26 +01:00
|
|
|
return f'<h3>{title}</h3>\n'
|
2022-11-07 17:56:41 +01:00
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2023-02-01 14:49:26 +01:00
|
|
|
def get_html_text(text):
|
|
|
|
return f'<p>{text}</p>\n'
|
2022-11-07 17:56:41 +01:00
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2023-02-01 14:49:26 +01:00
|
|
|
def get_html_header(refresh_rate=10):
|
2022-11-07 17:56:41 +01:00
|
|
|
header = ['<!DOCTYPE html>',
|
|
|
|
'<html>',
|
2022-11-16 11:24:48 +01:00
|
|
|
'<head>',
|
2022-12-08 13:23:21 +01:00
|
|
|
' <link rel="stylesheet" media="only screen and (max-width: 400px)" href="mobile.css" />',
|
|
|
|
' <link rel="stylesheet" media="only screen and (min-width: 401px)" href="desktop.css" />',
|
2022-11-16 11:24:48 +01:00
|
|
|
'</head>',
|
2022-11-08 12:06:00 +01:00
|
|
|
f'<meta http-equiv="refresh" content="{refresh_rate}" >',
|
|
|
|
'<meta charset="utf-8">',
|
2022-11-29 10:42:15 +01:00
|
|
|
'<meta name="viewport" content="width=device-width, initial-scale=1">',
|
2022-11-07 17:56:41 +01:00
|
|
|
'<body>']
|
2023-02-01 14:49:26 +01:00
|
|
|
header = _convert_to_textstring(header)
|
|
|
|
return header
|
2022-11-07 17:56:41 +01:00
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2023-02-01 14:49:26 +01:00
|
|
|
def get_mail_html_header():
|
|
|
|
header = ['<html>',
|
|
|
|
'<head>',
|
|
|
|
'</head>',
|
|
|
|
'<body>']
|
|
|
|
header = _convert_to_textstring(header)
|
|
|
|
return header
|
2022-11-07 17:56:41 +01:00
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2023-02-01 14:49:26 +01:00
|
|
|
def init_html_table():
|
|
|
|
return '<table style="width:100%">\n'
|
2022-11-07 17:56:41 +01:00
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2023-02-01 14:49:26 +01:00
|
|
|
def finish_html_table():
|
|
|
|
return '</table>\n'
|
|
|
|
|
|
|
|
|
|
|
|
def html_footer():
|
2022-11-07 17:56:41 +01:00
|
|
|
footer = ['</body>',
|
|
|
|
'</html>']
|
2023-02-01 14:49:26 +01:00
|
|
|
footer = _convert_to_textstring(footer)
|
|
|
|
return footer
|
|
|
|
|
|
|
|
|
|
|
|
def add_html_image(img_data, img_format='png'):
|
2023-02-16 15:49:07 +01:00
|
|
|
return f"""<br>\n<img width="100%" src="data:image/{img_format};base64, {b64encode(img_data).decode('ascii')}">"""
|
2022-11-07 17:56:41 +01:00
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2023-02-01 14:49:26 +01:00
|
|
|
def get_html_row(items, html_key='td'):
|
|
|
|
row_string = ''
|
2022-11-08 12:06:00 +01:00
|
|
|
default_space = ' '
|
2023-02-01 14:49:26 +01:00
|
|
|
row_string += default_space + '<tr>\n'
|
2022-11-07 17:56:41 +01:00
|
|
|
for item in items:
|
2022-11-08 16:45:21 +01:00
|
|
|
text = item.get('text')
|
2022-11-22 18:06:25 +01:00
|
|
|
if item.get('bold'):
|
|
|
|
text = '<b>' + text + '</b>'
|
|
|
|
if item.get('italic'):
|
|
|
|
text = '<i>' + text + '</i>'
|
2022-11-08 16:45:21 +01:00
|
|
|
tooltip = item.get('tooltip')
|
|
|
|
color = item.get('color')
|
|
|
|
# check for black background of headers (shouldnt happen anymore)
|
2022-11-07 17:56:41 +01:00
|
|
|
color = '#e6e6e6' if color == '#000000' else color
|
2022-11-15 13:44:19 +01:00
|
|
|
hyperlink = item.get('hyperlink')
|
2023-04-17 17:20:35 +02:00
|
|
|
text_str = f'<a href="{hyperlink}"> {text} </a>' if hyperlink else text
|
2022-11-29 10:42:15 +01:00
|
|
|
html_class = item.get('html_class')
|
|
|
|
class_str = f' class="{html_class}"' if html_class else ''
|
2023-04-17 17:20:35 +02:00
|
|
|
row_string += 2 * default_space + f'<{html_key}{class_str} bgcolor="{color}" title="{tooltip}"> {text_str}'\
|
|
|
|
+ f'</{html_key}>\n'
|
2023-02-01 14:49:26 +01:00
|
|
|
row_string += default_space + '</tr>\n'
|
|
|
|
return row_string
|
2022-11-07 17:56:41 +01:00
|
|
|
|
2022-11-22 18:06:25 +01:00
|
|
|
|
2022-11-07 17:56:41 +01:00
|
|
|
def get_print_title_str(parameters):
|
|
|
|
timespan = parameters.get('timespan') * 24 * 3600
|
2022-11-21 15:31:32 +01:00
|
|
|
tdelta_str = str(timedelta(seconds=int(timespan))).replace(', 0:00:00', '')
|
2022-11-07 17:56:41 +01:00
|
|
|
return f'Analysis table of router quality within the last {tdelta_str}'
|