Skip to content

Commit e733208

Browse files
authored
Merge pull request #6 from okfn/define_ai_access
Add a activity info logo an link in the user page
2 parents a26bebe + ca639aa commit e733208

File tree

7 files changed

+102
-13
lines changed

7 files changed

+102
-13
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
.btn {
22
margin: 7px;
33
}
4+
5+
.fa-activity-info-ico {
6+
/* Use kobo official image */
7+
background-image: url(/img/logo-activity-info.png);
8+
background-size: 18px 18px;
9+
height: 18px;
10+
width: 18px;
11+
content: "";
12+
display: inline-block;
13+
}

ckanext/activityinfo/blueprints/activity_info.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313

1414
@activityinfo_bp.route('/')
1515
def index():
16+
""" Home page
17+
If the user has an API key, redirect to databases
18+
If not, show a page to enter the API key
19+
"""
20+
extra_vars = {
21+
'api_key': get_user_token(current_user.name),
22+
}
23+
if extra_vars['api_key']:
24+
return toolkit.redirect_to('activity_info.databases')
25+
return toolkit.render('activity_info/index.html', extra_vars)
26+
27+
28+
@activityinfo_bp.route('/api-key')
29+
def api_key():
30+
""" Create or update the current ActivityInfo API key for the logged-in user.
31+
"""
1632
extra_vars = {
1733
'api_key': get_user_token(current_user.name),
1834
}
@@ -30,7 +46,7 @@ def databases():
3046
message = f"Could not retrieve ActivityInfo databases: {e}"
3147
log.error(message)
3248
toolkit.h.flash_error(message)
33-
return toolkit.redirect_to('activity_info.index')
49+
return toolkit.redirect_to('activity_info.api_key')
3450

3551
log.info(f"Retrieved {ai_databases}")
3652
# add the ActivityInfo URL to each database
@@ -110,7 +126,7 @@ def update_api_key():
110126
message = 'Missing ActivityInfo API key.'
111127
log.error(message)
112128
toolkit.h.flash_error(message)
113-
return toolkit.redirect_to('activity_info.index')
129+
return toolkit.redirect_to('activity_info.api_key')
114130

115131
plugin_extras = get_activity_info_user_plugin_extras(toolkit.c.user) or {}
116132
activity_info_extras = plugin_extras.get('activity_info', {})
@@ -125,7 +141,7 @@ def update_api_key():
125141
}
126142
)
127143
toolkit.h.flash_success('ActivityInfo API key updated successfully.')
128-
return toolkit.redirect_to('activity_info.index')
144+
return toolkit.redirect_to('activity_info.api_key')
129145

130146

131147
@activityinfo_bp.route('/remove-api-key', methods=['POST'])
@@ -134,7 +150,7 @@ def remove_api_key():
134150
plugin_extras = get_activity_info_user_plugin_extras(toolkit.c.user) or {}
135151
if not plugin_extras or 'activity_info' not in plugin_extras:
136152
toolkit.h.flash_error('No ActivityInfo API key found to remove.')
137-
return toolkit.redirect_to('activity_info.index')
153+
return toolkit.redirect_to('activity_info.api_key')
138154

139155
activity_info_extras = plugin_extras.get('activity_info', {})
140156
activity_info_extras.pop('api_key', None)
@@ -148,4 +164,4 @@ def remove_api_key():
148164
}
149165
)
150166
toolkit.h.flash_success('ActivityInfo API key removed successfully.')
151-
return toolkit.redirect_to('activity_info.index')
167+
return toolkit.redirect_to('activity_info.api_key')
1.35 KB
Loading

ckanext/activityinfo/templates/activity_info/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ <h2 class="module-heading"><i class="fa fa-line-chart"></i> Activity Info</h2>
1212
<a href="{% url_for 'activity_info.databases' %}" title="My databases">My databases</a>
1313
</li>
1414
<li class="nav-item">
15-
<a href="{% url_for 'activity_info.index' %}" title="Change my ActivityInfo API key">Change my ActivityInfo API key</a>
15+
<a href="{% url_for 'activity_info.api_key' %}" title="Change my ActivityInfo API key">Change my ActivityInfo API key</a>
1616
</li>
1717
{% else %}
1818
<li class="nav-item">
19-
<a href="{% url_for 'activity_info.index' %}" title="Set my ActivityInfo API key">Set my ActivityInfo API key</a>
19+
<a href="{% url_for 'activity_info.api_key' %}" title="Set my ActivityInfo API key">Set my ActivityInfo API key</a>
2020
</li>
2121
{% endif %}
2222
</ul>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% ckan_extends %}
2+
3+
{% block content_primary_nav %}
4+
{{ super() }}
5+
6+
{{ h.build_nav_icon(
7+
'activity_info.index', 'Activity Info',
8+
icon='activity-info-ico'
9+
)
10+
}}
11+
12+
{% endblock %}

ckanext/activityinfo/tests/test_home.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest import mock
12
from types import SimpleNamespace
23
import pytest
34
from ckantoolkit.tests import factories as ckan_factories
@@ -22,13 +23,29 @@ def test_regular_user(self, app, setup_data):
2223

2324
resp = app.get("/activity-info", headers=environ)
2425
assert resp.status_code == 200
25-
# CKAN users without a registered ActivityInfo token, will be asked to add one
26+
# This must be redirected to the /api-key URL
2627
assert "Add API key" in resp.body
2728

2829
def test_activityinfo_user(self, app, setup_data):
2930
environ = {"Authorization": setup_data.activityinfo_user["token"]}
30-
31-
resp = app.get("/activity-info", headers=environ)
32-
assert resp.status_code == 200
33-
# Activity info users will see the ActivityInfo UI
34-
assert "Update API key" in resp.body
31+
fake_databases = [
32+
{
33+
"databaseId": "0001",
34+
"label": "Database label 01",
35+
"description": "",
36+
"ownerId": "999999999",
37+
"billingAccountId": 8888888888888888,
38+
"suspended": False,
39+
"publishedTemplate": False,
40+
"languages": []
41+
},
42+
]
43+
44+
with mock.patch(
45+
"ckanext.activityinfo.data.base.ActivityInfoClient.get_databases",
46+
return_value=fake_databases,
47+
):
48+
resp = app.get("/activity-info", headers=environ)
49+
assert resp.status_code == 200
50+
# Activity info users will be redirected to see their databases
51+
assert "Activity Info databases" in resp.body
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from types import SimpleNamespace
2+
import pytest
3+
from ckantoolkit.tests import factories as ckan_factories
4+
from ckanext.activityinfo.tests import factories
5+
6+
7+
@pytest.fixture
8+
def setup_data():
9+
"""test setup data"""
10+
obj = SimpleNamespace()
11+
# Create CKAN users
12+
obj.activityinfo_user = factories.ActivityInfoUser()
13+
obj.regular_user = ckan_factories.UserWithToken()
14+
15+
return obj
16+
17+
18+
@pytest.mark.usefixtures("clean_db")
19+
class TestActivityInfoUIApiKey:
20+
def test_regular_user(self, app, setup_data):
21+
environ = {"Authorization": setup_data.regular_user["token"]}
22+
23+
resp = app.get("/activity-info/api-key", headers=environ)
24+
assert resp.status_code == 200
25+
# CKAN users without a registered ActivityInfo token, will be asked to add one
26+
assert "Add API key" in resp.body
27+
28+
def test_activityinfo_user(self, app, setup_data):
29+
environ = {"Authorization": setup_data.activityinfo_user["token"]}
30+
31+
resp = app.get("/activity-info/api-key", headers=environ)
32+
assert resp.status_code == 200
33+
# Activity info users will be redirected to see their databases
34+
assert "Update API key" in resp.body

0 commit comments

Comments
 (0)