Nicer conversation appearance

I added some style to display messages more nicely.
This commit is contained in:
Sasha MOREL 2021-09-05 11:07:28 +02:00
parent 84d8c8bcc8
commit 3dc6d4323b
1 changed files with 69 additions and 7 deletions

76
sms.py
View File

@ -24,7 +24,56 @@ class HTTPServer (http4_server_do_not_use):
def html_header (title = 'No title'):
return ('<html>\n<head>\n\t<title>' + title + '</title>\n<head>\n<body>')
return ('<html>\n<head>\n\t<title>' + title + '</title>\n' + '''\t<style>
body {
margin: 0 auto;
max-width: 800px;
padding: 0 20px;
}
.message {
border: 2px solid #dedede;
background-color: #f1f1f1;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
}
.darker {
border-color: #ccc;
background-color: #ddd;
}
.message::after {
content: "";
clear: both;
display: table;
}
.message img {
float: left;
max-width: 60px;
width: 100%;
margin-right: 20px;
border-radius: 50%;
}
.message img.right {
float: right;
margin-left: 20px;
margin-right:0;
}
.time-right {
float: right;
color: #aaa;
}
.time-left {
float: left;
color: #999;
}
</style>''' + '<head>\n<body>')
def html_title (text):
@ -60,6 +109,22 @@ def html_table (body, table_style = None, cell_style = None):
return (t)
def html_message (msg, sent, time, name, avatar = None):
r = '\n\t<div class="message'
if sent:
r += ' darker'
r += '">\n\t\t<img src="' + str (avatar) + '" alt="' + name + '"'
if sent:
r += ' class="right"'
r += 'style"width:100%;">\n\t\t<p>' + msg.replace ('\n', '<br />') + '</p>\n\t\t<spam class="time-'
if sent:
r += 'left'
else:
r += 'right'
r += '">' + time + '</span>\n\t</div>'
return (r)
def html_form (body, url, post = True):
return ('\n\t<form action="' + url + '"' + (' method="post"' if post else '') + '>' + body + '\n\t</form>')
@ -269,12 +334,9 @@ class Web_Service (BaseHTTPRequestHandler):
self.send_response (200)
self.send_header ('Content-type', 'text/html; charset=UTF-8')
self.end_headers ()
t = []
msgs = ''
for i in sorted (conv, key = lambda item: item [0], reverse = True) [0:2000]:
if i [1]:
t.append ([i [2], '' + i [0]])
else:
t.append (['' + i [0], i [2]])
msgs += html_message (i [2], not i [1], i [0], '' if i [1] else '')
if name is None:
name = number
else:
@ -290,7 +352,7 @@ class Web_Service (BaseHTTPRequestHandler):
'/send'
) +
html_link (self.path, text = 'Refresh') +
html_table (t, table_style = 'table-layout: fixed; border: 1px solid black', cell_style = 'width: 500px; border: 1px solid black') +
msgs +
html_footer ()
).encode ('utf-8'))
else: