150 lines
4.6 KiB
Python
150 lines
4.6 KiB
Python
# coding: utf-8
|
|
|
|
import json
|
|
import datetime
|
|
import time
|
|
|
|
import cherrypy
|
|
|
|
from app import database
|
|
|
|
# Method-Dispatching!
|
|
|
|
# Übersicht Anforderungen / Methoden
|
|
# (beachte: / relativ zu /discussion, siehe Konfiguration Server!)
|
|
|
|
"""
|
|
|
|
Anforderung GET PUT POST DELETE
|
|
----------------------------------------------------------------
|
|
|
|
|
|
/beitrag/{diskussion-id} alle Beiträge neuen Beitrag - -
|
|
zur Diskussion zur Diskussion speichern
|
|
anfordern beitrag-id zurück
|
|
|
|
/beitrag/{beitrag-id} einen Beitrag - Beitrag mit ID Beitrag mit ID
|
|
anfordern ändern löschen
|
|
|
|
"""
|
|
|
|
#----------------------------------------------------------
|
|
class Post_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, tId, dId, pId = None):
|
|
#-------------------------------------------------------
|
|
retVal_o = {'data': None, 'id': pId, 'tId':tId, 'dId': dId}
|
|
|
|
if dId == None:
|
|
# Anforderung der Liste
|
|
retVal_o['data'] = self.getList_p(dId)
|
|
else:
|
|
# Anforderung eines Dokuments
|
|
retVal_o['data'] = self.getPost(dId, pId)
|
|
|
|
if retVal_o['data'] == None:
|
|
cherrypy.response.status = 404
|
|
|
|
return json.dumps(retVal_o)
|
|
|
|
#-------------------------------------------------------
|
|
def PUT(self, tId, dId, **data_opl):
|
|
#-------------------------------------------------------
|
|
retVal_o = {
|
|
'id': None,
|
|
'dId': dId
|
|
}
|
|
|
|
# data_opl: Dictionary mit den gelieferten key-value-Paaren
|
|
if dId == 0 or dId == "0":
|
|
discData_o = self.db_o.getDefaultDiscussion_px()
|
|
discData_o['name'] = data_opl['title']
|
|
discData_o['owner'] = data_opl['owner']
|
|
dId = self.db_o.createDiscussion_px(tId, discData_o)
|
|
retVal_o['dId'] = dId
|
|
|
|
# hier müsste man prüfen, ob die Daten korrekt vorliegen!
|
|
data_o = self.db_o.getDefaultPost_px()
|
|
data_o['title'] = data_opl['title']
|
|
data_o['text'] = data_opl['text']
|
|
data_o['owner'] = data_opl['owner']
|
|
|
|
# Create-Operation
|
|
id_s = self.db_o.createPost_px(dId, data_o)
|
|
retVal_o['id'] = id_s
|
|
if retVal_o['id'] == None:
|
|
cherrypy.response.status = 409
|
|
|
|
return json.dumps(retVal_o)
|
|
|
|
#-------------------------------------------------------
|
|
def POST(self, tId, dId, 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,
|
|
'tId': tId,
|
|
'dId': dId
|
|
}
|
|
|
|
# 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['posts'][id_]
|
|
data_o['text'] = data_opl['text']
|
|
data_o['title'] = data_opl['title']
|
|
|
|
# Update-Operation
|
|
retVal_o['id'] = id_
|
|
if self.db_o.updatePost_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.deletePost_px(id_):
|
|
pass
|
|
else:
|
|
cherrypy.response.status = 404
|
|
|
|
return json.dumps(retVal_o)
|
|
|
|
#-------------------------------------------------------
|
|
def getList_p(self, tId):
|
|
#-------------------------------------------------------
|
|
data_o = self.db_o.readPost_px(tId)
|
|
|
|
return data_o
|
|
getList_p.exposed = False;
|
|
|
|
#-------------------------------------------------------
|
|
def getPost(self, tId, dId):
|
|
#-------------------------------------------------------
|
|
data_o = self.db_o.readPost_px(tId, dId)
|
|
|
|
return data_o
|
|
getPost.exposed = False;
|
|
|
|
# EOF |