Merge pull request #67 from ckan/2.10-support

CKAN 2.10 support
This commit is contained in:
Sergey 2022-06-08 19:16:38 +03:00 committed by GitHub
commit c2c7b1ce9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 58 deletions

View File

@ -19,7 +19,7 @@ jobs:
needs: lint needs: lint
strategy: strategy:
matrix: matrix:
ckan-version: ["2.10", 2.9, 2.9-py2, 2.8, 2.7] ckan-version: ["2.10", 2.9, 2.9-py2, 2.8]
fail-fast: false fail-fast: false
name: CKAN ${{ matrix.ckan-version }} name: CKAN ${{ matrix.ckan-version }}
@ -49,19 +49,27 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Install requirements - name: Install requirements
run: | run: |
pip install -r dev-requirements.txt pip install -r requirements.txt
pip install -e . pip install -e .
# Replace default path to CKAN core config file with the one on the container # Replace default path to CKAN core config file with the one on the container
sed -i -e 's/use = config:.*/use = config:\/srv\/app\/src\/ckan\/test-core.ini/' test.ini sed -i -e 's/use = config:.*/use = config:\/srv\/app\/src\/ckan\/test-core.ini/' test.ini
- name: Setup extension (CKAN >= 2.9) - name: Setup extension (CKAN 2.10)
if: ${{ matrix.ckan-version != '2.7' && matrix.ckan-version != '2.8' }} if: ${{ matrix.ckan-version == '2.10' }}
run: | run: |
pip install -r dev-requirements.txt
ckan -c test.ini db init
ckan -c test.ini db upgrade -p googleanalytics
- name: Setup extension (CKAN == 2.9)
if: ${{ matrix.ckan-version == '2.9' || matrix.ckan-version == '2.9-py2' }}
run: |
pip install -r dev-requirements-2.9.txt
ckan -c test.ini db init ckan -c test.ini db init
ckan -c test.ini db upgrade -p googleanalytics ckan -c test.ini db upgrade -p googleanalytics
- name: Setup extension (CKAN < 2.9) - name: Setup extension (CKAN < 2.9)
if: ${{ matrix.ckan-version == '2.7' || matrix.ckan-version == '2.8' }} if: ${{ matrix.ckan-version == '2.8' }}
run: | run: |
pip install -r dev-requirements-2.9.txt
paster --plugin=ckan db init -c test.ini paster --plugin=ckan db init -c test.ini
paster --plugin=ckanext-googleanalytics initdb -c test.ini paster --plugin=ckanext-googleanalytics initdb -c test.ini
- name: Run tests - name: Run tests
run: pytest --ckan-ini=test.ini --disable-warnings ckanext/googleanalytics/tests run: pytest --ckan-ini=test.ini --cov=ckanext.googleanalytics --disable-warnings ckanext/googleanalytics/tests

View File

@ -2,7 +2,7 @@
**Status:** Production **Status:** Production
**CKAN Version:** >= 2.7 **CKAN Version:** >= 2.8
A CKAN extension that both sends tracking data to Google Analytics and A CKAN extension that both sends tracking data to Google Analytics and
retrieves statistics from Google Analytics and inserts them into CKAN pages. retrieves statistics from Google Analytics and inserts them into CKAN pages.
@ -106,7 +106,7 @@ See `Googles' documentation<https://support.google.com/analytics/answer/1034342?
set up the required database tables (of course, altering the set up the required database tables (of course, altering the
``--config`` option to point to your site config file): ``--config`` option to point to your site config file):
paster initdb --config=../ckan/development.ini ckan -c ../ckan/ckan.ini initdb
3. Follow the steps in the *Authorization* section below. 3. Follow the steps in the *Authorization* section below.
@ -160,9 +160,9 @@ Before ckanext-googleanalytics can retrieve statistics from Google Analytics, yo
There are some very high-level functional tests that you can run using:: There are some very high-level functional tests that you can run using::
(pyenv)~/pyenv/src/ckan$ nosetests --ckan ../ckanext-googleanalytics/tests/ $ pip install -r dev-requirements.txt
$ pytest --ckan-ini=test.ini
(note -- that's run from the CKAN software root, not the extension root)
## Future ## Future

View File

@ -4,7 +4,6 @@ import logging
from ckan.lib.base import BaseController, c, render, request from ckan.lib.base import BaseController, c, render, request
from . import dbutil from . import dbutil
import ckan.logic as logic
import hashlib import hashlib
from . import plugin from . import plugin
@ -12,17 +11,9 @@ from paste.util.multidict import MultiDict
from ckan.controllers.api import ApiController from ckan.controllers.api import ApiController
from ckan.exceptions import CkanVersionException
import ckan.plugins.toolkit as tk import ckan.plugins.toolkit as tk
from ckanext.googleanalytics import utils from ckanext.googleanalytics import utils
try:
tk.requires_ckan_version("2.9")
except CkanVersionException:
pass
else:
from builtins import str
log = logging.getLogger("ckanext.googleanalytics") log = logging.getLogger("ckanext.googleanalytics")
@ -56,7 +47,7 @@ class GAApiController(ApiController):
def action(self, logic_function, ver=None): def action(self, logic_function, ver=None):
try: try:
function = logic.get_action(logic_function) function = tk.get_action(logic_function)
side_effect_free = getattr(function, "side_effect_free", False) side_effect_free = getattr(function, "side_effect_free", False)
request_data = self._get_request_data( request_data = self._get_request_data(
try_url_params=side_effect_free try_url_params=side_effect_free

View File

@ -78,36 +78,6 @@ def get_resource_visits_for_url(url):
return count and count[0] or "" return count and count[0] or ""
""" get_top_packages is broken, and needs to be rewritten to work with
CKAN 2.*. This is because ckan.authz has been removed in CKAN 2.*
See commit ffa86c010d5d25fa1881c6b915e48f3b44657612
"""
def get_top_packages(limit=20):
items = []
# caveat emptor: the query below will not filter out private
# or deleted datasets (TODO)
q = model.Session.query(model.Package)
connection = model.Session.connection()
package_stats = get_table("package_stats")
s = select(
[
package_stats.c.package_id,
package_stats.c.visits_recently,
package_stats.c.visits_ever,
]
).order_by(package_stats.c.visits_recently.desc())
res = connection.execute(s).fetchmany(limit)
for package_id, recent, ever in res:
item = q.filter(text("package.id = '%s'" % package_id))
if not item.count():
continue
items.append((item.first(), recent, ever))
return items
def get_top_resources(limit=20): def get_top_resources(limit=20):
items = [] items = []
connection = model.Session.connection() connection = model.Session.connection()

View File

@ -3,16 +3,25 @@ import factory
from factory.alchemy import SQLAlchemyModelFactory from factory.alchemy import SQLAlchemyModelFactory
from pytest_factoryboy import register from pytest_factoryboy import register
from ckan.plugins import toolkit
import ckan.model as model import ckan.model as model
from ckanext.googleanalytics.model import PackageStats, ResourceStats from ckanext.googleanalytics.model import PackageStats, ResourceStats
@pytest.fixture() if toolkit.check_ckan_version("2.9"):
def clean_db(reset_db, migrate_db_for): @pytest.fixture()
reset_db() def clean_db(reset_db, migrate_db_for):
migrate_db_for("googleanalytics") reset_db()
migrate_db_for("googleanalytics")
else:
from ckanext.googleanalytics.dbutil import init_tables
@pytest.fixture()
def clean_db(reset_db):
reset_db()
init_tables()
@register @register

View File

@ -7,7 +7,6 @@ import six
from flask import Blueprint from flask import Blueprint
from werkzeug.utils import import_string from werkzeug.utils import import_string
import ckan.logic as logic
import ckan.plugins.toolkit as tk import ckan.plugins.toolkit as tk
import ckan.views.api as api import ckan.views.api as api
import ckan.views.resource as resource import ckan.views.resource as resource
@ -22,7 +21,7 @@ ga = Blueprint("google_analytics", "google_analytics")
def action(logic_function, ver=api.API_MAX_VERSION): def action(logic_function, ver=api.API_MAX_VERSION):
try: try:
function = logic.get_action(logic_function) function = tk.get_action(logic_function)
side_effect_free = getattr(function, "side_effect_free", False) side_effect_free = getattr(function, "side_effect_free", False)
request_data = api._get_request_data(try_url_params=side_effect_free) request_data = api._get_request_data(try_url_params=side_effect_free)
if isinstance(request_data, dict): if isinstance(request_data, dict):

4
dev-requirements-2.9.txt Normal file
View File

@ -0,0 +1,4 @@
pytest<=6.0.2
pytest-ckan
pytest-factoryboy
pytest-cov

View File

@ -1,2 +1 @@
pytest-ckan pytest-ckan
pytest-factoryboy