panasonic-log-reader/www/index.py
2017-08-25 16:37:26 +03:00

93 lines
4.0 KiB
Python
Executable File

import os, sys, \
pymysql, urlparse3
def application(environ, start_response):
#sys.stdout._encoding = 'utf-8'
status = '200 OK'
# проверяем наличие параметров в запросе и всяко-разно реагируем
if environ['QUERY_STRING'] == '':
output = bytes((header() + body() + footer()).encode('utf8'))
elif environ['QUERY_STRING'].split('&'):
paramDict = {item.split('=')[0]: item.split('=')[1] for item in environ['QUERY_STRING'].split('&')}
if paramDict.get('query_type') == 'internal':
output = bytes((header() + body() + getInternalNumbers() + footer()).encode('utf8'))
elif paramDict.get('query_type') == 'external':
output = bytes((header() + body() + getCOline() + footer()).encode('utf8'))
else:
output = bytes((header() + body() + footer()).encode('utf8'))
response_headers = [('Content-type', 'text/html;charset=utf-8'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
def header():
txtHeader = '<html><head><meta charset="utf-8"><link rel="shortcut icon" href="favicon.png" >\n' \
'<link rel="icon" type="image/png" href="favicon.png" >\n' \
'<link href="css/layout.css" rel="stylesheet" type="text/css" />\n' \
'<title>Телефоны</title>\n' \
'<link href="css/menu.css" rel="stylesheet" type="text/css" /></head><body>\n'
return txtHeader
def footer():
txtFooter = '</td></tr></table></body></html>\n'
return txtFooter
def body():
txtBody = '<table><tr><td><h2 align=left>ТЕЛЕФОНЫ</h2></td></tr>\n' \
'<tr><td>{}</td></tr><tr><td>\n'.format(menu())
return txtBody
def menu():
#<div class="container">
txtMenu = '<div class="container"><ul id="nav"></li>\n' \
'<li><a class="hsubs" href="#">Справочник</a><ul class="subs">\n' \
'<li><a href="?query_type=external">Городские телефоны</a></li>\n' \
'<li><a href="?query_type=internal">Внутренние</a></li></ul></li>\n' \
'<li><a class="hsubs" href="">Отчёты</a><ul class="subs"><li><a href="#">Звонки по отделам</a></li>\n' \
'</ul></div>\n'
return txtMenu
def getInternalNumbers():
conn = connectDB()
c = conn.cursor()
order = 'int_number'
result = '<h4 align=center>Список внутренних телефонов</h4>'
qwr = "SELECT * FROM int_number ORDER by " + order
c.execute(qwr)
listHeader = '<table class="table_dark"><tr><th>№ п/п</th>\n' \
'<th><a href=index.py?query_type=internal&order=int_number>Телефон</a></th>\n' \
'<th><a href=index.py?query_type=internal&order=fio>ФИО</a></th>\n' \
'<th>Описание<th>Электро-почта</th></tr>\n'
result = result + listHeader
for row in c.fetchall():
rowData = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n" % (row[0], row[1], row[2], row[4], row[3])
result = result + rowData
return (result + "</table>\n")
def getCOline():
conn = connectDB()
c = conn.cursor()
order = 'ext_co_line'
result = '<h4 align=center>Список внешних линий</h4>'
qwr = "SELECT * FROM ext_co_line ORDER by " + order
c.execute(qwr)
listHeader = '<table class="table_dark"><tr><th>№ п/п</th>\n' \
'<th>Линия</a></th>\n' \
'<th>Номер телефона</th>\n' \
'<th>Описание</th></tr>\n'
result = result + listHeader
for row in c.fetchall():
rowData = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (row[0], row[1], row[2], row[3])
result = result + rowData
return (result + "</table>\n")
def connectDB():
c = pymysql.connect(
db='ats_test',
user='ats',
passwd='',
host='kis',
charset='utf8')
return c