def test_lsc_should_fall_back_to_modified(self): from zope.dublincore.interfaces import IDCTimes import datetime import pytz content = self.get_content() now = datetime.datetime(2011, 1, 2, 3, 4, tzinfo=pytz.UTC) IDCTimes(content).modified = now xml = self.convert(content) self.assertEqual(['2011-01-02T03:04:00Z'], xml.xpath('//field[@name="last-semantic-change"]'))
def test_lsc_should_not_be_duplicate_if_both_lsc_and_modified_set(self): from zeit.cms.content.interfaces import ISemanticChange from zope.dublincore.interfaces import IDCTimes import datetime import pytz content = self.get_content() modified = datetime.datetime(2011, 1, 2, 3, 4, tzinfo=pytz.UTC) IDCTimes(content).modified = modified lsc = datetime.datetime(2010, 12, 13, 14, 15, tzinfo=pytz.UTC) ISemanticChange(content).last_semantic_change = lsc xml = self.convert(content) self.assertEqual(['2010-12-13T14:15:00Z'], xml.xpath('//field[@name="last-semantic-change"]'))
def getModifiedTime(self): """ get the modified time from Dublin Core """ return IDCTimes(self).modified
def show(self): """Sets various headers if the request is present and returns the data of the file. If the "If-Modified-Since" header is set and the context is adaptable to IDCTimes, data is only returned if the modification date of the context is newer than the date in the "If-Modified-Since" header >>> from zope.publisher.browser import BrowserView, TestRequest >>> class FileTestView(FileView, BrowserView): pass >>> import pytz >>> class MyFile(object): ... contentType = 'text/plain' ... data = b"data of file" ... modified = datetime(2006,1,1,tzinfo=pytz.utc) ... def getSize(self): ... return len(self.data) >>> aFile = MyFile() >>> request = TestRequest() >>> view = FileTestView(aFile,request) >>> view.show() == MyFile.data True >>> request.response.getHeader('Content-Type') 'text/plain' >>> request.response.getHeader('Content-Length') '12' If the file is adaptable to IDCTimes the "Last-Modified" header is also set. >>> request.response.getHeader('Last-Modified') is None True For the test we just declare that our file provides IZopeDublinCore >>> from zope.dublincore.interfaces import IZopeDublinCore >>> from zope.interface import directlyProvides >>> directlyProvides(aFile,IZopeDublinCore) >>> request = TestRequest() >>> view = FileTestView(aFile,request) >>> view.show() == MyFile.data True >>> request.response.getHeader('Last-Modified') 'Sun, 01 Jan 2006 00:00:00 GMT' If the "If-Modified-Since" header is set and is newer a 304 status is returned and the value is empty. >>> modified = datetime(2007,12,31,tzinfo=pytz.utc) >>> modHeader = zope.datetime.rfc1123_date( ... zope.datetime.time(modified.isoformat())) >>> request = TestRequest(IF_MODIFIED_SINCE=modHeader) >>> view = FileTestView(aFile,request) >>> view.show() == b'' True >>> request.response.getStatus() 304 An invalid value for that header is ignored. >>> request = TestRequest(IF_MODIFIED_SINCE="Not Valid") >>> view = FileTestView(aFile,request) >>> view.show() == MyFile.data True """ if self.request is not None: self.request.response.setHeader('Content-Type', self.context.contentType) self.request.response.setHeader('Content-Length', self.context.getSize()) try: modified = IDCTimes(self.context).modified except TypeError: modified = None if modified is None or not isinstance(modified, datetime): return self.context.data header = self.request.getHeader('If-Modified-Since', None) lmt = zope.datetime.time(modified.isoformat()) if header is not None: header = header.split(';')[0] try: mod_since = int(zope.datetime.time(header)) except zope.datetime.SyntaxError: mod_since = None if mod_since is not None: if lmt <= mod_since: self.request.response.setStatus(304) return b'' self.request.response.setHeader('Last-Modified', zope.datetime.rfc1123_date(lmt)) return self.context.data
def show(self): """Sets various headers if the request is present and returns the data of the file. If the "If-Modified-Since" header is set and the context is adaptable to IDCTimes, data is only returned if the modification date of the context is newer than the date in the "If-Modified-Since" header >>> from zope.publisher.browser import BrowserView, TestRequest >>> class FileTestView(FileView, BrowserView): pass >>> import pytz >>> class MyFile(object): ... contentType='text/plain' ... data="data of file" ... modified = datetime(2006,1,1,tzinfo=pytz.utc) ... def getSize(self): ... return len(self.data) >>> aFile = MyFile() >>> request = TestRequest() >>> view = FileTestView(aFile,request) >>> view.show() 'data of file' >>> request.response.getHeader('Content-Type') 'text/plain' >>> request.response.getHeader('Content-Length') '12' If the file is adaptable to IDCTimes the "Last-Modified" header is also set. >>> request.response.getHeader('Last-Modified') is None True For the test we just declare that our file provides IZopeDublinCore >>> from zope.dublincore.interfaces import IZopeDublinCore >>> from zope.interface import directlyProvides >>> directlyProvides(aFile,IZopeDublinCore) >>> request = TestRequest() >>> view = FileTestView(aFile,request) >>> view.show() 'data of file' >>> request.response.getHeader('Last-Modified') 'Sun, 01 Jan 2006 00:00:00 GMT' If the "If-Modified-Since" header is set and is newer a 304 status is returned and the value is empty. >>> modified = datetime(2007,12,31,tzinfo=pytz.utc) >>> modHeader = zope.datetime.rfc1123_date(zope.datetime.time(modified.isoformat())) >>> request = TestRequest(IF_MODIFIED_SINCE=modHeader) >>> view = FileTestView(aFile,request) >>> view.show() '' >>> request.response.getStatus() 304 """ if self.request is not None: self.request.response.setHeader('Content-Type', self.context.contentType) self.request.response.setHeader('Content-Length', self.context.getSize()) try: modified = IDCTimes(self.context).modified except TypeError: modified=None if modified is None or not isinstance(modified,datetime): return self.context.data header= self.request.getHeader('If-Modified-Since', None) lmt = zope.datetime.time(modified.isoformat()) if header is not None: header = header.split(';')[0] try: mod_since=long(zope.datetime.time(header)) except: mod_since=None if mod_since is not None: if lmt <= mod_since: self.request.response.setStatus(304) return '' self.request.response.setHeader('Last-Modified', zope.datetime.rfc1123_date(lmt)) return self.context.data