beedesk / dingo

Django inspired web development framework on NodeJS
BSD 2-Clause "Simplified" License
1 stars 1 forks source link

Implement `urlresolvers.reverse()` #2

Open thomasyip opened 10 years ago

thomasyip commented 10 years ago

Follow Django patterns:

https://docs.djangoproject.com/en/dev/ref/urlresolvers/

thomasyip commented 10 years ago

Here an example of urls and reverse usage.


-- apps/urls.py

P_SLUG = '(?P<prefix>[a-z]+)-(?P<pk>[0-9]+)'
P_PREVIEWS_SIZE = '(?P<size>([0-9a-zA-Z]*))'
P_GITHASH = '(?P<spread>[0-9a-f]{2})(?P<hash>[0-9a-f]{38})'

urlpatterns = patterns('',
    url(r'^p/%s/%s/%s$' % (P_SLUG, P_PREVIEWS_SIZE, P_GITHASH),
        redirect_to_s3_preview, name="redirect-to-s3-preview"),
)
urlpatterns += staticfiles_urlpatterns()

-- apps/media/views.py

def get_artifact_preview_path(artifact, size=''):
    githash = artifact.githash
    seg = githash[0:2]
    name = githash[2:]

    path = reverse('redirect-to-s3-preview', kwargs={
            u'prefix': 'owner', 
            u'pk': str(artifact.owner.pk), u'size': size, 
            u'spread': seg, u'hash': name
    })
    return path