Add actions
This commit is contained in:
parent
1d48695a93
commit
86ec2858a5
|
@ -0,0 +1,48 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
import ckan.plugins.toolkit as tk
|
||||
from ckan.logic import validate
|
||||
|
||||
from . import schema
|
||||
from ..model import PackageStats, ResourceStats
|
||||
|
||||
|
||||
def get_actions():
|
||||
return dict(
|
||||
googleanalytics_package_stats_show=googleanalytics_package_stats_show,
|
||||
googleanalytics_resource_stats_show=googleanalytics_resource_stats_show
|
||||
)
|
||||
|
||||
|
||||
@validate(schema.googleanalytics_package_stats_show)
|
||||
@tk.side_effect_free
|
||||
def googleanalytics_package_stats_show(context, data_dict):
|
||||
tk.check_access("googleanalytics_package_stats_show", context, data_dict)
|
||||
rec = (
|
||||
context["session"]
|
||||
.query(PackageStats)
|
||||
.filter(PackageStats.package_id == data_dict["id"])
|
||||
.one_or_none()
|
||||
)
|
||||
|
||||
if not rec:
|
||||
raise tk.ObjectNotFound()
|
||||
|
||||
return rec.for_json(context)
|
||||
|
||||
|
||||
@validate(schema.googleanalytics_resource_stats_show)
|
||||
@tk.side_effect_free
|
||||
def googleanalytics_resource_stats_show(context, data_dict):
|
||||
tk.check_access("googleanalytics_resource_stats_show", context, data_dict)
|
||||
rec = (
|
||||
context["session"]
|
||||
.query(ResourceStats)
|
||||
.filter(ResourceStats.resource_id == data_dict["id"])
|
||||
.one_or_none()
|
||||
)
|
||||
|
||||
if not rec:
|
||||
raise tk.ObjectNotFound()
|
||||
|
||||
return rec.for_json(context)
|
|
@ -0,0 +1,18 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
from ckan.authz import is_authorized
|
||||
|
||||
|
||||
def get_auth():
|
||||
return dict(
|
||||
googleanalytics_package_stats_show=googleanalytics_package_stats_show,
|
||||
googleanalytics_resource_stats_show=googleanalytics_resource_stats_show
|
||||
)
|
||||
|
||||
|
||||
def googleanalytics_package_stats_show(context, data_dict):
|
||||
return {"success": is_authorized("package_show", context, data_dict)}
|
||||
|
||||
|
||||
def googleanalytics_resource_stats_show(context, data_dict):
|
||||
return {"success": is_authorized("resource_show", context, data_dict)}
|
|
@ -0,0 +1,11 @@
|
|||
from ckan.logic.schema import validator_args
|
||||
|
||||
|
||||
@validator_args
|
||||
def googleanalytics_package_stats_show(not_empty):
|
||||
return {"id": [not_empty]}
|
||||
|
||||
|
||||
@validator_args
|
||||
def googleanalytics_resource_stats_show(not_empty):
|
||||
return {"id": [not_empty]}
|
|
@ -0,0 +1,29 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
from sqlalchemy import Column, String, Integer
|
||||
|
||||
from ckan.lib.dictization import table_dictize
|
||||
|
||||
from .base import Base
|
||||
|
||||
|
||||
class PackageStats(Base):
|
||||
__tablename__ = "package_stats"
|
||||
|
||||
package_id = Column(String(60), primary_key=True)
|
||||
visits_recently = Column(Integer)
|
||||
visits_ever = Column(Integer)
|
||||
|
||||
def for_json(self, context):
|
||||
return table_dictize(self, context)
|
||||
|
||||
|
||||
class ResourceStats(Base):
|
||||
__tablename__ = "resource_stats"
|
||||
|
||||
resource_id = Column(String(60), primary_key=True)
|
||||
visits_recently = Column(Integer)
|
||||
visits_ever = Column(Integer)
|
||||
|
||||
def for_json(self, context):
|
||||
return table_dictize(self, context)
|
|
@ -0,0 +1,4 @@
|
|||
from sqlalchemy.ext.declarative import declarative_base
|
||||
import ckan.model.meta as meta
|
||||
|
||||
Base = declarative_base(metadata=meta.metadata)
|
|
@ -12,7 +12,8 @@ import ckan.plugins.toolkit as tk
|
|||
|
||||
from ckan.exceptions import CkanConfigurationException, CkanVersionException
|
||||
|
||||
from ckanext.googleanalytics import helpers
|
||||
from .. import helpers
|
||||
from ..logic import action, auth
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -57,6 +58,14 @@ class GoogleAnalyticsPlugin(GAMixinPlugin, p.SingletonPlugin):
|
|||
p.implements(p.IConfigurable, inherit=True)
|
||||
p.implements(p.IConfigurer, inherit=True)
|
||||
p.implements(p.ITemplateHelpers)
|
||||
p.implements(p.IActions)
|
||||
p.implements(p.IAuthFunctions)
|
||||
|
||||
def get_auth_functions(self):
|
||||
return auth.get_auth()
|
||||
|
||||
def get_actions(self):
|
||||
return action.get_actions()
|
||||
|
||||
def configure(self, config):
|
||||
# spawn a pool of 5 threads, and pass them queue instance
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
import pytest
|
||||
import factory
|
||||
|
||||
from factory.alchemy import SQLAlchemyModelFactory
|
||||
from pytest_factoryboy import register
|
||||
|
||||
import ckan.model as model
|
||||
|
||||
from ckanext.googleanalytics.model import PackageStats, ResourceStats
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def clean_db(reset_db, migrate_db_for):
|
||||
reset_db()
|
||||
migrate_db_for("googleanalytics")
|
||||
|
||||
|
||||
@register
|
||||
class PackageStatsFactory(SQLAlchemyModelFactory):
|
||||
class Meta:
|
||||
sqlalchemy_session = model.Session
|
||||
model = PackageStats
|
||||
|
||||
package_id = factory.Faker("uuid4")
|
||||
visits_recently = factory.Faker("pyint")
|
||||
visits_ever = factory.Faker("pyint")
|
||||
|
||||
|
||||
@register
|
||||
class ResourceStatsFactory(SQLAlchemyModelFactory):
|
||||
class Meta:
|
||||
sqlalchemy_session = model.Session
|
||||
model = ResourceStats
|
||||
|
||||
resource_id = factory.Faker("uuid4")
|
||||
visits_recently = factory.Faker("pyint")
|
||||
visits_ever = factory.Faker("pyint")
|
|
@ -0,0 +1,44 @@
|
|||
import pytest
|
||||
|
||||
from ckan.tests.helpers import call_action
|
||||
import ckan.plugins.toolkit as tk
|
||||
import ckan.model as model
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("with_plugins", "clean_db")
|
||||
class TestPackageStatsShow:
|
||||
|
||||
def test_existing(self, package_stats):
|
||||
with pytest.raises(tk.ObjectNotFound):
|
||||
call_action(
|
||||
"googleanalytics_package_stats_show",
|
||||
id=package_stats.package_id
|
||||
)
|
||||
model.Session.commit()
|
||||
|
||||
rec = call_action(
|
||||
"googleanalytics_package_stats_show", id=package_stats.package_id
|
||||
)
|
||||
|
||||
assert rec["visits_recently"] == rec["visits_recently"]
|
||||
assert rec["visits_ever"] == rec["visits_ever"]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("with_plugins", "clean_db")
|
||||
class TestResourceStatsShow:
|
||||
|
||||
def test_existing(self, resource_stats):
|
||||
with pytest.raises(tk.ObjectNotFound):
|
||||
call_action(
|
||||
"googleanalytics_resource_stats_show",
|
||||
id=resource_stats.resource_id
|
||||
)
|
||||
model.Session.commit()
|
||||
|
||||
rec = call_action(
|
||||
"googleanalytics_resource_stats_show",
|
||||
id=resource_stats.resource_id
|
||||
)
|
||||
|
||||
assert rec["visits_recently"] == rec["visits_recently"]
|
||||
assert rec["visits_ever"] == rec["visits_ever"]
|
|
@ -1 +1,2 @@
|
|||
pytest-ckan
|
||||
pytest-factoryboy
|
||||
|
|
Loading…
Reference in New Issue