Generate `urls.js` into static directory (add unit tests).
Closed this issue · 1 comments
It is the first part of a series of task regarding URL dispatcher
(urls.py in django) and equivalent URL dispatcher
in (urls.py in storeys
).
Consider these two django urls.py file:
# file: django-project/apps/urls
from django.conf import settings
from django.conf.urls import patterns, include, url
from storeys.conf.urls import urlref
urlpatterns = patterns('',
url(r'^api/', include('api.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^receipts/', include('receipts.urls')),
url(r'^aboutus/$', TemplateView.as_view(template_name='home/aboutus.html'),
name='about-us'),
url(r'^pricing/$', TemplateView.as_view(template_name='home/pricing.html'),
name='pricing'),
)
urlpatterns += staticfiles_urlpatterns()
# storeys specifics
non_exported_urlpatterns = (
urlref(module_name='api.urls'),
urlref(module_name='admin.site.urls'),
urlre(name='pricing'),
)
# file: django-project/apps/receipts/urls
urlpatterns = patterns('',
url(r'^receipt-(?P<pk>[0-9]+)$',
StoreysView.as_view(
template_name='common/base.html',
prerender_content='receipts/actions.html',
),
name='receipts-index-view'
),
url(r'^create/$',
StoreysView.as_view(
template_name='common/base.html',
prerender_content='receipts/create.html',
),
name='receipts-create-view'
),
)
The goal of this task is to generate proper urls.js
files into django's /static
folder. In this task, create a django "management command".
(In the next task, the we would make a "compiler" for django-pipeline
such that the file is generated every time collect static
is called. So, don't do anything unconventional that will forbidden the code to run as a pipeline compiler. but, it is not part of this task.)
# file: django-project/apps/static/urls.js
define(
['storeys/conf/urls'],
function(urls) {
var url = urls.url,
include = urls.include;
return [
url('^receipts/', include('receipts/urls')),
url(r'^pricing/$', TemplateView.as_view(template_name='home/pricing.html'),
name='pricing'),
];
}
);
# file: django-project/apps/static/receipts/urls.js
define(
['storeys/conf/urls', 'storeys/contrib/auth/decorators'],
function(urls, decorators, views) {
var url = urls.url;
return [
url('^receipt-(?P<pk>[0-9]+)$',
StoreysView.as_view(
template_name='common/base.html',
prerender_content='receipts/actions.html',
), 'receipts-index-view')
url('^create/$',
StoreysView.as_view(
template_name='common/base.html',
prerender_content='receipts/create.html',
), 'receipts-create-view'),
];
}
);
The only three type of views you need to support:
StoreysView.as_view()
,TemplateView.as_view()
,- Other views that has
discompose()
method
For StoreysView
, you might add a def discompose()
method to StoreysView
to obtain template_name
and pretender_content
.
For TemplateView
, just find the instance valuable, and err out if the typical template path cannot be found.
For other views, please proposal api.
Split this task into at least 2 milestones. Implement non_exported_urlpatterns = ()
into milestone 2.
There is a incomplete python regex clone done here:
https://github.com/storeys/storeys/blob/master/storeys/js/conf/re.js
It only support a subset of python regex. But, you can assume all regex in urls.py
is compatible, including those with param (i.e., (?P<pk>[0-9]+)$
).
You will add django tests to the project.