WEB/Praktikum2/ppm1/app/database.py
2016-11-06 23:38:59 +01:00

121 lines
4.7 KiB
Python

# 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 = {}
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 ReadEntry(self, category = None, id = None):
#-------------------------------------------------------
data = None
if id == None:
data = self.data
else:
if id in self.data[category]:
data = self.data[category][id]
return data
#-------------------------------------------------------
def Save(self, data, category):
#-------------------------------------------------------
status_b = False
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(self, category, id):
#-------------------------------------------------------
status_b = False
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 IdNext(self, category):
#-------------------------------------------------------
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:
file = open(os.path.join('data', category, 'last.json'), 'w+')
last = str(int(0))
file.write(last)
file.close()
return last
# EOF