Но необходимо подключиться к базе данных mysql, и оттуда брать данные, а не к csv/
Как это делается в питон-файлах?
Подскажите, где необходимо это подключение прописать!?)
По аналогии разобраться могу, а просто из головы взять сложно!)
Код
"""
Web интерфейс
"""
import os
from Cheetah.Template import Template
import cherrypy
class Root:
@staticmethod
def prepare(telephoneDir):
Root.collaborators = list(sorted(telephoneDir.subdivision))
Root.subdivisions = list(sorted(telephoneDir.subdivision.iterSubdivision()))
Root.subdivisions.insert(0, 'все')
Root.telephoneTypes = list(sorted(telephoneDir.telephones.telephoneTypes))
Root.telephoneTypes.insert(0, 'все')
Root.telephoneDir = list(sorted(telephoneDir))
@cherrypy.expose
def index(self, page='0', subdivision='0', collaborator='', number='', telephoneType='0'):
page = int(page)
subdivision = int(subdivision)
telephoneType = int(telephoneType)
if not subdivision:
lambdaSubdivision = lambda rec: True
else:
s = Root.subdivisions[subdivision]
lambdaSubdivision = lambda rec: rec.collaborator in s
if not collaborator:
lambdaCollaborator = lambda rec: True
else:
l = len(collaborator)
lambdaCollaborator = lambda rec: str(rec.collaborator)[0:l] == collaborator
if not number:
lambdaNumber = lambda rec: True
else:
l = len(number)
lambdaNumber = lambda rec: str(rec.telephone.number)[0:l] == number
if not telephoneType:
lambdaTelephoneType = lambda rec: True
else:
t = Root.telephoneTypes[telephoneType]
lambdaTelephoneType = lambda rec: rec.telephone.type == t
root = Template(file=os.path.join(os.curdir, 'index.tmpl'))
root.page = page
root.subdivision = subdivision
root.subdivisions = Root.subdivisions
root.collaborator = collaborator
root.number = number
root.telephoneType = telephoneType
root.telephoneTypes = Root.telephoneTypes
root.telephoneDir = filter(lambda telephone: lambdaSubdivision(telephone) and \
lambdaCollaborator(telephone) and \
lambdaNumber(telephone) and \
lambdaTelephoneType(telephone), Root.telephoneDir)
return str(root)
root = Root()
cherrypy.config.update({
'log.screen': True,
'environment': 'production',
'server.socket_port': 8080,
'server.threadPool':10,
'tools.staticfile.on': False,
'tools.sessions.on': True,
'tools.sessions.timeout': 60,
})
conf = {
'/style.css': {
'tools.staticfile.on': True,
'tools.staticfile.filename': os.path.join(os.getcwd(), 'style.css'),
},
}
def run(telephoneDir):
Root.prepare(telephoneDir)
cherrypy.tree.mount(root, config=conf)
cherrypy.server.quickstart()
cherrypy.engine.start()
if __name__ == '__main__':
import tdcsv
telephoneDir = tdcsv.load()
run(telephoneDir)