I am trying to create a Friendly URL search method in GAE and Python. Has anyone done this?
It works for me, but it is very VERY shaky. How it works is that it takes a path (let them say / foo / bar /) and splits it into a list. Accepts the last item in the list and queries the database for matching. Now comes some kind of headache.
If there is another “bar”, as there may be another “bar”, but on a different page, how to tell them apart? At the moment, my solution is to iterate over the result of "bar" and look at the property of the link to the parent page. The parent "foo" could also happen more than once, so now we have to iterate over this value. It is easy to see that this cloud is easy to fold for a large number of cycles.
Just to make it a little worse, one page could work in more than one language, and the URL should match the language of Denmark.
My current data warehouse setup:
class Pages(db.Model):
name = db.StringProperty()
parentKey = db.SelfReferenceProperty()
class UrlProvider(db.Model):
name = db.StringProperty()
path = db.StringProperty()
langCode = db.StringProperty()
page = db.ReferenceProperty(Pages)
And to get the page using firendly url:
pageFromUrl = UrlProvider.gql('WHERE path = :path AND langCode = :lang', path = path, lang = templateObject.lang).fetch(100)
for pageUrl in pageFromUrl:
parentFromUrl = UrlProvider.gql('WHERE page = :page AND langCode = :lang', page = pageUrl.page.parentKey, lang = templateObject.lang).fetch(100)
for parentUrl in parentFromUrl:
if parentUrl.path == templateObject.path[-2]:
templateObject.url = parentUrl.path + '/' + path
page = pageUrl.page
, UrlProvider , templateObject.path. .
.
, .
- ?