Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

URL routes should match finite crop ratios #33

Open
emcconville opened this issue Jul 15, 2014 · 0 comments
Open

URL routes should match finite crop ratios #33

emcconville opened this issue Jul 15, 2014 · 0 comments

Comments

@emcconville
Copy link
Contributor

All views seem to validate & raise Http404 if given ratio_slug doesn't match pre-defined enumerations. Why bother with views at all, if the regex can reject bad request at the route level.

index f14190f..c5e2076 100644
--- a/betty/cropper/urls.py
+++ b/betty/cropper/urls.py
@@ -3,11 +3,11 @@ from django.conf.urls import patterns, url, include
 urlpatterns = patterns('betty.cropper.views',
     url(r'image\.js', "image_js"),
     url(  # noqa
-        r'^(?P<id>\d{5,})/(?P<ratio_slug>[a-z0-9]+)/(?P<width>\d+)\.(?P<extension>(jpg|png))',
+        r'^(?P<id>\d{5,})/(?P<ratio_slug>(original|1x1|2x1|3x1|3x4|4x3|16x9))/(?P<width>\d+)\.(?P<extension>(jpg|png))',
         'redirect_crop'
     ),
     url(  # noqa
-        r'^(?P<id>[0-9/]+)/(?P<ratio_slug>[a-z0-9]+)/(?P<width>\d+)\.(?P<extension>(jpg|png))',
+        r'^(?P<id>[0-9/]+)/(?P<ratio_slug>(original|1x1|2x1|3x1|3x4|4x3|16x9))/(?P<width>\d+)\.(?P<extension>(jpg|png))',
         'crop'
     ),
     url(r'api/', include("betty.cropper.api.urls")),

Better yet, just compile a regex directly from settings

re.compile('(original|{})'.format('|'.join(betty.conf.app.settings.BETTY_RATIOS))

Then reduce

--- a/betty/cropper/views.py
+++ b/betty/cropper/views.py
@@ -56,13 +56,6 @@ def redirect_crop(request, id, ratio_slug, width, extension):

 @cache_control(max_age=300)
 def crop(request, id, ratio_slug, width, extension):
-    if ratio_slug != "original" and ratio_slug not in settings.BETTY_RATIOS:
-        raise Http404
-
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant