From b70b26f39237ae9a86bf4d161b445a1651c5c08b Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Fri, 6 May 2022 17:29:58 +0300 Subject: [PATCH] Prepare to v2.10 --- README.md | 13 -- .../assets/googleanalytics_event_tracking.js | 28 ++-- ckanext/googleanalytics/helpers.py | 67 ++++++++++ ckanext/googleanalytics/plugin/__init__.py | 92 +++---------- requirements.txt | 4 - setup.cfg | 75 +++++++++++ setup.py | 48 +------ tests/__init__.py | 0 tests/accountsfixture.xml | 1 - tests/downloadfixture.xml | 8 -- tests/mockgoogleanalytics.py | 61 --------- tests/packagefixture.xml | 9 -- tests/test_general.py | 122 ------------------ 13 files changed, 174 insertions(+), 354 deletions(-) create mode 100644 ckanext/googleanalytics/helpers.py delete mode 100644 requirements.txt create mode 100644 setup.cfg delete mode 100644 tests/__init__.py delete mode 100644 tests/accountsfixture.xml delete mode 100644 tests/downloadfixture.xml delete mode 100644 tests/mockgoogleanalytics.py delete mode 100644 tests/packagefixture.xml delete mode 100644 tests/test_general.py diff --git a/README.md b/README.md index 6523c0b..d5ad362 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,6 @@ retrieves statistics from Google Analytics and inserts them into CKAN pages. googleanalytics_resource_prefix = /downloads/ googleanalytics.domain = auto - googleanalytics.track_events = false googleanalytics.fields = {} googleanalytics.enable_user_id = false googleanalytics.download_handler = ckan.views.resource:download @@ -76,11 +75,6 @@ retrieves statistics from Google Analytics and inserts them into CKAN pages. `_ for more info. - If ``track_events`` is set, Google Analytics event tracking will be - enabled. *CKAN 1.x only.* *Note that event tracking for resource downloads - is always enabled,* ``track_events`` *enables event tracking for other - pages as well.* - ``fields`` allows you to specify various options when creating the tracker. See `Google's documentation `. If ``enable_user_id`` is set to ``true``, then logged in users will be tracked into the Google Analytics' dashboard. @@ -114,13 +108,6 @@ See `Googles' documentation=2.0.0 -google-api-python-client>=1.6.1, <1.7.0 -pyOpenSSL>=16.2.0 -rsa>=3.1.4, <=4.0 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..0872496 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,75 @@ +[metadata] +name = ckanext-googleanalytics +version = 2.0.8 +description = Add GA tracking and reporting to CKAN instance +long_description = file: README.md +long_description_content_type = text/markdown +url = https://github.com/ckan/ckanext-googleanalytics +author = Seb Bacon +author_email = seb.bacon@gmail.com +license = AGPL +classifiers = + Development Status :: 4 - Beta + License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+) + Programming Language :: Python :: 2.7 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 +keywords = + CKAN + +[options] +# python_requires = >= 3.7 +install_requires = + typing_extensions + +packages = find: +namespace_packages = ckanext +include_package_data = True + +[options.extras_require] +requirements = + gdata>=2.0.0 + google-api-python-client>=1.6.1, <1.7.0 + pyOpenSSL>=16.2.0 + rsa>=3.1.4, <=4.0 + +[options.entry_points] +ckan.plugins = + googleanalytics = ckanext.googleanalytics.plugin:GoogleAnalyticsPlugin +paste.paster_command = + loadanalytics = ckanext.googleanalytics.commands:LoadAnalytics + initdb = ckanext.googleanalytics.commands:InitDB + +babel.extractors = + ckan = ckan.lib.extract:extract_ckan +[extract_messages] +keywords = translate isPlural +add_comments = TRANSLATORS: +output_file = ckanext/googleanalytics/i18n/ckanext-googleanalytics.pot +width = 80 + +[init_catalog] +domain = ckanext-googleanalytics +input_file = ckanext/googleanalytics/i18n/ckanext-googleanalytics.pot +output_dir = ckanext/googleanalytics/i18n + +[update_catalog] +domain = ckanext-googleanalytics +input_file = ckanext/googleanalytics/i18n/ckanext-googleanalytics.pot +output_dir = ckanext/googleanalytics/i18n +previous = true + +[compile_catalog] +domain = ckanext-googleanalytics +directory = ckanext/googleanalytics/i18n +statistics = true + +[tool:pytest] +filterwarnings = + ignore::sqlalchemy.exc.SADeprecationWarning + ignore::sqlalchemy.exc.SAWarning + ignore::DeprecationWarning + +addopts = --ckan-ini test.ini diff --git a/setup.py b/setup.py index 6da33ac..6068493 100644 --- a/setup.py +++ b/setup.py @@ -1,47 +1,3 @@ -import os -from io import open -from setuptools import setup, find_packages -HERE = os.path.dirname(__file__) +from setuptools import setup -version = "2.0.7" - -extras_require = {} -_extras_groups = [ - ('requirements', 'requirements.txt'), -] -for group, filepath in _extras_groups: - with open(os.path.join(HERE, filepath), 'r') as f: - extras_require[group] = f.readlines() - -# Get the long description from the relevant file -with open(os.path.join(HERE, 'README.md'), encoding='utf-8') as f: - long_description = f.read() - -setup( - name="ckanext-googleanalytics", - version=version, - description="Add GA tracking and reporting to CKAN instance", - long_description=long_description, - long_description_content_type="text/markdown", - classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers - keywords="", - author="Seb Bacon", - author_email="seb.bacon@gmail.com", - url="", - license="", - packages=find_packages(exclude=["ez_setup", "examples", "tests"]), - namespace_packages=["ckanext", "ckanext.googleanalytics"], - include_package_data=True, - zip_safe=False, - install_requires=[], - extras_require=extras_require, - entry_points=""" - [ckan.plugins] - # Add plugins here, eg - googleanalytics=ckanext.googleanalytics.plugin:GoogleAnalyticsPlugin - - [paste.paster_command] - loadanalytics = ckanext.googleanalytics.commands:LoadAnalytics - initdb = ckanext.googleanalytics.commands:InitDB - """, -) +setup() diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/accountsfixture.xml b/tests/accountsfixture.xml deleted file mode 100644 index 565ad08..0000000 --- a/tests/accountsfixture.xml +++ /dev/null @@ -1 +0,0 @@ -2011-03-13T01:59:55.435-08:00http://www.google.com/analytics/feeds/accounts/seb.bacon@okfn.orgGoogle AnalyticsGoogle Analytics ga:visitorType==New Visitorga:visitorType==Returning Visitorga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==ppcga:medium==organicga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==organic,ga:medium==ppcga:medium==(none)ga:medium==referralga:goalCompletionsAll>0ga:transactions>0ga:isMobile==Yesga:bounces==011300http://www.google.com/analytics/feeds/accounts/ga:42156377ga:421563772011-03-13T01:59:55.435-08:00borfProfile list for seb.bacon@okfn.org diff --git a/tests/downloadfixture.xml b/tests/downloadfixture.xml deleted file mode 100644 index 6db89d5..0000000 --- a/tests/downloadfixture.xml +++ /dev/null @@ -1,8 +0,0 @@ -http://www.google.com/analytics/feeds/data?ids=ga:42156377&dimensions=ga:pagePath&metrics=ga:newVisits,ga:uniquePageviews,ga:visitors,ga:visits&filters=ga:pagePath%3D~%5E/downloads/&start-date=2011-03-22&end-date=2011-04-05datagm.staging.ckan.net/ga:421563772011-04-05T03:08:58.420-07:00false - -http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/downloads/http%3A%2F%2Fwww.annakarenina.com%2Findex.json&filters=ga:pagePath%3D~%5E/downloads/&start-date=2011-03-22&end-date=2011-04-052011-04-04T17:00:00.001-07:00ga:pagePath=/downloads/http%3A%2F%2Fwww.annakarenina.com%2Findex.json - - -missingthing2011-04-04T17:00:00.001-07:00ga:pagePath=/downloads/missingthing - -Google Analytics1Google Analytics Data for Profile 42156377Google Analytics2011-04-05432011-03-2210000 diff --git a/tests/mockgoogleanalytics.py b/tests/mockgoogleanalytics.py deleted file mode 100644 index 1babbf5..0000000 --- a/tests/mockgoogleanalytics.py +++ /dev/null @@ -1,61 +0,0 @@ -import os -import BaseHTTPServer -import threading - -here_dir = os.path.dirname(os.path.abspath(__file__)) - - -class MockHandler(BaseHTTPServer.BaseHTTPRequestHandler): - def do_GET(self): - if "feeds/accounts/default" in self.path: - self.send_response(200) - self.end_headers() - fixture = os.path.join(here_dir, "accountsfixture.xml") - content = open(fixture, "r").read() - elif "analytics/feeds/data" in self.path: - if "dataset" in self.path: - fixture = os.path.join(here_dir, "packagefixture.xml") - elif "download" in self.path: - fixture = os.path.join(here_dir, "downloadfixture.xml") - self.send_response(200) - self.end_headers() - content = open(fixture, "r").read() - else: - self.send_response(200) - self.end_headers() - content = "empty" - self.wfile.write(content) - - def do_POST(self): - if "ClientLogin" in self.path: - self.send_response(200) - self.end_headers() - content = "Auth=blah" - else: - self.send_response(200) - self.end_headers() - content = "empty" - self.wfile.write(content) - - def do_QUIT(self): - self.send_response(200) - self.end_headers() - self.server.stop = True - - -class ReusableServer(BaseHTTPServer.HTTPServer): - allow_reuse_address = 1 - - def serve_til_quit(self): - self.stop = False - while not self.stop: - self.handle_request() - - -def runmockserver(): - server_address = ("localhost", 6969) - httpd = ReusableServer(server_address, MockHandler) - httpd_thread = threading.Thread(target=httpd.serve_til_quit) - httpd_thread.setDaemon(True) - httpd_thread.start() - return httpd_thread diff --git a/tests/packagefixture.xml b/tests/packagefixture.xml deleted file mode 100644 index 26a5417..0000000 --- a/tests/packagefixture.xml +++ /dev/null @@ -1,9 +0,0 @@ -http://www.google.com/analytics/feeds/data?ids=ga:42156377&dimensions=ga:pagePath&metrics=ga:newVisits,ga:uniquePageviews,ga:visitors,ga:visits&filters=ga:pagePath%3D~%5E/dataset/&start-date=2011-03-22&end-date=2011-04-05datagm.staging.ckan.net/ga:421563772011-04-05T03:08:57.106-07:00false - -http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/dataset/annakarenina&filters=ga:pagePath%3D~%5E/dataset/&start-date=2011-03-22&end-date=2011-04-052011-04-04T17:00:00.001-07:00ga:pagePath=/dataset/annakarenina - -http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/dataset/annakarenina/invalid&filters=ga:pagePath%3D~%5E/dataset/&start-date=2011-03-22&end-date=2011-04-052011-04-04T17:00:00.001-07:00ga:pagePath=/dataset/annakarenina/invalid - -http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/dataset/annakarenina-invalid&filters=ga:pagePath%3D~%5E/dataset/&start-date=2011-03-22&end-date=2011-04-052011-04-04T17:00:00.001-07:00ga:pagePath=/dataset/annakarenina-invalid - -Google Analytics1Google Analytics Data for Profile 42156377Google Analytics2011-04-051522011-03-2210000 diff --git a/tests/test_general.py b/tests/test_general.py deleted file mode 100644 index 060ddbc..0000000 --- a/tests/test_general.py +++ /dev/null @@ -1,122 +0,0 @@ -import httplib -from unittest import TestCase - -from ckan.config.middleware import make_app -from paste.deploy import appconfig -import paste.fixture -from ckan.tests import conf_dir, url_for, CreateTestData - -from mockgoogleanalytics import runmockserver -from ckanext.googleanalytics.commands import LoadAnalytics -from ckanext.googleanalytics.commands import InitDB -from ckanext.googleanalytics import dbutil -import ckanext.googleanalytics.gasnippet as gasnippet - - -class MockClient(httplib.HTTPConnection): - def request(self, http_request): - filters = http_request.uri.query.get("filters") - path = http_request.uri.path - if filters: - if "dataset" in filters: - path += "/dataset" - else: - path += "/download" - httplib.HTTPConnection.request(self, http_request.method, path) - resp = self.getresponse() - return resp - - -class TestConfig(TestCase): - def test_config(self): - config = appconfig("config:test.ini", relative_to=conf_dir) - config.local_conf["ckan.plugins"] = "googleanalytics" - config.local_conf["googleanalytics.id"] = "" - command = LoadAnalytics("loadanalytics") - command.CONFIG = config.local_conf - self.assertRaises(Exception, command.run, []) - - -class TestLoadCommand(TestCase): - @classmethod - def setup_class(cls): - InitDB("initdb").run([]) # set up database tables - - config = appconfig("config:test.ini", relative_to=conf_dir) - config.local_conf["ckan.plugins"] = "googleanalytics" - config.local_conf["googleanalytics.username"] = "borf" - config.local_conf["googleanalytics.password"] = "borf" - config.local_conf["googleanalytics.id"] = "UA-borf-1" - config.local_conf["googleanalytics.show_downloads"] = "true" - cls.config = config.local_conf - wsgiapp = make_app(config.global_conf, **config.local_conf) - env = { - "HTTP_ACCEPT": ( - "text/html;q=0.9,text/plain;" "q=0.8,image/png,*/*;q=0.5" - ) - } - cls.app = paste.fixture.TestApp(wsgiapp, extra_environ=env) - CreateTestData.create() - runmockserver() - - @classmethod - def teardown_class(cls): - CreateTestData.delete() - conn = httplib.HTTPConnection("localhost:%d" % 6969) - conn.request("QUIT", "/") - conn.getresponse() - - def test_analytics_snippet(self): - response = self.app.get(url_for(controller="tag", action="index")) - code = gasnippet.header_code % ( - self.config["googleanalytics.id"], - "auto", - ) - assert code in response.body - - def test_top_packages(self): - command = LoadAnalytics("loadanalytics") - command.TEST_HOST = MockClient("localhost", 6969) - command.CONFIG = self.config - command.run([]) - packages = dbutil.get_top_packages() - resources = dbutil.get_top_resources() - self.assertEquals(packages[0][1], 2) - self.assertEquals(resources[0][1], 4) - - def test_download_count_inserted(self): - command = LoadAnalytics("loadanalytics") - command.TEST_HOST = MockClient("localhost", 6969) - command.CONFIG = self.config - command.run([]) - response = self.app.get( - url_for(controller="package", action="read", id="annakarenina") - ) - assert "[downloaded 4 times]" in response.body - - def test_js_inserted_resource_view(self): - from nose import SkipTest - - raise SkipTest("Test won't work until CKAN 1.5.2") - - from ckan.logic.action import get - from ckan import model - - context = {"model": model, "ignore_auth": True} - data = {"id": "annakarenina"} - pkg = get.package_show(context, data) - resource_id = pkg["resources"][0]["id"] - - command = LoadAnalytics("loadanalytics") - command.TEST_HOST = MockClient("localhost", 6969) - command.CONFIG = self.config - command.run([]) - response = self.app.get( - url_for( - controller="package", - action="resource_read", - id="annakarenina", - resource_id=resource_id, - ) - ) - assert 'onclick="javascript: _gaq.push(' in response.body