webapp2 routes for external links - google-app-engine

I want to manage external links in a central place in my app so that if they change I need only change them in one place.
Since I'm already using webapp2s routing, I figured I could use that, and use url_for just like every other link. So, tried the following:
Route('http://www.google.com', name='google', build_only=True)
but when I render the link, like this:
uri_for('google')
It encodes the http:// bit, like this:
http%3A//www.google.com
which means if you use it in a href tag, you end up with a relative link like this:
http://localhost:8080/some/path/http%3A//www.google.com
So, questions are:
Is webapp2s routing even designed for external links?
If yes, how to you add absolute URLs?
If no, is there a similar mechanism for doing so?
Would be nice to use webapp2s routing so I can seamlessly use url_for without having to write another 'same but different' method.

I managed to solve the issue by implementing my own uri_for method, and using that instead
def uri_for(_name, _request=None, *args, **kwargs):
uri = webapp2.uri_for(_name, _request, *args, **kwargs)
return urllib.unquote(uri)

Instead of _full (as voscausa mentioned above), you can put _netloc with the server name when calling uri_for. Putting _netloc forces _full to True.
See http://webapp-improved.appspot.com/api/webapp2.html#uri-routing

Related

Href opening link in http://localhost:3000/LINK rather than opening the link seprately [duplicate]

I just have created primitive html page. Here it is: example
And here is its markup:
www.google.com
<br/>
http://www.google.com
As you can see it contains two links. The first one's href doesn't have 'http'-prefix and when I click this link browser redirects me to non-existing page https://fiddle.jshell.net/_display/www.google.com. The second one's href has this prefix and browser produces correct url http://www.google.com/. Is it possible to use hrefs such as www.something.com, without http(s) prefixes?
It's possible, and indeed you're doing it right now. It just doesn't do what you think it does.
Consider what the browser does when you link to this:
href="index.html"
What then would it do when you link to this?:
href="index.com"
Or this?:
href="www.html"
Or?:
href="www.index.com.html"
The browser doesn't know what you meant, it only knows what you told it. Without the prefix, it's going to follow the standard for the current HTTP address. The prefix is what tells it that it needs to start at a new root address entirely.
Note that you don't need the http: part, you can do this:
href="//www.google.com"
The browser will use whatever the current protocol is (http, https, etc.) but the // tells it that this is a new root address.
You can omit the protocol by using // in front of the path. Here is an example:
Google
By using //, you can tell the browser that this is actually a new (full) link, and not a relative one (relative to your current link).
I've created a little function in React project that could help you:
const getClickableLink = link => {
return link.startsWith("http://") || link.startsWith("https://") ?
link
: `http://${link}`;
};
And you can implement it like this:
const link = "google.com";
<a href={getClickableLink(link)}>{link}</a>
Omitting the the protocol by just using // in front of the path is a very bad idea in term of SEO.
Ok, most of the modern browsers will work fine. On the other hand, most of the robots will get in trouble scanning your site. Masjestic will not count the flow from those links. Audit tools, like SEMrush, will not be able to perform their jobs

Clean the URL like of Google+

Sorry for so brief title. I am wondering how Google+ makes user URLs so clean. I mean like :
https://plus.google.com/+PuruVijay
Would take me to my page. I want to know how is that + after/ was put and how it loaded the corresponding page. I want a database to get the URL. The URL actually should have been like
Plus.Google. com/user?id=134566
Looking for a good answer please help
Edit:
An example is of this page's URL
You can also do like that... just you need to create folder of name
e.g. http://yoursite.com/PuruVijay
here PuruVijay is folder you need to create in you Website directory.. and put index file in that folder
In a comment you say you are using an Apache server. The typical way to handle URL manipulations like this is the module mod_rewrite, which you can find documentation on here. This uses regular expressions to match URLs and direct to another. For example, a rule for /~user to /u/user is
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2
For the Google+ example, you say you want to translate from /+PuruVijay to /user?id=134566. This will be a little more complicated because the URL as given does not include the User ID, so you will have to retrieve the number some other way. You can however use mod_rewrite to redirect to /user?name=+PuruVijay. This would look something like (not tested!)
RewriteRule ^/\+(.*) /user?id=$1
Then, your user page can get the id parameter and look it up in the database to display the correct page, while allowing the user to type in an easy-to-remember URL.
As far as mapping PuruVijay to 134566, Google+ requires the custom URLs to be unique, so there is a 1-1 correspondence between the handle PuruVijay and the user ID number 134566. Otherwise it would be impossible to look up a specific ID number given a custom URL. Your site would have to place a similar restriction if you decide to allow custom handles like this.

Appengine wildcard URL issues

I have a page in my Google appengine that needs to take records from the datastore based on parameters fed through the URL of the page. The URL of the page looks like http://example.appspot.com/page/WILDCARD/, where WILDCARD is a unique identifier (32-character hex string) for a record. It then takes the record specified by WILDCARD and displays it on the template page.html, or at least that's what it's supposed to do. Whenever I access the page as http://example.appspot.com/page/WILDCARD/, I get a 404 error. Does anyone know why/can anyone suggest reasons for why this might be? Thanks.
In my WSGI class handlers, I have
('/page/([^/]+)/', PageHandler),
and the PageHandler class itself is
class PageHandler(webapp2.RequestHandler):
def get(self, recordID):
allrecords = db.GqlQuery("SELECT RECORD FROM RECORDS WHERE recordid = :record",record = recordID)
if not allrecords:
self.redirect("../") #the URL is screwed up, so we're redirecting you back to the main page
else:
for row in allrecords:
template = JINJA.get_template("page.html")
self.response.out.write(template.render({ "recordtext":row.RECORD, "recordid":recordID }))
Additional details: JINJA is just the default jinja environment, I'm pretty sure there's nothing wrong with that. RECORDS is a db.Model type class that has members RECORD, a text property and recordid, a string property. It's currently not populated with any data.
EDIT: Nevermind, I solved the problem. It was my own stupidity when I simplified the problem down. It turns out that in actual implementation (i.e. the complicated version of this example), I had a tiny discrepancy between what I thought my URL was and what it actually was for /page/. Apologies.
Try
webapp2.Route('/page/<recordID>/', handler=PageHandler)
for your route definition.
You can insert it along your other route tuples like:
app = webapp2.WSGIApplication([
('/', Homepage),
webapp2.Route('/page/<recordID>/', handler=PageHandler),
('/otherpage', SomeOtherPage)
])
What you have might work fine as well with proper regex but I find it easier to read code when using template URLs defining expected paths and naming them.
See more: https://webapp-improved.appspot.com/guide/routing.html
Nevermind, I solved the problem. It was my own stupidity when I simplified the problem down. It turns out that in actual implementation (i.e. the complicated version of this example), I had a tiny discrepancy between what I thought my URL was and what it actually was for /page/. Apologies.
This what work for me:
app = webapp2.WSGIApplication([
('/', Homepage),
('/page/.*', PageHandler),
('/otherpage', SomeOtherPage)
])

Intercepting Camel Endpoint Dynamically

I am trying to intercept an endpoint where the value of the URI matches some information in the exchange header.
Let say I have a field in the header called DatabaseName. I want to enforce that a specific route is only writing to the database specified in the header.
Can I do something like this?
interceptSendToEndpoint("mock:${in.header.DatabaseName}")
I tried that but it does not seem to work. What are my options?
I was also thinking of doing something like:
interceptSendToEndpoint("mock:*").when(...)?
But in this case, I am not sure if I can reference the URI of the intercepted node in the when expression.
Thanks
You can intercept using a wildcard and combine that with when to do what you want, see details at: http://camel.apache.org/intercept
The is a header on the Message with the key Exchange.INTERCEPTED_ENDPOINT (CamelInterceptedEndpoint) that has the endpoint uri that was intercepted. You can use that in the when to match the predicate. Something a like:
interceptSendToEndpoint("mock:*")
.when(simple("${header.CamelInterceptedEndpoint} == ${in.header.DatabaseName}"))
...
Use the recipientList instruction for this: http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html

How to use inbulit django templatetags in google-app-engine

I am trying to use Django in built templatetags like markup and
humanize in my google app , but its not working.
I added markup and humanize in the INSTALLED_APPS. Still not working.
How to use that?
Here is how to do it for humanize, others should be similar. At the end of the controller that invokes your template there is a function that looks like:
def main():
run_wsgi_app(application)
Add the following two lines just after def main():
from google.appengine.ext.webapp import template
template.register_template_library(
'django.contrib.humanize.templatetags.humanize')
No need to add {% load humanize %} in your template.
Thanks to this posting http://blog.yjl.im/2011/02/few-things-on-google-app-engine-i.html that hinted the solution for me.
It is possible to do this, but you'll need to provide more information before your particular situation can be addressed. It sounds like there might be an exception that is being thrown--is this the case? If there is one, what is being printed out to the console (or the log)?
I'm using app-engine-patch and have been able to use both django.contrib.humanize and django.contrib.markup, so you might see if this works for you.

Resources