190 lines
8.3 KiB
Python
190 lines
8.3 KiB
Python
"""
|
|
Session helper
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
|
|
"""
|
|
import uuid
|
|
import datetime
|
|
|
|
sessions = []
|
|
begin_time = None
|
|
city = None
|
|
greenery_catalog = None
|
|
construction_catalog = None
|
|
usage_catalog = None
|
|
|
|
roofs_associated_to_percentage = {'10': ['BuildingAX5th:ZoneX56Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_1',
|
|
'BuildingAX5th:Corridor_Roof_1_0_2',
|
|
'BuildingAX5th:Corridor_Roof_1_0_3',
|
|
'BuildingAX3rd:ZoneX76Xspace_Roof_1_0_0'],
|
|
'20': ['BuildingBX2nd:Airbnb2_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_1'],
|
|
'30': ['BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX53Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_0'],
|
|
'40': ['AdditionalBuildings:ZoneX73Xspace_Roof_1_0_2',
|
|
'BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_4',
|
|
'BuildingAX5th:ZoneX54Xspace_Roof_1_0_0'],
|
|
'50': ['BuildingBX2nd:Airbnb2_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX53Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_1',
|
|
'BuildingAX5th:ZoneX55Xspace_Roof_1_0_0'],
|
|
'60': ['BuildingBX2nd:Airbnb2_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX53Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX54Xspace_Roof_1_0_0'],
|
|
'70': ['BuildingBX2nd:Airbnb2_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX56Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX53Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_1',
|
|
'BuildingAX5th:Corridor_Roof_1_0_2',
|
|
'BuildingAX5th:Corridor_Roof_1_0_3',
|
|
'BuildingAX5th:ZoneX54Xspace_Roof_1_0_0',
|
|
'BuildingAX3rd:ZoneX76Xspace_Roof_1_0_0'],
|
|
'80': ['BuildingCX2nd:ZoneX41Xspace_Roof_1_0_0',
|
|
'BuildingBX2nd:Corridor_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb2_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX53Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_2',
|
|
'BuildingAX5th:Corridor_Roof_1_0_4',
|
|
'BuildingAX5th:ZoneX54Xspace_Roof_1_0_0'],
|
|
'90': ['AdditionalBuildings:ZoneX73Xspace_Roof_1_0_0',
|
|
'AdditionalBuildings:ZoneX73Xspace_Roof_1_0_1',
|
|
'AdditionalBuildings:ZoneX73Xspace_Roof_1_0_2',
|
|
'BuildingCX2nd:ZoneX41Xspace_Roof_1_0_0',
|
|
'BuildingBX2nd:Corridor_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb2_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX53Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_4',
|
|
'BuildingAX5th:ZoneX54Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX55Xspace_Roof_1_0_0'],
|
|
'100': ['AdditionalBuildings:ZoneX73Xspace_Roof_1_0_0',
|
|
'AdditionalBuildings:ZoneX73Xspace_Roof_1_0_1',
|
|
'AdditionalBuildings:ZoneX73Xspace_Roof_1_0_2',
|
|
'BuildingCX2nd:ZoneX41Xspace_Roof_1_0_0',
|
|
'BuildingBX2nd:Corridor_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb2_Roof_1_0_0',
|
|
'BuildingBX2nd:Airbnb_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX56Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX53Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_0',
|
|
'BuildingAX5th:Corridor_Roof_1_0_1',
|
|
'BuildingAX5th:Corridor_Roof_1_0_2',
|
|
'BuildingAX5th:Corridor_Roof_1_0_3',
|
|
'BuildingAX5th:Corridor_Roof_1_0_4',
|
|
'BuildingAX5th:ZoneX54Xspace_Roof_1_0_0',
|
|
'BuildingAX5th:ZoneX55Xspace_Roof_1_0_0',
|
|
'BuildingAX3rd:ZoneX76Xspace_Roof_1_0_0'],
|
|
}
|
|
|
|
|
|
class SessionData:
|
|
def __init__(self, session):
|
|
self._session = session
|
|
|
|
@property
|
|
def city(self):
|
|
return self._session['city']
|
|
|
|
@property
|
|
def id(self):
|
|
return self._session['session_id']
|
|
|
|
@property
|
|
def token(self):
|
|
return self._session['token']
|
|
|
|
@property
|
|
def headers(self):
|
|
return {'session_id': str(self.id), 'token': str(self.token)}
|
|
|
|
@property
|
|
def greenery_percentage(self):
|
|
return self._session['greenery_percentage']
|
|
|
|
@greenery_percentage.setter
|
|
def greenery_percentage(self, value):
|
|
self._session['greenery_percentage'] = value
|
|
|
|
@property
|
|
def greenery_catalog(self):
|
|
return greenery_catalog
|
|
|
|
@property
|
|
def construction_catalog(self):
|
|
return construction_catalog
|
|
|
|
@property
|
|
def usage_catalog(self):
|
|
return usage_catalog
|
|
|
|
|
|
def active_session(session_id):
|
|
for i in range(len(sessions)):
|
|
_session = sessions[i]
|
|
_expire = datetime.datetime.strptime(_session['expire'], '%Y-%m-%d %H:%M:%S.%f')
|
|
if _session['session_id'] == session_id:
|
|
if _expire > datetime.datetime.now():
|
|
return True, i
|
|
else:
|
|
# Ensure remove old sessions
|
|
del sessions[i]
|
|
return False, -1
|
|
return False, -1
|
|
|
|
|
|
def _valid_session(session_id, ip, token):
|
|
active, i = active_session(session_id)
|
|
if active:
|
|
return sessions[i]['token'] == token and sessions[i]['ip'] == ip, i
|
|
return False, -1
|
|
|
|
|
|
def remove_session(request):
|
|
"""
|
|
Remove a session from the sessions array
|
|
"""
|
|
session = eval(request.headers.get('session'))
|
|
session_id = session['session_id']
|
|
token = session['token']
|
|
ip = request.remote_addr
|
|
valid, i = _valid_session(session_id, ip, token)
|
|
if valid:
|
|
del sessions[i]
|
|
return valid
|
|
|
|
|
|
def refresh_session(request):
|
|
"""
|
|
Validate and extend current session
|
|
:return: valid, token, city
|
|
"""
|
|
session_header = request.headers.get('session')
|
|
if session_header is None:
|
|
return None
|
|
session = eval(session_header)
|
|
session_id = session['session_id']
|
|
token = session['token']
|
|
ip = request.remote_addr
|
|
valid, i = _valid_session(session_id, ip, token)
|
|
if valid:
|
|
if session_id == 'debug':
|
|
new_token = 'debug'
|
|
else:
|
|
new_token = str(uuid.uuid4())
|
|
expire = str(datetime.datetime.now() + datetime.timedelta(minutes=5))
|
|
sessions[i]['token'] = new_token
|
|
sessions[i]['expire'] = expire
|
|
return SessionData(sessions[i])
|
|
else:
|
|
return None
|