You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6 KiB
Python

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?
class FotoUndefiniert(BaseModel):
message_id = peewee.IntegerField(primary_key=True)
chat_id = peewee.IntegerField()
file_id = peewee.TextField()
def db_create_table():
DB_PROXY.create_tables([Ordner, Foto, FotoUndefiniert])
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