Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ export/
/package.json
/watch-run.py
/watchable-grunt.js
cache/
39 changes: 39 additions & 0 deletions castle/cms/_scripts/events-audit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import argparse
import logging
import os
import time

from castle.cms.audit import _record
from castle.cms.constants import AUDIT_CACHE_DIRECTORY
from castle.cms.utils import ESConnectionFactoryFactory
from diskcache import Cache


logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(
description='...')
parser.add_argument('--site-id', dest='site_id', default='Plone')
parser.add_argument('--cache-dir', dest='cache_dir', default=AUDIT_CACHE_DIRECTORY)
args, _ = parser.parse_known_args()

site = app[args.site_id]
cache_dir = os.path.relpath(args.cache_dir)
registry = site.portal_registry
conn_factory = ESConnectionFactoryFactory(registry)

while (True):
cache = Cache(cache_dir)
if len(cache) == 0:
cache.close()
time.sleep(30)
else:
with Cache(cache.directory) as reference:
for key in reference:
args = (conn_factory, reference[key]['site_path'], reference[key]['data'])
kwargs = reference[key]['kwargs']
try:
_record(*args, **kwargs)
del reference[key]
except:
logger.error('could not add record to audit log')
10 changes: 4 additions & 6 deletions castle/cms/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import os


from castle.cms.events import (ICacheInvalidatedEvent,
IMetaTileEditedEvent,
ITrashEmptiedEvent)
from castle.cms.interfaces import ITrashed
from plone import api
from plone.app.iterate.interfaces import IAfterCheckinEvent
from plone.app.iterate.interfaces import ICancelCheckoutEvent
Expand All @@ -55,12 +59,6 @@
from zope.lifecycleevent.interfaces import IObjectRemovedEvent


from castle.cms.events import ICacheInvalidatedEvent
from castle.cms.events import IMetaTileEditedEvent
from castle.cms.events import ITrashEmptiedEvent
from castle.cms.interfaces import ITrashed


logger = logging.getLogger("Plone")

DEFAULT_AUDIT_LOGGER_CONFIG = {
Expand Down
1 change: 1 addition & 0 deletions castle/cms/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
VALID_CSS_FONT_SIZE_PATTERN = compile("^\s*\d+\s*({})\s*$".format( # noqa:W605
'|'.join(ABSOLUTE_FONT_UNITS)
))
AUDIT_CACHE_DIRECTORY = 'cache/auditcache'
47 changes: 47 additions & 0 deletions castle/cms/tests/test_audit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import unittest

from castle.cms.constants import AUDIT_CACHE_DIRECTORY
from castle.cms.testing import CASTLE_PLONE_INTEGRATION_TESTING
from diskcache import Cache
from plone import api
from plone.app.testing import TEST_USER_ID
from plone.app.testing import TEST_USER_NAME
from plone.app.testing import login
from plone.app.testing import setRoles


class TestAudit(unittest.TestCase):

layer = CASTLE_PLONE_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']
login(self.portal, TEST_USER_NAME)
setRoles(self.portal, TEST_USER_ID, ('Member', 'Manager'))
api.portal.set_registry_record(
'collective.elasticsearch.interfaces.IElasticSettings.enabled', True)

def test_cache_object(self):
obj = api.content.create(type='Document', id='doc1',
container=self.portal)
api.content.transition(obj=obj, to_state='published')
obj.reindexObject()
obj_id = getattr(obj, '_plone.uuid')
cache = Cache(AUDIT_CACHE_DIRECTORY)
self.assertTrue(obj_id in cache)
cache.clear()

def test_es_custom_index(self):
obj = api.content.create(type='Document', id='doc1',
container=self.portal)
api.portal.set_registry_record('castle.es_index_enabled', True)
api.portal.set_registry_record('castle.es_index', u'test-index')
api.content.transition(obj=obj, to_state='published')
obj.reindexObject()
obj_id = getattr(obj, '_plone.uuid')
cache = Cache(AUDIT_CACHE_DIRECTORY)
self.assertTrue(obj_id in cache)
self.assertTrue(cache[obj_id]['kwargs']['es_custom_index_name_enabled'])
self.assertEqual(cache[obj_id]['kwargs']['custom_index_value'], u'test-index')
cache.clear()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def read(*rnames):
'tendo',
'pylru',
'sqlalchemy',
'diskcache',

# misc
'z3c.unconfigure',
Expand Down
1 change: 1 addition & 0 deletions versions.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ futures = 3.3.0
jmespath = 0.9.4
s3transfer = 0.2.1
pylru = 1.2.0
diskcache = 4.1.0
configparser = 4.0.2
zipp = 1.2.0

Expand Down