Skip to content

Commit c3799cd

Browse files
committed
Tidy files-and-images.md
1 parent bf3c1fc commit c3799cd

File tree

1 file changed

+60
-74
lines changed

1 file changed

+60
-74
lines changed
Lines changed: 60 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,78 @@
11
---
22
myst:
33
html_meta:
4-
"description": ""
5-
"property=og:description": ""
6-
"property=og:title": ""
7-
"keywords": ""
4+
"description": "How to work with file and image fields, including blobs, in Plone content types"
5+
"property=og:description": "How to work with file and image fields, including blobs, in Plone content types"
6+
"property=og:title": "How to work with file and image fields, including blobs, in Plone content types"
7+
"keywords": "Plone, content types, files, images, blobs"
88
---
99

1010
# Files and images
1111

12-
**Working with file and image fields, including BLOBs**
13-
14-
Plone has dedicated `File` and `Image` types, and it is often preferable
15-
to use these for managing files and images. However, it is sometimes
16-
useful to treat fields on an object as binary data. When working with
17-
Dexterity, you can accomplish this by using [plone.namedfile] and
18-
[plone.formwidget.namedfile].
19-
20-
The [plone.namedfile] package includes four field types, all found in
21-
the `plone.namedfile.field` module:
22-
23-
- `NamedFile` stores non-BLOB files. This is useful for small files
24-
when you don’t want to configure BLOB storage.
25-
- `NamedImage` stores non-BLOB images.
26-
- `NamedBlobFile` stores BLOB files (see note below). It is otherwise
27-
identical to `NamedFile`.
28-
- `NamedBlobImage` stores BLOB images (see note below). It is otherwise
29-
identical to `NamedImage`.
30-
31-
In use, the four field types are all pretty similar. They actually store
32-
persistent objects of type `plone.namedfile.NamedFile`,
33-
`plone.namedfile.NamedImage`, `plone.namedfile.NamedBlobFile` and `plone.namedfile.NamedBlobImage`,
34-
respectively. Note the different module! These objects have attributes
35-
like `data`, to access the raw binary data, `contentType`, to get a MIME
36-
type, and `filename`, to get the original filename. The image values
37-
also support `_height` and `_width` to get image dimensions.
38-
39-
To use the non-BLOB image and file fields, it is sufficient to depend on
40-
`plone.formwidget.namedfile`, since this includes `plone.namefile` as a
41-
dependency. We prefer to be explicit in `setup.py`, however, since we
42-
will actually import directly from `plone.namedfile`:
12+
This chapter describes how to work with file and image fields, including blobs.
13+
14+
Plone has dedicated `File` and `Image` types, and it is often preferable to use these for managing files and images.
15+
However, it is sometimes useful to treat fields on an object as binary data.
16+
When working with Dexterity, you can accomplish this by using [`plone.namedfile`](https://pypi.org/project/plone.namedfile/) and [`plone.formwidget.namedfile`](https://pypi.org/project/plone.formwidget.namedfile/).
17+
18+
The `plone.namedfile` package includes four field types, all found in the `plone.namedfile.field` module.
19+
20+
`NamedFile`
21+
: Stores non-blob files.
22+
This is useful for small files when you don't want to configure blob storage.
23+
24+
`NamedImage`
25+
Stores non-blob images.
26+
27+
`NamedBlobFile`
28+
: Stores blob files (see note below).
29+
It is otherwise identical to `NamedFile`.
30+
31+
`NamedBlobImage`
32+
: Stores blob images (see note below).
33+
It is otherwise identical to `NamedImage`.
34+
35+
In use, the four field types are all pretty similar.
36+
They actually store persistent objects of type `plone.namedfile.NamedFile`, `plone.namedfile.NamedImage`, `plone.namedfile.NamedBlobFile` and `plone.namedfile.NamedBlobImage`, respectively.
37+
Note the different module!
38+
These objects have attributes, such as `data` to access the raw binary data, `contentType`, to get a MIME type, and `filename` to get the original filename.
39+
The image values also support `_height` and `_width` to get image dimensions.
40+
41+
To use the non-blob image and file fields, it is sufficient to depend on `plone.formwidget.namedfile`, since this includes `plone.namefile` as a dependency.
42+
We prefer to be explicit in `setup.py`, however, since we will actually import it directly from `plone.namedfile`.
4343

4444
```ini
4545
install_requires=[
46-
...
47-
'plone.namedfile',
48-
'plone.formwidget.namedfile',
46+
"plone.namedfile",
47+
"plone.formwidget.namedfile",
4948
],
5049
```
5150

52-
:::{note}
53-
Again, we do not need separate `<include />` lines in
54-
`configure.zcml` for these new dependencies, because we use
55-
`<includeDependencies />`.
56-
:::
51+
```{note}
52+
Again, we do not need separate `<include />` lines in `configure.zcml` for these new dependencies, because we use `<includeDependencies />`.
53+
```
5754

58-
For the sake of illustration, we will add an image of the
59-
speaker to the `Presenter` type. In `presenter.py`, we add:
55+
To demonstrate, we will add an image of the speaker to the `Presenter` type.
56+
In {file}`presenter.py`, we add the following.
6057

61-
```
58+
```python
6259
from plone.namedfile.field import NamedBlobImage
6360

6461
class IPresenter(model.Schema):
65-
...
6662

6763
picture = NamedBlobImage(
6864
title=_("Please upload an image"),
6965
required=False,
7066
)
7167
```
7268

73-
To use this in a view, we can either use a display widget via a
74-
`DisplayForm`, or construct a download URL manually. Since we don’t have
75-
a `DisplayForm` for the `Presenter` type, we’ll do the latter (of
76-
course, we could easily turn the view into a display form as well).
69+
To use this in a view, we can either use a display widget via a `DisplayForm`, or construct a download URL manually.
70+
Since we don't have a `DisplayForm` for the `Presenter` type, we'll do the latter.
71+
Of course, we could easily turn the view into a display form as well.
7772

78-
In `presenter_templates/view.pt`, we add this block of TAL:
73+
In {file}`presenter_templates/view.pt`, we add the following block of TAL.
7974

80-
```
75+
```xml
8176
<div tal:define="picture nocall:context/picture"
8277
tal:condition="nocall:picture">
8378
<img tal:attributes="src string:${context/absolute_url}/@@download/picture/${picture/filename};
@@ -87,39 +82,30 @@ In `presenter_templates/view.pt`, we add this block of TAL:
8782
</div>
8883
```
8984

90-
This constructs an image URL using the `@@download` view from
91-
`plone.namedfile`. This view takes the name of the field containing the
92-
file or image on the traversal subpath (`/picture`), and optionally a
93-
filename on a further sub-path. The filename is used mainly so that the
94-
URL ends in the correct extension, which can help ensure web browsers
95-
display the picture correctly. We also define the `height` and `width`
96-
of the image based on the values set on the object.
85+
This constructs an image URL using the `@@download` view from `plone.namedfile`.
86+
This view takes the name of the field containing the file or image on the traversal subpath (`/picture`), and optionally a filename on a further sub-path.
87+
The filename is used mainly so that the URL ends in the correct extension, which can help ensure web browsers display the picture correctly.
88+
We also define the `height` and `width` of the image based on the values set on the object.
9789

98-
Access to image scales is similar:
90+
Access to image scales is similar, as shown below.
9991

100-
```
92+
```xml
10193
<div tal:define="picture nocall:context/picture"
10294
tal:condition="nocall:picture">
10395
<img tal:replace="structure context/@@images/picture/scale" />
10496
</div>
10597
```
10698

107-
where `scales` is large, preview, mini, thumb, tile, icon, or a custom scale.
108-
This code generates a full tag, including height and width attributes and alt and title based on the context title.
109-
To generate just a URL, use code like:
99+
Where `scales` is either `large`, `preview`, `mini`, `thumb`, `tile`, `icon`, or a custom scale.
100+
This code generates a full tag, including height, width, alt, and title attributes based on the context title.
101+
To generate just a URL, use code as shown:
110102

111-
```
103+
```xml
112104
<img tal:attributes="src string: ${context/absolute_url}/@@images/picture/scale" />
113105
```
114106

115-
For file fields, you can construct a download URL in a similar way,
116-
using an `<a />` tag, e.g.:
107+
For file fields, you can construct a download URL in a similar way, using an `<a />` tag.
117108

118-
```
109+
```xml
119110
<a tal:attributes="href string:${context/absolute_url}/@@download/some_field/${context/some_field/filename}" />
120111
```
121-
122-
[extra]: http://peak.telecommunity.com/DevCenter/setuptools#declaring-extras-optional-features-with-their-own-dependencies
123-
[plone.formwidget.namedfile]: http://pypi.python.org/pypi/plone.formwidget.namedfile
124-
[plone.namedfile]: http://pypi.python.org/pypi/plone.namedfile
125-
[z3c.blobfile]: http://pypi.python.org/pypi/z3c.blobfile

0 commit comments

Comments
 (0)