I'm trying to direct wagtail to use a template in a centralised location at the top of the project tree. So for example:
Project
|_ HomePage
|_ Search
|_ Mypage
|_ templates
My project re-uses templates and I'd like to give UX/UI Developers access to a single folder rather than multiple sub-folders in multiple pages.
I've tried in Mypage/models.py
class AdvisorSummaryPages(Page):
intro = models.CharField(max_length=250, blank=True, null=True)
content_panels = Page.content_panels + [
FieldPanel('intro'),
InlinePanel('carousell', heading="Carousell Images")
]
template = "advisors.html"
With the following template setting in Project/Project/settings/base.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
With no luck. I can't seem to find any solution on SO or through the documentation or Google that might work. There is a solution presented here using separate model admins but that doesn't work for me. How might I specify the location of the template differently to a subdirectory of templates in the MyPage App?
Thanks
Below is how I organize templates and static assets. I have a themes folder that is located in the main project folder with named theme subfolders within the themes folder. I then have the following in settings/base.py:
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
THEMES_URL = '/themes/'
THEME_PATH = os.path.join(BASE_DIR, THEMES_URL.replace('/', ''), 'name_of_theme')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
THEME_PATH,
...other directories...,
],
... other TEMPLATES-RELATED CODE
}
]
STATICFILES_DIRS = [
os.path.join(THEME_PATH, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
The name_of_theme folder in the THEME_PATH definition contains all of the templates and static files for the theme. In order for the static files to be collected correctly, the folder structure for the css, js, etc. files within each theme folder needs to be:
/themes/name_of_theme/static/name_of_theme/js (or css, etc.)/filename.js (or filename.css, etc.)
The /name_of_theme/static/name_of_theme/ namespacing is necessary for collectstatic to be able to collect the files correctly (see Staticfile namespacing here for more info). When including a reference to a static file in a template, you then do:
{% static 'name_of_theme/js/filename.js' %}
The STATICFILES_DIRS definition is only set up for one theme. You would need to change or add to that if you're using more than one theme.
Some time ago I also came across this Wagtail package: Wagtail Themes. It looks very interesting, but I'm not sure if it provides for handling static files organized within named theme folders as I describe above.
Related
I'm working on a #nrwl/nx monorepo. I want to import the folders inside the project src by the absolute paths. I tried specifying the baseUrl but didn't work. The only solution worked is, adding the path to the monorepo root tsConfig.json file as follows.
"paths": {
"*": ["apps/my-app/src/*"]
}
But, the problem is, if I have another project, I will have to add that project as well to this path. I tried something as follows.
"paths": {
"*": ["apps/*/src/*"]
}
But, this doesn't work anymore. It doesn't match the project folder name.
How can I solve this? Or, is there any better way to import by absolute paths?
I'm facing the same problem, due to organizing common DTOs and Event.ts files in the nx monorepo. I found useful to update the tsconfig.base.json with a simpler path shortcut, that allow cross app imports and at the same time mantains the options of setting an absolute path in the single apps tsconfig.json file.
Here's my base.json:
"baseUrl": ".",
"paths": {
"libs": [
"libs/"
],
"app1: [
"apps/app1/"
],
"app2": [
"apps/app2/"
],
}
Now I have a sort of absolute imports that point to app names as base:
import {CreateUserEvent} from 'libs/events/create-user.event';
This is a random file in the app1/src/app/ folder that import a file in libs folder
Folder structure is:
root ('.')
|__ app1/src/app/file_with_import.ts
|__ ...
|__ ...
|__ libs/events/create_user.event.ts
Hope it helps
Im wondering if there is a way to completely flatten the folder structure within the build directory of a create-react-app. Im attempting to do a quick port over of my app onto a sharepoint site but the sharepoint site does not play well with the static/ folder and its children folders such as css/, js/, and media/ is there a way to do a npm run build such that the paths created are all within the same folder?
for example the asset-manifest.json currently shows files like this
{
"files": {
"main.css": "./static/css/main.ad49c970.chunk.css",
"main.js": "./static/js/main.bfd3a96b.chunk.js",
"main.js.map": "./static/js/main.bfd3a96b.chunk.js.map",
"runtime-main.js": "./static/js/runtime-main.c0915b68.js",
"runtime-main.js.map": "./static/js/runtime-main.c0915b68.js.map",
"static/css/2.d9ad5f5c.chunk.css": "./static/css/2.d9ad5f5c.chunk.css",
"static/js/2.3c65d00b.chunk.js": "./static/js/2.3c65d00b.chunk.js",
"static/js/2.3c65d00b.chunk.js.map": "./static/js/2.3c65d00b.chunk.js.map",
"index.html": "./index.html",
"precache-manifest.8fc2b5edb6f6029051530a49398ae5c2.js": "./precache-manifest.8fc2b5edb6f6029051530a49398ae5c2.js",
"service-worker.js": "./service-worker.js",
"static/css/2.d9ad5f5c.chunk.css.map": "./static/css/2.d9ad5f5c.chunk.css.map",
"static/css/main.ad49c970.chunk.css.map": "./static/css/main.ad49c970.chunk.css.map",
"static/js/2.3c65d00b.chunk.js.LICENSE.txt": "./static/js/2.3c65d00b.chunk.js.LICENSE.txt",
"static/media/logo.svg": "./static/media/logo.5d5d9eef.svg"
},
"entrypoints": [
"static/js/runtime-main.c0915b68.js",
"static/css/2.d9ad5f5c.chunk.css",
"static/js/2.3c65d00b.chunk.js",
"static/css/main.ad49c970.chunk.css",
"static/js/main.bfd3a96b.chunk.js"
]
}
Ideally I just want everything under the root/ folder without static/js/ etc.
Question: How do I load a configuration file from a Plugin/config Directory?
Demo Project: https://github.com/CakePHPKitchen/CakeDC-Users-Permissions-Example
I am using CakeDC/users plugin and it has a permissions.php file that it loads the RBAC permissions from. From what I can tell, it either loads the default permissions file that is in the user plugin's config folder OR it loads the permissions.php file from the app/config folder.
Now for my app skeleton I have a bunch of permissions in the app/config/permissions.php, however, I do not want to modify that file as I will be doing git pulls from the upstream repo and I would like to avoid conflicts.
So what I would like to do is, in the app skeleton bootstrap
I would like to
foreach(Plugin::loaded() as $plugin) {
$path = Plugin::path($plugin) . 'config/permissions.php';
if(file_exists($path)) {
Configure::load($path, 'default', true);
}
}
But I am getting the following error....
Error: The application is trying to load a file from the /Users/jlroberts/Projects/JeffreyLRobertsCom/CakePHPKitchen/PluginDemos/plugins/SharpAgent/config/permissions plugin.
Make sure your plugin /Users/jlroberts/Projects/JeffreyLRobertsCom/CakePHPKitchen/PluginDemos/plugins/SharpAgent/config/permissions is in the /Users/jlroberts/Projects/JeffreyLRobertsCom/CakePHPKitchen/PluginDemos/plugins/ directory and was loaded.
Any ideas on how I can load the permissions.php file from the Plugin/config directory?
EDITED: You can load permissions.php file from the Plugin as it is doing now, but change the contents of permissions.php to preserve existing permissions defined in configuration, for example:
config/permissions.php
$permissions = [
// add your app permissions here
[
// ...
],
];
// there are more permissions in this config key, defined across your plugins
$morePermissions = \Cake\Core\Configure::read('MyPermissions');
$allPerms = array_merge($permissions, $morePermissions);
return ['CakeDC/Auth.permissions' => $allPerms];
Then inside each plugin you could have:
YOUR_PLUGIN/config/bootstrap.php
$permissions = \Cake\Core\Configure::read('MyPermissions');
$someMorePermissions = [
[
// permissions injected into the app from this plugin
]
];
$permissions = array_merge((array)$permissions, $someMorePermissions);
\Cake\Core\Configure::write('MyPermissions', $permissions);
Allowing each plugin to dynamically inject/manage permissions into the app.
I've created a c9.io environment with this code here https://ide.c9.io/steinkel/users-35-custom-permissions
Using Flask, I can render different templates based on the url. For example :
http://example.com/site1/ ; will render templates/site1/{htmls}
http://example.com/site2/ ; will render templates/site2/{htmls}
This works really great with jinja_loader and a custom Loader, but now I'm stuck with the static files.
The static files depends on the template, so they are located in templates/static/site{0-9}, but of course, I cannot set a jinja_loader on the static_folder parameter because it's not related to Jinja but to Flask.
How can I render the correct static folder based on the current URL?
As an example, here's the loaded code:
Flask(app_name,
static_url_path = '/public',
static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates/static')
)
and in templates/static, I have :
static/
site1/
css/
js/
images/
site2/
css/
js/
images/
etc...
You either have to use explicit paths in your static views:
url_for('static', filename='site1/css/...')
where site1 could be taken from request.path.split('/', 1)[0]:
url_for('static', filename='{}/css/...'.format(request.path.split('/', 1)[0]))
You could create a custom static view:
from flask import request, send_from_directory, current_app, safe_join
import os.path
#app.route('/<site>/static/<path:filename>')
def per_site_static(site, filename):
if site is None:
# pick site from the URL; only works if there is a `/site/` first element.
site = request.path.split('/')[0]
static_folder = safe_join(current_app.static_folder, site)
cache_timeout = current_app.get_send_file_max_age(filename)
return send_from_directory(static_folder, filename,
cache_timeout=cache_timeout)
Then use url_for() to generate urls:
{{ url_for('per_site_static', site=None, filename='css/...') }}
An other interesting alternative is the following :
class GenericStaticFlask(Flask):
def send_static_file(self, filename):
# g.site contains the name of the template path for siteX
return super(GenericStaticFlask, self).send_static_file("%s/%s" % (g.site, filename))
app = GenericStaticFlask(app_name,
static_url_path = '/public',
static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates/static')
)
In the app.yaml file, I have put 2 lines to specify the url mapping:
url: /blog/.*
script: blog.app
url: /
script: home.app
the problem is I can't use the "uri_for" function to generate a url for blog module in home.py, case there's no Route added in home moudle:
here is the code in home module:
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler=HomeHandler, name='home')
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
and code in blog.py:
app = webapp2.WSGIApplication([
webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler, name="blog")
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
so, if I have code like this: {{ uri_for('blog', blog_id=blabla) }} in home.html, it can't work.
You should consolidate those routes into one app.
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler=HomeHandler, name='home'),
webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler, name="blog")
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
and actually those are only the view blog post routes.
If you wanted to do a full CRUD app, you might need to add some more.
app = webapp2.WSGIApplication([
webapp2.Route(r'/admin/blog', handler='admin.AdminBlogHandler:list, name="admin.blog.list"),
webapp2.Route(r'/admin/blog/new', handler='admin.AdminBlogHandler:new', name='admin.blog.edit'),
webapp2.Route(r'/admin/blog/<id:[^/]+>/edit', handler='admin.AdminBlogHandler:edit', name='admin.blog.edit'),
webapp2.Route(r'/admin/blog/<id:[^/]+>', handler='admin.AdminBlogHandler:view', name='admin.blog.view')
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
Note for these examples:
1) you prefix a name to load the handlers from a different file (admin.AdminBlogHandler will look in 'admin.py' for 'class AdminBlogHandler'
2) you specify the method to run after the handler name, after the colon.
3) in each method I am creating functionality for get and post, so there are not discrete RESTful URLs for edit and update.