Beispiel #1
0
    def save(self):
        image = self.cleaned_data["image"]
        hash = hashlib.md5(image.read()).hexdigest()
        image.name = u"".join((hash, ".", image.format))
        upload_to = os.path.join('spirit', 'images', str(self.user.pk))
        image.url = os.path.join(settings.MEDIA_URL, upload_to, image.name).replace("\\", "/")
        media_path = os.path.join(settings.MEDIA_ROOT, upload_to)
        utils.mkdir_p(media_path)

        with open(os.path.join(media_path, image.name), "wb") as fh:
            image.seek(0)
            fh.write(image.read())
            image.close()

        return image
Beispiel #2
0
    def save(self):
        image = self.cleaned_data["image"]
        hash = hashlib.md5(image.read()).hexdigest()
        name, ext = os.path.splitext(image.name)

        # Remove the extension if not allowed
        if ext and ext[1:].lower() not in settings.ST_ALLOWED_UPLOAD_IMAGE_EXT:
            ext = ""

        image.name = u"".join((hash, ext.lower()))
        upload_to = os.path.join('spirit', 'images', str(self.user.pk))
        image.url = os.path.join(settings.MEDIA_URL, upload_to, image.name).replace("\\", "/")
        media_path = os.path.join(settings.MEDIA_ROOT, upload_to)
        utils.mkdir_p(media_path)

        with open(os.path.join(media_path, image.name), "wb") as fh:
            image.seek(0)
            fh.write(image.read())
            image.close()

        return image
Beispiel #3
0
    def save(self):
        image = self.cleaned_data["image"]
        hash = hashlib.md5(image.read()).hexdigest()
        name, ext = os.path.splitext(image.name)

        # Remove the extension if not allowed
        if ext and ext[1:].lower() not in settings.ST_ALLOWED_UPLOAD_IMAGE_EXT:
            ext = ""

        image.name = u"".join((hash, ext.lower()))
        upload_to = os.path.join('spirit', 'images', str(self.user.pk))
        image.url = os.path.join(settings.MEDIA_URL, upload_to,
                                 image.name).replace("\\", "/")
        media_path = os.path.join(settings.MEDIA_ROOT, upload_to)
        utils.mkdir_p(media_path)

        with open(os.path.join(media_path, image.name), "wb") as fh:
            image.seek(0)
            fh.write(image.read())
            image.close()

        return image
Beispiel #4
0
    def test_mkdir_p(self):
        """
        mkdir -p
        """
        # Empty path should raise an exception
        self.assertRaises(OSError, spirit_utils.mkdir_p, "")

        # Try to create an existing dir should do nothing
        self.assertTrue(os.path.isdir(settings.BASE_DIR))
        spirit_utils.mkdir_p(settings.BASE_DIR)

        # Create path tree
        # setup
        path = os.path.join(settings.BASE_DIR, "test_foo")
        sub_path = os.path.join(path, "bar")
        self.assertFalse(os.path.isdir(sub_path))
        self.assertFalse(os.path.isdir(path))
        # test
        spirit_utils.mkdir_p(sub_path)
        self.assertTrue(os.path.isdir(sub_path))
        # clean up
        os.rmdir(sub_path)
        os.rmdir(path)
Beispiel #5
0
    def test_mkdir_p(self):
        """
        mkdir -p
        """
        # Empty path should raise an exception
        self.assertRaises(OSError, spirit_utils.mkdir_p, "")

        # Try to create an existing dir should do nothing
        self.assertTrue(os.path.isdir(settings.BASE_DIR))
        spirit_utils.mkdir_p(settings.BASE_DIR)

        # Create path tree
        # setup
        path = os.path.join(settings.BASE_DIR, "test_foo")
        sub_path = os.path.join(path, "bar")
        self.assertFalse(os.path.isdir(sub_path))
        self.assertFalse(os.path.isdir(path))
        # test
        spirit_utils.mkdir_p(sub_path)
        self.assertTrue(os.path.isdir(sub_path))
        # clean up
        os.rmdir(sub_path)
        os.rmdir(path)