Is it possible to make URLs conditional with django? - django

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

Related

Options to map new URL Patterns to Existing Code

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?

fake a request in Django

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.

Django URL Aliases

I'm new to Django and I have a BIG problem. I don't like the "url pattern" philosophy of Django.
I don't want my pages to look like
http://domain.com/object/title-of-object
I want
http://domain.com/title-of-object
and of course I will have more than one type of object.
Is there an elegant way to achieve this with Django (not using hard-coded urls)?
Thanks!
Ever wondered that, if what you want to do seems so hard to acheive, you're doing it wrong? What is so wrong with /foo/name-of-foo/ ?
I'm trying to imagine your use-case and wondering if you need 'human' URLs for only a handful of pages. If so, it would work to go with the /foo/slug-for-foo/ approach but then use the django.contrib.redirects app to support hand-written URLs that redirect to the saner, more RESTful ones?
It is possible. You'll have to create one catch-all URL pattern, for which you'll create a view that will search all possible object types, find the matching one, and process and return that. Usually, this is a bad idea.

Is there something between middleware and view in Django so that I can plug my code into?

Is there something between middleware and view so that I can plug my code or do I have to subclass something from Django to provide this functionality?
Let me first explain why I need this, maybe there is a better solution that you can suggest. I want to restrict some of my url's based on some configuration. And,
- I want this configuration to be part of url configuration
- According to the config provided, I want to redirect, etc to some other view.
What I mean by 'part of url configuration' is something like the following.
url(r'^admin/blah/blah$', do_something, name='admin-blah-blah', {'security_level': 'very_secure', 'auth_method' : 'oauth', 'auth_url', 'http://www.foo.com'})
It seems like it is something that should be done by middlewares, but I don't want to do it with middlewares for 2 reasons.
- I don't want to maintain a separate config.
- I don't want to do regex matching for url patterns one more time, url resolver is already doing that
So if I can just find a way to plug some functionality just before view and can reach the configuration provided, it solves my problem.
Sounds like you could do this with a decorator on your views:
#restrict_url(security_level='very_secure', auth_method='oauth',
auth_url= 'http://www.foo.com')
def my_view(request):
... etc ...
You can get some ideas of how to write the restrict_url decorator by looking at the ones provided in django.contrib.auth.decorators.

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.