161 lines
4.8 KiB
Python
161 lines
4.8 KiB
Python
|
# coding: utf-8
|
||
|
|
||
|
import json
|
||
|
|
||
|
import cherrypy
|
||
|
|
||
|
from app import database
|
||
|
from app import view
|
||
|
|
||
|
# Method-Dispatching!
|
||
|
|
||
|
# Übersicht Anforderungen / Methoden
|
||
|
# (beachte: / relativ zu /lit, siehe Konfiguration Server!)
|
||
|
|
||
|
"""
|
||
|
|
||
|
Anforderung GET PUT POST DELETE
|
||
|
----------------------------------------------------------------
|
||
|
/ Liste - - -
|
||
|
Literatur
|
||
|
liefern
|
||
|
|
||
|
/0 Dokument Dokument - -
|
||
|
mit id=0 anlegen
|
||
|
liefern
|
||
|
(Vorgabe-Werte)
|
||
|
|
||
|
/{id} Dokument - Dokument Dokument
|
||
|
mit {id} ändern löschen
|
||
|
liefern
|
||
|
|
||
|
id > 0 !
|
||
|
|
||
|
"""
|
||
|
|
||
|
#----------------------------------------------------------
|
||
|
class Application_cl(object):
|
||
|
#----------------------------------------------------------
|
||
|
|
||
|
exposed = True # gilt für alle Methoden
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def __init__(self):
|
||
|
#-------------------------------------------------------
|
||
|
# spezielle Initialisierung können hier eingetragen werden
|
||
|
self.db_o = database.Database_cl()
|
||
|
self.view_o = view.View_cl()
|
||
|
|
||
|
# es wird keine index-Methode vorgesehen, weil stattdessen
|
||
|
# die Webseite index.html ausgeliefert wird (siehe Konfiguration)
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def GET(self, id=None):
|
||
|
#-------------------------------------------------------
|
||
|
retVal_o = {
|
||
|
'data': None
|
||
|
}
|
||
|
if id == None:
|
||
|
# Anforderung der Liste
|
||
|
retVal_o['data'] = self.getList_p()
|
||
|
else:
|
||
|
# Anforderung eines Dokuments
|
||
|
retVal_o['data'] = self.getForm_p(id)
|
||
|
|
||
|
if retVal_o['data'] == None:
|
||
|
cherrypy.response.status = 404
|
||
|
|
||
|
return json.dumps(retVal_o)
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def PUT(self, **data_opl):
|
||
|
#-------------------------------------------------------
|
||
|
retVal_o = {
|
||
|
'id': None
|
||
|
}
|
||
|
|
||
|
# data_opl: Dictionary mit den gelieferten key-value-Paaren
|
||
|
|
||
|
# hier müsste man prüfen, ob die Daten korrekt vorliegen!
|
||
|
|
||
|
data_o = {
|
||
|
'name': data_opl["name_s"],
|
||
|
'typ': data_opl["typ_s"],
|
||
|
'referenz': data_opl["referenz_s"]
|
||
|
}
|
||
|
# Create-Operation
|
||
|
id_s = self.db_o.create_px(data_o)
|
||
|
retVal_o['id'] = id_s
|
||
|
if id_s == None:
|
||
|
cherrypy.response.status = 409
|
||
|
|
||
|
return json.dumps(retVal_o)
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def POST(self, id, **data_opl):
|
||
|
#-------------------------------------------------------
|
||
|
# Sichern der Daten: jetzt wird keine vollständige Seite
|
||
|
# zurückgeliefert, sondern nur noch die Information, ob das
|
||
|
# Speichern erfolgreich war
|
||
|
|
||
|
retVal_o = {
|
||
|
'id': None
|
||
|
}
|
||
|
|
||
|
# data_opl: Dictionary mit den gelieferten key-value-Paaren
|
||
|
# hier müsste man prüfen, ob die Daten korrekt vorliegen!
|
||
|
|
||
|
id_s = data_opl["id_s"]
|
||
|
data_o = {
|
||
|
'name': data_opl["name_s"],
|
||
|
'typ': data_opl["typ_s"],
|
||
|
'referenz': data_opl["referenz_s"]
|
||
|
}
|
||
|
# Update-Operation
|
||
|
retVal_o['id'] = id_s
|
||
|
if self.db_o.update_px(id_s, data_o):
|
||
|
pass
|
||
|
else:
|
||
|
cherrypy.response.status = 404
|
||
|
|
||
|
return json.dumps(retVal_o)
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def DELETE(self, id):
|
||
|
#-------------------------------------------------------
|
||
|
# Eintrag löschen, nur noch Rückmeldung liefern
|
||
|
retVal_o = {
|
||
|
'id': id
|
||
|
}
|
||
|
|
||
|
if self.db_o.delete_px(id):
|
||
|
pass
|
||
|
else:
|
||
|
cherrypy.response.status = 404
|
||
|
|
||
|
return json.dumps(retVal_o)
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def default(self, *arguments, **kwargs):
|
||
|
#-------------------------------------------------------
|
||
|
msg_s = "unbekannte Anforderung: " + \
|
||
|
str(arguments) + \
|
||
|
' ' + \
|
||
|
str(kwargs)
|
||
|
raise cherrypy.HTTPError(404, msg_s)
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def getList_p(self):
|
||
|
#-------------------------------------------------------
|
||
|
data_o = self.db_o.read_px()
|
||
|
return self.view_o.createList_px(data_o)
|
||
|
|
||
|
#-------------------------------------------------------
|
||
|
def getForm_p(self, id_spl):
|
||
|
#-------------------------------------------------------
|
||
|
data_o = self.db_o.read_px(id_spl)
|
||
|
if data_o != None:
|
||
|
return self.view_o.createForm_px(id_spl, data_o)
|
||
|
else:
|
||
|
return None
|
||
|
# EOF
|