Praktikum1_Kai
This commit is contained in:
1
Praktikum1_Kai/webteams/app/__init__.py
Normal file
1
Praktikum1_Kai/webteams/app/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# kennzeichnet ein Verzeichnis als Python-Package
|
BIN
Praktikum1_Kai/webteams/app/__pycache__/__init__.cpython-35.pyc
Normal file
BIN
Praktikum1_Kai/webteams/app/__pycache__/__init__.cpython-35.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
Praktikum1_Kai/webteams/app/__pycache__/database.cpython-35.pyc
Normal file
BIN
Praktikum1_Kai/webteams/app/__pycache__/database.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Praktikum1_Kai/webteams/app/__pycache__/view.cpython-35.pyc
Normal file
BIN
Praktikum1_Kai/webteams/app/__pycache__/view.cpython-35.pyc
Normal file
Binary file not shown.
130
Praktikum1_Kai/webteams/app/application.py
Normal file
130
Praktikum1_Kai/webteams/app/application.py
Normal file
@ -0,0 +1,130 @@
|
||||
# coding: utf-8
|
||||
|
||||
import cherrypy
|
||||
from .database import Database_cl
|
||||
from .view import View_cl
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Application_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
# spezielle Initialisierung können hier eingetragen werden
|
||||
self.db_o = Database_cl()
|
||||
self.view_o = View_cl()
|
||||
self.liste = 0
|
||||
@cherrypy.expose
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def index(self, form="NULL"):
|
||||
#-------------------------------------------------------
|
||||
if(form == "0"):
|
||||
return self.createListAufz_p()
|
||||
elif(form == "1"):
|
||||
return self.createList_p()
|
||||
elif(form == "NULL"):
|
||||
return self.createList_p()
|
||||
@cherrypy.expose
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def add(self):
|
||||
#-------------------------------------------------------
|
||||
return self.createForm_p()
|
||||
@cherrypy.expose
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def edit(self, id):
|
||||
#-------------------------------------------------------
|
||||
return self.createForm_p(id)
|
||||
@cherrypy.expose
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def save(self, **data_opl):
|
||||
#-------------------------------------------------------
|
||||
# Sichern der Daten: aufgrund der Formularbearbeitung muss
|
||||
# eine vollständige HTML-Seite zurückgeliefert werden!
|
||||
# data_opl: Dictionary mit den gelieferten key-value-Paaren
|
||||
# hier müsste man prüfen, ob die Daten korrekt vorliegen!
|
||||
# HIER müssen Sie die Semesterzahl(en) ergänzen
|
||||
id_s = data_opl["id_s"]
|
||||
data_a = [ data_opl["name1_s"]
|
||||
, data_opl["vorname1_s"]
|
||||
, data_opl["matrnr1_s"]
|
||||
, data_opl["semesteranzahl1_s"]
|
||||
, data_opl["name2_s"]
|
||||
, data_opl["vorname2_s"]
|
||||
, data_opl["matrnr2_s"]
|
||||
, data_opl["semesteranzahl2_s"]
|
||||
]
|
||||
|
||||
if id_s != "None":
|
||||
# Update-Operation
|
||||
self.db_o.update_px(id_s, data_a)
|
||||
else:
|
||||
# Create-Operation
|
||||
id_s = self.db_o.create_px(data_a)
|
||||
|
||||
return self.createForm_p(id_s)
|
||||
@cherrypy.expose
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def delete(self, id):
|
||||
#-------------------------------------------------------
|
||||
# Eintrag löschen, dann Liste neu anzeigen
|
||||
self.db_o.delete_px(id)
|
||||
|
||||
return self.createListAufz_p()
|
||||
@cherrypy.expose
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def default(self, *arguments, **kwargs):
|
||||
#-------------------------------------------------------
|
||||
msg_s = "unbekannte Anforderung: " + \
|
||||
str(arguments) + \
|
||||
''+ \
|
||||
str(kwargs)
|
||||
raise cherrypy.HTTPError(404, msg_s)
|
||||
default.exposed= True
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createList_p(self):
|
||||
#-------------------------------------------------------
|
||||
print("---------- List ----------")
|
||||
data_o = self.db_o.read_px()
|
||||
# mit diesen Daten Markup erzeugen
|
||||
|
||||
return self.view_o.createList_px(data_o)
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createListAufz_p(self):
|
||||
#-------------------------------------------------------
|
||||
print("---------- ListAufz ----------")
|
||||
data_o = self.db_o.read_px()
|
||||
# mit diesen Daten Markup erzeugen
|
||||
|
||||
return self.view_o.createListAufz_px(data_o)
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createForm_p(self, id_spl = None):
|
||||
#-------------------------------------------------------
|
||||
if id_spl != None:
|
||||
data_o = self.db_o.read_px(id_spl)
|
||||
else:
|
||||
data_o = self.db_o.getDefault_px()
|
||||
|
||||
# mit diesen Daten Markup erzeugen
|
||||
return self.view_o.createForm_px(id_spl, data_o)
|
||||
|
||||
|
||||
# EOF
|
122
Praktikum1_Kai/webteams/app/database.py
Normal file
122
Praktikum1_Kai/webteams/app/database.py
Normal file
@ -0,0 +1,122 @@
|
||||
# coding: utf-8
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import codecs
|
||||
import json
|
||||
|
||||
#----------------------------------------------------------
|
||||
class Database_cl(object):
|
||||
#----------------------------------------------------------
|
||||
# da es hier nur darum geht, die Daten dauerhaft zu speichern,
|
||||
# wird ein sehr einfacher Ansatz verwendet:
|
||||
# - es können Daten zu genau 15 Teams gespeichert werden
|
||||
# - je Team werden 2 Teilnehmer mit Namen, Vornamen und Matrikelnummer
|
||||
# berücksichtigt
|
||||
# - die Daten werden als eine JSON-Datei abgelegt
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
self.data_o = None
|
||||
self.readData_p()
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def create_px(self, data_opl):
|
||||
#-------------------------------------------------------
|
||||
# Überprüfung der Daten müsste ergänzt werden!
|
||||
# 'freien' Platz suchen,
|
||||
# falls vorhanden: belegen und Nummer des Platzes als Id zurückgeben
|
||||
|
||||
id_s = None
|
||||
for loop_i in range(0,15):
|
||||
if self.data_o[str(loop_i)][0] == '':
|
||||
id_s = str(loop_i)
|
||||
self.data_o[id_s] = data_opl
|
||||
self.saveData_p()
|
||||
break
|
||||
|
||||
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
|
||||
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:
|
||||
self.data_o[id_spl] = data_opl
|
||||
self.saveData_p()
|
||||
status_b = True
|
||||
|
||||
return status_b
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def delete_px(self, id_spl):
|
||||
#-------------------------------------------------------
|
||||
#print("\n---------------------- DELETE ---------------------")
|
||||
status_b = False
|
||||
if id_spl in self.data_o:
|
||||
pass
|
||||
# hier müssen Sie den Code ergänzen
|
||||
# Löschen als Zurücksetzen auf die Default-Werte implementieren
|
||||
# Ihre Ergänzung
|
||||
self.data_o[id_spl] = {}
|
||||
default_s = self.getDefault_px()
|
||||
self.data_o[id_spl] = default_s
|
||||
self.saveData_p()
|
||||
|
||||
return status_b
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def getDefault_px(self):
|
||||
#-------------------------------------------------------
|
||||
return ['', '', '', '', '', '', '', ''] # HIER müssen Sie eine Ergänzung vornehmen
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def readData_p(self):
|
||||
#-------------------------------------------------------
|
||||
try:
|
||||
fp_o = codecs.open(os.path.join('data', 'webteams.json'), 'r', 'utf-8')
|
||||
except:
|
||||
# Datei neu anlegen self.data_o = {}
|
||||
self.data_o = {}
|
||||
for loop_i in range(0,15):
|
||||
self.data_o[str(loop_i)] = ['', '', '', '', '', '', '', '']
|
||||
self.saveData_p()
|
||||
else:
|
||||
with fp_o:
|
||||
self.data_o = json.load(fp_o)
|
||||
|
||||
return
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def saveData_p(self):
|
||||
#-------------------------------------------------------
|
||||
with codecs.open(os.path.join('data', 'webteams.json'), 'w', 'utf-8') as fp_o:
|
||||
json.dump(self.data_o, fp_o)
|
||||
|
||||
|
||||
# EOF
|
114
Praktikum1_Kai/webteams/app/view.py
Normal file
114
Praktikum1_Kai/webteams/app/view.py
Normal file
@ -0,0 +1,114 @@
|
||||
# coding: utf-8
|
||||
|
||||
# sehr einfache Erzeugung des Markups für vollständige Seiten
|
||||
# jeweils 3 Abschnitte:
|
||||
# - begin
|
||||
# - content
|
||||
# - end
|
||||
|
||||
# bei der Liste wird der content-Abschnitt wiederholt
|
||||
# beim Formular nicht
|
||||
|
||||
import codecs
|
||||
import os.path
|
||||
import string
|
||||
|
||||
#----------------------------------------------------------
|
||||
class View_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
pass
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createList_px(self, data_opl):
|
||||
#-------------------------------------------------------
|
||||
# hier müsste noch eine Fehlerbehandlung ergänzt werden !
|
||||
markup_s = ''
|
||||
markup_s += self.readFile_p('list0.tpl')
|
||||
markupV_s = self.readFile_p('list1.tpl')
|
||||
lineT_o = string.Template(markupV_s)
|
||||
|
||||
# mehrfach nutzen, um die einzelnen Zeilen der Tabelle zu erzeugen
|
||||
for loop_i in range(0,15):
|
||||
data_a = data_opl[str(loop_i)]
|
||||
markup_s += lineT_o.safe_substitute (name1_s=data_a[0]
|
||||
, vorname1_s=data_a[1]
|
||||
, matrnr1_s=data_a[2]
|
||||
, semesteranzahl1_s=data_a[3]
|
||||
, name2_s=data_a[4]
|
||||
, vorname2_s=data_a[5]
|
||||
, matrnr2_s=data_a[6]
|
||||
, semesteranzahl2_s=data_a[7]
|
||||
, id_s=str(loop_i)
|
||||
)
|
||||
|
||||
markup_s += self.readFile_p('list2.tpl')
|
||||
|
||||
return markup_s
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createListAufz_px(self, data_opl):
|
||||
#-------------------------------------------------------
|
||||
# hier müsste noch eine Fehlerbehandlung ergänzt werden !
|
||||
markup_s = ''
|
||||
markup_s += self.readFile_p('listaufz0.tpl')
|
||||
markupV_s = self.readFile_p('listaufz1.tpl')
|
||||
lineT_o = string.Template(markupV_s)
|
||||
|
||||
# mehrfach nutzen, um die einzelnen Zeilen der Tabelle zu erzeugen
|
||||
for loop_i in range(0,15):
|
||||
data_a = data_opl[str(loop_i)]
|
||||
markup_s += lineT_o.safe_substitute (name1_s=data_a[0]
|
||||
, vorname1_s=data_a[1]
|
||||
, matrnr1_s=data_a[2]
|
||||
, semesteranzahl1_s=data_a[3]
|
||||
, name2_s=data_a[4]
|
||||
, vorname2_s=data_a[5]
|
||||
, matrnr2_s=data_a[6]
|
||||
, semesteranzahl2_s=data_a[7]
|
||||
, id_s=str(loop_i)
|
||||
)
|
||||
|
||||
markup_s += self.readFile_p('listaufz2.tpl')
|
||||
|
||||
return markup_s
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createForm_px(self, id_spl, data_opl):
|
||||
#-------------------------------------------------------
|
||||
# hier müsste noch eine Fehlerbehandlung ergänzt werden !
|
||||
markup_s = ''
|
||||
markup_s += self.readFile_p('form0.tpl')
|
||||
markupV_s = self.readFile_p('form1.tpl')
|
||||
lineT_o = string.Template(markupV_s)
|
||||
markup_s += lineT_o.safe_substitute (name1_s=data_opl[0]
|
||||
, vorname1_s=data_opl[1]
|
||||
, matrnr1_s=data_opl[2]
|
||||
, semesteranzahl1_s=data_opl[3]
|
||||
, name2_s=data_opl[4]
|
||||
, vorname2_s=data_opl[5]
|
||||
, matrnr2_s=data_opl[6]
|
||||
, semesteranzahl2_s=data_opl[7]
|
||||
, id_s=id_spl
|
||||
)
|
||||
markup_s += self.readFile_p('form2.tpl')
|
||||
|
||||
return markup_s
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def readFile_p(self, fileName_spl):
|
||||
#-------------------------------------------------------
|
||||
content_s = ''
|
||||
with codecs.open(os.path.join('template', fileName_spl), 'r', 'utf-8') as fp_o:
|
||||
content_s = fp_o.read()
|
||||
|
||||
return content_s
|
||||
|
||||
|
||||
# EOF
|
Reference in New Issue
Block a user