WEB/Praktikum2/ppm1/app/application.py

128 lines
4.4 KiB
Python
Raw Normal View History

2016-11-03 20:35:10 +01:00
import cherrypy
from .database import Database_cl
from .view import View_cl
#----------------------------------------------------------
class Application_cl(object):
#----------------------------------------------------------
2016-11-06 23:38:59 +01:00
#-------------------------------------------------------
# Request Processing
#-------------------------------------------------------
2016-11-03 20:35:10 +01:00
#-------------------------------------------------------
def __init__(self):
#-------------------------------------------------------
# spezielle Initialisierung können hier eingetragen werden
2016-11-06 23:38:59 +01:00
self.db = Database_cl()
self.view = View_cl()
@cherrypy.expose
#-------------------------------------------------------
def index(self):
#-------------------------------------------------------
print("Index\n")
return self.GenerateIndex()
@cherrypy.expose
#-------------------------------------------------------
def category(self, cat=None):
#-------------------------------------------------------
print("Category: ", cat, "\n")
if(cat==None):
return self.GenerateIndex()
else:
return self.GenerateList(cat)
@cherrypy.expose
#-------------------------------------------------------
def detail(self, cat=None, id=None):
#-------------------------------------------------------
if(cat!=None):
2016-11-07 17:59:41 +01:00
if(id!=None):
print("ID!=None Cat=", cat, " id=", id)
return self.GenerateDetail(cat, id)
else:
print("ID=None Cat=", cat)
return self.GenerateDetail(cat)
2016-11-06 23:38:59 +01:00
else:
return self.GenerateIndex()
@cherrypy.expose
#-------------------------------------------------------
def save(self, cat=None, **data):
#-------------------------------------------------------
print("Save: ", cat)
dataTmp = data
return self.GenerateSave(dataTmp, cat)
2016-11-03 20:35:10 +01:00
@cherrypy.expose
#-------------------------------------------------------
def delete(self, id, form=None):
#-------------------------------------------------------
# Eintrag löschen, dann Liste neu anzeigen
self.db_o.delete_px(id)
print("Delete",form)
return self.createList_p(form)
@cherrypy.expose
#-------------------------------------------------------
def default(self, *arguments, **kwargs):
#-------------------------------------------------------
msg_s = "unbekannte Anforderung: " + \
str(arguments) + \
''+ \
str(kwargs)
raise cherrypy.HTTPError(404, msg_s)
2016-11-06 23:38:59 +01:00
default.exposed= True
#-------------------------------------------------------
# Functions
#-------------------------------------------------------
#-------------------------------------------------------
def GenerateIndex(self):
#-------------------------------------------------------
return self.view.CreateIndex()
#-------------------------------------------------------
def GenerateList(self, category):
#-------------------------------------------------------
data = {}
data['content'] = {}
data['headings'] = {}
data['category'] = category
data['content'] = self.db.data[category]
print(data, "\n")
if(len(data['content']) != 0):
print(len(data['content']))
contentFirst = list(data['content'].keys())[0]
data['headings'] = list(data['content'][contentFirst].keys())
print(data, "\n")
return self.view.CreateList(data)
#-------------------------------------------------------
def GenerateDetail(self, category, id=None):
#-------------------------------------------------------
data = {}
2016-11-07 17:59:41 +01:00
data['category'] = category
2016-11-06 23:38:59 +01:00
if(id != None):
2016-11-07 17:59:41 +01:00
data['id'] = id
2016-11-06 23:38:59 +01:00
data['content'] = self.db.ReadEntry(category, id)
else:
data['id'] = None
data['content'] = self.db.GetDefault(category)
print(data, "\n")
return self.view.CreateDetail(data)
#-------------------------------------------------------
def GenerateSave(self, dataTmp, category):
#-------------------------------------------------------
if(category == None):
return self.view.CreateIndex()
else:
self.db.Save(dataTmp, category)
return self.GenerateList(category)
#EOF