Praktikum 2 Progress
This commit is contained in:
BIN
Praktikum2/ppm1/app/__pycache__/__init__.cpython-35.pyc
Normal file
BIN
Praktikum2/ppm1/app/__pycache__/__init__.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Praktikum2/ppm1/app/__pycache__/application.cpython-35.pyc
Normal file
BIN
Praktikum2/ppm1/app/__pycache__/application.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Praktikum2/ppm1/app/__pycache__/database.cpython-35.pyc
Normal file
BIN
Praktikum2/ppm1/app/__pycache__/database.cpython-35.pyc
Normal file
Binary file not shown.
BIN
Praktikum2/ppm1/app/__pycache__/view.cpython-35.pyc
Normal file
BIN
Praktikum2/ppm1/app/__pycache__/view.cpython-35.pyc
Normal file
Binary file not shown.
@ -6,13 +6,51 @@ from .view import View_cl
|
||||
class Application_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
# Request Processing
|
||||
#-------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
# spezielle Initialisierung können hier eingetragen werden
|
||||
self.db_o = Database_cl()
|
||||
self.view_o = View_cl()
|
||||
self.liste = 0
|
||||
self.db = Database_cl()
|
||||
self.view = View_cl()
|
||||
@cherrypy.expose
|
||||
|
||||
#-------------------------------------------------------
|
||||
def index(self):
|
||||
#-------------------------------------------------------
|
||||
print("Index\n")
|
||||
return self.GenerateIndex()
|
||||
@cherrypy.expose
|
||||
|
||||
#-------------------------------------------------------
|
||||
def category(self, cat=None):
|
||||
#-------------------------------------------------------
|
||||
print("Category: ", cat, "\n")
|
||||
if(cat==None):
|
||||
return self.GenerateIndex()
|
||||
else:
|
||||
return self.GenerateList(cat)
|
||||
@cherrypy.expose
|
||||
|
||||
#-------------------------------------------------------
|
||||
def detail(self, cat=None, id=None):
|
||||
#-------------------------------------------------------
|
||||
print("Add: ", cat)
|
||||
if(cat!=None):
|
||||
return self.GenerateDetail(cat)
|
||||
else:
|
||||
return self.GenerateIndex()
|
||||
@cherrypy.expose
|
||||
|
||||
#-------------------------------------------------------
|
||||
def save(self, cat=None, **data):
|
||||
#-------------------------------------------------------
|
||||
print("Save: ", cat)
|
||||
dataTmp = data
|
||||
return self.GenerateSave(dataTmp, cat)
|
||||
@cherrypy.expose
|
||||
|
||||
#-------------------------------------------------------
|
||||
@ -24,7 +62,6 @@ class Application_cl(object):
|
||||
return self.createList_p(form)
|
||||
@cherrypy.expose
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def default(self, *arguments, **kwargs):
|
||||
#-------------------------------------------------------
|
||||
@ -33,4 +70,54 @@ class Application_cl(object):
|
||||
''+ \
|
||||
str(kwargs)
|
||||
raise cherrypy.HTTPError(404, msg_s)
|
||||
default.exposed= True
|
||||
default.exposed= True
|
||||
|
||||
#-------------------------------------------------------
|
||||
# Functions
|
||||
#-------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GenerateIndex(self):
|
||||
#-------------------------------------------------------
|
||||
return self.view.CreateIndex()
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GenerateList(self, category):
|
||||
#-------------------------------------------------------
|
||||
data = {}
|
||||
data['content'] = {}
|
||||
data['headings'] = {}
|
||||
data['category'] = category
|
||||
data['content'] = self.db.data[category]
|
||||
print(data, "\n")
|
||||
if(len(data['content']) != 0):
|
||||
print(len(data['content']))
|
||||
contentFirst = list(data['content'].keys())[0]
|
||||
data['headings'] = list(data['content'][contentFirst].keys())
|
||||
print(data, "\n")
|
||||
|
||||
return self.view.CreateList(data)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GenerateDetail(self, category, id=None):
|
||||
#-------------------------------------------------------
|
||||
data = {}
|
||||
if(id != None):
|
||||
data['content'] = self.db.ReadEntry(category, id)
|
||||
else:
|
||||
data['id'] = None
|
||||
data['category'] = category
|
||||
data['content'] = self.db.GetDefault(category)
|
||||
print(data, "\n")
|
||||
return self.view.CreateDetail(data)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GenerateSave(self, dataTmp, category):
|
||||
#-------------------------------------------------------
|
||||
if(category == None):
|
||||
return self.view.CreateIndex()
|
||||
else:
|
||||
self.db.Save(dataTmp, category)
|
||||
return self.GenerateList(category)
|
||||
|
||||
#EOF
|
@ -19,104 +19,103 @@ class Database_cl(object):
|
||||
#-------------------------------------------------------
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
self.data_o = None
|
||||
self.readData_p()
|
||||
self.data = {}
|
||||
self.data['students'] = {}
|
||||
self.data['teachers'] = {}
|
||||
self.data['companies'] = {}
|
||||
self.data['offerings'] = {}
|
||||
self.Read('students')
|
||||
self.Read('teachers')
|
||||
self.Read('companies')
|
||||
self.Read('offerings')
|
||||
|
||||
#-------------------------------------------------------
|
||||
def Read(self, category):
|
||||
#-------------------------------------------------------
|
||||
path = 'data/' + category
|
||||
if not(os.path.exists(path)):
|
||||
os.makedirs(path)
|
||||
categoryDir = os.listdir(path)
|
||||
for fileName in categoryDir:
|
||||
if fileName.endswith('.json') and fileName != 'last.json':
|
||||
file = codecs.open(os.path.join('data', category, fileName), 'rU', 'utf-8')
|
||||
fileContent = file.read()
|
||||
id = fileName[:-5]
|
||||
self.data[category][id] = json.loads(fileContent)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def create_px(self, data_opl):
|
||||
def ReadEntry(self, category = None, id = None):
|
||||
#-------------------------------------------------------
|
||||
# Ü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
|
||||
data = None
|
||||
if id == None:
|
||||
data = self.data
|
||||
else:
|
||||
if id_spl in self.data_o:
|
||||
data_o = self.data_o[id_spl]
|
||||
if id in self.data[category]:
|
||||
data = self.data[category][id]
|
||||
|
||||
return data_o
|
||||
|
||||
return data
|
||||
|
||||
#-------------------------------------------------------
|
||||
def update_px(self, id_spl, data_opl):
|
||||
def Save(self, data, category):
|
||||
#-------------------------------------------------------
|
||||
# Ü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()
|
||||
id = data['id']
|
||||
print("ID: ", id, "\n")
|
||||
if(id != "None"):
|
||||
if id in self.data[category]:
|
||||
file = codecs.open(os.path.join('data', category, id+'.json'), 'w', 'utf-8')
|
||||
file.write(json.dumps(data, indent=3, ensure_ascii=True))
|
||||
file.close()
|
||||
self.data[category][id] = data
|
||||
status_b = True
|
||||
else:
|
||||
data['id'] = self.IdNext(category)
|
||||
file = codecs.open(os.path.join('data', category, data['id']+'.json'), 'w', 'utf-8')
|
||||
file.write(json.dumps(data, indent=3, ensure_ascii=True))
|
||||
file.close()
|
||||
self.data[category][id] = data
|
||||
status_b = True
|
||||
|
||||
return status_b
|
||||
|
||||
|
||||
#-------------------------------------------------------
|
||||
def delete_px(self, id_spl):
|
||||
def Delete(self, category, id):
|
||||
#-------------------------------------------------------
|
||||
#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()
|
||||
if id in self.data[category]:
|
||||
os.remove(os.path.join('data', category, id+'json'))
|
||||
del self.data[category][id]
|
||||
|
||||
return status_b
|
||||
|
||||
#-------------------------------------------------------
|
||||
def GetDefault(self, category):
|
||||
#-------------------------------------------------------
|
||||
if(category == 'students'):
|
||||
return {'name':'name', 'vorname':'vorname', 'matrikelnummer':'matrikelnummer'}
|
||||
elif(category == 'teachers'):
|
||||
return {'titel':'titel', 'name':'name', 'vorname':'vorname', 'lehrgebiet':'lehrgebiet'}
|
||||
elif(category == 'companies'):
|
||||
return {'name':'name', 'branche':'branche', 'schwerpunkt':'schwerpunkt', 'sitz':'sitz', 'anzahlMitarbeiter':'anzahlMitarbeiter'}
|
||||
elif(category == 'offerings'):
|
||||
return {'name':'name', 'company':'company', 'beschreibung':'beschreibung', 'voraussetzungen':'voraussetzungen', 'firmenbetreuer':'firmenbetreuer'}
|
||||
|
||||
#-------------------------------------------------------
|
||||
def getDefault_px(self):
|
||||
def IdNext(self, category):
|
||||
#-------------------------------------------------------
|
||||
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()
|
||||
path = 'data/' + category + '/last.json'
|
||||
if(os.path.isfile(path)):
|
||||
file = open(os.path.join('data', category, 'last.json'), 'r+')
|
||||
last = file.read()
|
||||
last = str(int(last)+1)
|
||||
file.seek(0)
|
||||
file.write(last)
|
||||
file.close()
|
||||
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)
|
||||
|
||||
file = open(os.path.join('data', category, 'last.json'), 'w+')
|
||||
last = str(int(0))
|
||||
file.write(last)
|
||||
file.close()
|
||||
return last
|
||||
|
||||
# EOF
|
@ -7,21 +7,31 @@ class View_cl(object):
|
||||
#----------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------
|
||||
def __init__(self, path_spl):
|
||||
def __init__(self):
|
||||
#-------------------------------------------------------
|
||||
# Pfad hier zur Vereinfachung fest vorgeben
|
||||
self.path_s = os.path.join(path_spl, "template")
|
||||
self.lookup_o = TemplateLookup(directories=[self.path_s])
|
||||
# ... weitere Methoden
|
||||
self.path = 'templates'
|
||||
self.lookup = TemplateLookup(directories=['/'])
|
||||
|
||||
#-------------------------------------------------------
|
||||
def create_p(self, template_spl, data_opl):
|
||||
def Create(self, template, data):
|
||||
#-------------------------------------------------------
|
||||
# Auswertung mit templates
|
||||
template_o = self.lookup_o.get_template(template_spl)
|
||||
return template_o.render(data_o = data_opl) # hier wird da Template ausgeführt für die übergebenen Daten
|
||||
print("CreateView\n")
|
||||
template = Template(filename=os.path.join(self.path, template), output_encoding='utf-8', lookup=self.lookup)
|
||||
return template.render(data = data)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def createList_px(self, data_opl):
|
||||
def CreateIndex(self):
|
||||
#-------------------------------------------------------
|
||||
return self.create_p('liste.tpl', data_opl)
|
||||
print("CreateIndex\n")
|
||||
data = None
|
||||
return self.Create('index.tpl', data)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def CreateList(self, data):
|
||||
#-------------------------------------------------------
|
||||
return self.Create('list.tpl', data)
|
||||
|
||||
#-------------------------------------------------------
|
||||
def CreateDetail(self, data):
|
||||
#-------------------------------------------------------
|
||||
return self.Create('detail.tpl', data)
|
Reference in New Issue
Block a user