panasonic-log-reader/www/index.py

134 lines
6.3 KiB
Python
Raw Normal View History

2017-08-28 15:39:17 +03:00
import os, sys, pymysql, urlparse3
2017-08-21 15:11:10 +03:00
2017-08-25 16:37:26 +03:00
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'))
2017-08-28 15:39:17 +03:00
elif paramDict.get('query_type') == 'report':
output = bytes((header() + body() + ReportForm() + ReportData(environ) + footer()).encode('utf8'))
2017-08-25 16:37:26 +03:00
else:
output = bytes((header() + body() + footer()).encode('utf8'))
2017-08-21 15:11:10 +03:00
2017-08-25 16:37:26 +03:00
response_headers = [('Content-type', 'text/html;charset=utf-8'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
2017-08-21 15:11:10 +03:00
2017-08-25 16:37:26 +03:00
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():
2017-08-28 15:39:17 +03:00
txtFooter = '</body></html>\n'
2017-08-25 16:37:26 +03:00
return txtFooter
def body():
txtBody = '<table><tr><td><h2 align=left>ТЕЛЕФОНЫ</h2></td></tr>\n' \
2017-08-28 15:39:17 +03:00
'<tr><td>{}</td></tr></table>\n'.format(menu())
2017-08-25 16:37:26 +03:00
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' \
2017-08-28 15:39:17 +03:00
'<li><a class="hsubs" href="">Отчёты</a><ul class="subs"><li><a href="?query_type=report">Звонки по отделам</a></li>\n' \
2017-08-25 16:37:26 +03:00
'</ul></div>\n'
return txtMenu
def getInternalNumbers():
conn = connectDB()
c = conn.cursor()
order = 'int_number'
result = '<h4 align=center>Список внутренних телефонов</h4>'
2017-08-21 15:11:10 +03:00
qwr = "SELECT * FROM int_number ORDER by " + order
c.execute(qwr)
2017-08-25 16:37:26 +03:00
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
2017-08-21 15:11:10 +03:00
for row in c.fetchall():
2017-08-25 16:37:26 +03:00
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")
2017-08-21 15:11:10 +03:00
2017-08-25 16:37:26 +03:00
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")
2017-08-21 15:11:10 +03:00
2017-08-28 15:39:17 +03:00
def ReportForm():
txtReportForm = '<h3 align=center>Отчёт по звонкам</h3><table><tr valign=top><td>' \
'<form><input type=hidden name="query_type" value="report">' \
'<fieldset class="report">' \
'<label>Номер<input type="text" name="int_number"></label>' \
'<label>Период с<input type="date" name="date_begin"></label>' \
'<label>По<input type="date" name="date_end"></label>' \
'</fieldset>' \
'<fieldset class="report-action">' \
'<input class="btn" type="submit" name="submit" value="Выбрать">' \
'</fieldset></form></td>'
return txtReportForm
def ReportData(environ):
paramDict = {item.split('=')[0]: item.split('=')[1] for item in environ['QUERY_STRING'].split('&')}
numbers = paramDict.get('int_number')
date_begin = paramDict.get('date_begin')
date_end = paramDict.get('date_end')
conn = connectDB()
c = conn.cursor()
order = 'call_date'
result = '<td><h4 align=left>Список звонков с номера {}</h4>'.format(numbers)
qwr = "SELECT * FROM ats.cdr where int_number=\'{}\' AND call_date BETWEEN CAST(\'{}\' AS DATE) AND CAST(\'{}\' AS DATE) LIMIT 1000".format(numbers, date_begin, date_end)
c.execute(qwr)
listHeader = '<table class="table_dark"><tr>' \
'<th>№ п/п</th><th>Дата</th><th>Время</th><th>Внут.номер</th><th>Внеш.линия</th><th>Вызываемый номер</th>' \
'<th>Ring</th><th>Длительность</th><th>АСС</th><th>Код звонка</th><th>Направление</th><tr>\n'
result = result + listHeader
for row in c.fetchall():
rowData = '<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>'\
% (row[0], row[1], row[2], row[3], row[4], row[5], row[5], row[7], row[9], row[9], row[10])
result = result + rowData
return (result + "</table></td></tr></table>\n" + qwr)
2017-08-25 16:37:26 +03:00
def connectDB():
c = pymysql.connect(
db='ats_test',
user='ats',
2017-08-28 15:40:00 +03:00
passwd='',
2017-08-25 16:37:26 +03:00
host='kis',
charset='utf8')
2017-08-28 15:39:17 +03:00
return c