Skip to content

Commit

Permalink
Merge branch 'bug/overwrite-same-name-files' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
UmSenhorQualquer committed Sep 3, 2019
2 parents ba115ea + 778e655 commit 67ee52d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pyforms_web/widgets/django/modelform.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import datetime
from django.utils import timezone


import string
import random

class ModelFormWidget(BaseWidget):
"""
Expand Down Expand Up @@ -734,6 +735,14 @@ def update_object_fields(self, obj):

if os.path.exists(from_path):
to_path = os.path.join(settings.MEDIA_ROOT, dirpath, filename)


while os.path.exists(to_path):
name, ext = os.path.splitext(filename)
sufix = ''.join([random.choice(string.ascii_uppercase + string.digits) for _ in range(3)])
filename = name+'_'+sufix+ext
to_path = os.path.join(settings.MEDIA_ROOT, dirpath, filename)

os.rename(from_path, to_path)

url = '/'.join([dirpath]+[filename])
Expand Down

0 comments on commit 67ee52d

Please sign in to comment.