[Django]-Force django_compressor to recompile css/less files

2πŸ‘

βœ…

I used a different approach. I am now using offline compression, which is faster and better for multi server deployments anyways.

I give the user an interface to change certain css and less values. I save those css/less values in a database table, so that the user can edit the stuff easily.

To make the new css/less values available to the frontend (compiled css files) I write the values the user entered in a less file to the disk and re-run the python manage.py compress command.

This way the compiled compressor files are generated and if the user entered invalid less code, which would lead to compile errors, the compressor stops and keeps the old css files.

Here’s my save() method:

 def save(self, *args, **kwargs):

        #write the less file to the file system
        #everytime the model is saved
        try:

            file_location = os.path.join(settings.STATIC_ROOT, 'styles', 'less', 'custom_styles.less')

            with open(file_location, 'w') as f:
                f.write(render_to_string('custom_styles/custom_stylesheet_tmpl.txt', {'platform_customizations': self}))

        except IOError:
            #TODO show error message to user in admin backend and via messaging system
            raise IOError

        #re-run offline compress command in prod mode
        management.call_command('compress')


        super(PlatformCustomizations, self).save(*args, **kwargs)
πŸ‘€j7nn7k

5πŸ‘

I can come up with two possible solutions, I don’t like both of them very much.

You can call compress (doc) from within incron or cron:

python manage.py compress

Or you can set a very low COMPRESS_REBUILD_TIMEOUT. (doc)

BTW you have the user scripts as a seperate bundle, right?

πŸ‘€muhuk

Leave a comment