Skip to content

Commit

Permalink
Initial commit. Testing and working locally on Django 2.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAnthony committed Nov 27, 2019
0 parents commit dc1cb78
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*__pycache__/
*.pyc
*.egg-info/
.DS_Store
build/
dist/
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The Changelog
=============

1.0.0 (27.11.2019)
~~~~~~~~~~~~~~~~~~
* Initial release
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Tim Kamanin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include *.txt
include *.md
recursive-include docs *
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# django-flexible-manifest-staticfiles

This extendion to django.contrib.staticfiles.storage.ManifestStaticFilesStorage allows you to specify a list of file patterns that should be ignored/excluded from being versioned and added to the manifest. You can also specify it the other way around and say which files should be included.

This allows you to add versioned naming to only static files of your choosing. For example, you may want CSS and JS files to be versioned, but not fonts, images or other assets.

I've written and tested this against Django 2.2. Your mileage elsewhere may vary!

## Installation

Use pip:

```shell
pip install django-flexible-manifest-staticfiles
```

## Setup

Set in your `settings.py`:

```python
STATICFILES_STORAGE = 'django_flexible_manifest_staticfiles.storages.ForgivingManifestStaticFilesStorage'
```

## Usage

Set in your `settings.py` you can set two settings:

- `STATICFILES_VERSIONED_INCLUDE` - This is a list of patterns you want to be included. Only files that match at least one of these patterns will be included. If you omit this setting, then all files are included by default.
- `STATICFILES_VERSIONED_EXCLUDE` - Any file matching any of these patterns will be excluded from being versioned.

Note that the complete path relative to the `static` directory is available for matching against.

## Examples

This would only version `.css` and `.js` files, but would exclude minified files:

```python
STATICFILES_VERSIONED_INCLUDE = ['.css$', '.js$']
STATICFILES_VERSIONED_EXCLUDE = ['min.css$', 'min.js$']
```

This would version all files asides from `.jpg` files:

```python
STATICFILES_VERSIONED_EXCLUDE = ['.jpg$']
```

This would only version things in your `/static/scripts`:

```python
STATICFILES_VERSIONED_INCLUDE = ['scripts/.*$']
```
1 change: 1 addition & 0 deletions django_flexible_manifest_staticfiles/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.0.0"
25 changes: 25 additions & 0 deletions django_flexible_manifest_staticfiles/storages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import re

from collections import OrderedDict

from django.conf import settings
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
from django.contrib.staticfiles.utils import matches_patterns
from django.core.files.base import ContentFile

class FlexibleManifestStaticFilesStorage(ManifestStaticFilesStorage):

whitelisted_patterns = getattr(settings, "STATICFILES_VERSIONED_INCLUDE", ['.*'])
blacklisted_patterns = getattr(settings, "STATICFILES_VERSIONED_EXCLUDE", [])

def _post_process(self, paths, adjustable_paths, hashed_files):

filtered_paths = OrderedDict()

for name, val in paths.items():
if any(re.search(wp, name) for wp in self.whitelisted_patterns):
if not any(re.search(wp, name) for wp in self.blacklisted_patterns):
filtered_paths[name] = val

return super()._post_process(filtered_paths, adjustable_paths, hashed_files)
20 changes: 20 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python

from os.path import exists
from setuptools import setup, find_packages

from django_flexible_manifest_staticfiles import __version__

setup(
name='django-flexible-manifest-staticfiles',
version=__version__,
author='Tom Anthony',
author_email='[email protected]',
packages=find_packages(),
scripts=[],
url='https://github.com/TomAnthony/django-flexible-manifest-staticfiles',
license='MIT',
description='An extension of Django ManifestStaticFilesStorage that allows ignoring (excluding) specified files from being versioned, such as images or fonts.',
long_description=open('README.md').read(),
install_requires=[],
)

0 comments on commit dc1cb78

Please sign in to comment.