149 lines
4.2 KiB
Python
149 lines
4.2 KiB
Python
# coding: utf-8
|
|
|
|
import json
|
|
|
|
import cherrypy
|
|
|
|
from app import database
|
|
|
|
# Method-Dispatching!
|
|
|
|
# Übersicht Anforderungen / Methoden
|
|
# (beachte: / relativ zu /topic, siehe Konfiguration Server!)
|
|
|
|
"""
|
|
|
|
Anforderung GET PUT POST DELETE
|
|
----------------------------------------------------------------
|
|
/ Liste - - -
|
|
Themen
|
|
liefern
|
|
|
|
/0 Thema Thema - -
|
|
mit id=0 anlegen
|
|
liefern
|
|
(Vorgabe-Werte)
|
|
|
|
/{id} Thema - Thema Thema
|
|
mit {id} ändern löschen
|
|
liefern
|
|
|
|
id > 0 !
|
|
|
|
"""
|
|
|
|
#----------------------------------------------------------
|
|
class Topic_cl(object):
|
|
#----------------------------------------------------------
|
|
|
|
exposed = True # gilt für alle Methoden
|
|
|
|
#-------------------------------------------------------
|
|
def __init__(self):
|
|
#-------------------------------------------------------
|
|
# spezielle Initialisierung können hier eingetragen werden
|
|
self.db_o = database.db_o
|
|
|
|
#-------------------------------------------------------
|
|
def GET(self, id_ = None):
|
|
#-------------------------------------------------------
|
|
retVal_o = {'data': None, 'id': None}
|
|
|
|
if id == None:
|
|
# Anforderung der Liste
|
|
retVal_o['data'] = self.getList_p()
|
|
else:
|
|
# Anforderung eines Dokuments
|
|
retVal_o['data'] = self.getTopic(id_)
|
|
retVal_o['id'] = 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!
|
|
|
|
print(data_opl)
|
|
data_o = {
|
|
'name' : data_opl['name'],
|
|
'owner': data_opl['owner'],
|
|
'deleted': False,
|
|
'discs': []
|
|
}
|
|
|
|
# Create-Operation
|
|
id_s = self.db_o.createTopic_px(data_o)
|
|
retVal_o['id'] = id_s
|
|
if retVal_o['id'] == 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_ = str(id_)
|
|
data_o = self.db_o.data_o['topics'][id_]
|
|
data_o['name'] = data_opl['name']
|
|
|
|
# Update-Operation
|
|
retVal_o['id'] = id_
|
|
if self.db_o.updateTopic_px(id_, 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.deleteTopic_px(id_):
|
|
pass
|
|
else:
|
|
cherrypy.response.status = 404
|
|
|
|
return json.dumps(retVal_o)
|
|
|
|
#-------------------------------------------------------
|
|
def getList_p(self):
|
|
#-------------------------------------------------------
|
|
data_o = self.db_o.readTopic_px()
|
|
|
|
return data_o
|
|
getList_p.exposed = False;
|
|
|
|
#-------------------------------------------------------
|
|
def getTopic(self, id_spl):
|
|
#-------------------------------------------------------
|
|
data_o = self.db_o.readTopic_px(id_spl)
|
|
|
|
return data_o
|
|
getTopic.exposed = False;
|
|
|
|
# EOF |