2026-01-09 13:36:21 +01:00
|
|
|
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
|
2026-01-12 15:03:47 +01:00
|
|
|
- user: the user as a dict
|
2026-01-09 13:36:21 +01:00
|
|
|
- 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):
|
2026-01-10 16:08:21 +01:00
|
|
|
return True
|
2026-01-09 13:36:21 +01:00
|
|
|
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):
|
2026-01-12 15:03:47 +01:00
|
|
|
# return True
|
|
|
|
|
if 'user' in self.data:
|
|
|
|
|
print(f"found user {self.data['user']['name']}")
|
2026-01-09 13:36:21 +01:00
|
|
|
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
|
|
|
|
|
|
2026-01-12 15:03:47 +01:00
|
|
|
def store_login(self, user_dict):
|
|
|
|
|
self.data['user'] = user_dict
|
2026-01-09 13:36:21 +01:00
|
|
|
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):
|
2026-01-12 15:03:47 +01:00
|
|
|
self.data.pop('user', None)
|
2026-01-09 13:36:21 +01:00
|
|
|
self.data.pop('lastlogin_date', None)
|
2026-01-12 15:03:47 +01:00
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
|
def get_userid(self):
|
|
|
|
|
result = 0
|
|
|
|
|
if 'user' in self.data:
|
|
|
|
|
print()
|
|
|
|
|
result = self.data['user']['id']
|
|
|
|
|
return result
|