Skip to content

Commit

Permalink
(SIO-2203) Support max_length in FiletrackerStorage.save
Browse files Browse the repository at this point in the history
This fixes following warning:
    RemovedInDjango110Warning: Backwards compatibility for storage backends without support for the `max_length` argument in Storage.save() will be removed in Django 1.10.

Change-Id: I6d9a3769baae760349d9d7a827a1e6ed237442ca
  • Loading branch information
cytadela8 committed Apr 2, 2019
1 parent 709fb85 commit 257672c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions oioioi/filetracker/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.core.files import File
from django.core.files.storage import Storage
from django.core.urlresolvers import reverse
from django.core.exceptions import SuspiciousFileOperation

from oioioi.filetracker.client import get_client
from oioioi.filetracker.filename import FiletrackerFilename
Expand Down Expand Up @@ -83,16 +84,21 @@ def read_using_cache(self, name):
reader, _version = self.client.get_stream(path, serve_from_cache=True)
return File(reader, FiletrackerFilename(name))

def save(self, name, content):
# FIXME: max_length might not be implemented properly. It's guaranteed that we won't return
# a name exceeding max_length, but we might fail to generate short name even if it's possible.
def save(self, name, content, max_length=None):
# Well, the default Django implementation of save coerces the returned
# value to unicode using force_text. This is not what we want, as we
# have to preserve FiletrackerFilename.
if name is None:
name = content.name
if not hasattr(content, 'chunks'):
content = File(content)
name = self.get_available_name(name)
return self._save(name, content)
name = self.get_available_name(name, max_length=max_length)
name = self._save(name, content)
if max_length is not None and len(name) > max_length:
raise SuspiciousFileOperation
return name

def delete(self, name):
path = self._make_filetracker_path(name)
Expand Down

0 comments on commit 257672c

Please sign in to comment.