Multiple arguments in bottle's route - python-2.7

In bottle, the route like this:
#get('/ws/contacts/:uid')
how can i add more arguments to the route, and how to code the #get() ?

Just use multiple wildcards:
#get('/ws/contacts/:uid/:itemid')
def get_user_item(uid, itemid):
return 'you passed in uid={} and itemid={}'.format(uid, itemid)
P.S., You're using a deprecated wildcard syntax. Unless you're required to use an old version of Bottle (0.9 or earlier), I recommend that you use the modern syntax, like this:
#get('/ws/contacts/<uid:int>/<itemid:int>')

Related

How do you add parameters to the current URL?

I want to add a language modifier via query, by adding "?lang=xx" to the current URL.
I'm currently using something like this:
=link_to request.request_parameters.slice(:lang).merge(lang: 'en')
This is fine, except it adds "welcome/index" or the correspondent route when I use customized routes, which is not great for sharing links.
I also tried this:
=link_to "#{request.path}/?lang=en"
Simple, it works, but doesn't allow me to remove unwanted parameters.
I read somewhere that there was one of these that was supposed to not include "welcome/index", I've already tried request.query_parameters and request.request_parameters, both include 'welcome/index' when I do something while on my website's root.
I could add conditionals and remove the controller and action when I'm in a customized route, but I think that's unnecessary work.
Is there a clean way to do this without overengineering a solution?
I'm currently on Rails 4.2.5 and thinking on upgrading to 5.0.
Guess I had to overengineer it. This is the simplest implementation I could think of:
Slim:
=link_to_if request.path == '/', 'Home', root_path(lang: :en) do
=link_to 'Not Home', request.request_parameters.slice(:lang).merge(lang: 'en')
I know it's two lines, I just hate adding more code than it'd seem necessary.

Doctrine2 apply function to query parameter

I use the doctrine query builder to build my query.
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(array('u'))->from('Account', 'a');
-- problem here
$qb->where('lower_unaccent(u.email) LIKE :search');
$qb->setParameter('search', $search['search'] . '%');
This works fine, but I would also like to apply the lower_unaccent function to the search parameter.
Is there a way to do this with the query builder?
Because when I do LOWER_UNACCENT(u.email) LIKE LOWER_UNACCENT(:search) I get the following error:
Error: Expected Doctrine\ORM\Query\Lexer::T_STRING, got 'LOWER_UNACCENT'
Even if I change LOWER_UNACCENT TO LOWER, I get the same error message.
You have to use $qb->expr()->lower('u.email') and $qb->expr()->lower(':search'). To create the "LIKE" pass those as arguments to $qb->expr()->like().
There doesn't seem to be a unaccent method though, but maybe there is a way to customize this. Let me know how it went.
EDIT:
Looking into lower()'s code, it seems like you can use any function you want like this:
new Expr\Func('LOWER', array($x));
By default, I'm pretty sure you can't do that.
lower_unaccent is not standard SQL, and as far as I can see, not supported by Doctrine.
What you can do is extend DQL with user-defined functions, which will do whatever you want. It is not so hard to implement. Here is some documentation:
http://docs.doctrine-project.org/en/2.0.x/cookbook/dql-user-defined-functions.html
http://docs.doctrine-project.org/en/2.0.x/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language

How to use django-urls-sugar and ssl middleware together?

I have been using django for a few months and the ssl middleware to redirect to https from http in my urls. In order to use more complex urls, I decided to use django-urls-sugar and it works fine but I do not know how to pass more parameters than the url parameters, the view and the name identifying the view.
url_sugar([Constant('ardataset'),
Variable('band_id', '\d+'),
Variable('version', '\d+'),
], LoggedInViewArDataset.as_view(), name='ar-dataset-view-get'),
This works but as soon as I want to use {'SSL': True} or passing the value as a keyword argument, it does not work.
Should work with the new version of django-urls-sugar, released today.
BTW, replying to what #chris-pratt said, it's true that it doesn't do anything that you can't do by yourself, but this is what any "sugar" library is intended to do, right? django-templatetags-sugar for template tags, django-urls-sugar for urls. You can perfectly live without them, they are just intended to make you life easier in some (rare?) cases.

django internalization in urls? how to make urls like this: "en/articles" and "pt/artigos"...?

Hy!
I would need to make urls based on language.
Example:
If I had the language english and subcategory named "articles" then the url might be like this:
/en/articles/...
If the language were portuguese and subcategory already translated is "artigos" then the url will be:
/pt/artigos/...
How can I do it?
I must use the urls.py or some monkeypatches?
thanks
This features is already existing in the yet-to-be released version 1.4. You can read about it in the release note.
If your really need this feature, and no existing app feets your needs, you can still try to apply the corresponding patch yourself.
Django LocaleURL is a piece of middleware that does exactly this. The documentation can be found in the source, or online.
Edit: I read over the fact that you want to translate the url itself... I'm not aware of any piece of code that provides this. Perhaps you could extend the LocalURL middleware to take care of the translations for you. Say you have a regex match like (?P<articles>\w+), you could in the middleware determine which view you want to use. Something like the following mapping perhaps?
if article_slug in ['articles', 'artigos', 'article']:
article(request) # Call the article view
I've been using transurlvania with great success, it does exactly what you need and more, however i see that in the next Django release django-i18nurls will be included in django core so perhaps it would be better to learn that

how to make RSS feeds where the /rss/ is at the end of the URL, not at the beginning?

http://docs.djangoproject.com/en/dev/ref/contrib/syndication/ describes the way to use the Feeds class, and it works well for me, but it requires the URL to be like http://example.com/rss/feedid/parameters/
I need it to be http://example.com/feedid/parameters/rss/
How to do that?
Since Django's URLs are based on regexes, I think that you can use a rule like this:
(r'^(?P<url>.*)/rss/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),
Though personally, I have never used the Django syndication framework - I just use generic views (or wrappers around generic views) with the content_type option, and generate the RSS/Atom with a template.