addons-server/apps/api/urls.py

66 строки
1.8 KiB
Python
Исходник Обычный вид История

2010-02-17 03:22:22 +03:00
from django.conf.urls.defaults import patterns, url, include
from . import views
2010-02-17 03:22:22 +03:00
# Wrap class views in a lambda call so we get an instance of the class for view
# so we can be thread-safe. Yes, this lambda function returns a lambda
# function.
class_view = lambda x: lambda *args, **kwargs: x()(*args, **kwargs)
2010-02-20 04:13:42 +03:00
# Regular expressions that we use in our urls.
type_regexp = '/(?P<type>[^/]*)'
limit_regexp = '/(?P<limit>\d*)'
platform_regexp = '/(?P<platform>\w*)'
version_regexp = '/(?P<version>[^/]*)'
def build_urls(base, appendages):
"""
Many of our urls build off each other:
e.g.
/search/:query
/search/:query/:type
.
.
/search/:query/:type/:limit/:platform/:version
"""
urls = [base]
for i in range(len(appendages)):
urls.append(base+''.join(appendages[:i+1]))
return urls
2010-02-17 03:22:22 +03:00
base_search_regexp = r'search/(?P<query>[^/]+)'
2010-02-20 04:13:42 +03:00
appendages = [type_regexp, limit_regexp, platform_regexp, version_regexp]
search_regexps = build_urls(base_search_regexp, appendages)
base_list_regexp = r'list'
appendages.insert(0, '/(?P<list_type>[^/]+)')
list_regexps = build_urls(base_list_regexp, appendages)
2010-02-17 03:22:22 +03:00
api_patterns = patterns('',
# Addon_details
url('addon/(?P<addon_id>\d+)$',
class_view(views.AddonDetailView),
name='api.addon_detail'),)
for regexp in search_regexps:
api_patterns += patterns('',
url(regexp + '/?$', class_view(views.SearchView), name='api.search'))
2010-02-20 04:13:42 +03:00
for regexp in list_regexps:
api_patterns += patterns('',
url(regexp + '/?$', class_view(views.ListView), name='api.list'))
urlpatterns = patterns('',
# Redirect api requests without versions
2010-02-20 04:13:42 +03:00
url('^((?:addon|search|list)/.*)$', views.redirect_view),
2010-02-17 03:22:22 +03:00
# Append api_version to the real api views
url(r'^(?P<api_version>\d+|\d+.\d+)/', include(api_patterns)),
)