@@ -57,7 +57,7 @@ class PpmsConnection:
57
57
``auth_httpstatus``
58
58
"""
59
59
60
- def __init__ (self , url , api_key , timeout = 10 , cache = "" ):
60
+ def __init__ (self , url , api_key , timeout = 10 , cache = "" , cache_users_only = False ):
61
61
"""Constructor for the PPMS connection object.
62
62
63
63
Open a connection to the PUMAPI defined in `url` and try to authenticate
@@ -83,6 +83,11 @@ def __init__(self, url, api_key, timeout=10, cache=""):
83
83
individual text files. Useful for testing and for speeding up
84
84
slow requests like 'getusers'. By default empty, which will result
85
85
in no caching being done.
86
+ cache_users_only : bool, optional
87
+ If set to `True`, only `getuser` requests will be cached on disk.
88
+ This can be used in to speed up the slow requests (through the
89
+ cache), while everything else will be handled through online
90
+ requests. By default `False`.
86
91
87
92
Raises
88
93
------
@@ -101,6 +106,7 @@ def __init__(self, url, api_key, timeout=10, cache=""):
101
106
"auth_httpstatus" : - 1 ,
102
107
}
103
108
self .cache_path = cache
109
+ self .cache_users_only = cache_users_only
104
110
105
111
# run in cache-only mode (e.g. for testing or off-line usage) if no API
106
112
# key has been specified, skip authentication then:
@@ -241,6 +247,11 @@ def __interception_path(self, req_data, create_dir=False):
241
247
request (except credentials like 'apikey').
242
248
"""
243
249
action = req_data ["action" ]
250
+
251
+ if self .cache_users_only and action != "getuser" :
252
+ LOG .debug (f"NOT caching '{ action } ' (cache_users_only is set)" )
253
+ return None
254
+
244
255
intercept_dir = os .path .join (self .cache_path , action )
245
256
if create_dir and not os .path .exists (intercept_dir ): # pragma: no cover
246
257
try :
@@ -300,7 +311,7 @@ def __init__(self, text, status_code):
300
311
raise LookupError ("No cache path configured" )
301
312
302
313
intercept_file = self .__interception_path (req_data , create_dir = False )
303
- if not os .path .exists (intercept_file ): # pragma: no cover
314
+ if not intercept_file or not os .path .exists (intercept_file ): # pragma: no cover
304
315
raise LookupError (f"No cache hit for [{ intercept_file } ]" )
305
316
306
317
with open (intercept_file , "r" , encoding = "utf-8" ) as infile :
@@ -563,7 +574,7 @@ def get_running_sheet(self, core_facility_ref, date, ignore_uncached_users=False
563
574
similar. Note that only the date part is relevant, time will be ignored.
564
575
ignore_uncached_users : bool, optional
565
576
If set to `True` any booking for a user that is not present in the instance
566
- attribuge `fullname_mapping` will be ignored in the resulting list.
577
+ attribute `fullname_mapping` will be ignored in the resulting list.
567
578
568
579
Returns
569
580
-------
@@ -743,7 +754,7 @@ def get_user(self, login_name, skip_cache=False):
743
754
744
755
if not response .text :
745
756
msg = f"User [{ login_name } ] is unknown to PPMS"
746
- LOG .error (msg )
757
+ LOG .debug (msg )
747
758
raise KeyError (msg )
748
759
749
760
user = PpmsUser (response .text )
@@ -871,14 +882,17 @@ def get_user_ids(self, active=False):
871
882
LOG .debug (", " .join (users ))
872
883
return users
873
884
874
- def get_users (self , force_refresh = False ):
885
+ def get_users (self , force_refresh = False , active_only = True ):
875
886
"""Get user objects for all (or cached) PPMS users.
876
887
877
888
Parameters
878
889
----------
879
890
force_refresh : bool, optional
880
891
Re-request information from PPMS even if user details have been
881
892
cached locally before, by default False.
893
+ active_only : bool, optional
894
+ If set to `False` also "inactive" users will be fetched from PPMS,
895
+ by default `True`.
882
896
883
897
Returns
884
898
-------
@@ -888,7 +902,7 @@ def get_users(self, force_refresh=False):
888
902
if self .users and not force_refresh :
889
903
LOG .debug ("Using cached details for %s users" , len (self .users ))
890
904
else :
891
- self .update_users ()
905
+ self .update_users (active_only = active_only )
892
906
893
907
return self .users
894
908
@@ -1185,7 +1199,7 @@ def update_systems(self):
1185
1199
1186
1200
self .systems = systems
1187
1201
1188
- def update_users (self , user_ids = []):
1202
+ def update_users (self , user_ids = [], active_only = True ):
1189
1203
"""Update cached details for a list of users from PPMS.
1190
1204
1191
1205
Get the user details on a list of users (or all active ones) from PPMS and store
@@ -1199,9 +1213,12 @@ def update_users(self, user_ids=[]):
1199
1213
user_ids : list(str), optional
1200
1214
A list of user IDs (login names) to request the cache for, by
1201
1215
default [] which will result in all *active* users to be requested.
1216
+ active_only : bool, optional
1217
+ If set to `False` also "inactive" users will be fetched from PPMS,
1218
+ by default `True`.
1202
1219
"""
1203
1220
if not user_ids :
1204
- user_ids = self .get_user_ids (active = True )
1221
+ user_ids = self .get_user_ids (active = active_only )
1205
1222
1206
1223
LOG .debug ("Updating details on %s users" , len (user_ids ))
1207
1224
for user_id in user_ids :
0 commit comments