Skip to content

Commit

Permalink
implemented storage and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsasha committed Feb 23, 2014
1 parent 120e45c commit d16e118
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
20 changes: 19 additions & 1 deletion randomfilestorage/storage.py
@@ -1 +1,19 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-

import os
from django.utils.crypto import get_random_string
from django.core.files.storage import FileSystemStorage


class RandomFileSystemStorage(FileSystemStorage):

def get_valid_name(self, name):
file_root, file_ext = os.path.splitext(name)
return "%s%s" % (get_random_string(32).lower(), file_ext)

def get_available_name(self, name):
dir_name, file_name = os.path.split(name)
file_root, file_ext = os.path.splitext(file_name)
while self.exists(name):
name = os.path.join(dir_name, "%s%s" % (get_random_string(32).lower(), file_ext))
return name
27 changes: 17 additions & 10 deletions tests/test_storage.py
Expand Up @@ -7,21 +7,28 @@
Tests for `django-random-filestorage` models module.
"""

import tempfile
import os
import shutil
import unittest

from randomfilestorage import models
from randomfilestorage.storage import RandomFileSystemStorage


class TestRandomfilestorage(unittest.TestCase):

def setUp(self):
pass

def test_something(self):
pass

def tearDown(self):
pass
self.temp_storage_location = tempfile.mkdtemp()
self.storage = RandomFileSystemStorage(location=self.temp_storage_location)

def test_get_valid_name(self):
name1 = self.storage.get_valid_name('foobar')
name2 = self.storage.get_valid_name('foobar.txt')
self.assertNotEqual(name1, name2)
self.assertFalse('foobar' in name1 or 'foobar' in name2)

def test_get_available_name(self):
file_path = os.path.join(self.temp_storage_location, 'foobar.txt')
open(file_path, 'a').close()
available_path = self.storage.get_available_name(file_path)
self.assertNotEqual(available_path, file_path)
self.assertFalse('foobar' in available_path)

0 comments on commit d16e118

Please sign in to comment.