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

Implement upload options #13

Open
shangxiao opened this issue Dec 20, 2017 · 1 comment
Open

Implement upload options #13

shangxiao opened this issue Dec 20, 2017 · 1 comment

Comments

@shangxiao
Copy link

Hey thanks for creating what I believe is the correct way to do this!

I'd like to request the ability to specify upload options: My specific use-case is incoming transformations.

I'm not entirely sure how this would be communicated to the storage layer but declaring the options on the model's field would be nice.

With pycloudinary the way they've supported this is to allow you to extend and override CloudinaryField.upload_options(). I don't like this style though as it's preferred to specify options like this as arguments to the field's constructor.

@klis87
Copy link
Owner

klis87 commented Dec 21, 2017

Because we have quite a busy times (just before Christmas), I am not able to look more deeply into this problem, but at first glance, for now you could subclass MediaCloudinaryStorage and overwrite _upload:

class MyCustomMediaCloudinaryStorage(MediaCloudinaryStorage):
    def _upload(self, name, content):
        // adjust your options here
        options = {'use_filename': True, 'resource_type': self._get_resource_type(name), 'tags': self.TAG}
        folder = os.path.dirname(name)
        if folder:
            options['folder'] = folder
        return cloudinary.uploader.upload(content, **options)

Then you could use this storage directly in your models, like:

class MyModel(models.Model):
    raw_file = models.ImageField(upload_to='/files', blank=True, storage=MyCustomMediaCloudinaryStorage())

You could even overwrite __init__ in your custom storage to allow options kwarg and use it in _upload. I now this is not perfect, and ideally this should be built in the core, but I won't be able to implement this in this year, but if you have time, PR will be appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants