I have a new caller who can call my application only in a specific new url pattern:-
applicationpath?send=N&msg=<MessageContent>&dest=<PHONE>&stime=MM/DD/YY HH:MM:SS PM/AM&Operator=P&Circle=Q
My existing urlconf calls different methods based on the message text:-
(('api/(?P<phone>\w+)/MessageA', handle_a_message),
('api/(?P<phone>\w+)/MessageB', handle_b_message),
...)
I can think of writing another urlconf that redirects to these methods along these lines:-
(('applicationpath?send=N&msg=MessageA&dest=(?P<phone>\w+)/\w+', handle_a_message),
('applicationpath?send=N&msg=MessageB&dest=(?P<phone>\w+)/\w+', handle_b_message),
...)
Are there simpler recommended alternatives I am missing? Is redirection the best alternative?
Related
I'm trying to migrate my app to the new emberjs routing API.
With old router I had some workarounds to provide similar URI for objects saved by ID and for new objects which described by set of params. This were done for ability of exchange links to objects between users without permanently saving it. This is two simplified valid routes from my app:
/objects/12 // fetch object by id (/objects/:object_id)
/objects/<serialized params> // build new object from params (/objects/:params)
Both of this routes are similar to router because they all have dynamic parts and static parts are equal. So I wrote custom RouteMatcher to pickup right route. Lack of query string parsing forced me to do this hack as quick and semilegal solution, also there is ancient ticket about this feature on github.
With the new router matching has been extracted to separate package (route-recognizer) so I cannot do the trick (or it will be full of hacks and injections).
As I can see I have to choose from these options:
Totally rewrite my URIs and separate all intersecting routes
Rewrite URIs but try to implement query string parser for the new Ember.Router
Put all logic into one route and reimplement only serialize/deserialize methods (something dirty)
Second solution seems to be more clean.
What will be the best non complicated decision? Should I try to find another way?
The current router does not support query-string parameters.
We are tracking this bug at https://github.com/emberjs/ember.js/issues/1773. You may want to follow it.
In the meantime, your best bet is probably to use a dynamic segment and manually serialize (with the serialize hook) and deserialize (with the model hook).
I have a Django equipped with Tastypie and under REST style it's not easy to combine objectes of different types together, so I'm thinking if it's posible to provide a special view for combining response of several REST urls into a bigger JSON object and return to client. The url may look like,
http:// domain.com /combined_view/?p={rest url 1...}&p={rest url
2...}&p={rest url 3...}
and returned JSON would be,
[ {response of rest url 1...},
{response of rest url 2...},
{response of rest url 3...},
...
]
The question is, inside a normal django view, how can I fake a request object, and process it into a response object? Thx.
Calling your own RESTful API from inside your view is a waste of resources. Directly access the objects using the database ORM instead.
Also unrelated Resources/Objects are supposed to be not combined together. If you think that the models should be combined together then maybe your model needs to take care of it and have a relation combining the two.
And to answer your question directly, you can call your urls using httplib2 and parse the response.
I urge you to reconsider whatever it is that your doing, because whatever answer we give you here is bound to go directly against the resourceful design of REST interfaces.
If you have Foo, Bar and Baz models and you create equivalent resources for them, it's impossible to generate a request that's going to return a mixed collection of Foo, Bar, Baz resources, unless these are nested resources in a joint relationship.
Your either not thinking in a resourceful manner or don't need to, but definitely don't turn RESTful architectures into something they haven't been designed for.
I'd like to create a URL hierarchy using Tastypie but am running into some errors. Here's how I'd like the hierarchy to work:
/recipe
/recipe/ID
/recipe/ID/spice
/recipe/ID/spice/ID
I can't find out how to do this. When I set this up following the Tastypi instructions my URLs would be like this:
/recipe
/recipe/ID
/spice
/spice/ID
If I change the resource_name for spice to "/recipe/spice" then I get a "NotFound: Invalid resource lookup data provided (mismatched type)" error.
Any suggestions about what I could do?
Tastypie is meant to help implement a REST API, and thus by default only supports URLs that conform to REST practices. Namely, each URL should contain a resource name ('recipe' or 'spice') and optionally an identifier for that resource ('ID'). Anything outside of this breaks from REST practices and if you're not implementing a REST API you may want to re-consider whether or not you should be using Tastypie.
That being said, Tastypie does provide a ton of hooks for customizing things. For custom URLs, you'll want to define the method override_urls to map certain URLs to custom views and do some pre-processing before sending it to the regular dispatchers.
If possible, I'd recommend just using standard REST practices and break things up as separate 'recipe' and 'spice' resources. If you need to filter on recipes based on spices that are in them, 'spices' should be passed in as a GET parameter rather than part of the base URL. Hope that helps.
Whenever I learn a new language/framework, I always make a content management system...
I'm learning Python & Django and I'm stuck with making a URL pattern that will pick the right page.
For example, for a single-level URL pattern, I have:
url(r'^(?P<segment>[-\w]+)/$', views.page_by_slug, name='pg_slug'),
Which works great for urls like:
http://localhost:8000/page/
Now, I'm not sure if I can get Django's URL system to bring back a list of slugs ala:
http://localhost:8000/parent/child/grandchild/
would return parent, child, grandchild.
So is this something that Django does already? Or do I modify my original URL pattern to allow slashes and extract the URL data there?
Thanks for the help in advance.
That's because your regular expression does not allow middle '/' characters. Recursive definition of url segments pattern may be possible, but anyway it would be passed as a chunk to your view function.
Try this
url(r'^(?P<segments>[-/\w]+)/$', views.page_by_slug, name='pg_slug'),
and split segments argument passed to page_by_slug() by '/', then you will get ['parent', 'child', 'grandchild']. I'm not sure how you've organized the page model, but if it is not much sophiscated, consider using or improving flatpages package that is already included in Django.
Note that if you have other kind of urls that does not indicate user-generated pages but system's own pages, you should put them before the pattern you listed because Django's url matching mechanism follows the given order.
I'm using the middleware to detect the subdomain and place a corresponding object in the request scope. Would it be possible to take it further and declare that the subdomain implements these urls but not those?
Something like?
if request.subdomain.is_blue:
include(these.urls)
the urlconf is executed at startup time, not for each request; so you don't have the opportunity to include or not according to the URL used to acess.
the best would be to write your own middleware, or a restricting decorator (like #login_required), it's quite easy to write your own decorator (i like them more than middlewares for most specific tasks)
You can poke around with request.urlconf, but that can break things