Skip to content

Commit

Permalink
Add config options/remove comments
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed May 4, 2019
1 parent 5963647 commit 96ee222
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 46 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Expand Up @@ -31,9 +31,6 @@ fl_notes.txt
# custom style
ckan/public/base/less/custom.less

# webassets
ckan/public/webassets

# nosetest coverage output
.coverage
htmlcov/*
Expand Down
6 changes: 6 additions & 0 deletions ckan/config/deployment.ini_tmpl
Expand Up @@ -160,6 +160,12 @@ ckan.feeds.author_link =
#ckan.max_resource_size = 10
#ckan.max_image_size = 2

## Webassets Settings
#ckan.webassets.use_x_sendfile = false
#ckan.webassets.external = false
#ckan.webassets.path = /var/lib/ckan/webassets


## Datapusher settings

# Make sure you have set up the DataStore
Expand Down
26 changes: 16 additions & 10 deletions ckan/config/middleware/flask_app.py
Expand Up @@ -31,7 +31,8 @@
from ckan.common import config, g, request, ungettext
import ckan.lib.app_globals as app_globals
import ckan.lib.plugins as lib_plugins

import ckan.plugins.toolkit as toolkit
from ckan.lib.webassets_tools import get_webassets_path

from ckan.plugins import PluginImplementations
from ckan.plugins.interfaces import IBlueprint, IMiddleware, ITranslation
Expand All @@ -41,7 +42,6 @@
set_controller_and_action
)

import ckan.lib.plugins as lib_plugins
import logging
from logging.handlers import SMTPHandler
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -185,14 +185,7 @@ def hello_world_post():
return 'Hello World, this was posted to Flask'

# WebAssets
public_folder = config.get('ckan.base_public_folder')
webassets_folder = os.path.join(
os.path.dirname(ckan.__file__), public_folder, 'webassets'
)

@app.route('/webassets/<path:path>')
def webassets(path):
return send_from_directory(webassets_folder, path)
_setup_webassets(app)

# Auto-register all blueprints defined in the `views` folder
_register_core_blueprints(app)
Expand Down Expand Up @@ -504,3 +497,16 @@ def filter(self, log_record):
context_provider = ContextualFilter()
app.logger.addFilter(context_provider)
app.logger.addHandler(mail_handler)


def _setup_webassets(app):
app.use_x_sendfile = toolkit.asbool(
config.get('ckan.webassets.use_x_sendfile')
)

if not toolkit.asbool(config.get('ckan.webassets.external')):
webassets_folder = get_webassets_path()

@app.route('/webassets/<path:path>')
def webassets(path):
return send_from_directory(webassets_folder, path)
4 changes: 0 additions & 4 deletions ckan/lib/jinja_extensions.py
Expand Up @@ -324,10 +324,6 @@ class ResourceExtension(BaseExtension):

@classmethod
def _call(cls, args, kwargs):
log.warn(
'`resource` tag is deprecated. '
'Use `assets`'
'<https://docs.ckan.org/en/latest/theming/webassets.html> instead')
assert len(args) == 1
assert len(kwargs) == 0
h.include_resource(args[0], **kwargs)
Expand Down
26 changes: 24 additions & 2 deletions ckan/lib/webassets_tools.py
Expand Up @@ -2,14 +2,15 @@

import logging
import os
import tempfile

from markupsafe import Markup

from webassets import Environment
from webassets.loaders import YAMLLoader

from ckan.common import config, g


logger = logging.getLogger(__name__)
env = None

Expand All @@ -32,6 +33,7 @@ def create_library(name, path):
# with the same name twice. For now, let's just pop existing
# bundle and avoid name-conflicts
# TODO: make PR into webassets with preferable solution
# Issue: https://github.com/miracle2k/webassets/issues/519
for name, bundle in bundles.items():
env._named_bundles.pop(name, None)
env.register(name, bundle)
Expand All @@ -42,13 +44,14 @@ def create_library(name, path):
def webassets_init():
global env

static_path = get_webassets_path()

public = config.get(u'ckan.base_public_folder')

public_folder = os.path.abspath(os.path.join(
os.path.dirname(__file__), u'..', public))

base_path = os.path.join(public_folder, u'base')
static_path = os.path.join(public_folder, u'webassets')

env = Environment()
env.directory = static_path
Expand Down Expand Up @@ -137,3 +140,22 @@ def render_assets(type_):
tags = u'\n'.join([_to_tag(asset, type_) for asset in assets[type_]])
collection[:] = []
return Markup(tags)


def get_webassets_path():
webassets_path = config.get('ckan.webassets.path')

if not webassets_path:
storage_path = config.get(
'ckan.storage_path'
) or tempfile.gettempdir()

if storage_path:
webassets_path = os.path.join(storage_path, 'webassets')

if not webassets_path:
raise RuntimeError(
'Either `ckan.webassets.path` or `ckan.storage_path` '
'must be specified'
)
return webassets_path
1 change: 0 additions & 1 deletion ckan/templates/package/new_resource.html
Expand Up @@ -20,6 +20,5 @@

{% block scripts %}
{{ super() }}
{#% resource 'vendor/fileupload' %#}
{% asset 'vendor/fileupload' %}
{% endblock %}
1 change: 0 additions & 1 deletion ckan/templates/package/resource_edit_base.html
Expand Up @@ -37,6 +37,5 @@ <h1 class="hide-heading">{% block form_title %}{{ _('Edit resource') }}{% endblo

{% block scripts %}
{{ super() }}
{#% resource 'vendor/fileupload' %#}
{% asset 'vendor/fileupload' %}
{% endblock %}
1 change: 0 additions & 1 deletion ckan/templates/package/resource_views.html
Expand Up @@ -17,7 +17,6 @@
{% endfor %}
</ul>
</div>
{#% resource 'vendor/reorder' %#}
{% asset 'vendor/reorder' %}
{% endblock %}

Expand Down
1 change: 0 additions & 1 deletion ckan/templates/package/resources.html
Expand Up @@ -26,7 +26,6 @@
{% block scripts %}
{{ super() }}
{% if has_reorder %}
{#% resource 'vendor/reorder' %#}
{% asset 'vendor/reorder' %}
{% endif %}
{% endblock %}
5 changes: 0 additions & 5 deletions ckan/templates/page.html
Expand Up @@ -128,14 +128,9 @@ <h1>My page content</h1>
{%- endblock -%}

{%- block scripts %}
{#% resource 'base/main' %#}
{% asset 'base/main' %}

{#% resource 'base/ckan' %#}
{% asset 'base/ckan' %}

{% if g.tracking_enabled %}
{#% resource 'base/tracking.js' %#}
{% asset 'base/tracking' %}
{% endif %}
{{ super() }}
Expand Down
Expand Up @@ -53,7 +53,6 @@
<input type="hidden" name="filters" value="{{ request.args.get('filters', '')|e }}" />
</form>

{#% resource 'ckanext-datatablesview/main' %#}
{% asset 'ckanext-datatablesview/main' %}
{% endblock %}

Expand Down
11 changes: 5 additions & 6 deletions ckanext/example_theme_docs/v15_fanstatic/templates/base.html
Expand Up @@ -3,12 +3,11 @@
{% block styles %}
{{ super() }}

{# Import example_theme.css using Fanstatic.
'example_theme/' is the name that the example_theme/fanstatic directory
was registered with when the toolkit.add_resource() function was called.
'example_theme.css' is the path to the CSS file, relative to the root of
the fanstatic directory. #}
{#% resource 'example_theme/example_theme.css' %#}
{# Import example_theme.css using Webassets. 'example_theme/' is
the name that the example_theme/fanstatic directory was registered
with when the toolkit.add_resource() function was called.
'example_theme' is the name to the Webassets bundle file,
registered inside webassets.yml file. #}
{% asset 'example_theme/example_theme' %}

{% endblock %}
Expand Up @@ -3,10 +3,9 @@
{% block content %}
{{ super() }}

{# Use Fanstatic to include our custom JavaScript module.
{# Use Webassets to include our custom JavaScript module.
A <script> tag for the module will be inserted in the right place at the
bottom of the page. #}
{#% resource 'example_theme/example_theme_popover.js' %#}
{% asset 'example_theme/example_theme' %}

{# Apply our JavaScript module to an HTML element. The data-module attribute,
Expand Down
Expand Up @@ -2,7 +2,6 @@

{% block content %}
{{ super() }}
{#% resource 'example_theme/example_theme_popover.js' %#}
{% asset 'example_theme/example_theme' %}

{# Apply our JavaScript module to an HTML <button> element.
Expand Down
Expand Up @@ -3,8 +3,6 @@
{% block content %}
{{ super() }}

{#% resource 'example_theme/example_theme_popover.js' %#}
{#% resource 'example_theme/example_theme_popover.css' %#}
{% asset 'example_theme/example_theme' %}

<button data-module="example_theme_popover"
Expand Down
Expand Up @@ -3,9 +3,6 @@
{% block content %}
{{ super() }}

{#% resource 'example_theme/example_theme_popover.js' %#}
{#% resource 'example_theme/example_theme_popover.css' %#}
{#% resource 'example_theme/jquery.greenify.js' %#}
{% asset 'example_theme/example_theme' %}


Expand Down
1 change: 0 additions & 1 deletion ckanext/reclineview/theme/templates/recline_view.html
Expand Up @@ -15,7 +15,6 @@ <h4 class="loading-dialog">
</h4>
</div>

{#% resource 'ckanext-reclineview/main' %#}
{% asset 'ckanext-reclineview/main' %}
{% endblock %}

Expand Down
1 change: 0 additions & 1 deletion ckanext/stats/templates/ckanext/stats/index.html
Expand Up @@ -189,6 +189,5 @@ <h2 class="module-heading"><i class="fa fa-bar-chart-o fa-medium"></i> {{ _('Sta

{% block scripts %}
{{ super() }}
{#% resource "ckanext_stats/stats" %#}
{% asset "ckanext_stats/stats" %}
{% endblock %}
1 change: 0 additions & 1 deletion ckanext/textview/theme/templates/text_view.html
Expand Up @@ -20,6 +20,5 @@
{% endblock %}


{#% resource 'ckanext-textview/main' %#}
{% asset 'ckanext-textview/main' %}
{% endblock %}

0 comments on commit 96ee222

Please sign in to comment.