11import logging
22from functools import wraps
33from ckan .plugins import toolkit
4+ from ckan import model
5+ from sqlalchemy import and_
46
57
68log = logging .getLogger (__name__ )
@@ -98,6 +100,36 @@ def get_ai_resources(limit=100):
98100 return ret
99101
100102
103+ def get_users_with_activity_info_token ():
104+ """
105+ Get all users that have an ActivityInfo API key set in their plugin_extras.
106+ Uses SQLAlchemy to query the JSONB plugin_extras column.
107+
108+ Returns:
109+ A list of user objects with ActivityInfo API keys.
110+ """
111+ # Query users where plugin_extras -> 'activity_info' -> 'api_key' exists and is not null
112+ # Use chained -> operators: plugin_extras -> 'activity_info' ->> 'api_key'
113+ users = model .Session .query (model .User ).filter (
114+ and_ (
115+ model .User .state == 'active' ,
116+ model .User .plugin_extras .isnot (None ),
117+ model .User .plugin_extras ['activity_info' ].isnot (None ),
118+ model .User .plugin_extras ['activity_info' ]['api_key' ].astext .isnot (None ),
119+ model .User .plugin_extras ['activity_info' ]['api_key' ].astext != '' ,
120+ )
121+ ).all ()
122+
123+ final_users = []
124+ for user in users :
125+ final_users .append ({
126+ 'id' : user .id ,
127+ 'name' : user .name ,
128+ })
129+
130+ return final_users
131+
132+
101133def require_sysadmin_user (func ):
102134 '''
103135 Decorator for flask view functions. Returns 403 response if no user is logged in or if the login user is external
@@ -111,4 +143,4 @@ def view_wrapper(*args, **kwargs):
111143 return toolkit .abort (403 , "Sysadmin user required" )
112144 return func (* args , ** kwargs )
113145
114- return view_wrapper
146+ return view_wrapper
0 commit comments