file backend should be fully implemented now

This commit is contained in:
Zergling_man 2024-02-23 20:03:45 +11:00
parent d097acaecf
commit 620f780113
3 changed files with 45 additions and 19 deletions

View File

@ -11,10 +11,12 @@ def load_user(username): pass
def save_user(user): pass def save_user(user): pass
def delete_user(user): pass def delete_user(user): pass
def load_token(tokenid): pass
def load_tokens(user): pass def load_tokens(user): pass
def save_token(token): pass def save_token(token): pass
def delete_token(token): pass def delete_token(token): pass
def load_invite(inviteid): pass
def load_invites(user): pass def load_invites(user): pass
def save_invite(token): pass def save_invite(token): pass
def delete_invite(token): pass def delete_invite(token): pass

View File

@ -11,29 +11,53 @@ def init():
try: open(p,'a').close() try: open(p,'a').close()
except PermissionError: raise Exception(f"Backend file can't be written: {p}") except PermissionError: raise Exception(f"Backend file can't be written: {p}")
def _load(name,pos,file,obj):
for line in linegen(file):
split=line.split(',')
if split[pos]==name: return obj(*split)
raise Exception('No such',obj.__name__,'with identifier',name)
def _load_multi(name,pos,file,obj):
out=[]
for line in linegen(file):
split=line.split(',')
if split[pos]==name: out.append(obj(*split))
return out
def _save(thing,pos,file,comp):
gen=linegen(file)
for line in gen:
split=line.split(',')
if split[pos]==comp: update(file,{gen.send(True):thing.serialise}); break
else: update(file,{-1:thing.serialise})
def _delete(thing,pos,file,comp):
gen=linegen(file)
for line in gen:
split=line.split(',')
if split[pos]==comp: remove(file,[gen.send(True)]); break
def load_user(username): def load_user(username):
for line in linegen(users): return _load(username,0,users,models.User)
split=line.split(',')
if split[0]==username: return models.User(*split)
def save_user(user): def save_user(user):
gen=linegen(users) return _save(user,0,users,user.username)
for line in gen:
split=line.split(',')
if split[0]==user.username: update(users,{gen.send(True):user.csv}); break
else: update(users,{-1:user.csv})
def delete_user(user): def delete_user(user):
gen=linegen(users) return _delete(user,0,users,user.username)
for line in gen:
split=line.split(',')
if split[0]==user.username: remove(users,[gen.send(True)]); break
def load_tokens(user): pass def load_token(tokeid):
def save_token(token): pass return _load(tokeid,0,access_tokens,models.AccessToken)
def delete_token(token): pass def load_tokens(user):
return _load_multi(user.username,1,access_tokens,models.AccessToken)
def save_token(token):
return _save(token,0,access_tokens,token.value)
def delete_token(token):
return _delete(token,0,access_tokens,token.value)
def load_invites(user): pass def load_invite(tokeid):
def save_invite(token): pass return _load(tokeid,0,invite_tokens,models.InviteToken)
def delete_invite(token): pass def load_invites(user):
return _load_multi(user.username,1,invite_tokens,models.InviteToken)
def save_invite(token):
return _save(token,0,invite_tokens,token.value)
def delete_invite(token):
return _delete(token,0,invite_tokens,token.value)
def linegen(path): def linegen(path):
i=0 i=0

View File

@ -58,7 +58,7 @@ class User(AbstractUser):
invited_by: AbstractUser=None # Root node will just reference itself invited_by: AbstractUser=None # Root node will just reference itself
email: str='' email: str=''
@property @property
def csv(self): def serialise(self):
return ','.join([self.username,self.password_hash,str(self.salt),self.invited_by.username,self.email]) return ','.join([self.username,self.password_hash,str(self.salt),self.invited_by.username,self.email])
def create_inv_token(self,*args,**kwargs): def create_inv_token(self,*args,**kwargs):
tok=InviteToken(self,*args,**kwargs) tok=InviteToken(self,*args,**kwargs)