Matching the root URL not working in Pyramid - pyramid

According to the official documentation,
setting
config.add_route('root', '')
or
config.add_route('root', '/')
is supposed to take me to the root URL. I set this in my __init__.py.
I want my root to be at www.hello.com/language.
I have
config.include('.view', route_prefix='/language')
in my project's __init__.py
I also have a view configured
#view_config(route_name='root', renderer='template.jinja2')
in views.py
But, when I go to www.hello.com/language, I get a 404 not found.
What could be the issue?

It will be available at http://www.hello.com/language/. URL + trailing slash.
Perhaps you should look this issue https://github.com/Pylons/pyramid/issues/406
And for more understanding you can control your routes with command:
$ proutes development.ini
Name Pattern View Method
---- ------- ---- ------
__admin/sacrud_deform_static/ /admin/sacrud_deform_static/*subpath sacrud_deform:static/ *
__admin/deform_static/ /admin/deform_static/*subpath deform:static/ *
__admin/sa_static/ /admin/sa_static/*subpath pyramid_sacrud:static/ *
pyramid_sacrud_home /admin/ pyramid_sacrud.views.sa_home *
pyramid_sacrud_list /admin/{table}/ pyramid_sacrud.views.CRUD.List GET
pyramid_sacrud_create /admin/{table}/create/ pyramid_sacrud.views.CRUD.Add GET,POST
pyramid_sacrud_update /admin/{table}/update/*pk pyramid_sacrud.views.CRUD.Add GET,POST
pyramid_sacrud_delete /admin/{table}/delete/*pk pyramid_sacrud.views.CRUD.Delete GET
pyramid_sacrud_mass_action /admin/{table}/action/ pyramid_sacrud.views.CRUD.Action POST

Related

Django allauth urls produce 404

So I'm trying to add all auth to existing DjangoCMS project and ran into this kinda weird issue. I added urls like this:
urlpatterns += i18n_patterns(
url(r'^admin/', admin.site.urls), # NOQA
url(r'^', include('cms.urls')),
path('account/', include('allauth.urls')), # <-- here )
But when I go to any url, say mydomain.com/en/account/login/ I get 404. In debug mode Django lists all sorts of patterns it tried to match, and among them - en/ account/ login/ [name='account_login'], concluding with
"The current path, /en/account/login/, didn't match any of these."
Moreover if I add {% url 'account_login' %} to a template, it works just fine, producing a link to mydomain.com/en/account/login/ which is still a 404 error. I have no idea where to even start debugging this :( Any suggestions?
Edit:
the contents of my allauth urls.py https://pastebin.com/PZDH6EDm
Django version 3.0.7
Replace the url with path:
urlpatterns += i18n_patterns[
path(r'^admin/', admin.site.urls), # NOQA
path(r'^', include('cms.urls')),
path('account/', include('allauth.urls')), # <-- here
]

308 redirect when using POST to upload a file to Flask application

I am building a small server application in Flask as part of a project I am working on. Part of the functionality is the ability to upload a small file of instructions for one of the key injectors to download. Each key injector has a name (''') which corresponds to a client and the file is uploaded to the server via a POST request.
The following listing is the Flask code.
#app.route('/upload/instructions/<ducky_name>/', methods = ['POST'])
def upload_Instruction(ducky_name):
file = request.files()
path = os.getcwd() +"/files/" + ducky_name
with open(path, "w") as f:
f.write(file)
print(f)
f.close()
return "Success"
And I am using this curl command to upload the file.
curl -X POST -d #test http://127.0.0.1:5000/upload/instructions/test1
I then get a 308 redirect, and the file is not uploaded. This is the first time I've dealt with uploading files as a POST in this way, and the also the first time I've used Flask.
Thanks
The URL you use in the curl request does not have the trailing slash as in your Flask route. In this case the framework redirects you to the route with slash (see the documentation entry). So just add the trailing slash:
curl -X POST -d #test http://127.0.0.1:5000/upload/instructions/test1/
Flask uses 308 HTTP response code instead of more common 301 to preserve the request method and body during redirect.
Flask use werkzeug.routing.Rule, which enable strict_slashes as default, visiting a branch URL without a trailing slash will redirect to the URL with a slash appended. Which cause response with 308 (Permanent Redirect).
If you want to support both routes:
/upload/instructions/<ducky_name>/
/upload/instructions/<ducky_name>
Just set app.route with strict_slashes=False, like this:
#app.route('/upload/instructions/<ducky_name>/', methods = ['POST'],
strict_slashes=False)
def upload_Instruction(ducky_name):
pass
refer: https://werkzeug.palletsprojects.com/en/1.0.x/routing/

Puppet: Error 400 Could not find any files

I double checked all settings but did not find the issue and that's why I try to get help here.
Let me show the configuration:
puppet.conf:
[...]
[master]
environmentpath = $confdir/environments/
hiera_config = $confdir/environments/production/sites/xy/config/hiera.yaml
default_manifest = ./sites/xy/config/
environment_timeout = 0
fileserver.conf:
[...]
[sites]
path /etc/puppet/environments/production/sites/
allow *
auth.conf:
[...]
# extra mountpoint
path /sites
allow *
[...]
Now whenever I run Puppet and it tries to implement a specific file I get this:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find any files from puppet:///sites/xy/files/xy/xy.key.pub at /etc/puppet/environments/production/modules/xy/manifests/xy.pp:88 on node xy
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
Note that I had to replace sensitive information by xy but for debugging purposes I try to give every detail where possible.
So /sites points to /etc/puppet/environments/production/sites/ according to fileserver.conf and the directory exists like this (with correct permissions imho):
/etc/puppet % ls -ld /etc/puppet/environments/production/sites/
drwxr-xr-x 8 root puppet 4096 Oct 7 12:46 /etc/puppet/environments/production/sites/
The mentioned file puppet:///sites/xy/files/xy/xy.key.pub should therefore be located in /etc/puppet/environments/production/sites/xy/files/xy/xy.key.pub which looks like this:
/etc/puppet % ls -l /etc/puppet/environments/production/sites/*/files/*/*.key.pub
-rw-r--r-- 1 root puppet 725 Oct 7 12:46 /etc/puppet/environments/production/sites/xy/files/xy/xy.key.pub
And the in the error message mentioned line 88 of the module which loads the file it looks like this:
$sshpubkey = file("puppet:///${sitefiles}/xy/${s0_pubkey}")
where $s0_pubkey is xy.key.pub and ${sitefiles} is sites/$site/files which leads to the evaluated path of the requested file like this: puppet:///sites/xy/files/xy/xy.key.pub
The function file() in Puppet can not handle Puppet mountpoints with puppet://. The official docs at https://docs.puppet.com/puppet/latest/reference/function.html#file don't mention it specifically but obviously without mentioning it it means it can't handle extra mountpoints and wants to load files from a modules files directory.
My solution: I will use a variable which declares the absolute path to my "sites files" like this: $sitefiles_absolute = "/etc/puppet/environments/${environment}/sites/xy/files/" which will never change or at least not very often. And with keeping it in the site.pp file it can be used on every node and module.

nanoc does not find my PHP files on autocompile

I'm using nanoc to build a static website which has a PHP script for sending mails. Simplified structure:
/content/index.html
/content/contact.html
/content/mail.php
However, when I do a nanoc compile everything is fine in my output folder:
index.html
contact/index.html
mail/index.php
But I can't call the PHP script when doing a nanoc autocompile. /contact/ works, but /mail/ does not.
This is a part of my Rules-file:
route '*' do
if item.binary?
# Write item with identifier /foo/ to /foo.ext
original_filename(item)
else
# Write item with identifier /foo/ to /foo/index.extension
item.identifier + "index.#{item[:extension]}"
end
end
PHP is treated as non-binary. Does anyone know how I can get this to work with autocompile?
If anybody is having similar problems: I found the answer:
The autocompiler does not have support for PHP files. If a directory
is requested, the autocompiler looks for an index.html file in it, but
it will ignore an index.php file. The autocompiler can’t find a MIME
type for it, so it sends the file as application/octet-stream back to
the browser.
It's pretty obvious, but I did not have in mind that autocompile runs a light-weight server with no PHP support (of course).

Add rewrite rule in plugin: with .htaccess in plugin folder or using WordPress functions

I need add a rewrite rule in my plugin, and distribute it with my code. All works fine if I put the rule in the .htaccess in the WordPress root folder, but I need distribute the plugin with my rule.
I try to put a .htaccess inside the plugin folder and try to use the add_rewrite_rule function but doesn't works either.
Here the .htaccess code that works correctly in WordPress root folder but doesn't works in my plugin folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule my-plugin/pages/tp(.*)\.php$ wp-content/plugins/my-plugin/pages/request.php?pid=$1
</IfModule>
I try the follow code in my plugin but doesn't works either:
add_filter( 'query_vars', 'add_query_vars' );
function add_query_vars( $query_vars )
{
$query_vars[] = 'pid';
return $query_vars;
}
add_action( 'init', 'add_init' );
function add_init()
{
$plugin_url = 'the-path-to-my-plugin-folder';
add_rewrite_rule('my-plugin/pages/tp(.*)\.php'
, $plugin_url . 'pages/request.php?pid=$matches[1]','top');
global $wp_rewrite;
$wp_rewrite->flush_rewrite_rules(); // I know this should be called only one time, but I put it here just to keep simple the sample code
}
But I always get the error that the URL wasn't found.
What I'm doing wrong? How can I do what I need? I searched for similar questions but none solve my problem.
My plugin folder structure is:
Main folder: /wp-content/plugins/my-plugin
------ /pages (sub folder)
-------------/request.php (script that should receive the request)
NOTE: WordPress Rewrite API is not the same as Apache Rewrite module.
WP Rewrite API doesn't redirect a request to another URL, it used to
parse current URL and fill query_vars array.
The issue is in the second parameter of you add_rewrite_rule function call. It has to start from index.php? and then there should be your arguments, like pid, for example:
"index.php?pid=$matches[1]...."
So your add_init function should be like this:
add_action( 'init', 'wpse8170_add_init' );
function wpse8170_add_init()
{
add_rewrite_rule('my-plugin/pages/tp(.*)\.php', 'index.php?pid=$matches[1]', 'top');
}
Don't forget to flush rewrite rules by visiting Settings » Permalinks page.
Further reading:
The Rewrite API: The Basics
The Rewrite API: Post Types & Taxonomies
WP handles the plugins from the /wp-admin directory with a PHP script (admin.php), like this:
http://MyWP.com/wp-admin/admin.php?page=MyPlugin/module.php
Therefore, .htaccess files in the plugin directory are not parsed when the plugin is called. They have to be placed in the wp-admin directory or in the root directory, as you already found out.
Although copying the .htacces file to the root directory when the plugin is installed -and deleting it when it is removed- is possible, I don't think it is the best option. Having .htaccess files in the WP space doesn't seem like a good idea.
Your second approach looks much better: Creating rewrite rules in the main script.
Looking at your code, I think the problem are the pattern (Incoming URL string to match) and possibly the substitution URL path ($plugin_url in your question).
The $rule parameter in the add_rewrite_rule() function should capture a segment of the URL (Above) used to call the plugin's modules.
I can't suggest anything else because you don't supply enough information about the plugin and it's directory tree, except what can be guessed from the regex in the rewrite rule. But, this is a general idea of a way to achieve what you need.

Resources