praktikum 3
This commit is contained in:
3
Praktikum3/lit-x/app/__init__.py
Executable file
3
Praktikum3/lit-x/app/__init__.py
Executable file
@ -0,0 +1,3 @@
|
||||
# coding: utf-8
|
||||
|
||||
# hier können Paket-Initialisierungen eingetragen werden
|
BIN
Praktikum3/lit-x/app/__pycache__/__init__.cpython-36.pyc
Normal file
BIN
Praktikum3/lit-x/app/__pycache__/__init__.cpython-36.pyc
Normal file
Binary file not shown.
BIN
Praktikum3/lit-x/app/__pycache__/application.cpython-36.pyc
Normal file
BIN
Praktikum3/lit-x/app/__pycache__/application.cpython-36.pyc
Normal file
Binary file not shown.
BIN
Praktikum3/lit-x/app/__pycache__/database.cpython-36.pyc
Normal file
BIN
Praktikum3/lit-x/app/__pycache__/database.cpython-36.pyc
Normal file
Binary file not shown.
BIN
Praktikum3/lit-x/app/__pycache__/navigation.cpython-36.pyc
Normal file
BIN
Praktikum3/lit-x/app/__pycache__/navigation.cpython-36.pyc
Normal file
Binary file not shown.
BIN
Praktikum3/lit-x/app/__pycache__/templates.cpython-36.pyc
Normal file
BIN
Praktikum3/lit-x/app/__pycache__/templates.cpython-36.pyc
Normal file
Binary file not shown.
348
Praktikum3/lit-x/app/application.py
Executable file
348
Praktikum3/lit-x/app/application.py
Executable file
@ -0,0 +1,348 @@
|
||||
# coding: utf-8
|
||||
|
||||
import json
|
||||
|
||||
import cherrypy
|
||||
|
||||
from .database import SourceDatabase_cl, EvaluatedDatabase_cl
|
||||
|
||||
# Method-Dispatching!
|
||||
|
||||
# Übersicht Anforderungen / Methoden
|
||||
|
||||
"""
|
||||
|
||||
Anforderung GET POST PUT DELETE
|
||||
----------------------------------------------------------------
|
||||
/ Liste - - -
|
||||
Literatur
|
||||
liefern
|
||||
|
||||
/source Liste - - -
|
||||
Literatur
|
||||
liefern
|
||||
|
||||
/evaluated Liste - - -
|
||||
Auswertungen
|
||||
liefern
|
||||
|
||||
/source/0 Dokument Dokument - -
|
||||
mit id=0 anlegen
|
||||
liefern
|
||||
(Vorgabe-Werte)
|
||||
|
||||
/evaluated/0 Dokument Dokument - -
|
||||
mit id=0 anlegen
|
||||
liefern
|
||||
(Vorgabe-Werte)
|
||||
|
||||
/source/{id} Dokument - Dokument Dokument
|
||||
mit {id} ändern löschen
|
||||
liefern
|
||||
(Literatur)
|
||||
|
||||
/evaluated/{id} Dokument - Dokument Dokument
|
||||
mit {id} ändern löschen
|
||||
liefern
|
||||
(Auswertungen)
|
||||
|
||||
id > 0 !
|
||||
|
||||
"""
|
||||
|
||||
#-------------------------------------------------------
|
||||
def adjustId_p(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
|
||||
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Source_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
self.db_o = SourceDatabase_cl()
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GET(self, id):
|
||||
#-------------------------------------------------------
|
||||
retVal_o = {
|
||||
'data': None
|
||||
}
|
||||
if id == None:
|
||||
# Anforderung der Liste
|
||||
retVal_o['data'] = self.db_o.read_px()
|
||||
else:
|
||||
# Anforderung eines Dokuments
|
||||
data_o = self.db_o.read_px(id)
|
||||
if data_o != None:
|
||||
retVal_o['data'] = adjustId_p(id, data_o)
|
||||
|
||||
return retVal_o
|
||||
|
||||
#-------------------------------------------------------
|
||||
def POST(self, data_opl):
|
||||
#-------------------------------------------------------
|
||||
retVal_o = {
|
||||
'id': None
|
||||
}
|
||||
|
||||
# data_opl: Dictionary mit den gelieferten key-value-Paaren
|
||||
|
||||
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
|
||||
|
||||
return retVal_o
|
||||
|
||||
#-------------------------------------------------------
|
||||
def PUT(self, 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
|
||||
|
||||
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:
|
||||
retVal_o['id'] = None
|
||||
|
||||
return 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:
|
||||
retVal_o['id'] = None
|
||||
|
||||
return retVal_o
|
||||
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Evaluated_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
self.db_o = EvaluatedDatabase_cl()
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GET(self, id):
|
||||
#-------------------------------------------------------
|
||||
retVal_o = {
|
||||
'data': None
|
||||
}
|
||||
if id == None:
|
||||
# Anforderung der Liste
|
||||
retVal_o['data'] = self.db_o.read_px()
|
||||
else:
|
||||
# Anforderung eines Dokuments
|
||||
data_o = self.db_o.read_px(id)
|
||||
if data_o != None:
|
||||
retVal_o['data'] = adjustId_p(id, data_o)
|
||||
|
||||
return retVal_o
|
||||
|
||||
#-------------------------------------------------------
|
||||
def POST(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 = {
|
||||
'title': data_opl["title_s"],
|
||||
'author': data_opl["author_s"],
|
||||
'evalyear': data_opl["evalyear_s"]
|
||||
}
|
||||
# Create-Operation
|
||||
id_s = self.db_o.create_px(data_o)
|
||||
retVal_o['id'] = id_s
|
||||
|
||||
return retVal_o
|
||||
|
||||
#-------------------------------------------------------
|
||||
def PUT(self, 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 = {
|
||||
'title': data_opl["title_s"],
|
||||
'author': data_opl["author_s"],
|
||||
'evalyear': data_opl["evalyear_s"]
|
||||
}
|
||||
# Update-Operation
|
||||
retVal_o['id'] = id_s
|
||||
if self.db_o.update_px(id_s, data_o):
|
||||
pass
|
||||
else:
|
||||
retVal_o['id'] = None
|
||||
|
||||
return 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:
|
||||
retVal_o['id'] = None
|
||||
|
||||
return retVal_o
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Application_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
exposed = True # gilt für alle Methoden
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
self.handler_o = {
|
||||
'source': Source_cl(),
|
||||
'evaluated': Evaluated_cl()
|
||||
}
|
||||
|
||||
# es wird keine index-Methode vorgesehen, weil stattdessen
|
||||
# die Webseite index.html ausgeliefert wird (siehe Konfiguration)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GET(self, path_spl = 'source', id=None):
|
||||
#-------------------------------------------------------
|
||||
retVal_o = {
|
||||
'data': None
|
||||
}
|
||||
|
||||
if path_spl in self.handler_o:
|
||||
retVal_o = self.handler_o[path_spl].GET(id)
|
||||
|
||||
if retVal_o['data'] == None:
|
||||
cherrypy.response.status = 404
|
||||
|
||||
return json.dumps(retVal_o)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def POST(self, path_spl = 'source', **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!
|
||||
|
||||
if path_spl in self.handler_o:
|
||||
retVal_o = self.handler_o[path_spl].POST(data_opl)
|
||||
|
||||
if retVal_o['id'] == None:
|
||||
cherrypy.response.status = 409
|
||||
|
||||
return json.dumps(retVal_o)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def PUT(self, path_spl = 'source', **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!
|
||||
|
||||
if path_spl in self.handler_o:
|
||||
retVal_o = self.handler_o[path_spl].PUT(data_opl)
|
||||
|
||||
if retVal_o['id'] == None:
|
||||
cherrypy.response.status = 404
|
||||
|
||||
return json.dumps(retVal_o)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def DELETE(self, path_spl = 'source', id=None):
|
||||
#-------------------------------------------------------
|
||||
# Eintrag löschen, nur noch Rückmeldung liefern
|
||||
retVal_o = {
|
||||
'id': id
|
||||
}
|
||||
|
||||
if path_spl in self.handler_o:
|
||||
retVal_o = self.handler_o[path_spl].DELETE(id)
|
||||
|
||||
if retVal_o['id'] == None:
|
||||
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)
|
||||
|
||||
# EOF
|
162
Praktikum3/lit-x/app/database.py
Executable file
162
Praktikum3/lit-x/app/database.py
Executable file
@ -0,0 +1,162 @@
|
||||
# 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/<type>" befinden (siehe Konstruktor)
|
||||
|
||||
# es wird ferner angenommen, dass die Datei "data/<type>/maxid.dat" bereits existiert
|
||||
# und als einzigen Eintrag den aktuellen Wert der maximalen Id enthält
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self, type_spl):
|
||||
#-------------------------------------------------------
|
||||
self.type_s = type_spl
|
||||
self.path_s = os.path.join('data', type_spl)
|
||||
self.data_o = {}
|
||||
self.readData_p()
|
||||
|
||||
#-------------------------------------------------------
|
||||
def create_px(self, data_opl):
|
||||
#-------------------------------------------------------
|
||||
# Überprüfung der Datenn müsste ergänzt werden!
|
||||
id_s = self.nextId_p()
|
||||
# Datei erzeugen
|
||||
file_o = codecs.open(os.path.join(self.path_s , 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(self.path_s, 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(self.path_s, id_spl+'.dat'))
|
||||
|
||||
del self.data_o[id_spl]
|
||||
status_b = True
|
||||
|
||||
return status_b
|
||||
|
||||
#-------------------------------------------------------
|
||||
def getDefault_px(self):
|
||||
#-------------------------------------------------------
|
||||
# sollte überschrieben werden!
|
||||
return {}
|
||||
|
||||
#-------------------------------------------------------
|
||||
def readData_p(self):
|
||||
#-------------------------------------------------------
|
||||
|
||||
files_a = os.listdir(self.path_s)
|
||||
for fileName_s in files_a:
|
||||
if fileName_s.endswith('.dat') and fileName_s != 'maxid.dat':
|
||||
file_o = codecs.open(os.path.join(self.path_s, 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(self.path_s, '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
|
||||
|
||||
#----------------------------------------------------------
|
||||
class SourceDatabase_cl(Database_cl):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
super().__init__('source')
|
||||
|
||||
#-------------------------------------------------------
|
||||
def getDefault_px(self):
|
||||
#-------------------------------------------------------
|
||||
|
||||
return {
|
||||
'name': '',
|
||||
'typ': 'Typ1',
|
||||
'referenz': ''
|
||||
}
|
||||
|
||||
#----------------------------------------------------------
|
||||
class EvaluatedDatabase_cl(Database_cl):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
super().__init__('evaluated')
|
||||
|
||||
#-------------------------------------------------------
|
||||
def getDefault_px(self):
|
||||
#-------------------------------------------------------
|
||||
|
||||
return {
|
||||
'title': '',
|
||||
'author': '',
|
||||
'evalyear': ''
|
||||
}
|
||||
|
||||
# EOF
|
42
Praktikum3/lit-x/app/navigation.py
Executable file
42
Praktikum3/lit-x/app/navigation.py
Executable file
@ -0,0 +1,42 @@
|
||||
# coding: utf-8
|
||||
|
||||
import json
|
||||
|
||||
import cherrypy
|
||||
|
||||
# Method-Dispatching!
|
||||
|
||||
# Übersicht Anforderungen / Methoden
|
||||
# (beachte: / relativ zu /navigation, siehe Konfiguration Server!)
|
||||
|
||||
"""
|
||||
|
||||
Anforderung GET PUT POST DELETE
|
||||
----------------------------------------------------------------
|
||||
/ Nav-Entries - - -
|
||||
|
||||
"""
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Navigation_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
exposed = True # gilt für alle Methoden
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
pass
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GET(self):
|
||||
#-------------------------------------------------------
|
||||
# Hinweis: könnte man auch aus einer Datei einlesen
|
||||
retVal_o = [
|
||||
{'action': 'source' , 'text': 'Quellen'},
|
||||
{'action': 'evaluated', 'text': 'Ausgewertet'}
|
||||
]
|
||||
|
||||
return json.dumps(retVal_o)
|
||||
|
||||
# EOF
|
52
Praktikum3/lit-x/app/templates.py
Executable file
52
Praktikum3/lit-x/app/templates.py
Executable 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 Templates_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
|
Reference in New Issue
Block a user