예제 #1
0
def serve_media(request, path):

    full_path = os.path.join(settings.MEDIA_ROOT, path)
    if not os.path.exists(full_path):
        storage = DropboxStorage(location=settings.DROPBOX_LOCATION)
        if not storage.exists(path):
            raise Http404
        dir_name = os.path.dirname(full_path)
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        with open(full_path, "wb") as f:
            image = storage.open(path)
            f.write(image.read())
            image.close()

    statobj = os.stat(full_path)
    mimetype, encoding = mimetypes.guess_type(full_path)
    mimetype = mimetype or "application/octet-stream"
    with open(full_path, "rb") as f:
        response = HttpResponse(f.read(), mimetype=mimetype)
    if not settings.DEBUG:
        os.remove(full_path)
    response["Last-Modified"] = http_date(statobj.st_mtime)
    if stat.S_ISREG(statobj.st_mode):
        response["Content-Length"] = statobj.st_size
    if encoding:
        response["Content-Encoding"] = encoding

    return response
예제 #2
0
 def setUp(self):
   self.location = '/Public/testing'
   self.storage = DropboxStorage(location=self.location)
   self.tmpfile = tempfile.NamedTemporaryFile()
예제 #3
0
class DropboxStorageTest(TestCase):
  def setUp(self):
    self.location = '/Public/testing'
    self.storage = DropboxStorage(location=self.location)
    self.tmpfile = tempfile.NamedTemporaryFile()

  def tearDown(self):
    self.tmpfile.close()

  def test_file_access(self):
    """
    Standard file access options are available, and work as expected.
    """
    self.assertFalse(self.storage.exists('storage_test'))
    
    with self.storage.open('storage_test', 'w') as f:
      f.write('storage contents')
    self.assertTrue(self.storage.exists('storage_test'))

    with self.storage.open('storage_test', 'r') as f:
      self.assertEqual(f.read(), 'storage contents')

    self.storage.delete('storage_test')
    self.assertFalse(self.storage.exists('storage_test'))

  def test_exists_folder(self):
    self.assertFalse(self.storage.exists('storage_test_exists'))
    self.storage.client.file_create_folder(self.location + '/storage_test_exists')
    self.assertTrue(self.storage.exists('storage_test_exists'))
    self.storage.delete('storage_test_exists')
    self.assertFalse(self.storage.exists('storage_test_exists'))

  def test_listdir(self):
    """
    File storage returns a tuple containing directories and files.
    """
    self.assertFalse(self.storage.exists('storage_test_1'))
    self.assertFalse(self.storage.exists('storage_test_2'))
    self.assertFalse(self.storage.exists('storage_dir_1'))

    f = self.storage.save('storage_test_1', ContentFile('custom content'))
    f = self.storage.save('storage_test_2', ContentFile('custom content'))
    self.storage.client.file_create_folder(self.location + '/storage_dir_1')

    dirs, files = self.storage.listdir(self.location)
    self.assertEqual(set(dirs), set([u'storage_dir_1']))
    self.assertEqual(set(files),
                     set([u'storage_test_1', u'storage_test_2']))

    self.storage.delete('storage_test_1')
    self.storage.delete('storage_test_2')
    self.storage.delete('storage_dir_1')
    
  def test_file_url(self):
    """
    File storage returns a url to access a given file from the Web.
    """
    # url = self.share(path)
    # self.assertRaises(ValueError, self.storage.url, 'test.file')
    pass

  def test_file_size(self):
    """
    File storage returns a url to access a given file from the Web.
    """
    self.assertFalse(self.storage.exists('storage_test_size'))

    contents = "here are some file contents."
    with self.storage.open('storage_test_size', 'w') as f:
      f.write(contents)
    self.assertTrue(self.storage.exists('storage_test_size'))

    with self.storage.open('storage_test_size', 'r') as f:
      self.assertEqual(f.size, len(contents))

    self.storage.delete('storage_test_size')
    self.assertFalse(self.storage.exists('storage_test_size'))
예제 #4
0
# -*- coding: utf-8 -*-
from django.conf import settings

DEFAULT_MEDIA_STORAGE = getattr(settings, 'DEFAULT_MEDIA_STORAGE', 'DROPBOX')
DEFAULT_MEDIA_STORAGE_LOCATION = getattr(settings,
                                         'DEFAULT_MEDIA_STORAGE_LOCATION',
                                         '/simple_classroom')

if DEFAULT_MEDIA_STORAGE.upper() == 'DROPBOX':
    from django_dropbox.storage import DropboxStorage

    STORAGE = DropboxStorage(location=DEFAULT_MEDIA_STORAGE_LOCATION)
else:
    from django.core.files.storage import FileSystemStorage
    STORAGE = FileSystemStorage()
예제 #5
0
import os
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django_dropbox.storage import DropboxStorage
from django.utils.encoding import force_text


STORAGE = DropboxStorage(location="/test/djangodropbox")


@python_2_unicode_compatible
class TestDropbox(models.Model):
    """
    Model for test django-dropbox storage
    """
    file_test = models.FileField(upload_to=".",storage = STORAGE, null=True)

    def __str__(self):
        return os.path.basename(self.file_test.name)
예제 #6
0
 def setUp(self):
     self.location = '/Public/testing'
     self.storage = DropboxStorage(location=self.location)
     self.storage.base_url = '/test_media_url/'
예제 #7
0
class DropboxStorageTest(TestCase):
    def setUp(self):
        self.location = '/Public/testing'
        self.storage = DropboxStorage(location=self.location)
        self.storage.base_url = '/test_media_url/'

    def test_file_access_options(self):
        """
        Standard file access options are available, and work as expected.
        """
        self.assertFalse(self.storage.exists('storage_test'))
        f = self.storage.open('storage_test', 'w')
        f.write('storage contents')
        f.close()
        self.assertTrue(self.storage.exists('storage_test'))

        f = self.storage.open('storage_test', 'r')
        self.assertEqual(f.read(), 'storage contents')
        f.close()

        self.storage.delete('storage_test')
        self.assertFalse(self.storage.exists('storage_test'))

    def test_exists_folder(self):
        self.assertFalse(self.storage.exists('storage_test_exists'))
        self.storage.client.file_create_folder(self.location +
                                               '/storage_test_exists')
        self.assertTrue(self.storage.exists('storage_test_exists'))
        self.storage.delete('storage_test_exists')
        self.assertFalse(self.storage.exists('storage_test_exists'))

    def test_listdir(self):
        """
        File storage returns a tuple containing directories and files.
        """
        self.assertFalse(self.storage.exists('storage_test_1'))
        self.assertFalse(self.storage.exists('storage_test_2'))
        self.assertFalse(self.storage.exists('storage_dir_1'))

        f = self.storage.save('storage_test_1', ContentFile('custom content'))
        f = self.storage.save('storage_test_2', ContentFile('custom content'))
        self.storage.client.file_create_folder(self.location +
                                               '/storage_dir_1')

        dirs, files = self.storage.listdir(self.location)
        self.assertEqual(set(dirs), set([u'storage_dir_1']))
        self.assertEqual(set(files),
                         set([u'storage_test_1', u'storage_test_2']))

        self.storage.delete('storage_test_1')
        self.storage.delete('storage_test_2')
        self.storage.delete('storage_dir_1')

    def test_file_size(self):
        """
        File storage returns a url to access a given file from the Web.
        """
        self.assertFalse(self.storage.exists('storage_test_size'))
        f = self.storage.open('storage_test_size', 'w')
        f.write('these are 18 bytes')
        f.close()
        self.assertTrue(self.storage.exists('storage_test_size'))

        f = self.storage.open('storage_test_size', 'r')
        self.assertEqual(f.size, 18)
        f.close()

        self.storage.delete('storage_test_size')
        self.assertFalse(self.storage.exists('storage_test_size'))
예제 #8
0
    def image(self, obj):
        if obj.photo:
            return '<img src="%s">' % obj.photo.url
        return ''

    image.allow_tags = True


admin.site.register(Person, PersonAdmin)
########NEW FILE########
__FILENAME__ = models
from django.db import models
from django_dropbox.storage import DropboxStorage

STORAGE = DropboxStorage()


class Person(models.Model):
    photo = models.ImageField(upload_to='photos',
                              storage=STORAGE,
                              null=True,
                              blank=True)
    resume = models.FileField(upload_to='resumes',
                              storage=STORAGE,
                              null=True,
                              blank=True)


########NEW FILE########
__FILENAME__ = tests
예제 #9
0
파일: tests.py 프로젝트: AthenaCES29/Athena
 def setUp(self):
     """Setup any state specific to the execution of the given module."""
     self.location = '/Public/testing'
     self.storage = DropboxStorage(location=self.location)
     self.storage.base_url = '/test_media_url/'
     self.storage.delete('/Public/testing')
예제 #10
0
파일: tests.py 프로젝트: AthenaCES29/Athena
class DropboxStorageTest(TestCase):

    def setUp(self):
        """Setup any state specific to the execution of the given module."""
        self.location = '/Public/testing'
        self.storage = DropboxStorage(location=self.location)
        self.storage.base_url = '/test_media_url/'
        self.storage.delete('/Public/testing')

    def tearDown(self):
        """Teardown any state that was previously setup with a setup."""
        self.storage.delete('/Public/testing')

    def test_file_create(self):
        """File storage should delete file."""
        self.assertFalse(self.storage.exists('storage_test_1'))
        self.storage.save('storage_test_1', ContentFile('custom content'))
        self.assertTrue(self.storage.exists('storage_test_1'))
        self.assertFalse(self.storage.exists('storage_test_2'))
        self.storage.save('storage_test_2', ContentFile('custom content'))
        self.assertTrue(self.storage.exists('storage_test_2'))

    def test_file_delete(self):
        """File storage should delete file."""
        self.assertFalse(self.storage.exists('storage_test_1'))
        self.storage.save('storage_test_1', ContentFile('custom content'))
        self.assertTrue(self.storage.exists('storage_test_1'))
        self.storage.delete('storage_test_1')
        self.assertFalse(self.storage.exists('storage_test_1'))

    def test_file_delete2(self):
        """File storage should delete dir."""
        self.storage.delete('storage_dir_1')
        self.assertFalse(self.storage.exists('storage_dir_1'))
        self.storage.client.file_create_folder(self.location + '/storage_dir_1')
        self.assertTrue(self.storage.exists('storage_dir_1'))
        self.storage.delete('storage_dir_1')
        self.assertFalse(self.storage.exists('storage_dir_1'))

    def test_file_access_options(self):
        """Standard file access options are available, and work as expected."""
        self.assertFalse(self.storage.exists('storage_test'))
        f = self.storage.open('storage_test', 'w')
        f.write('storage contents')
        f.close()
        self.assertTrue(self.storage.exists('storage_test'))

        f = self.storage.open('storage_test', 'r')
        self.assertEqual(f.read(), six.b('storage contents'))
        f.close()

        self.storage.delete('storage_test')
        self.assertFalse(self.storage.exists('storage_test'))

    def test_exists_folder(self):
        self.storage.delete('storage_test_exists')
        self.assertFalse(self.storage.exists('storage_test_exists'))
        self.storage.client.file_create_folder(self.location + '/storage_test_exists')
        self.assertTrue(self.storage.exists('storage_test_exists'))
        self.storage.delete('storage_test_exists')
        self.assertFalse(self.storage.exists('storage_test_exists'))

    def test_listdir(self):
        """File storage returns a tuple containing directories and files."""
        self.assertFalse(self.storage.exists('storage_test_1'))
        self.assertFalse(self.storage.exists('storage_test_2'))
        self.assertFalse(self.storage.exists('storage_dir_1'))

        self.storage.save('storage_test_1', ContentFile(six.b('custom content')))
        self.storage.save('storage_test_2', ContentFile(six.b('custom content')))
        self.storage.client.file_create_folder(self.location + '/storage_dir_1')

        dirs, files = self.storage.listdir(self.location)
        self.assertEqual(set(dirs), set([u'storage_dir_1']))
        self.assertEqual(set(files),
                         set([u'storage_test_1', u'storage_test_2']))

    def test_file_size(self):
        """File storage returns a url to access a given file from the Web."""
        self.assertFalse(self.storage.exists('storage_test_size'))
        f = self.storage.open('storage_test_size', 'w')
        f.write('these are 18 bytes')
        f.close()
        self.assertTrue(self.storage.exists('storage_test_size'))

        f = self.storage.open('storage_test_size', 'r')
        self.assertEqual(f.size, 18)
        f.close()

        self.storage.delete('storage_test_size')
        self.assertFalse(self.storage.exists('storage_test_size'))
예제 #11
0
 def setUp(self):
     self.location = '/Public/testing'
     self.storage = DropboxStorage(location=self.location)
     self.tmpfile = tempfile.NamedTemporaryFile()
예제 #12
0
class DropboxStorageTest(TestCase):
    def setUp(self):
        self.location = '/Public/testing'
        self.storage = DropboxStorage(location=self.location)
        self.tmpfile = tempfile.NamedTemporaryFile()

    def tearDown(self):
        self.tmpfile.close()

    def test_file_access(self):
        """
    Standard file access options are available, and work as expected.
    """
        self.assertFalse(self.storage.exists('storage_test'))

        with self.storage.open('storage_test', 'w') as f:
            f.write('storage contents')
        self.assertTrue(self.storage.exists('storage_test'))

        with self.storage.open('storage_test', 'r') as f:
            self.assertEqual(f.read(), 'storage contents')

        self.storage.delete('storage_test')
        self.assertFalse(self.storage.exists('storage_test'))

    def test_exists_folder(self):
        self.assertFalse(self.storage.exists('storage_test_exists'))
        self.storage.client.file_create_folder(self.location +
                                               '/storage_test_exists')
        self.assertTrue(self.storage.exists('storage_test_exists'))
        self.storage.delete('storage_test_exists')
        self.assertFalse(self.storage.exists('storage_test_exists'))

    def test_listdir(self):
        """
    File storage returns a tuple containing directories and files.
    """
        self.assertFalse(self.storage.exists('storage_test_1'))
        self.assertFalse(self.storage.exists('storage_test_2'))
        self.assertFalse(self.storage.exists('storage_dir_1'))

        f = self.storage.save('storage_test_1', ContentFile('custom content'))
        f = self.storage.save('storage_test_2', ContentFile('custom content'))
        self.storage.client.file_create_folder(self.location +
                                               '/storage_dir_1')

        dirs, files = self.storage.listdir(self.location)
        self.assertEqual(set(dirs), set([u'storage_dir_1']))
        self.assertEqual(set(files),
                         set([u'storage_test_1', u'storage_test_2']))

        self.storage.delete('storage_test_1')
        self.storage.delete('storage_test_2')
        self.storage.delete('storage_dir_1')

    def test_file_url(self):
        """
    File storage returns a url to access a given file from the Web.
    """
        # url = self.share(path)
        # self.assertRaises(ValueError, self.storage.url, 'test.file')
        pass

    def test_file_size(self):
        """
    File storage returns a url to access a given file from the Web.
    """
        self.assertFalse(self.storage.exists('storage_test_size'))

        contents = "here are some file contents."
        with self.storage.open('storage_test_size', 'w') as f:
            f.write(contents)
        self.assertTrue(self.storage.exists('storage_test_size'))

        with self.storage.open('storage_test_size', 'r') as f:
            self.assertEqual(f.size, len(contents))

        self.storage.delete('storage_test_size')
        self.assertFalse(self.storage.exists('storage_test_size'))
예제 #13
0
 def setUp(self):
     self.location = '/Public/testing'
     self.storage = DropboxStorage(location=self.location)
     self.storage.base_url = '/test_media_url/'
예제 #14
0
class DropboxStorageTest(TestCase):

    def setUp(self):
        self.location = '/Public/testing'
        self.storage = DropboxStorage(location=self.location)
        self.storage.base_url = '/test_media_url/'

    def test_file_access_options(self):
        """
        Standard file access options are available, and work as expected.
        """
        self.assertFalse(self.storage.exists('storage_test'))
        f = self.storage.open('storage_test', 'w')
        f.write('storage contents')
        f.close()
        self.assertTrue(self.storage.exists('storage_test'))

        f = self.storage.open('storage_test', 'r')
        self.assertEqual(f.read(), 'storage contents')
        f.close()

        self.storage.delete('storage_test')
        self.assertFalse(self.storage.exists('storage_test'))

    def test_exists_folder(self):
        self.assertFalse(self.storage.exists('storage_test_exists'))
        self.storage.client.file_create_folder(self.location + '/storage_test_exists')
        self.assertTrue(self.storage.exists('storage_test_exists'))
        self.storage.delete('storage_test_exists')
        self.assertFalse(self.storage.exists('storage_test_exists'))

    def test_listdir(self):
        """
        File storage returns a tuple containing directories and files.
        """
        self.assertFalse(self.storage.exists('storage_test_1'))
        self.assertFalse(self.storage.exists('storage_test_2'))
        self.assertFalse(self.storage.exists('storage_dir_1'))

        f = self.storage.save('storage_test_1', ContentFile('custom content'))
        f = self.storage.save('storage_test_2', ContentFile('custom content'))
        self.storage.client.file_create_folder(self.location + '/storage_dir_1')

        dirs, files = self.storage.listdir(self.location)
        self.assertEqual(set(dirs), set([u'storage_dir_1']))
        self.assertEqual(set(files),
                         set([u'storage_test_1', u'storage_test_2']))

        self.storage.delete('storage_test_1')
        self.storage.delete('storage_test_2')
        self.storage.delete('storage_dir_1')
        
    def test_file_url(self):
        """
        File storage returns a url to access a given file from the Web.
        """
        self.assertEqual(self.storage.url('test.file'),
            '%s%s' % (self.storage.base_url, 'test.file'))

        # should encode special chars except ~!*()'
        # like encodeURIComponent() JavaScript function do
        self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""),
            """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")

        # should stanslate os path separator(s) to the url path separator
        self.assertEqual(self.storage.url("""a/b\\c.file"""),
            """/test_media_url/a/b/c.file""")

        self.storage.base_url = None
        self.assertRaises(ValueError, self.storage.url, 'test.file')

    def test_file_size(self):
        """
        File storage returns a url to access a given file from the Web.
        """
        self.assertFalse(self.storage.exists('storage_test_size'))
        f = self.storage.open('storage_test_size', 'w')
        f.write('these are 18 bytes')
        f.close()
        self.assertTrue(self.storage.exists('storage_test_size'))

        f = self.storage.open('storage_test_size', 'r')
        self.assertEqual(f.size, 18)
        f.close()

        self.storage.delete('storage_test_size')
        self.assertFalse(self.storage.exists('storage_test_size'))