Add username and orgname to the config object instead of pass them to every object

This commit is contained in:
Mario de Frutos
2016-01-21 18:03:23 +01:00
parent 7c3ab87b78
commit 4d0cabb429
4 changed files with 25 additions and 11 deletions

View File

@@ -11,7 +11,7 @@ RETURNS Geometry AS $$
user_geocoder_config = GD["user_geocoder_config_{0}".format(username)]
# -- Check the quota
quota_service = quota_service.QuotaService(user_geocoder_config, redis_conn, username, orgname)
quota_service = quota_service.QuotaService(user_geocoder_config, redis_conn)
if not quota_service.check_user_quota():
plpy.error('You have reach the limit of your quota')

View File

@@ -11,7 +11,8 @@ class GeocoderConfig:
GEOCODER_CONFIG_KEYS = ['google_maps_client_id', 'google_maps_api_key',
'geocoding_quota', 'soft_geocoding_limit',
'geocoder_type', 'period_end_date',
'heremaps_app_id', 'heremaps_app_code']
'heremaps_app_id', 'heremaps_app_code', 'username',
'orgname']
NOKIA_GEOCODER_MANDATORY_KEYS = ['geocoding_quota', 'soft_geocoding_limit',
'heremaps_app_id', 'heremaps_app_code']
NOKIA_GEOCODER = 'heremaps'
@@ -23,6 +24,8 @@ class GeocoderConfig:
GEOCODER_TYPE = 'geocoder_type'
QUOTA_KEY = 'geocoding_quota'
SOFT_LIMIT_KEY = 'soft_geocoding_limit'
USERNAME_KEY = 'username'
ORGNAME_KEY = 'orgname'
PERIOD_END_DATE = 'period_end_date'
def __init__(self, redis_connection, username, orgname=None,
@@ -38,6 +41,8 @@ class GeocoderConfig:
heremaps_app_code=None):
user_config = self._redis_connection.hgetall(
"rails:users:{0}".format(username))
user_config[self.USERNAME_KEY] = username
user_config[self.ORGNAME_KEY] = orgname
user_config[self.NOKIA_GEOCODER_APP_ID_KEY] = heremaps_app_id
user_config[self.NOKIA_GEOCODER_APP_CODE_KEY] = heremaps_app_code
if orgname:
@@ -64,6 +69,11 @@ class GeocoderConfig:
return True
def __parse_config(self, filtered_config):
self._username = filtered_config[self.USERNAME_KEY].lower()
if filtered_config[self.ORGNAME_KEY]:
self._orgname = filtered_config[self.ORGNAME_KEY].lower()
else:
self._orgname = None
self._geocoder_type = filtered_config[self.GEOCODER_TYPE].lower()
self._period_end_date = date_parse(filtered_config[self.PERIOD_END_DATE])
self._google_maps_private_key = None
@@ -123,3 +133,11 @@ class GeocoderConfig:
@property
def heremaps_app_code(self):
return self._heremaps_app_code
@property
def username(self):
return self._username
@property
def organization(self):
return self._orgname

View File

@@ -6,14 +6,10 @@ class QuotaService:
""" Class to manage all the quota operation for
the Geocoder SQL API Extension """
def __init__(self, user_geocoder_config, redis_connection, username, orgname=None):
def __init__(self, user_geocoder_config, redis_connection):
self._user_geocoder_config = user_geocoder_config
self._user_service = user_service.UserService(
self._user_geocoder_config,
redis_connection,
username,
orgname
)
self._user_geocoder_config, redis_connection)
def check_user_quota(self):
""" Check if the current user quota surpasses the current quota """

View File

@@ -17,11 +17,11 @@ class UserService:
REDIS_CONNECTION_PORT = "redis_port"
REDIS_CONNECTION_DB = "redis_db"
def __init__(self, user_geocoder_config, redis_connection, username, orgname=None):
def __init__(self, user_geocoder_config, redis_connection):
self._user_geocoder_config = user_geocoder_config
self._redis_connection = redis_connection
self._username = username
self._orgname = orgname
self._username = user_geocoder_config.username
self._orgname = user_geocoder_config.organization
def used_quota(self, service_type, date):
""" Recover the used quota for the user in the current month """