You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**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`.
43
43
44
44
```ini
45
45
install_requires=[
46
-
...
47
-
'plone.namedfile',
48
-
'plone.formwidget.namedfile',
46
+
"plone.namedfile",
47
+
"plone.formwidget.namedfile",
49
48
],
50
49
```
51
50
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
+
```
57
54
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.
60
57
61
-
```
58
+
```python
62
59
from plone.namedfile.field import NamedBlobImage
63
60
64
61
classIPresenter(model.Schema):
65
-
...
66
62
67
63
picture = NamedBlobImage(
68
64
title=_("Please upload an image"),
69
65
required=False,
70
66
)
71
67
```
72
68
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.
77
72
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.
@@ -87,39 +82,30 @@ In `presenter_templates/view.pt`, we add this block of TAL:
87
82
</div>
88
83
```
89
84
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.
97
89
98
-
Access to image scales is similar:
90
+
Access to image scales is similar, as shown below.
0 commit comments