Sammlung
This commit is contained in:
3
Sammlung/lit-8/app/__init__.py
Normal file
3
Sammlung/lit-8/app/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
# coding: utf-8
|
||||
|
||||
# hier können Paket-Initialisierungen eingetragen werden
|
BIN
Sammlung/lit-8/app/__pycache__/__init__.cpython-35.pyc
Normal file
BIN
Sammlung/lit-8/app/__pycache__/__init__.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Sammlung/lit-8/app/__pycache__/application.cpython-35.pyc
Normal file
BIN
Sammlung/lit-8/app/__pycache__/application.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Sammlung/lit-8/app/__pycache__/database.cpython-35.pyc
Normal file
BIN
Sammlung/lit-8/app/__pycache__/database.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Sammlung/lit-8/app/__pycache__/template.cpython-35.pyc
Normal file
BIN
Sammlung/lit-8/app/__pycache__/template.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Sammlung/lit-8/app/__pycache__/view.cpython-35.pyc
Normal file
BIN
Sammlung/lit-8/app/__pycache__/view.cpython-35.pyc
Normal file
Binary file not shown.
161
Sammlung/lit-8/app/application.py
Normal file
161
Sammlung/lit-8/app/application.py
Normal file
@ -0,0 +1,161 @@
|
||||
# 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
|
125
Sammlung/lit-8/app/database.py
Normal file
125
Sammlung/lit-8/app/database.py
Normal file
@ -0,0 +1,125 @@
|
||||
# coding: utf-8
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import codecs
|
||||
|
||||
import json
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Database_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
# Daten in dieser Variante dauerhaft (persistent) speichern
|
||||
# dazu jedes Element in einer Datei, die entsprechend der id benannt ist, speichern
|
||||
# alle Elemente werden zur Laufzeit des Servers zur Vereinfachung auch im
|
||||
# Hauptspeicher abgelegt
|
||||
|
||||
# die nächste zu vergebende Id wird ebenfalls dauerhaft gespeichert
|
||||
|
||||
# zur Vereinfachung wird hier fest vorgegebenen, dass sich die Daten
|
||||
# im Unterverzeichnis "data" befinden
|
||||
|
||||
# es wird ferner angenommen, dass die Datei "data/maxid.dat" bereits existiert
|
||||
# und als einzigen Eintrag den aktuellen Wert der maximalen Id enthält
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
self.data_o = {}
|
||||
self.readData_p()
|
||||
|
||||
#-------------------------------------------------------
|
||||
def create_px(self, data_opl):
|
||||
#-------------------------------------------------------
|
||||
# Überprüfung der Daten müsste ergänzt werden!
|
||||
id_s = self.nextId_p()
|
||||
# Datei erzeugen
|
||||
file_o = codecs.open(os.path.join('data', id_s+'.dat'), 'w', 'utf-8')
|
||||
file_o.write(json.dumps(data_opl, indent=3, ensure_ascii=True))
|
||||
file_o.close()
|
||||
|
||||
self.data_o[id_s] = data_opl
|
||||
|
||||
return id_s
|
||||
|
||||
#-------------------------------------------------------
|
||||
def read_px(self, id_spl = None):
|
||||
#-------------------------------------------------------
|
||||
# hier zur Vereinfachung:
|
||||
# Aufruf ohne id: alle Einträge liefern
|
||||
data_o = None
|
||||
if id_spl == None:
|
||||
data_o = self.data_o
|
||||
elif id_spl == '0':
|
||||
data_o = self.getDefault_px()
|
||||
else:
|
||||
if id_spl in self.data_o:
|
||||
data_o = self.data_o[id_spl]
|
||||
|
||||
return data_o
|
||||
|
||||
#-------------------------------------------------------
|
||||
def update_px(self, id_spl, data_opl):
|
||||
#-------------------------------------------------------
|
||||
# Überprüfung der Daten müsste ergänzt werden!
|
||||
status_b = False
|
||||
if id_spl in self.data_o:
|
||||
# Datei aktualisieren
|
||||
file_o = codecs.open(os.path.join('data', id_spl+'.dat'), 'w', 'utf-8')
|
||||
file_o.write(json.dumps(data_opl, indent=3, ensure_ascii=True))
|
||||
file_o.close()
|
||||
|
||||
self.data_o[id_spl] = data_opl
|
||||
status_b = True
|
||||
|
||||
return status_b
|
||||
|
||||
#-------------------------------------------------------
|
||||
def delete_px(self, id_spl):
|
||||
#-------------------------------------------------------
|
||||
status_b = False
|
||||
if id_spl in self.data_o:
|
||||
# Datei entfernen
|
||||
os.remove(os.path.join('data', id_spl+'.dat'))
|
||||
|
||||
del self.data_o[id_spl]
|
||||
status_b = True
|
||||
|
||||
return status_b
|
||||
|
||||
#-------------------------------------------------------
|
||||
def getDefault_px(self):
|
||||
#-------------------------------------------------------
|
||||
|
||||
return {
|
||||
'name': '',
|
||||
'typ': 'Typ1',
|
||||
'referenz': ''
|
||||
}
|
||||
|
||||
#-------------------------------------------------------
|
||||
def readData_p(self):
|
||||
#-------------------------------------------------------
|
||||
|
||||
files_a = os.listdir('data')
|
||||
for fileName_s in files_a:
|
||||
if fileName_s.endswith('.dat') and fileName_s != 'maxid.dat':
|
||||
file_o = codecs.open(os.path.join('data', fileName_s), 'rU', 'utf-8')
|
||||
content_s = file_o.read()
|
||||
file_o.close()
|
||||
id_s = fileName_s[:-4]
|
||||
self.data_o[id_s] = json.loads(content_s)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def nextId_p(self):
|
||||
#-------------------------------------------------------
|
||||
file_o = open(os.path.join('data', 'maxid.dat'), 'r+')
|
||||
maxId_s = file_o.read()
|
||||
maxId_s = str(int(maxId_s)+1)
|
||||
file_o.seek(0)
|
||||
file_o.write(maxId_s)
|
||||
file_o.close()
|
||||
|
||||
return maxId_s
|
||||
# EOF
|
52
Sammlung/lit-8/app/template.py
Normal file
52
Sammlung/lit-8/app/template.py
Normal file
@ -0,0 +1,52 @@
|
||||
# coding: utf-8
|
||||
|
||||
import json
|
||||
|
||||
import os
|
||||
import codecs
|
||||
|
||||
import cherrypy
|
||||
|
||||
# Method-Dispatching!
|
||||
|
||||
# Übersicht Anforderungen / Methoden
|
||||
# (beachte: / relativ zu /template, siehe Konfiguration Server!)
|
||||
|
||||
"""
|
||||
|
||||
Anforderung GET PUT POST DELETE
|
||||
----------------------------------------------------------------
|
||||
/ Alle - - -
|
||||
Templates
|
||||
liefern
|
||||
|
||||
"""
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Template_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
exposed = True # gilt für alle Methoden
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
pass
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GET(self):
|
||||
#-------------------------------------------------------
|
||||
retVal_o = {
|
||||
'templates': {}
|
||||
}
|
||||
|
||||
files_a = os.listdir('templates')
|
||||
for fileName_s in files_a:
|
||||
file_o = codecs.open(os.path.join('templates', fileName_s), 'rU', 'utf-8')
|
||||
content_s = file_o.read()
|
||||
file_o.close()
|
||||
retVal_o["templates"][fileName_s] = content_s
|
||||
|
||||
return json.dumps(retVal_o)
|
||||
|
||||
# EOF
|
32
Sammlung/lit-8/app/view.py
Normal file
32
Sammlung/lit-8/app/view.py
Normal file
@ -0,0 +1,32 @@
|
||||
# coding: utf-8
|
||||
|
||||
#----------------------------------------------------------
|
||||
class View_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
pass
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createList_px(self, data_opl):
|
||||
#-------------------------------------------------------
|
||||
|
||||
return data_opl
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createForm_px(self, id_spl, data_opl):
|
||||
#-------------------------------------------------------
|
||||
|
||||
if id_spl == None:
|
||||
data_opl['id'] = ''
|
||||
elif id_spl == '':
|
||||
data_opl['id'] = ''
|
||||
elif id_spl == '0':
|
||||
data_opl['id'] = ''
|
||||
else:
|
||||
data_opl['id'] = id_spl
|
||||
return data_opl
|
||||
|
||||
# EOF
|
Reference in New Issue
Block a user