Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified example app so that creating a FineFile model places the file in a custom location. #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions django_fine_uploader/fineuploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def storage(self):
file_storage = utils.import_class(self.storage_class)
return file_storage()

def rmtree_containing_final_file(self):
shutil.rmtree(self.storage.path(self.file_path))

def save(self):
raise NotImplementedError

Expand Down
5 changes: 4 additions & 1 deletion example/myapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@


def file_using_key_path(instance, filename):
return '{0}/{1}'.format(instance.key, filename)
if instance.key is not None and len(instance.key) > 0:
return 'fine_files/{0}/{1}'.format(instance.key, filename)
else:
return 'fine_files/{}'.format(filename)


class FineFile(models.Model):
Expand Down
11 changes: 9 additions & 2 deletions example/myapp/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os

from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.core.files import File
from django.views import generic

from django_fine_uploader.fineuploader import SimpleFineUploader
Expand Down Expand Up @@ -81,8 +84,12 @@ def form_valid(self, form):
data = {'success': True}
if self.upload.finished:
data['file_url'] = self.upload.url
# Let's save in database?
FineFile.objects.create(fine_file=self.upload.real_path)
filename = os.path.split(self.upload.real_path)[1]
with self.upload.storage.open(self.upload.real_path, 'rb') as f:
file = File(f, name=filename)
# Let's save in database?
FineFile.objects.create(fine_file=file)
self.upload.rmtree_containing_final_file()
return self.make_response(data)


Expand Down