Skip to content

Commit

Permalink
Improve basewidget parent property; add filefield upload_to call support
Browse files Browse the repository at this point in the history
  • Loading branch information
UmSenhorQualquer committed May 22, 2019
1 parent 5c2be32 commit 513efdc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
19 changes: 17 additions & 2 deletions pyforms_web/basewidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ def deserialize_form(self, params):
"""
widgets = []

if hasattr(self, 'parent') and isinstance(self.parent, (str,str)):
self.parent = PyFormsMiddleware.get_instance(self.parent)
#if hasattr(self, 'parent') and isinstance(self.parent, str):
# self.parent = PyFormsMiddleware.get_instance(self.parent)


for key, value in params.items():
Expand Down Expand Up @@ -625,6 +625,9 @@ def serialize_form(self):
except:
pass

if self.parent:
self._parent_win_id = self.parent.uid

return res


Expand Down Expand Up @@ -771,6 +774,18 @@ def refresh_timeout(self):
def refresh_timeout(self, value): self._refresh_timeout = value


@property
def parent(self):
if hasattr(self, '_parent_win_id'):
return PyFormsMiddleware.get_instance(self._parent_win_id)
else:
return self._parent_win

@parent.setter
def parent(self, value):
if hasattr(self, '_parent_win_id'):
del self._parent_win_id
self._parent_win = value

############################################################################
############ WEB Properties ################################################
Expand Down
31 changes: 24 additions & 7 deletions pyforms_web/widgets/django/modelform.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,20 +687,37 @@ def update_object_fields(self, obj):
# if FileField
elif isinstance(field, models.FileField):
getattr(self, field.name).error = False
value = getattr(self, field.name).value
if value:

# get the temporary path of the file
tmp_filepath = getattr(self, field.name).value

if tmp_filepath:
# get the temporary filename
tmp_filename = os.path.basename(tmp_filepath)

# in case the upload_to is callable get
if callable(field.upload_to):
# in the case the upload_to property it is a function
filepath = field.upload_to(obj, tmp_filename)
filename = os.path.basename(filepath)
dirpath = os.path.dirname(filepath)
else:
dirpath = field.upload_to
filename = tmp_filename

try:
os.makedirs(os.path.join(settings.MEDIA_ROOT, field.upload_to))
os.makedirs(os.path.join(settings.MEDIA_ROOT, dirpath))
except os.error as e:
pass

paths = [p for p in value.split('/') if len(p)>0][1:]
from_path = os.path.join(settings.MEDIA_ROOT,*paths)
paths = [p for p in value.split(os.path.sep) if len(p)>0][1:]
from_path = os.path.join(settings.MEDIA_ROOT,*paths)

if os.path.exists(from_path):
to_path = os.path.join(settings.MEDIA_ROOT, field.upload_to, os.path.basename(value) )
to_path = os.path.join(settings.MEDIA_ROOT, dirpath, filename)
os.rename(from_path, to_path)

url = '/'.join([field.upload_to]+[os.path.basename(value) ])
url = '/'.join([dirpath]+[filename])
if url[0]=='/': url = url[1:]
setattr(obj, field.name, url)
elif field.null:
Expand Down

0 comments on commit 513efdc

Please sign in to comment.