Skip to content

Commit 44d4225

Browse files
committed
Add users
1 parent 28449dd commit 44d4225

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

ckanext/activityinfo/blueprints/admin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import logging
22
from flask import Blueprint
33
from ckan.plugins import toolkit
4-
from ckanext.activityinfo.utils import require_sysadmin_user, get_ai_resources
4+
from ckanext.activityinfo.utils import (
5+
require_sysadmin_user,
6+
get_ai_resources,
7+
get_users_with_activity_info_token,
8+
)
59

610

711
log = logging.getLogger(__name__)
@@ -17,7 +21,7 @@ def index():
1721
"""
1822
log.info('ActivityInfo admin index view')
1923

20-
ai_users = []
24+
ai_users = get_users_with_activity_info_token()
2125
ai_resources = get_ai_resources()
2226

2327
ctx = {

ckanext/activityinfo/templates/activity_info/admin.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<div class="internal-div-section">
77
<h1>Activity Info admin page</h1>
88

9+
<h2>Users with Activity Info tokens</h2>
910
<table id="activity-info-admin-users-list-table"
1011
class="table table-header table-hover table-bordered table-responsive">
1112
<thead>
@@ -26,6 +27,7 @@ <h1>Activity Info admin page</h1>
2627
</tbody>
2728
</table>
2829

30+
<h2>Activity Info linked resources</h2>
2931
<table id="activity-info-admin-resources"
3032
class="table table-header table-hover table-bordered table-responsive">
3133
<thead>

ckanext/activityinfo/utils.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
from functools import wraps
33
from ckan.plugins import toolkit
4+
from ckan import model
5+
from sqlalchemy import and_
46

57

68
log = 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+
101133
def 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

Comments
 (0)