Skip to content

Commit

Permalink
Creme staticfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
hsmett committed Aug 2, 2024
1 parent 26cc042 commit 3a5565e
Show file tree
Hide file tree
Showing 101 changed files with 1,646 additions and 19,033 deletions.
7 changes: 1 addition & 6 deletions .circleci/circleci_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@
'creme.mobile',
])

GENERATED_MEDIA_NAMES_FILE = BASE_DIR / '_generated_media_names.py'
# NB: "creme_project" same name in config.yml
GENERATED_MEDIA_NAMES_MODULE = 'creme_project._generated_media_names'

MEDIA_ROOT = BASE_DIR / 'media' / 'upload'
STATIC_ROOT = BASE_DIR / 'media' / 'static'

MEDIA_ROOT = BASE_DIR / 'media' / 'upload'
# JOBMANAGER_BROKER = 'redis://@localhost:6379/0'

GEOLOCATION_TOWNS = [
Expand Down
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ commands:
description: Setup Creme static resources
steps:
- run: creme generatemedia --settings=<< pipeline.parameters.instance_directory >>.settings
- run: creme collectstatic --settings=<< pipeline.parameters.instance_directory >>.settings --no-input

setup-creme-unit-tests:
description: Setup Creme database
Expand All @@ -109,6 +110,7 @@ commands:
- run: creme migrate --settings=<< pipeline.parameters.instance_directory >>.settings
- run: creme creme_populate --settings=<< pipeline.parameters.instance_directory >>.settings
- run: creme generatemedia --settings=<< pipeline.parameters.instance_directory >>.settings
- run: creme collectstatic --settings=<< pipeline.parameters.instance_directory >>.settings --no-input

run-creme-unit-tests:
description: Run Creme unit tests
Expand Down
5 changes: 2 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__pycache__/
*.py[cod]
**/__pycache__/
**/*.py[cod]

.git/
.githooks/
Expand All @@ -11,7 +11,6 @@ __pycache__/
# creme/media/upload/*/*
# creme/media/static/*
# creme/local_settings.py
# creme/_generated_media_names.py
# *.sqlite
# *.db

Expand Down
5 changes: 3 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/*.min.js
**/js/jquery/1.11/*
**/js/jquery/1.12/*
**/js/jquery/2.x/*
Expand All @@ -16,7 +17,7 @@
**/js/jquery/ui/*
**/js/lib/underscore/*
**/js/lib/momentjs/*
**/js/lib/leaflet*.js
**/js/lib/leaflet/*
**/js/lib/editor/*
**/js/lib/Sortable/*
**/js/lib/Select2/*
Expand All @@ -28,4 +29,4 @@
**/sketch/js/lib/filesaver-*.js
!**/sketch/js/lib/d3-radial-axis.js
**/tiny_mce/**
**/media/static/**
**/media/static/**
10 changes: 5 additions & 5 deletions .karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
return paths.map(function(path) {
if (isDirectory(path)) {
if (path[path.length - 1] !== '/') {
path = path + '/';
path = path + '/';
}

if (path.indexOf('/js/tests/') !== -1) {
Expand Down Expand Up @@ -72,9 +72,9 @@ module.exports = function(config) {

// TODO: use path from the config
var commonfiles = [
staticsPath + '/l10n--' + language + '-*.js',
staticsPath + '/lib*.js',
staticsPath + '/main*.js'
staticsPath + '/' + language + '.js',
staticsPath + '/lib.js',
staticsPath + '/main.js'
];

var qunitfiles = [
Expand All @@ -88,7 +88,7 @@ module.exports = function(config) {
THEME_NAME: 'icecream'
};
var coverageFiles = [
staticsPath + '/main*.js'
staticsPath + '/main.js'
]

var isEmpty = function(s) {
Expand Down
20 changes: 12 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ USER creme_user
COPY docker/docker_settings.py /srv/creme/docker_settings.py
ENV DJANGO_SETTINGS_MODULE docker_settings

COPY --chown=creme_user:creme_user . /srv/creme/src

RUN --mount=type=bind,source=.,target=/tmp/src \
--mount=type=cache,target=/srv/creme/.cache,uid=1001 \
set -eux; \
RUN set -eux; \
mkdir -p /srv/creme/logs; \
mkdir -p /srv/creme/data; \
cp -r /tmp/src /srv/creme/src; \
mkdir -p /srv/creme/data;

RUN --mount=type=cache,target=/srv/creme/.cache,uid=1001 \
set -eux; \
python3 -m venv /srv/creme/venv; \
/srv/creme/venv/bin/pip install --cache-dir=/srv/creme/.cache/pip --upgrade pip setuptools wheel; \
/srv/creme/venv/bin/pip install --cache-dir=/srv/creme/.cache/pip /srv/creme/src[mysql,pgsql]; \
/srv/creme/venv/bin/pip install --cache-dir=/srv/creme/.cache/pip --upgrade uWSGI supervisor; \
rm -rf /srv/creme/src; \
/srv/creme/venv/bin/creme generatemedia;
/srv/creme/venv/bin/pip install --cache-dir=/srv/creme/.cache/pip --upgrade uWSGI supervisor;

RUN set -eux; \
/srv/creme/venv/bin/creme generatemedia; \
/srv/creme/venv/bin/creme collectstatic; \
rm -rf /srv/creme/src;

ENV PATH /srv/creme:/srv/creme/venv/bin:$PATH

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ reset-db:
## Install or upgrade nodejs requirements
.PHONY: node-update
node-update:
pip install --upgrade nodeenv
nodeenv -n 18.15.0 --prebuilt -p
npm install --no-save
npm run eslint-install

Expand All @@ -66,6 +68,7 @@ ifeq (${DJANGO_SETTINGS_MODULE},)
endif
$(eval CREME_MEDIA ?= $(shell creme print_settings --settings=${DJANGO_SETTINGS_MODULE} --skip-checks --no-color STATIC_ROOT --format value))
creme generatemedia --settings=${DJANGO_SETTINGS_MODULE}
creme collectstatic --noinput


## Run the Django test suite
Expand Down
6 changes: 3 additions & 3 deletions creme/billing/templates/billing/bricks/exporters.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'creme_core/bricks/base/table.html' %}
{% load i18n media creme_bricks %}
{% load i18n static creme_bricks %}
{% load url from creme_core_tags %}

{% block brick_extra_class %}{{block.super}} billing-exporters-brick{% endblock %}
Expand All @@ -24,8 +24,8 @@
<span class="billing-exporter-name">{{exporter.verbose_name}}</span>
<div class="billing-exporter-screenshots">
{% for img_src in exporter.screenshots %}
<a onclick="creme.dialogs.image('{% media_url img_src %}').open();">
<img style="max-width:200px;max-height:200px;" src="{% media_url img_src %}"/>
<a onclick="creme.dialogs.image('{% static img_src %}').open();">
<img style="max-width:200px;max-height:200px;" src="{% static img_src %}"/>
</a>
{% endfor %}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{% load media %}
{% load static %}
{% if widget.wrap_label %}<label{% if widget.attrs.id %} for="{{widget.attrs.id}}"{% endif %}>{% endif %}
{% include 'django/forms/widgets/input.html' %}
{% if widget.wrap_label %}
<div class="billing-exporter-choice">
<div class="billing-exporter-choice-images">
{% for img_src in widget.value.images %}
<a onclick="creme.dialogs.image('{% media_url img_src %}').open();">
<img style="max-width:{{widget.thumbnail_width}};max-height:{{widget.thumbnail_height}};" src="{% media_url img_src %}"/>
<a onclick="creme.dialogs.image('{% static img_src %}').open();">
<img style="max-width:{{widget.thumbnail_width}};max-height:{{widget.thumbnail_height}};" src="{% static img_src %}"/>
</a>
{% endfor %}
</div>
Expand Down
50 changes: 0 additions & 50 deletions creme/creme_core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,56 +93,6 @@ def __get_extending_app_configs(self):

# Hooking of AppConfig [end] ------------

# TODO: remove when MediaGenerator is not used
class MediaGeneratorConfig(AppConfig):
name = 'mediagenerator'
verbose_name = 'Media generator' # _('Media generator')

def ready(self):
self._build_MEDIA_BUNDLES()

def _build_MEDIA_BUNDLES(self):
is_installed = apps.is_installed

MEDIA_BUNDLES = [
settings.CREME_I18N_JS,
[
*settings.CREME_LIB_JS,
*(js for app, js in settings.CREME_OPTLIB_JS if is_installed(app)),
],
[
*settings.CREME_CORE_JS,
*(js for app, js in settings.CREME_OPT_JS if is_installed(app)),
],
]

if settings.FORCE_JS_TESTVIEW:
MEDIA_BUNDLES.append(settings.TEST_CREME_LIB_JS)
MEDIA_BUNDLES.append([
*settings.TEST_CREME_CORE_JS,
*(js for app, js in settings.TEST_CREME_OPT_JS if is_installed(app)),
])

MEDIA_BUNDLES += settings.CREME_OPT_MEDIA_BUNDLES

CREME_CSS = [
*settings.CREME_CORE_CSS,
*(css for app, css in settings.CREME_OPT_CSS if is_installed(app)),
]
MEDIA_BUNDLES.extend(
[
theme_dir + CREME_CSS[0],
*(
css_file if isinstance(css_file, dict) else f'{theme_dir}/{css_file}'
for css_file in CREME_CSS[1:]
),
] for theme_dir, theme_vb_name in settings.THEMES
)

settings.CREME_CSS = CREME_CSS # For compatibility (should not be useful)
settings.MEDIA_BUNDLES = MEDIA_BUNDLES


class ContentTypesConfig(VanillaContentTypesConfig):
def ready(self):
super().ready()
Expand Down
1 change: 0 additions & 1 deletion creme/creme_core/conf/project_template/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ syntax: glob
*/media/static/*

*/local_settings.py
*/_generated_media_names.py
*.sqlite3

# Python package
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ TIME_ZONE = 'Europe/London'
# For example:
# INSTALLED_APPS.remove('creme.tickets')

# Path to the python file generated by the command 'generatemedia' which
# contains the names of the final assets (with MD5 hashing).
# The folder containing the generated file must be WRITABLE.
# You can leave it commented if you have ONLY ONE creme project in the
# current virtualenv (in this case the path of the file will look like
# '/path_to_virtualenv/bin/_generated_media_names.py', which is generally OK).
GENERATED_MEDIA_NAMES_FILE = BASE_DIR / '_generated_media_names.py'

# Python module corresponding to GENERATED_MEDIA_NAMES_FILE
GENERATED_MEDIA_NAMES_MODULE = '{{ project_name }}._generated_media_names'

# Absolute filesystem path to the directory that will hold user-uploaded files,
# and files that are generated dynamically (CSV, PDF...).
# Must be writable of course.
Expand Down
1 change: 0 additions & 1 deletion creme/creme_core/conf/project_template/setup.cfg-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ use_parentheses = True
[flake8]
exclude =
*/migrations/*
_generated_media_names.py
max-line-length = 99
ignore =
# Multiple spaces before operator
Expand Down
11 changes: 6 additions & 5 deletions creme/creme_core/gui/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from django.db.models import Model
from django.utils.html import format_html

from ..staticfiles import get_staticfile_url
from ..utils.media import get_creme_media_url

if TYPE_CHECKING:
Expand Down Expand Up @@ -268,22 +269,22 @@ def get_icon_by_name(name: str,

def _get_image_url():
try:
return get_creme_media_url(theme, f'images/{name}_{size_px}.png')
return get_staticfile_url(f'{theme}/images/{name}_{size_px}.png', verify=True)
except KeyError:
pass

try:
return get_creme_media_url(theme, f'images/{name}.png')
return get_staticfile_url(f'{theme}/images/{name}.png', verify=True)
except KeyError:
pass

try:
return get_creme_media_url(theme, f'images/{name}_{size_px}.gif')
return get_staticfile_url(f'{theme}/images/{name}_{size_px}.gif', verify=True)
except KeyError:
pass

try:
return get_creme_media_url(theme, f'images/{name}.gif')
return get_staticfile_url(f'{theme}/images/{name}.gif', verify=True)
except KeyError:
pass

Expand Down Expand Up @@ -338,7 +339,7 @@ def get_4_model(self,
path = path_fmt % {'size': size_px}

try:
url = get_creme_media_url(theme, path)
url = get_staticfile_url(f"{theme}/{path}", verify=True)
except KeyError:
logger.warning('Missing image: %s', path)

Expand Down
44 changes: 44 additions & 0 deletions creme/creme_core/management/commands/generatemedia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os

from django.apps import apps
from django.conf import settings
from django.contrib.staticfiles import finders
from django.core.management.base import BaseCommand

from creme.creme_core.staticfiles import (
I18NCatalog,
JavaScriptBundle,
StyleSheetBundle,
)


class Command(BaseCommand):
help = 'Generate media files'

BUNDLERS = {
"javascript": JavaScriptBundle,
"stylesheet": StyleSheetBundle,
}

def create_destination(self):
os.makedirs(settings.CREME_STATICFILES_TMP_DIRECTORY, exist_ok=True)

def bundle_i18n(self):
for language_code, _ in settings.LANGUAGES:
catalog = I18NCatalog(language_code)
catalog.save(destination=settings.CREME_STATICFILES_TMP_DIRECTORY)

def bundle_staticfiles(self):
for bundle in settings.CREME_STATICFILES_BUNDLES:
bundler_class = self.BUNDLERS[bundle["type"]]
bundler = bundler_class(filename=bundle["filename"])
for app_name, filepaths in bundle["files"].items():
if apps.is_installed(app_name):
for filepath in filepaths:
bundler.add(filepath, finders.find(filepath))
bundler.save(destination=settings.CREME_STATICFILES_TMP_DIRECTORY)

def handle(self, **options):
self.create_destination()
self.bundle_staticfiles()
self.bundle_i18n()
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ ul.ui-creme-widget.is-readonly li {
vertical-align: middle;
}

.ui-creme-overlay.overlay-active[status="wait"] { background-image: url("chantilly/images/wait.gif"); }
.ui-creme-overlay.overlay-active[status="wait"] { background-image: url("../../images/wait.gif"); }

/*----------------------------------*/
.ui-dialog-scrollbackground {
Expand Down
Loading

0 comments on commit 3a5565e

Please sign in to comment.