views:

54

answers:

2

Hi all.

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

+1  A: 

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/

OmerGertel
ok, i'll take a look into it, however I tend to have different CDN's based on certain aspects of the project. Would you still recommend this way?
ApPeL
@ApPel, I'd prefer any way in which changes are not done to third party code (photologue in this case). Within the signal handler you can determine which path you want to store.
OmerGertel
ok, cool. I'll take a look at it
ApPeL
A: 

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'
ApPeL