import shelve import datetime DBFILE = "config.shelve" class Configuration(): """ This class holds persistent configuration: - appkey: the app key, alowing traffic with the backend - last_key_validation_date: last date the appkey was validated, should be no more than a week ago otherwise the app will not run - user: the user as a dict - userlang: the users language - lastlogin_date: date of last sucessful login """ loaded = False data = {} def __init__(self, app): self.app = app self.load() def load(self): path = self.app.paths.config / DBFILE if path.exists(): shelve_file = shelve.open(path) self.data = shelve_file['data'] shelve_file.close() print("shelve opened") print(self.data) def save(self): shelve_file = shelve.open(self.app.paths.config / DBFILE) shelve_file['data'] = self.data shelve_file.close() print("shelve stored") def has_valid_app_key(self): return True if 'appkey' in self.data: today = datetime.date.today() last_month = today - datetime.timedelta(days=30) if self.data['last_key_validation_date'] > last_month: self.data['last_key_validation_date'] = today return True return False def store_app_key(self, new_key): self.data['appkey'] = new_key self.data['last_key_validation_date'] = datetime.date.today() self.save() def user_is_logged_in(self): # return True if 'user' in self.data: print(f"found user {self.data['user']['name']}") today = datetime.date.today() last_week = today - datetime.timedelta(days=7) if self.data['lastlogin_date'] > last_week: self.update_last_activity_date() return True return False def store_login(self, user_dict): self.data['user'] = user_dict self.data['lastlogin_date'] = datetime.date.today() self.save() def update_last_activity_date(self): today = datetime.date.today() self.data['last_key_validation_date'] = today self.data['lastlogin_date'] = today def logout_user(self): self.data.pop('user', None) self.data.pop('lastlogin_date', None) self.save() def get_userid(self): result = 0 if 'user' in self.data: print() result = self.data['user']['id'] return result