You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
11 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os, MySQLdb, binascii
from mod_python import apache, util
from htmlgen import HTMLgen
DB_NAME = 'bc'
DB_HOST = 'localhost'
DB_USER = 'bc'
DB_PASSWORD = 'super-secret-password'
ITEMS_PER_PAGE = 25
def _init(req):
sys.path.append(os.path.dirname(req.filename) + "/include")
def _pageTitle(req, meta='', title=''):
if len(title) > 0:
title += ' - '
if len(meta) > 0:
meta = '<meta %s>\n' % meta
page = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n'\
'<html lang="ru">\n'\
'<head>\n'\
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n'\
'%s' % meta +\
'<title>%sBusiness cards</title>\n' %title +\
'</head>\n'\
'<body>\n'\
'<font face="Calibri, Arial, Verdana, Sans">\n'\
'<H1><table width="100%" bgcolor="#336299"><tr><td><p align="center"><font color="white">Business cards</font></p></td></tr></table></H1>\n'
return page
def _pageFooter(req):
page = '<br><hr><p align="center">©2010 Сергей Морозов, ОАО \"Промышленно - строительное товарищество\"</p>\n'\
'</font>\n'\
'</body>\n'\
'</html>\n'
return page
def _pageImpForm(req):
page = '<H2>Импорт записей из .vcf файлов</H2>\n'\
'<form method="post" enctype="multipart/form-data" action="%s">\n'\
'Выберите файл: \n'\
'<input type="file" name="file" id="file" size="40">\n'\
'<input type="submit" value="Отправить"\n>'\
'</form>\n'\
'<br>\n'\
'<a href="delete">Удаление ранее загруженных записей</a>\n'\
% req.uri
return page
def _pageDone(req, additional = ''):
page = 'Операция завершена.<br>%s' % additional
return page
def _pageDeleteForm(req, list):
liststring = ''
for i in xrange(len(list)):
liststring += '<option>%s\n' % list[i]
page = '<H2>Удаление информации из базы данных</H2>\n'\
'<form method="post" enctype="multipart/form-data" action="%s">\n'\
'Выберите раздел: \n'\
'<select name="filename"\n>'\
'%s'\
'</select>\n'\
'<input type="submit" value="Удалить"\n>'\
'</form>'\
'<br>\n'\
'<a href="imp">Импорт записей из файлов .vcf</a>\n'\
% (req.uri, liststring)
return page
def _pageFileSelect(req, list):
page = '<H2 align="center">Выберите раздел:</H2>\n'\
'<p align="center">'
for i in xrange(len(list)):
page += '<a href="view?filename=%s"><b>%s</b></a><br>\n' % (list[i], list[i])
page += '</p>'
return page
def _pagePrintItem(req, record):
key = str(record[0])
FirstName = record[2]
LastName = record[3]
City = record[4]
Country = record[5]
Address = record[6]
ZipCode = record[7]
State = record[8]
Email = record[9]
WebSite = record[10]
Title = record[11]
Company = record[12]
Phone = record[13]
Phone2 = record[14]
Fax = record[15]
CellPhone = record[16]
textfields = ''
if FirstName != None: textfields += '<b>First name:</b> %s<br>\n' % FirstName
if LastName != None: textfields += '<b>Last name:</b> %s<br>\n' % LastName
if City != None: textfields += '<b>City:</b> %s<br>\n' % City
if Country != None: textfields += '<b>Country:</b> %s<br>\n' % Country
if Address != None: textfields += '<b>Address:</b> %s<br>\n' % Address
if ZipCode != None: textfields += '<b>Zip code:</b> %s<br>\n' % ZipCode
if State != None: textfields += '<b>State:</b> %s<br>\n' % State
if Email != None: textfields += '<b>Email:</b> <a href="mailto:%s">%s</a><br>\n' % (Email, Email)
if WebSite != None:
if WebSite[0:7] != 'http://' and WebSite[0:8] != 'https://' and WebSite[0:6] != 'ftp://':
WebSite = 'http://%s' % WebSite
textfields += '<b>Web site:</b> <a href="%s">%s</a><br>\n' % (WebSite, WebSite)
if Title != None: textfields += '<b>Title:</b> %s<br>\n' % Title
if Company != None: textfields += '<b>Company:</b> %s<br>\n' % Company
if Phone != None: textfields += '<b>Phone:</b> %s<br>\n' % Phone
if Phone2 != None: textfields += '<b>Phone2:</b> %s<br>\n' % Phone2
if Fax != None: textfields += '<b>Fax:</b> %s<br>\n' % Fax
if CellPhone != None: textfields += '<b>CellPhone:</b> %s<br>\n' % CellPhone
page = '<table width="100%" border=1 cellpadding=10>\n'\
'<tr>\n'\
'<td width="400px">\n'\
'<img src="getimage?key=' + key + '" width="400px">\n'\
'</td>\n'\
'<td valign="top">\n' +\
'%s' % textfields +\
'</td>\n'\
'</tr>\n'\
'</table>\n'\
'<br>'
# '<hr>'
return page
def _pagePrintPages(req, filename, numpages, page):
result = 'Страница: '
if numpages > 1:
for i in xrange(numpages - 1):
if i + 1 == page:
result += '<b>%s</b> ' % (i + 1)
else:
result += '<a href="view?filename=%s&page=%s">%s</a> ' % (filename, (i + 1), (i + 1))
else:
i = -1
if i + 2 == page:
result += '<b>%s</b><br>\n' % (i + 2)
else:
result += '<a href="view?filename=%s&page=%s">%s</a><br>\n' % (filename, (i + 2), (i + 2))
return result
def view(req):
_init(req)
req.content_type = 'text/html'
req.write(str(page))
def imp(req):
_init(req)
import bc
form = req.form
if not form:
req.content_type = 'text/html'
req.write(_pageTitle(req, title = 'Импорт'))
req.write(_pageImpForm(req))
req.write(_pageFooter(req))
else:
req.content_type = 'text/html'
filename = form['file'].filename
file = form['file'].file
filename = os.path.splitext(filename)[0]
db = MySQLdb.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWORD, db = DB_NAME, charset = "utf8", use_unicode = False)
dbfields = ['key', 'Filename' ,'FirstName', 'LastName', 'City', 'Country', 'Address', 'ZipCode', 'State', 'Email', 'WebSite', 'Title', 'Company', 'Phone', 'Phone2', 'Fax', 'CellPhone', 'Image']
items = bc.table(db, "vcards", dbfields)
items.create_if_not_exists([
'BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY', # key
'CHAR( 128 ) NULL', # Filename
'CHAR( 128 ) NULL', # FirstName
'CHAR( 128 ) NULL', # LastName
'CHAR( 128 ) NULL', # City
'CHAR( 128 ) NULL', # Country
'CHAR( 128 ) NULL', # Address
'CHAR( 128 ) NULL', # ZipCode
'CHAR( 128 ) NULL', # State
'CHAR( 128 ) NULL', # Email
'CHAR( 128 ) NULL', # WebSite
'CHAR( 128 ) NULL', # Title
'CHAR( 128 ) NULL', # Company
'CHAR( 128 ) NULL', # Phone
'CHAR( 128 ) NULL', # Phone2
'CHAR( 128 ) NULL', # Fax
'CHAR( 128 ) NULL', # CellPhone
'LONGBLOB NULL' # Image
])
items.delete_by_filter('WHERE `Filename` = \'%s\'' % filename)
vcards = bc.vcards(file.read())
for i in xrange(len(vcards)):
items.append([
None,
filename,
vcards[i].firstName,
vcards[i].lastName,
vcards[i].city,
vcards[i].country,
vcards[i].address,
vcards[i].zipCode,
vcards[i].state,
vcards[i].email,
vcards[i].webSite,
vcards[i].title,
vcards[i].company,
vcards[i].phone,
vcards[i].phone2,
vcards[i].fax,
vcards[i].cellPhone,
vcards[i].image
])
req.write(_pageTitle(req, 'http-equiv="refresh" content="3; url=imp"'))
try:
totalrecords = i + 1
except:
totalrecords = 0
req.write(_pageDone(req, additional = 'Записей импортировано: %s.' % totalrecords))
req.write(_pageFooter(req))
def getimage(req):
_init(req)
import bc
key = req.form['key']
db = MySQLdb.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWORD, db = DB_NAME, charset = "utf8", use_unicode = False)
dbfields = ['key', 'Filename' ,'FirstName', 'LastName', 'City', 'Country', 'Address', 'ZipCode', 'State', 'Email', 'WebSite', 'Title', 'Company', 'Phone', 'Phone2', 'Fax', 'CellPhone', 'Image']
items = bc.table(db, "vcards", dbfields)
key = int(key)
items.setfilter ('WHERE `key` = %s' % key)
try:
req.content_type = 'image/JPEG'
#req.content_type = 'text/plain'
image = items[0][17]
req.write(image)
except:
req.content_type = 'text/plain'
req.status = apache.HTTP_NOT_FOUND
req.write('Image not found')
def delete(req):
_init(req)
import bc
db = MySQLdb.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWORD, db = DB_NAME, charset = "utf8", use_unicode = False)
# dbfields = ['key', 'Filename' ,'FirstName', 'LastName', 'City', 'Country', 'Address', 'ZipCode', 'State', 'Email', 'WebSite', 'Title', 'Company', 'Phone', 'Phone2', 'Fax', 'CellPhone', 'Image']
dbfields = ['Filename']
items = bc.table(db, "vcards", dbfields)
form = req.form
if not form:
req.content_type = 'text/html'
req.write(_pageTitle(req, title = 'Удаление'))
items.selectopts = 'DISTINCT'
items.order = 'Filename'
files = []
for i in xrange(len(items)):
files += [items[i][0],]
# for i in xrange(len(files)):
# req.write(files[i] + '<br>')
req.write(_pageDeleteForm(req, files))
req.write(_pageFooter(req))
else:
req.content_type = 'text/html'
filename = form['filename']
items.delete_by_filter('WHERE `Filename` = \'%s\'' % filename)
req.write(_pageTitle(req, 'http-equiv="refresh" content="3; url=delete"'))
req.write(_pageDone(req))
req.write(_pageFooter(req))
def view(req):
_init(req)
import bc
db = MySQLdb.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWORD, db = DB_NAME, charset = "utf8", use_unicode = False)
dbfields = ['key', 'Filename' ,'FirstName', 'LastName', 'City', 'Country', 'Address', 'ZipCode', 'State', 'Email', 'WebSite', 'Title', 'Company', 'Phone', 'Phone2', 'Fax', 'CellPhone', 'Image']
items = bc.table(db, "vcards", dbfields)
form = req.form
if not form:
req.content_type = 'text/html'
req.write(_pageTitle(req, title = 'Выбор раздела'))
items.selectopts = 'DISTINCT'
items.columns = ['Filename']
items.order = 'Filename'
len_items = len(items)
files = []
for i in xrange(len_items):
files += [items[i][0],]
req.write(_pageFileSelect(req, files))
req.write(_pageFooter(req))
else:
#items.order = 'LastName'
items.setorder('LastName')
filename = form['filename']
try:
page = int(form['page'])
except:
page = 1
items.setfilter('WHERE `FileName` = \'%s\'' % filename)
len_items = len(items)
startpos = (page - 1) * ITEMS_PER_PAGE
endpos = startpos + ITEMS_PER_PAGE - 1
if startpos > (len_items - 1):
startpos = len_items - 1
if endpos > (len_items - 1):
endpos = len_items - 1
numpages = len_items / ITEMS_PER_PAGE
if len_items % ITEMS_PER_PAGE > 0:
numpages += 1
req.content_type = 'text/html'
if len_items > 0:
req.write(_pageTitle(req, title = filename))
req.write('<H2>Просмотр записей раздела %s</H2>' % filename)
req.write('<a href="view">Вернуться к выбору раздела</a><br><br>\n')
req.write('Количество записей: %s<br>\n' % len_items)
req.write(_pagePrintPages(req, filename, numpages, page))
req.write('Отображаемые записи: %s - %s<br>\n' % ((startpos + 1), (endpos + 1)))
for i in xrange(startpos, endpos + 1):
req.write(_pagePrintItem(req, items[i]))
req.write(_pagePrintPages(req, filename, numpages, page))
else:
req.write(_pageTitle(req, title = 'Error!'))
req.write('<H2><center><font color="red">ОШИБКА! Выбранный раздел отсутствует!</font></center></H2>')
req.write(_pageFooter(req))