-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
concept and conceptscheme visit log #214
- Loading branch information
Showing
12 changed files
with
285 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""visit_log | ||
Revision ID: 1ad2b6fbcf22 | ||
Revises: 441c5a16ef8 | ||
Create Date: 2015-07-27 13:29:04.840631 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '1ad2b6fbcf22' | ||
down_revision = '441c5a16ef8' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
op.create_table('conceptscheme_visit_log', | ||
sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True), | ||
sa.Column('conceptscheme_id', sa.String(), nullable=False), | ||
sa.Column('visited_at', sa.DateTime(), nullable=False), | ||
sa.Column('origin', sa.String, nullable=False) | ||
) | ||
op.create_table('concept_visit_log', | ||
sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True), | ||
sa.Column('concept_id', sa.Integer(), nullable=False), | ||
sa.Column('conceptscheme_id', sa.String(), nullable=False), | ||
sa.Column('visited_at', sa.DateTime(), nullable=False), | ||
sa.Column('origin', sa.String, nullable=False) | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table('concept_visit_log') | ||
op.drop_table('conceptscheme_visit_log') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
from atramhasis.data.models import ( | ||
ConceptschemeVisitLog, | ||
ConceptVisitLog | ||
) | ||
from pyramid.response import Response | ||
|
||
|
||
def _origin_from_request(request): | ||
if 'text/html' in request.accept: | ||
return 'HTML' | ||
elif 'application/json' in request.accept: | ||
return 'REST' | ||
elif 'application/rdf+xml' in request.accept \ | ||
or 'text/turtle' in request.accept \ | ||
or 'application/x-turtle' in request.accept: | ||
return 'RDF' | ||
else: | ||
return 'onbekend' | ||
|
||
|
||
def _origin_from_response(response): | ||
if response.content_type == 'text/html': | ||
return 'HTML' | ||
elif response.content_type == 'application/json': | ||
return 'REST' | ||
elif response.content_type == 'application/rdf+xml' \ | ||
or response.content_type == 'text/turtle' \ | ||
or response.content_type == 'application/x-turtle': | ||
return 'RDF' | ||
else: | ||
return 'onbekend' | ||
|
||
|
||
def audit(fn): | ||
''' | ||
use this decorator to audit an operation and to log the visit | ||
* rdf routes with .rdf, .ttl extensions have html accept mimetypes, | ||
the origin is derived form the response content type. | ||
* for REST and HTML the view results are not of the pyramid.response Response type, | ||
the origin is derived from the accept header. | ||
''' | ||
|
||
def advice(parent_object, *args, **kw): | ||
request = parent_object.request | ||
audit_manager = request.data_managers['audit_manager'] | ||
|
||
if 'c_id' in request.matchdict.keys(): | ||
visit_log = ConceptVisitLog( | ||
concept_id=request.matchdict['c_id'], | ||
conceptscheme_id=request.matchdict['scheme_id'] | ||
) | ||
else: | ||
visit_log = ConceptschemeVisitLog(conceptscheme_id=request.matchdict['scheme_id']) | ||
response = fn(parent_object, *args, **kw) | ||
|
||
if isinstance(response, Response): | ||
visit_log.origin = _origin_from_response(response) | ||
else: | ||
visit_log.origin = _origin_from_request(request) | ||
|
||
audit_manager.save(visit_log) | ||
|
||
return response | ||
|
||
return advice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy import ( | ||
Column, | ||
Integer, | ||
String, | ||
DateTime) | ||
from sqlalchemy.sql import ( | ||
func | ||
) | ||
|
||
Base = declarative_base() | ||
Base = declarative_base() | ||
|
||
|
||
class ConceptschemeVisitLog(Base): | ||
__tablename__ = 'conceptscheme_visit_log' | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
conceptscheme_id = Column(String(25), nullable=False) | ||
visited_at = Column(DateTime, default=func.now(), nullable=False) | ||
origin = Column(String(25), nullable=False) | ||
|
||
|
||
class ConceptVisitLog(Base): | ||
__tablename__ = 'concept_visit_log' | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
concept_id = Column(Integer, nullable=False) | ||
conceptscheme_id = Column(String(25), nullable=False) | ||
visited_at = Column(DateTime, default=func.now(), nullable=False) | ||
origin = Column(String(25), nullable=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
from atramhasis.audit import audit, _origin_from_response | ||
from pyramid.response import Response | ||
|
||
try: | ||
from unittest.mock import Mock, MagicMock | ||
except ImportError: | ||
from mock import Mock, MagicMock, call # pragma: no cover | ||
|
||
|
||
class RecordingManager(object): | ||
|
||
def __init__(self): | ||
self.saved_objects = [] | ||
|
||
def save(self, object): | ||
self.saved_objects.append(object) | ||
|
||
|
||
class DummyParent(object): | ||
|
||
def __init__(self): | ||
self.request = MagicMock() | ||
|
||
@audit | ||
def dummy(self): | ||
return None | ||
|
||
|
||
class AuditTests(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.audit_manager = RecordingManager() | ||
self.dummy_parent = DummyParent() | ||
self.dummy_parent.request.data_managers = { | ||
'audit_manager': self.audit_manager | ||
} | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def _check(self, nr, origin, type_id_list): | ||
self.assertEqual(nr, len(self.audit_manager.saved_objects)) | ||
self.assertEqual(origin, self.audit_manager.saved_objects[nr-1].origin) | ||
for type_id in type_id_list: | ||
self.assertEqual('1', getattr(self.audit_manager.saved_objects[nr-1], type_id)) | ||
|
||
def test_audit_rest(self): | ||
self.dummy_parent.request.accept = ['application/json'] | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(1, 'REST', ['conceptscheme_id']) | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(2, 'REST', ['conceptscheme_id', 'concept_id']) | ||
|
||
def test_audit_html(self): | ||
self.dummy_parent.request.accept = ['text/html'] | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(1, 'HTML', ['conceptscheme_id']) | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(2, 'HTML', ['conceptscheme_id', 'concept_id']) | ||
|
||
def test_audit_rdf_xml(self): | ||
self.dummy_parent.request.accept = ['application/rdf+xml'] | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(1, 'RDF', ['conceptscheme_id']) | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(2, 'RDF', ['conceptscheme_id', 'concept_id']) | ||
|
||
def test_audit_other(self): | ||
self.dummy_parent.request.accept = ['application/octet-stream'] | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(1, 'onbekend', ['conceptscheme_id']) | ||
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} | ||
self.dummy_parent.dummy() | ||
self._check(2, 'onbekend', ['conceptscheme_id', 'concept_id']) | ||
|
||
def test_origin_from_response(self): | ||
res = Response(content_type='application/rdf+xml') | ||
self.assertEqual('RDF', _origin_from_response(res)) | ||
res = Response(content_type='text/html') | ||
self.assertEqual('HTML', _origin_from_response(res)) | ||
res = Response(content_type='application/json') | ||
self.assertEqual('REST', _origin_from_response(res)) | ||
res = Response(content_type='application/octet-stream') | ||
self.assertEqual('onbekend', _origin_from_response(res)) | ||
|
Oops, something went wrong.