Dynamically compressing static files with django-pipeline - python

I am getting started with django-pipeline. If I understand it correctly, I need to specify the directories with my CSS/JS files to compress them. However, this is a tedious task as my project is quite big and has static files here and there (not only under /static/ directory).
I saw that it has collectstatic integration but it is not what I thought: it just runs the compressor after collecting the static files, and will only compress the files you manually specified in the settings and not all the static files.
Is there any way I could tell django-pipeline to just compress every static file I have?

You can use glob syntax to select multiples files. Like this way,
You don't want to use collectstatic right ?
then use this way,
from django.contrib.staticfiles import finders
all_js = ["{0}/{1}".format(st,"*.js") for st in finders.find("", all=True)]
all_css = ["{0}/{1}".format(st,"*.css") for st in finders.find("", all=True)]
PIPELINE_CSS = {
'colors': {
'source_filenames': tuple(all_css),
'output_filename': 'css/colors.css',
'extra_context': {
'media': 'screen,projection',
},
},
}
PIPELINE_JS = {
'stats': {
'source_filenames': tuple(all_js),
'output_filename': 'js/stats.js',
}
}

Related

How to use variables from backend in .scss files

I have a site built with Django, Python and Wagtail.
What I want is to be able to add some styles in the backend and then use it in my frontent's .scss files.
For example I want to be able to set a primary color to #fff via backend and then use it in my .scss file as:
$g-color-primary: primary_color_from_backend;
$g-font-size-default: primary_font_size_from_backend;
I do not have any idea how I can do that and if it's possible at all?
Thanks for the help.
Unfortunately, it is not possible. You can instead define different classes in the CSS file, then use them in your HTML template dependent on the Django template variables there.
This would require to write out the sass color variables content (with scss syntax) in a physical .scss file, which depends on your development environment. And then import it into other .scss file to get compiled and output through a frontend build process tool like Gulp, Webpack.
For example, Webpack's sass-loader plugin provides option to prepend sass code at Frontend build/compile time.
https://github.com/webpack-contrib/sass-loader
module.exports = {
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
prependData: '$env: ' + process.env.NODE_ENV + ';',
},
},
],
},
],
},
};

Serving static directories in CherryPy

I am trying to serve static files using CherryPy but I am unable to. I have looked in the tutorials but setting it up like that is also not working properly.
All this is using Python 3.4
Config
config = {
'/ws': {
'tools.websocket.on': True,
'tools.websocket.handler_cls': ChatWebSocketHandler,
'tools.websocket.protocols': ['toto', 'mytest', 'hithere']
},
'/assets': {
'tools.staticdir.on': True,
'tools.staticdir.dir': constants.TEMPLATE_PATH
},
}
I am starting up cherryPy like this
app_root = Root(args.host, args.port, args.ssl, ssl_port=args.ssl_port)
cherrypy.quickstart(app_root, '', config=config)
Constant Path is
TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)),"assets/")
I have tried using paths like assets/, /assets/ as well instead of the above constant.
The thing is it does not recognize anyone of them and always gives a 404 error.
I too have had difficulty setting this up. I have a rather complicated setup complete with multiple subdomains that has evolve through several early versions of CherryPy which may no longer be valid, and I've not verified this will work in the simpler quickstart configuration you have here. However the key lines in a setup that actually works for me is to put the config lines below in the webservice object that you mount. I put the config dict that defines the static dir in the class definition before any resources. It looks to me like you've defined your static dir in the configuration dict rather of a specific resource rather than the object. So perhaps try in your hosted service object:
class WebService(object):
_cp_config = {
'tools.staticdir.on': True,
'tools.staticdir.dir': '/path/to/serve/static/files/from'
}
#cherrypy.expose
def index(self):
[ ...additional resource definitions, etc ...]
Then later on:
my_cp_app =
cherrypy.tree.mount(subDomain.WebService(),
'/subdomainFileLocation',
subdomainConfigDict)
cherrypy.quickstart(config=domainConfig)
I know you're working on Python 3. This above works for me on Python 2.7 + cherrypy-8.1.2. I hope this is helpful.

React - Django webpack config with dynamic 'output'

Have:
I have a Django app. It has react for front-end. I have 2 django apps. movies_list and series_list. I have all my .jsx files inside baseapplication/movies_list/interfaces/ and baseapplication/series_list/interfaces/. The entry point of 2 apps is given ...../index as given in entry object of web-pack.config.js .
Need:
I need to put my compiled .js files inside baseapplication/movies_list/static/movies_list and baseapplication/series_list/static/series_list. So I need to find each entry in entry and get the abs path and construct my output path dynamically for the future apps. This will help my python manage.py collectstatic to get static files from each directory.
How to configure the output to make it dynamic?
module.exports = {
//The base directory (absolute path) for resolving the entry option
context: __dirname,
entry: {
movies: '../movies_list/interfaces/index',
series: '../series_list/interfaces/index',
},
output: {
// I need help here.
path: path.join('<', "static"),
//path: path.resolve('../[entry]/static/react_bundles/'),
filename: "[name].js",
},
}
https://github.com/webpack/docs/wiki/configuration
You can set entry points to a full path of the resulting file. Something like this should work:
module.exports = {
context: __dirname,
entry: {
'baseapplication/movies_list/static/movies_list/index': '../movies_list/interfaces/index',
'baseapplication/series_list/static/series_list/index': '../series_list/interfaces/index',
},
output: {
path: './',
filename: "[name].js",
},
}

django-pipeline with s3 storage is not compressing my js

I'm using django-pipeline with s3. I'm successfully using collectstatic to combined my Javascript files and store them in my s3 bucket, but they are not getting compressed for some reason (verified by looking at the file, its size, and its content-encoding). Otherwise things are working correctly with the combined scripts.js that is produced.
Here are the changes I made to use django-pipeline:
Added pipeline to installed apps.
Added 'pipeline.finders.PipelineFinder' to STATICFILES_FINDERS.
Set STATICFILES_STORAGE = 'mysite.custom_storages.S3PipelineManifestStorage' where this class is as defined in the documentation, as seen below.
Set PIPELINE_JS as seen below, which works but just isn't compressed.
PIPELINE_ENABLED = True since DEBUG = True and I'm running locally.
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor' even though this should be default.
Installed the Yuglify Compressor with npm -g install yuglify.
PIPELINE_YUGLIFY_BINARY = '/usr/local/bin/yuglify' even though the default with env should work.
Using the {% load pipeline %} and {% javascript 'scripts' %} which work.
More detail:
PIPELINE_JS = {
'scripts': {
'source_filenames': (
'lib/jquery-1.11.1.min.js',
...
),
'output_filename': 'lib/scripts.js',
}
}
class S3PipelineManifestStorage(PipelineMixin, ManifestFilesMixin, S3BotoStorage):
location = settings.STATICFILES_LOCATION
As mentioned, collectstatic does produce scripts.js just not compressed. The output of that command includes:
Post-processed 'lib/scripts.js' as 'lib/scripts.js'
I'm using Django 1.8, django-pipeline 1.5.2, and django-storages 1.1.8.
Similar questions:
django-pipeline not compressing
django pipeline with S3 storage not compressing
The missing step was to also extend GZipMixin, AND, it has to be first in the list of parents:
from pipeline.storage import GZIPMixin
class S3PipelineManifestStorage(GZIPMixin, PipelineMixin, ManifestFilesMixin, S3BotoStorage):
location = settings.STATICFILES_LOCATION
Now collectstatic produces a .gz version of each file as well, but my templates still weren't referencing the .gz version.
To address this the author says:
To make it work with S3, you would need to change the staticfiles
storage url method to return .gz urls (and staticfiles/pipeline
template tags depending if you care for clients that don't support
gzip). Also don't forget to setup the proper header on s3 to serve
theses assets as being gzipped.
I adapted an example he provided elsewhere, which overrides the url method:
class S3PipelineManifestStorage(GZIPMixin, PipelineMixin, ManifestFilesMixin, S3BotoStorage):
location = settings.STATICFILES_LOCATION
def url(self, name, force=False):
# Add *.css if you are compressing those as well.
gzip_patterns = ("*.js",)
url = super(GZIPMixin, self).url(name, force)
if matches_patterns(name, gzip_patterns):
return "{0}.gz".format(url)
return url
This still doesn't handle setting the Content-Encoding header.
A simpler alternative is to use the S3Boto Storages option AWS_IS_GZIPPED which performs gzipping AND sets the appropriate header.
More is required to support clients without gzip, however.
Also useful are these instructions from Amazon on serving compressed files from S3.

How do I regex search for python values? """string""", "string", 'string', (tuple), [list], {dict}

I'm trying to extract variables/values from a Django settings.py file (preserving comments, whitespace, etc.) and move or append them to another file programmatically using Python. Overall, I'm trying to split up the settings module into several files automatically (not manually using a text editor).
How do I find the value assigned to any variable in such a file? - If possible in a concise way, e.g. using regular expressions.
Example variables/values:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = [
'*'
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_PATH, 'database.sqlite'),
}
}
SECRET_KEY = '#*ji*0hbpj6#4-0=$%bl(z+z)c=pd(rn^9u*1_96f^ba+4w58v'
LONG_STRING_VALUE = \
"""
bla foo bar baz ...
"""
A_TUPLE = (
'...',
)
Notes:
I read the file as one long string, therefore regex matches can span several lines.
No need to fully parse/understand the value, just find the closing delimiter.
I'm wondering if the code of the Python interpreter is available (as a Python module) that parses values in Python code. Anybody knowing?
Partially working code is on GitHub: DjangoSettingsManager (identifies any variable until after equal sign, as of 15-feb-2014)
r'([ ]*#.*\n)*[ ]*(\A|\b)' + var + r'\s*=\s*\\?\s*'
Test this w/ Debuggex
Use django.conf.settings module, for instance you can know is django is in debug mode or not typing:
from django.conf import settings
settings.DEBUG
...
Doc here

Categories

Resources