From 620f780113cacea4c6dbc0f3a9322c3a26c385e1 Mon Sep 17 00:00:00 2001 From: Zergling_man Date: Fri, 23 Feb 2024 20:03:45 +1100 Subject: [PATCH] file backend should be fully implemented now --- backends/README.py | 2 ++ backends/file.py | 60 ++++++++++++++++++++++++++++++++-------------- models.py | 2 +- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/backends/README.py b/backends/README.py index b2eaaf2..1e23e67 100644 --- a/backends/README.py +++ b/backends/README.py @@ -11,10 +11,12 @@ def load_user(username): pass def save_user(user): pass def delete_user(user): pass +def load_token(tokenid): pass def load_tokens(user): pass def save_token(token): pass def delete_token(token): pass +def load_invite(inviteid): pass def load_invites(user): pass def save_invite(token): pass def delete_invite(token): pass \ No newline at end of file diff --git a/backends/file.py b/backends/file.py index b69f9b7..b19bbfc 100644 --- a/backends/file.py +++ b/backends/file.py @@ -11,29 +11,53 @@ def init(): try: open(p,'a').close() 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): - for line in linegen(users): - split=line.split(',') - if split[0]==username: return models.User(*split) + return _load(username,0,users,models.User) def save_user(user): - gen=linegen(users) - 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}) + return _save(user,0,users,user.username) def delete_user(user): - gen=linegen(users) - for line in gen: - split=line.split(',') - if split[0]==user.username: remove(users,[gen.send(True)]); break + return _delete(user,0,users,user.username) -def load_tokens(user): pass -def save_token(token): pass -def delete_token(token): pass +def load_token(tokeid): + return _load(tokeid,0,access_tokens,models.AccessToken) +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 save_invite(token): pass -def delete_invite(token): pass +def load_invite(tokeid): + return _load(tokeid,0,invite_tokens,models.InviteToken) +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): i=0 diff --git a/models.py b/models.py index 14d68e6..f64ca8b 100644 --- a/models.py +++ b/models.py @@ -58,7 +58,7 @@ class User(AbstractUser): invited_by: AbstractUser=None # Root node will just reference itself email: str='' @property - def csv(self): + def serialise(self): return ','.join([self.username,self.password_hash,str(self.salt),self.invited_by.username,self.email]) def create_inv_token(self,*args,**kwargs): tok=InviteToken(self,*args,**kwargs)