-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathwidgets.py
More file actions
40 lines (30 loc) · 1.32 KB
/
widgets.py
File metadata and controls
40 lines (30 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Custom widgets for Mongo fields."""
from markupsafe import Markup, escape
from mongoengine.fields import GridFSProxy, ImageGridFsProxy
from wtforms.widgets.core import FileInput
class MongoFileInput(FileInput):
"""Renders a file input field with delete option."""
template = """
<div>
<i class="icon-file"></i>%(name)s %(size)dk (%(content_type)s)
<input type="checkbox" name="%(marker)s">Delete</input>
</div>
"""
def _is_supported_file(self, field) -> bool:
"""Checks type of file input."""
return field.data and isinstance(field.data, GridFSProxy)
def __call__(self, field, **kwargs) -> Markup:
placeholder = ""
if self._is_supported_file(field):
placeholder = self.template % {
"name": escape(field.data.name),
"content_type": escape(field.data.content_type),
"size": field.data.length // 1024,
"marker": f"_{field.name}_delete",
}
return Markup(placeholder) + super().__call__(field, **kwargs)
class MongoImageInput(MongoFileInput):
"""Renders an image input field with delete option."""
def _is_supported_file(self, field) -> bool:
"""Checks type of file input."""
return field.data and isinstance(field.data, ImageGridFsProxy)