Praktikum 2 Progress
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user