Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZO-4800: Replace unused IResource.contentType with IResource.is_collection #669

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/docs/changelog/ZO-4800.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZO-4800: Replace unused IResource.contentType with IResource.is_collection
1 change: 0 additions & 1 deletion core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ clean-objectlog = "zeit.objectlog.objectlog:clean"
change-volume-access = "zeit.content.volume.volume:change_access"
dump_references = "zeit.cms.relation.migrate:dump_references"
load_references = "zeit.cms.relation.migrate:load_references"
dav-cache-clear = "zeit.connector.invalidator:invalidate_whole_cache"
dav-cache-sweep = "zeit.connector.cache:sweep"
set-properties = "zeit.connector.restore:set_props_from_file"
ingredients-collect-used = "zeit.wochenmarkt.ingredients:collect_used"
Expand Down
1 change: 0 additions & 1 deletion core/src/zeit/cms/repository/browser/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def update_file(self, file, data):
s = data.read(self.BUFFER_SIZE)
data.close()
target.close()
file.mimeType = data.headers['content-type']


class AddForm(FormBase, zeit.cms.browser.form.AddForm):
Expand Down
20 changes: 1 addition & 19 deletions core/src/zeit/cms/repository/browser/file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Open the default metadata view of a file:
<dd>
<a href="http://localhost/++skin++cms/repository/2006/DSC00109_2.JPG">DSC00109_2.JPG</a>
</dd>
<dt>Content type</dt>
<dd>image/jpeg</dd>
<dt>Size</dt>
<dd>
882409 bytes
Expand All @@ -43,12 +41,12 @@ Open the default metadata view of a file:
Download the file:

>>> bookmark = browser.url
>>> browser.handleErrors = False
>>> browser.getLink('DSC00109_2').click()
>>> print(browser.headers)
Status: 200 Ok
Content-Disposition: attachment; filename="DSC00109_2.JPG"
Content-Length: 882409
Content-Type: image/jpeg
...
>>> browser.contents
b'\xff\xd8\xff\xe1\x0c\xdaExif...'
Expand All @@ -67,8 +65,6 @@ Check the file out. The edit view also contains the metadata:
<dd>
<a href="http://localhost/++skin++cms/workingcopy/zope.user/DSC00109_2.JPG">DSC00109_2.JPG</a>
</dd>
<dt>Content type</dt>
<dd>image/jpeg</dd>
<dt>Size</dt>
<dd>
882409 bytes
Expand All @@ -84,7 +80,6 @@ We can also download the file here:
Status: 200 Ok
Content-Disposition: attachment; filename="DSC00109_2.JPG"
Content-Length: 882409
Content-Type: image/jpeg
...
>>> browser.contents
b'\xff\xd8\xff\xe1\x0c\xdaExif...'
Expand All @@ -107,8 +102,6 @@ Let's upload a new file:
<dd>
<a href="http://localhost/++skin++cms/workingcopy/zope.user/DSC00109_2.JPG">DSC00109_2.JPG</a>
</dd>
<dt>Content type</dt>
<dd>text/plain</dd>
<dt>Size</dt>
<dd>
23 bytes
Expand All @@ -124,7 +117,6 @@ We can download the new file now:
Status: 200 Ok
Content-Disposition: attachment; filename="DSC00109_2.JPG"
Content-Length: 23
Content-Type: text/plain
...
>>> browser.contents
b'Mary had a little lamb.'
Expand All @@ -143,8 +135,6 @@ When we check in the file is updated in the repository:
<dd>
<a href="http://localhost/++skin++cms/repository/2006/DSC00109_2.JPG">DSC00109_2.JPG</a>
</dd>
<dt>Content type</dt>
<dd>text/plain</dd>
<dt>Size</dt>
<dd>
23 bytes
Expand All @@ -160,7 +150,6 @@ We can still download the new file:
Status: 200 Ok
Content-Disposition: attachment; filename="DSC00109_2.JPG"
Content-Length: 23
Content-Type: text/plain
...
>>> browser.contents
b'Mary had a little lamb.'
Expand All @@ -177,8 +166,6 @@ The metadata preview also shows the file details:
<dd>
<a href="http://localhost/++skin++cms/repository/2006/DSC00109_2.JPG">DSC00109_2.JPG</a>
</dd>
<dt>Content type</dt>
<dd>text/plain</dd>
<dt>Size</dt>
<dd>
23 bytes
Expand Down Expand Up @@ -218,8 +205,6 @@ After adding we're at the edit form:
<dd>
<a href="http://localhost/++skin++cms/workingcopy/zope.user/ogre.txt">ogre.txt</a>
</dd>
<dt>Content type</dt>
<dd>text/plain</dd>
<dt>Size</dt>
<dd>
22 bytes
Expand All @@ -235,7 +220,6 @@ We can download the file:
Status: 200 Ok
Content-Disposition: attachment; filename="ogre.txt"
Content-Length: 22
Content-Type: text/plain
...
>>> browser.contents
b'Mary had a giant ogre.'
Expand All @@ -253,8 +237,6 @@ Check it in:
<dd>
<a href="http://localhost/++skin++cms/repository/2006/ogre.txt">ogre.txt</a>
</dd>
<dt>Content type</dt>
<dd>text/plain</dd>
<dt>Size</dt>
<dd>
22 bytes
Expand Down
2 changes: 0 additions & 2 deletions core/src/zeit/cms/repository/browser/file_details.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
foo.pdf
</a>
</dd>
<dt i18n:translate="">Content type</dt>
<dd tal:content="context/mimeType">application/pdf</dd>
<dt i18n:translate="">Size</dt>
<dd i18n:translate="">
<span tal:replace="context/size" i18n:name="size"/> bytes
Expand Down
16 changes: 7 additions & 9 deletions core/src/zeit/cms/repository/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
class RepositoryFile(zeit.cms.repository.repository.ContentBase):
"""A file in the repository."""

def __init__(self, uniqueId, mimeType):
mimeType = 'application/octet-stream' # Required by zope.file.download

def __init__(self, uniqueId):
super().__init__()
self.uniqueId = uniqueId
self.parameters = {}
self.mimeType = mimeType

def open(self, mode='r'):
if mode != 'r':
Expand All @@ -48,8 +49,8 @@ class LocalFile(persistent.Persistent, RepositoryFile):

local_data = None

def __init__(self, uniqueId=None, mimeType=''):
super().__init__(uniqueId, mimeType)
def __init__(self, uniqueId=None):
super().__init__(uniqueId)

def open(self, mode='r'):
if mode not in ('r', 'w'):
Expand All @@ -74,7 +75,7 @@ def open(self, mode='r'):
@zope.component.adapter(RepositoryFile)
@zope.interface.implementer(zeit.cms.workingcopy.interfaces.ILocalContent)
def localfile_factory(context):
f = LocalFile(context.uniqueId, context.mimeType)
f = LocalFile(context.uniqueId)
f.__name__ = context.__name__
return f

Expand All @@ -87,10 +88,7 @@ class FileType(zeit.cms.type.TypeDeclaration):
factory = RepositoryFile

def content(self, resource):
return self.factory(resource.id, resource.contentType)
return self.factory(resource.id)

def resource_body(self, content):
return zope.security.proxy.removeSecurityProxy(content.open('r'))

def resource_content_type(self, content):
return content.mimeType
19 changes: 2 additions & 17 deletions core/src/zeit/cms/repository/file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ required:

>>> import zeit.cms.repository.file
>>> f = zeit.cms.repository.file.RepositoryFile(
... 'http://xml.zeit.de/2006/DSC00109_2.JPG', 'image/jpeg')
... 'http://xml.zeit.de/2006/DSC00109_2.JPG')
>>> f
<zeit.cms.repository.file.RepositoryFile...>

Expand Down Expand Up @@ -65,11 +65,6 @@ Traceback (most recent call last):
...
ValueError: w

The mime type is available via the ``mimeType`` attribute

>>> f.mimeType
'image/jpeg'


LocalFile
---------
Expand All @@ -78,7 +73,7 @@ The LocalFile actually stores data.


>>> local = zeit.cms.repository.file.LocalFile(
... 'http://xml.zeit.de/2006/DSC00109_2.JPG', 'image/jpeg')
... 'http://xml.zeit.de/2006/DSC00109_2.JPG')
>>> local
<zeit.cms.repository.file.LocalFile...>

Expand Down Expand Up @@ -110,15 +105,12 @@ So far we have no local copy:

>>> local.local_data is None
True
>>> local.mimeType
'image/jpeg'

The LocalFile can be opened for writing:

>>> f_write = local.open('w')
>>> _ = f_write.write(b'New data')
>>> f_write.close()
>>> local.mimeType = 'text/plain'

The file has stored the new data now:

Expand Down Expand Up @@ -156,8 +148,6 @@ Writing works as usual:
>>> fh.read()
b'Eine wunderbare Heiterkeit hat meine ganze Seele eingenommen'
>>> fh.close()
>>> local.mimeType
''

A local file, which has been removed from the repository, but does not have
data by itself will return an empty file:
Expand Down Expand Up @@ -194,8 +184,6 @@ False
>>> fh.read(10)
b'\xff\xd8\xff\xe1\x0c\xdaExif'
>>> fh.close()
>>> f.mimeType
'image/jpeg'

When we adapt the RepositoryFile to ILocalContent we'll get a LocalFile:

Expand All @@ -211,7 +199,6 @@ When we change some data ...
>>> f_write = local.open('w')
>>> _ = f_write.write(b'Mary had a little lamb.')
>>> f_write.close()
>>> local.mimeType = 'text/plain'

... and adapt to IRepositoryContent we'll have the file updated in the
repository:
Expand All @@ -224,8 +211,6 @@ repository:
>>> fh.read()
b'Mary had a little lamb.'
>>> fh.close()
>>> repository_file.mimeType
'text/plain'


Clean up:
Expand Down
4 changes: 1 addition & 3 deletions core/src/zeit/cms/repository/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FolderType(zeit.cms.type.TypeDeclaration):
title = _('Folder')
addform = 'zeit.cms.repository.folder.Add'
factory = Folder
resource_is_collection = True

def content(self, resource):
folder = self.factory()
Expand All @@ -30,9 +31,6 @@ def content(self, resource):
def resource_body(self, content):
return zeit.cms.util.MemoryFile()

def resource_content_type(self, content):
return 'httpd/unix-directory'


@zope.interface.implementer(zeit.cms.content.interfaces.IContentSortKey)
@zope.component.adapter(zeit.cms.repository.interfaces.IFolder)
Expand Down
10 changes: 3 additions & 7 deletions core/src/zeit/cms/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class TypeDeclaration:
addpermission = None
register_as_type = True

resource_is_collection = False

def __init__(self):
if self.addform is None:
package = '.'.join(self.__module__.split('.')[:-1])
Expand All @@ -52,9 +54,6 @@ def content(self, resource):
def resource_body(self, content):
raise NotImplementedError

def resource_content_type(self, content):
return None

def resource_properties(self, content):
try:
return zeit.connector.interfaces.IWebDAVReadProperties(content)
Expand All @@ -68,8 +67,8 @@ def resource(self, content):
content.__name__,
self.type,
data=self.resource_body(content),
contentType=self.resource_content_type(content),
properties=self.resource_properties(content),
is_collection=self.resource_is_collection,
)

# Serializing/deserializing provided interfaces is independent of the
Expand Down Expand Up @@ -146,9 +145,6 @@ def content(self, resource):
def resource_body(self, content):
return zeit.cms.util.MemoryFile(zeit.cms.content.interfaces.IXMLSource(content))

def resource_content_type(self, content):
return 'text/xml'


def get_type(content):
for interface in zope.interface.providedBy(content):
Expand Down
15 changes: 8 additions & 7 deletions core/src/zeit/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ def __getitem__(self, id):
__traceback_info__ = (id,)
id = self._get_cannonical_id(id)
try:
content_type = self._get_resource_properties(id).get(('getcontenttype', 'DAV:'))
is_collection = (
self._get_resource_properties(id).get(('getcontenttype', 'DAV:'))
== 'httpd/unix-directory'
)
except (
zeit.connector.dav.interfaces.DAVNotFoundError,
zeit.connector.dav.interfaces.DAVBadRequestError,
Expand All @@ -243,7 +246,7 @@ def __getitem__(self, id):
self._get_resource_type(id),
lambda: self._get_resource_properties(id),
lambda: self._get_resource_body(id),
contentType=content_type,
is_collection=is_collection,
)

def __setitem__(self, id, object):
Expand Down Expand Up @@ -525,11 +528,10 @@ def _internal_add(self, id, resource, verify_etag=True):
self._invalidate_cache(id)
locktoken = self._get_my_locktoken(id)
autolock = locktoken is None
iscoll = resource.type == 'collection' or resource.contentType == 'httpd/unix-directory'
if iscoll and not id.endswith('/'):
if resource.is_collection and not id.endswith('/'):
id = id + '/'

if iscoll:
if resource.is_collection:
# It is not necessary (and not possible) to lock collections when
# they don't exist because MKCOL does *not* overwrite anything. So
# only lock for files
Expand All @@ -541,7 +543,7 @@ def _internal_add(self, id, resource, verify_etag=True):
id, 'AUTOLOCK', datetime.datetime.now(pytz.UTC) + datetime.timedelta(seconds=60)
)
try:
if not iscoll: # We are a file resource:
if not resource.is_collection: # We are a file resource:
if hasattr(resource.data, 'seek'):
resource.data.seek(0)
# We should pass the data as IO object. This is not supported
Expand All @@ -562,7 +564,6 @@ def _internal_add(self, id, resource, verify_etag=True):
conn.put(
self._id2loc(id),
data,
mime_type=resource.contentType,
locktoken=locktoken,
etag=etag,
extra_headers=headers,
Expand Down
8 changes: 4 additions & 4 deletions core/src/zeit/connector/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __getitem__(self, id):
type,
lambda: self._get_properties(id),
lambda: self._get_body(id),
contentType=self._get_content_type(id),
self._is_collection(id),
)

def _get_body(self, id):
Expand All @@ -147,12 +147,12 @@ def _get_body(self, id):
self.body_cache[id] = data
return BytesIO(data)

def _get_content_type(self, id):
def _is_collection(self, id):
properties = self._get_properties(id)
if properties[zeit.connector.interfaces.RESOURCE_TYPE_PROPERTY] == 'collection':
return 'httpd/unix-directory'
return True
davtype = ('getcontenttype', 'DAV:')
return properties.get(davtype, '')
return properties.get(davtype, '') == 'httpd/unix-directory'

def __setitem__(self, id, object):
raise NotImplementedError()
Expand Down
Loading