Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds UploadFileWithORMSupport, an upload field with onegov.file support
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Oct 17, 2018
1 parent 94af7a4 commit 624deb0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changelog
---------

- Adds UploadFileWithORMSupport, an upload field with onegov.file support.
[href]

0.38.0 (2018-10-08)
~~~~~~~~~~~~~~~~~~~

Expand Down
39 changes: 39 additions & 0 deletions onegov/form/fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import inspect
import phonenumbers

from depot.io.utils import FileIntent
from onegov.core.html import sanitize_html
from onegov.core.utils import binary_to_dictionary
from onegov.file.utils import content_type_from_fileobj
from onegov.file.utils import IMAGE_MIME_TYPES_AND_SVG
from onegov.form.validators import ValidPhoneNumber
from onegov.form.widgets import IconWidget
Expand Down Expand Up @@ -101,6 +103,43 @@ def process_fieldstorage(self, fs):
self.file.seek(0)


class UploadFileWithORMSupport(UploadField):
""" Extends the upload field with onegov.file support. """

def __init__(self, *args, **kwargs):
self.file_class = kwargs.pop('file_class')
super().__init__(*args, **kwargs)

def populate_obj(self, obj, name):
if self.action == 'keep':
pass

elif self.action == 'delete':
setattr(obj, name, None)

elif self.action == 'replace':
self.file.filename = self.filename
self.file.seek(0)

setattr(obj, name, self.file_class(
name=self.filename,
reference=self.file
))

else:
raise NotImplementedError(f"Unknown action: {self.action}")

def process_data(self, value):
if value:
self.data = {
'filename': value.name,
'size': value.reference.file.content_length,
'mimetype': value.reference.content_type
}
else:
super().process_data(value)


class HtmlField(TextAreaField):
""" A textfield with html with integrated sanitation. """

Expand Down

0 comments on commit 624deb0

Please sign in to comment.