@@ -52,6 +52,7 @@ class FlaskView(object):
52
52
route_base = None
53
53
route_prefix = None
54
54
trailing_slash = True
55
+ method_dashified = False # TODO(hoatle): make True as default instead, this is not a compatible change
55
56
special_methods = {
56
57
"get" : ["GET" ],
57
58
"put" : ["PUT" ],
@@ -63,7 +64,7 @@ class FlaskView(object):
63
64
64
65
@classmethod
65
66
def register (cls , app , route_base = None , subdomain = None , route_prefix = None ,
66
- trailing_slash = None ):
67
+ trailing_slash = None , method_dashified = None ):
67
68
"""Registers a FlaskView class for use with a specific instance of a
68
69
Flask app. Any methods not prefixes with an underscore are candidates
69
70
to be routed and will have routes registered when this method is
@@ -81,6 +82,10 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
81
82
:param route_prefix: A prefix to be applied to all routes registered
82
83
for this class. Precedes route_base. Overrides
83
84
the class' route_prefix if it has been set.
85
+ :param trailing_slash: An option to put trailing slashes at the end of routes
86
+ without parameters.
87
+ :param method_dashified: An option to dashify method name from some_route to /some-route/
88
+ route instead of default /some_route/
84
89
"""
85
90
86
91
if cls is FlaskView :
@@ -104,6 +109,10 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
104
109
cls .orig_trailing_slash = cls .trailing_slash
105
110
cls .trailing_slash = trailing_slash
106
111
112
+ if method_dashified is not None :
113
+ cls .orig_method_dashified = cls .method_dashified
114
+ cls .method_dashified = method_dashified
115
+
107
116
members = get_interesting_members (FlaskView , cls )
108
117
109
118
for name , value in members :
@@ -137,6 +146,8 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
137
146
app .add_url_rule (rule , route_name , proxy , methods = methods , subdomain = subdomain )
138
147
139
148
else :
149
+ if cls .method_dashified is True :
150
+ name = _dashify_underscore (name )
140
151
route_str = '/{0!s}/' .format (name )
141
152
if not cls .trailing_slash :
142
153
route_str = route_str .rstrip ('/' )
@@ -157,6 +168,10 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
157
168
cls .trailing_slash = cls .orig_trailing_slash
158
169
del cls .orig_trailing_slash
159
170
171
+ if hasattr (cls , "orig_method_dashified" ):
172
+ cls .method_dashified = cls .orig_method_dashified
173
+ del cls .orig_method_dashified
174
+
160
175
@classmethod
161
176
def parse_options (cls , options ):
162
177
"""Extracts subdomain and endpoint values from the options dict and returns
@@ -315,17 +330,11 @@ def get_route_base(cls):
315
330
316
331
@classmethod
317
332
def default_route_base (cls ):
318
- first_cap_re = re .compile ('(.)([A-Z][a-z]+)' )
319
- all_cap_re = re .compile ('([a-z0-9])([A-Z])' )
320
-
321
- def dashify (name ): #TODO(hoatle): refactor this
322
- s1 = first_cap_re .sub (r'\1-\2' , name )
323
- return all_cap_re .sub (r'\1-\2' , s1 ).lower ()
324
333
325
334
if cls .__name__ .endswith ("View" ):
326
- route_base = dashify (cls .__name__ [:- 4 ])
335
+ route_base = _dashify_uppercase (cls .__name__ [:- 4 ])
327
336
else :
328
- route_base = dashify (cls .__name__ )
337
+ route_base = _dashify_uppercase (cls .__name__ )
329
338
330
339
return route_base
331
340
@@ -340,6 +349,18 @@ def build_route_name(cls, method_name):
340
349
return cls .__name__ + ":{0!s}" .format (method_name )
341
350
342
351
352
+ def _dashify_uppercase (name ):
353
+ """convert somethingWithUppercase into something-with-uppercase"""
354
+ first_cap_re = re .compile ('(.)([A-Z][a-z]+)' ) # better to define this once
355
+ all_cap_re = re .compile ('([a-z0-9])([A-Z])' )
356
+ s1 = first_cap_re .sub (r'\1-\2' , name )
357
+ return all_cap_re .sub (r'\1-\2' , s1 ).lower ()
358
+
359
+ def _dashify_underscore (name ):
360
+ """convert something_with_underscore into something-with-underscore"""
361
+ return '-' .join (re .split ('_' , name ))
362
+
363
+
343
364
def get_interesting_members (base_class , cls ):
344
365
"""Returns a list of methods that can be routed to"""
345
366
0 commit comments