|
|
|
import peewee
|
|
|
|
|
|
|
|
|
|
|
|
DB_PROXY = peewee.Proxy()
|
|
|
|
|
|
|
|
|
|
|
|
class BaseModel(peewee.Model):
|
|
|
|
class Meta:
|
|
|
|
database = DB_PROXY
|
|
|
|
|
|
|
|
|
|
|
|
class Ordner(BaseModel):
|
|
|
|
id = peewee.AutoField()
|
|
|
|
parent = peewee.IntegerField()
|
|
|
|
name = peewee.TextField()
|
|
|
|
zuletzt_verwendet = peewee.TimestampField()
|
|
|
|
|
|
|
|
|
|
|
|
class Foto(BaseModel):
|
|
|
|
id = peewee.AutoField()
|
|
|
|
ordner = peewee.IntegerField()
|
|
|
|
name = peewee.CharField(null=True)
|
|
|
|
notiz = peewee.CharField(null=True) # 255?
|
|
|
|
|
|
|
|
|
|
|
|
def db_create_table():
|
|
|
|
DB_PROXY.create_tables([Ordner, Foto])
|
|
|
|
|
|
|
|
|
|
|
|
def update_root_verzeichnis(root_verzeichnis):
|
|
|
|
Ordner.update(name=root_verzeichnis, parent=0).where(Ordner.id == 1).execute()
|
|
|
|
|
|
|
|
|
|
|
|
def ordner_struktur_erstellen(ordner_liste, parent=1):
|
|
|
|
for ordner_einzel in ordner_liste:
|
|
|
|
ordner_existiert = False
|
|
|
|
for ordner in Ordner.select().where(Ordner.parent == parent and Ordner.name == ordner_einzel):
|
|
|
|
ordner_existiert = True
|
|
|
|
parent = ordner.id
|
|
|
|
if not ordner_existiert:
|
|
|
|
neuer_ordner = Ordner.create(parent=parent, name=ordner_einzel)
|
|
|
|
parent = neuer_ordner.id
|
|
|
|
return parent
|
|
|
|
|
|
|
|
|
|
|
|
def file_speichern(ordner_id, notiz, dateiendung):
|
|
|
|
ordner_name = Ordner.select(Ordner.name).where(Ordner.id == ordner_id).scalar()
|
|
|
|
file = Foto.create(ordner=ordner_id, notiz=notiz)
|
|
|
|
file_name = "{}_{}.{}".format(ordner_name, file.id, dateiendung)
|
|
|
|
file.name = file_name
|
|
|
|
file.save()
|
|
|
|
return file_name
|
|
|
|
|