Skip to content

Commit

Permalink
Merge pull request #228 from camptocamp/allow_file_url_field_customiz…
Browse files Browse the repository at this point in the history
…ation

Allow file URL field customization
  • Loading branch information
arnaud-morvan authored Sep 1, 2020
2 parents 14cb25c + 31481fc commit eed7a9b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions c2cgeoform/ext/deform_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,9 @@ class Photo(models.FileData, Base):
**Attributes/Arguments**
id_field (default to "id")
Name of field to pass to get_url.
get_url (optional)
A callback function `function(request, id) -> string` which returns
the URL to get the file. Example usage:
Expand All @@ -629,9 +632,11 @@ class Photo(models.FileData, Base):
'widget': deform_ext.FileUploadWidget(
_file_upload_temp_store,
id_field="id",
get_url=lambda request, id: request.route_url('file', id=id)
)
"""
id_field = "id"

def __init__(self, get_url=None, **kw):
DeformFileUploadWidget.__init__(self, None, **kw)
Expand All @@ -645,10 +650,10 @@ def serialize(self, field, cstruct, **kw):
if cstruct in (null, None):
cstruct = {}
kw['url'] = None
if 'uid' not in cstruct and 'id' in cstruct:
cstruct['uid'] = cstruct['id']
if cstruct['id'] != null and self.get_url:
kw['url'] = self.get_url(self.request, cstruct['id'])
if 'uid' not in cstruct and self.id_field in cstruct:
cstruct['uid'] = cstruct[self.id_field]
if cstruct[self.id_field] != null and self.get_url:
kw['url'] = self.get_url(self.request, cstruct[self.id_field])
if cstruct.get('filename', None) == null:
cstruct['filename'] = ""
return DeformFileUploadWidget.serialize(self, field, cstruct, **kw)
Expand Down

0 comments on commit eed7a9b

Please sign in to comment.