base app with app-key, registration, login, logout
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
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
|
||||
- username: the user name
|
||||
- userpwd: the users pwd
|
||||
- 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):
|
||||
if 'appkey' in self.data:
|
||||
print("found app key {self.data['appkey']}")
|
||||
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):
|
||||
if 'username' in self.data:
|
||||
print("found user {self.data['username']}")
|
||||
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, pwd):
|
||||
self.data['username'] = user
|
||||
self.data['userpwd'] = pwd
|
||||
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('username', None)
|
||||
self.data.pop('userpwd', None)
|
||||
self.data.pop('lastlogin_date', None)
|
||||
self.save()
|
||||
Reference in New Issue
Block a user