ApPeL
ApPeL

Reputation: 4911

django-photologue upload_to

I have been playing around with django-photologue for a while, and find this a great alternative to all other image handlings apps out there.

One thing though, I also use django-cumulus to push my uploads to my CDN instead of running it on my local machine / server.

When I used imagekit, I could always pass a upload_to='whatever' but I cannot seem to do this with photologue as it automatically inserts the imagefield. How would I go about achieving some sort of an overwrite?

Regards

Upvotes: 2

Views: 833

Answers (3)

Eric Clack
Eric Clack

Reputation: 1926

You can use the setting PHOTOLOGUE_PATH to provide your own callable. Define a method which takes instance and filename as parameters then return whatever you want. For example in your settings.py:

import photologue

...

def PHOTOLOGUE_PATH(instance, filename):
  folder = 'myphotos' # Add your logic here
  return os.path.join(photologue.models.PHOTOLOGUE_DIR, folder, filename)

Presumably (although I've not tested this) you could find out more about the Photo instance (e.g. what other instances it relates to) and choose the folder accordingly.

Upvotes: 0

ApPeL
ApPeL

Reputation: 4911

Managed to find a workaround for it, however this requires you to make the changes in photologue/models.py

if PHOTOLOGUE_PATH is not None:
    if callable(PHOTOLOGUE_PATH):
        get_storage_path = PHOTOLOGUE_PATH
    else:
        parts = PHOTOLOGUE_PATH.split('.')
        module_name = '.'.join(parts[:-1])
        module = __import__(module_name)
        get_storage_path = getattr(module, parts[-1])
else:
    def get_storage_path(instance, filename):
        dirname = 'photos'
        if hasattr(instance, 'upload_to'):
            if callable(instance.upload_to):
                dirname = instance.upload_to()
            else: dirname = instance.upload_to
        return os.path.join(PHOTOLOGUE_DIR, 
                        os.path.normpath( force_unicode(datetime.now().strftime(smart_str(dirname))) ), 
                        filename)

And then in your apps models do the following:

class MyModel(ImageModel):
    text = ...
    name = ...

    def upload_to(self):
        return 'yourdirectorynamehere'

Upvotes: 0

OmerGertel
OmerGertel

Reputation: 2583

I think you can hook into the pre_save signal of the Photo model, and change the upload_to field, just before the instance is saved to the database.

Take a look at this: http://docs.djangoproject.com/en/dev/topics/signals/

Upvotes: 1

Related Questions