Exemplo n.º 1
0
    def test_OFSFileLastModified_File(self):
        from OFS.Image import File

        dummy = File('dummy', 'Dummy', 'data')
        self.assertEqual(None, ILastModified(dummy)())

        timestamp = 987654321.0  # time stamp (in UTC)
        ts = TimeStamp(*time.gmtime(timestamp)[:6])  # corresponding TimeStamp

        # equivalent in local time, which is what the last-modified adapter
        # should return
        mod = datetime.datetime.fromtimestamp(timestamp, tzlocal())

        dummy._p_jar = FauxDataManager()
        dummy._p_serial = repr(ts)
        self.assertEqual(mod, ILastModified(dummy)())
Exemplo n.º 2
0
    def test_last_modified(self):
        import os.path
        import datetime

        from dateutil.tz import tzlocal

        from zope.component import provideAdapter
        from zope.publisher.browser import TestRequest

        from plone.resource.file import FilesystemFile
        from plone.resource.file import FileLastModified

        from z3c.caching.interfaces import ILastModified

        provideAdapter(FileLastModified)

        name = 'test.html'
        path = os.path.join(os.path.dirname(__file__), 'resources', 'demo',
                            'foo', name)
        mtime = os.path.getmtime(path)

        request = TestRequest()

        f = FilesystemFile(None, request, path, name)

        lastModified = ILastModified(f)
        mdate = datetime.datetime.fromtimestamp(mtime, tz=tzlocal())

        self.assertEqual(lastModified(), mdate)
    def test_with_adapter(self):
        @implementer(IView)
        class DummyView(object):
            def __init__(self, context, request):
                self.context = context
                self.request = request

        class DummyContext(object):
            pass

        class DummyRequest(dict):
            pass

        @implementer(ILastModified)
        @adapter(DummyContext)
        class DummyLastModified(object):
            def __init__(self, context):
                self.context = context
                self.request = request

        provideAdapter(DummyLastModified)

        context = DummyContext()
        request = DummyRequest()

        view = DummyView(context, request)

        lastModified = ILastModified(view)
        self.assertIsInstance(lastModified, DummyLastModified)

        self.assertEqual(context, lastModified.context)
        self.assertEqual(request, lastModified.request)
Exemplo n.º 4
0
    def test_CatalogableDublinCoreLastModified(self):
        from Products.CMFCore.interfaces import ICatalogableDublinCore
        from zope.interface import implements

        class Dummy(object):
            implements(ICatalogableDublinCore)

            _mod = None

            def modified(self):
                if self._mod is not None: return DateTime.DateTime(self._mod)
                return None

        d = Dummy()

        self.assertEqual(None, ILastModified(d)())

        d._mod = datetime.datetime(2001, 4, 19, 12, 25, 21, 120000)
        self.assertEqual(d._mod, ILastModified(d)())
Exemplo n.º 5
0
    def test_FSObjectLastModified_FSImage(self):
        from Products.CMFCore.FSImage import FSImage

        dummy = FSImage('dummy', __file__)  # not really an image, but anyway
        modtime = float(os.path.getmtime(__file__))
        mod = datetime.datetime.fromtimestamp(modtime, tzlocal())
        # different filesystems seem to handle datetime differently. some use microseconds
        # and others don't so to make jenkins happy lets omit the microseconds factor
        format = "%y%m%d%H%M%s"
        self.assertEqual(mod.strftime(format),
                         ILastModified(dummy)().strftime(format))
Exemplo n.º 6
0
    def test_FSObjectLastModified_FSFile(self):
        from Products.CMFCore.FSFile import FSFile

        dummy = FSFile('dummy', __file__)

        modtime = float(os.path.getmtime(__file__))
        mod = datetime.datetime.fromtimestamp(modtime, tzlocal())

        format = "%y%m%d%H%M%s"  # see note in test_FSObjectLastModified_FSImage
        self.assertEqual(mod.strftime(format),
                         ILastModified(dummy)().strftime(format))
Exemplo n.º 7
0
    def test_FSPageTemplateDelegateLastModified(self):
        from persistent import Persistent
        from Acquisition import Explicit

        class Dummy(Persistent, Explicit):
            _p_mtime = None

        provideAdapter(lastmodified.PersistentLastModified, adapts=(Dummy, ))

        d = Dummy()

        from Products.CMFCore.FSPageTemplate import FSPageTemplate
        zpt = FSPageTemplate('zpt', __file__).__of__(d)
        self.assertEqual(None, ILastModified(zpt)())

        timestamp = 987654321.0  # time stamp (in UTC)
        # equivalent in local time, which is what the last-modified adapter
        # should return
        mod = datetime.datetime.fromtimestamp(timestamp, tzlocal())

        d._p_mtime = 987654321.0
        self.assertEqual(mod, ILastModified(zpt)())
Exemplo n.º 8
0
    def test_DCTimesLastModified(self):
        try:
            from zope.dublincore.interfaces import IDCTimes
        except ImportError:
            return
        from zope.interface import implements

        class Dummy(object):
            implements(IDCTimes)

            _mod = None

            @property
            def modified(self):
                return self._mod

        d = Dummy()

        self.assertEqual(None, ILastModified(d)())

        d._mod = datetime.datetime(2001, 4, 19, 12, 25, 21, 120000)
        self.assertEqual(d._mod, ILastModified(d)())
Exemplo n.º 9
0
    def test_ResourceLastModified_Five(self):
        from Products.Five.browser.resource import FileResource
        from zope.browserresource.file import File

        class DummyRequest(dict):
            pass

        request = DummyRequest()

        f = File(__file__, 'test_lastmodified.py')  # not really an image
        r = FileResource(f, request)

        modtime = float(os.path.getmtime(__file__))
        mod = datetime.datetime.fromtimestamp(modtime, tz=tzlocal())

        self.assertEqual(mod, ILastModified(r)())
Exemplo n.º 10
0
    def test_no_adapter(self):
        @implementer(IView)
        class DummyView(object):
            def __init__(self, context, request):
                self.context = context
                self.request = request

        class DummyContext(object):
            pass

        class DummyRequest(dict):
            pass

        context = DummyContext()
        request = DummyRequest()

        view = DummyView(context, request)

        lastModified = ILastModified(view, None)
        self.assertIsNone(lastModified)
Exemplo n.º 11
0
    def test_with_adapter(self):
        class DummyView(object):
            implements(IView)

            def __init__(self, context, request):
                self.context = context
                self.request = request

        class DummyContext(object):
            pass

        class DummyRequest(dict):
            pass

        class DummyLastModified(object):
            implements(ILastModified)
            adapts(DummyContext)

            def __init__(self, context):
                self.context = context
                self.request = request

        provideAdapter(DummyLastModified)

        context = DummyContext()
        request = DummyRequest()

        view = DummyView(context, request)

        lastModified = ILastModified(view)
        self.failUnless(isinstance(
            lastModified,
            DummyLastModified,
        ))

        self.assertEquals(context, lastModified.context)
        self.assertEquals(request, lastModified.request)
Exemplo n.º 12
0
def getLastModified(published, lastModified=True):
    """Get a last modified date or None.

    If an ``ILastModified`` adapter can be found, and returns a date that is
    not timezone aware, assume it is local time and add timezone.
    """

    if not lastModified:
        return None

    lastModified = ILastModified(published, None)
    if lastModified is None:
        return None

    dt = lastModified()
    if dt is None:
        return None

    if dt.tzinfo is None:
        dt = datetime.datetime(dt.year, dt.month, dt.day, dt.hour,
                               dt.minute, dt.second, dt.microsecond,
                               dateutil.tz.tzlocal())

    return dt
Exemplo n.º 13
0
 def __call__(self):
     parent = getContext(self.context)
     if parent is not None:
         return ILastModified(parent)()
     return None
Exemplo n.º 14
0
def PageTemplateDelegateLastModified(template):
    """When looking up an ILastModified for a page template, look up an
    ILastModified for its context. May return None, in which case adaptation
    will fail.
    """
    return ILastModified(template.__parent__, None)
Exemplo n.º 15
0
def viewDelegateLastModified(view):
    """When looking up an ILastModified for a view, look up an ILastModified
    for its context. May return None, in which case adaptation will fail.
    """
    return ILastModified(view.context, None)