Django URL regex question - python

I had a quick question about Django URL configuration, and I guess REGEX as well. I have a small configuration application that takes in environments and artifacts in the following way:
url(r'^env/(?P<env>\w+)/artifact/(?P<artifact>\w+)/$', 'config.views.ipview', name="bothlist"),
Now, this works fine, but what I would like to do is have it be able to have additional parameters that are optional, such as a verbose mode or no formating mode. I know how to do this just fine in the views, but I can't wrap my head around the regex.
the call would be something like
GET /env/<env>/artifact/<artifact>/<opt:verbose>/<opt:noformat>
Any help would be appreciated, thanks!
-Shawn

I wouldn't put such options into the URL. As you said, these are optional options, they might only change the output. They don't belong in an URL.
Your initial regex should match URLs like:
/env/<env>/artifact/<artifact>?verbose=1&noformat=1
Imho this is a much better usage of URLs

Related

Specifying doc-id and plot-id (i.e. the URL) for output_server()

I would like to be able to specify the URL where output_server publishes my app in bokeh-server (as an example, I am trying to do this in the animate_widgets.py example that Christine Doig presented in Scipy2015).
I am already running bokeh-server in a separate terminal. When I run my app with output_server, is there any way to specify the URL where the app will be rendered?
It seems to currently follow the syntax:
http://localhost:5006/bokeh/doc/some-doc-id/some-plot-id
but I don't see the ability to specify those fields <doc-id> and <plot-id> with output_server (documentation for output_server below).
Is there any way to specify where exactly (URL-wise) I want the app to be published?
Note that just entering the string http://localhost:5006/bokeh/doc/some-doc-id/some-plot-id as URL for output_server() does not work.
The short answer is not really. Those URLs are meant to be unambiguous and to avoid collisions. Letting users choose their own URLs would be fairly unworkable in the general multi-user scenario. But that's OK, what you really probably want to is to embed a Bokeh server plot in your own document (as opposed to just linking to the bare page that has the plot and nothing else). This you can accomplish easily with server_session:
https://docs.bokeh.org/en/latest/docs/user_guide/embed.html#bokeh-applications
Edit: I won't actually say its impossible, but it's far enough outside normal usage that I don't know offhand how you could accomplish it, and even if you could it would probably not be advisable for several reasons.

URL Encoding/Decoding in python (whole url, not just the path)

I have done a lot of search and experimentation, and I havent been able to find the solution. So, if there is something trivial I missed, I appologize ahead of time.
Problem:
I have a python turbogears app that is downloading url resources. It is being given a URL to download by clients.
One client in particular sends unescaped urls. For eg, 'http://www.foo.com/file with space.txt'
When I try to download it, the download fails, because the server does not recognize this url. It needs to have the spaces escaped to be a valid url.
I know that there are methods ( urllib.urlencode/urllib.quote etc) that will encode strings. However they assume that the strings they work on are not urls. If you give a URL to these methods, they escape the scheme of the url, and make it even more invalid.
So, the summary is: How do I unescape a whole fully qualified url in python?
NOTE: I have tried using urlparse to parse out the url components to get at the path. However sometimes the url will have query parameters, fragments etc. So, I do not want to write code that splits the url into its parts, escapes whatever is required only from the path+query+fragment, and then reconstructs the url.
Is there any helper function that directly takes the url, and escapes it?
Also, note that sometimes I get valid escaped urls from clients. So, I want to handle them as well, without double escaping them.
Ok, I found the following on pypi. This seems to solve the problem.
https://github.com/seomoz/url-py/
This is the url egg from seomoz. Seems to do the job very well.
You can use regular expressions to separate the domain name and the file path, then only urlencode the path. Here's the regex documentation, here's a tutorial.

How can I write the regex for those urls

I'm trying to build a small wiki, but I'm having problems writing the regex rules for them.
What I'm trying to do is that every page should have an edit page of its own, and when I press submit on the edit page, it should redirect me to the wiki page.
I want to have the following urls in my application:
http://example.com/<page_name>
http://example.com/_edit/<page_name>
My URLConf has the following rules:
url(r'(_edit/?P<page_name>(?:[a-zA-Z0-9_-]+/?)*)', views.edit),
url(r'(?P<page_name>(^(?:_edit?)?:[a-zA-Z0-9_-]+/?)*)', views.page),
But they're not working for some reason.
How can I make this work?
It seems that one - or both - match the same things.
Following a more concise approach I'd really define the edit URL as:
http://example.com/<pagename>/edit
This is more clear and guessable in my humble opinion.
Then, remember that Django loops your url patterns, in the same order you defined them, and stops on the first one matching the incoming request. So the order they are defined with is really important.
Coming with the answer to your question:
^(?P<page_name>[\w]+)$ matches a request to any /PageName
Please always remember the starting caret and the final dollar signs, that are saying we expect the URL to start and stop respectively right before and after our regexp, otherwise any leading or trailing symbol/character would make the regexp match as well (while you likely want to show up a 404 in that case).
^_edit/(?P<page_name>[\w]+)$ matches the edit URL (or ^(?P<page_name>[\w]+)/edit$ if you like the user-friendly URL commonly referred to as REST urls, while RESTfullnes is a concept that has nothing to do with URL style).
Summarizing put the following in your urls:
url(r'^(?P<page_name>[\w]+)$', views.page)
url(r'^_edit/(?P<page_name>[\w]+)$', views.edit)
You can easily force URLs not to have some particular character by changing \w with a set defined by yourself.
To learn more about Django URL Dispatching read here.
Note: Regexp's are as powerful as dangerous, especially when coming on network. Keep it simple, and be sure to really understand what are you defining, otherwise your web application may be exposed to several security issues.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski
Please try the following URLs, that are simpler:
url(r'_edit/(?P<page_name>[\w-]+), views.edit)'
url(r'(?P<page_name>[\w-]+), views.page),

Configuring urls.py with this regular expression

I'm trying to build my first Django-powered blog, but I'm stuck in a point.
I'm trying to grab a permanent link from the URL visited in order to display a single post.
The permanent link I'm using is like that:
http://127.0.0.1:8000/blog/20-feb-2012/a-nice-post/
I'd like to grab both the date and the slug from this URL and pass them into a view's function.
I've made this regular expression:
(r'^blog/(?P<day>\d{2})-/(?P<month>\w{3})-/(?P<year>\d{4})/(P?<slug>[-\w]+)/$','blog.views.single_post'),
In the urls.py file, but it seems it is not working.
What's wrong with this regular expression?
You have included slashes between the day-month-year. Remove them.
(r'^blog/(?P<day>\d{2})-(?P<month>\w{3})-(?P<year>\d{4})/(?P<slug>[-\w]+)/$','blog.views.single_post'),
Without checking anything else, you have P? instead of ?P in the slug part.
For starters, you have extra slashes in your regexp for example here (?P<month>\w{3})-**/**(?P<year>\d{4}) and also you have a P? instead of ?P at the end.
In addition, I thought you might want to have a working regexp example. So I tested this one and it works for /blog/20-feb-2012/a-nice-post/:
r'^blog/(?P<day>\d{2})-(?P<month>\w{3})-(?P<year>\d{4})/(?P<slug>[-\w]+)/$'

Getting a URL with Python

I'm trying to do something similar to placekitten.com, wherein a user can input two strings after the base URL and have those strings alter the output. I'm doing this in Python, and I cannot for the life of me figure out how to grab the URL. In PHP I can do it with query string and $_REQUEST. I can't find a similar method in Python that doesn't rely on CGI.
(I know I could do this with Django, but that's serious overkill for this project.)
This is just by looking at the docs but have you tried it?
cherrypy.request.path_info
The docs say:
The ‘relative path’ portion of the Request-URI. This is relative to the script_name (‘mount point’) of the application which is handling this request.
http://docs.cherrypy.org/stable/refman/_cprequest.html#cherrypy._cprequest.Request.path_info

Categories

Resources