Skip to content

Commit e4d58f4

Browse files
author
Arnaud Coomans
committed
Fixed groups filtering
1 parent 07b0ed8 commit e4d58f4

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

flask_autodoc/autodoc.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Autodoc(object):
2424

2525
def __init__(self, app=None):
2626
self.app = app
27-
self.groups = defaultdict(set)
27+
self.func_groups = defaultdict(set)
2828
if app is not None:
2929
self.init_app(app)
3030

@@ -55,7 +55,7 @@ def nl2br(eval_ctx, value):
5555
for p in _paragraph_re.split(value))
5656
return result
5757

58-
def doc(self, group=None, aa=None, groups=None):
58+
def doc(self, groups=None):
5959
"""Add flask route to autodoc for automatic documentation
6060
6161
Any route decorated with this method will be added to the list of
@@ -66,19 +66,19 @@ def doc(self, group=None, aa=None, groups=None):
6666
or multiple other groups as well, besides the 'all' group.
6767
"""
6868
def decorator(f):
69-
if groups:
69+
if type(groups) is list:
7070
groupset = set(groups)
7171
else:
7272
groupset = set()
73-
if group:
74-
groupset.add(group)
73+
if type(groups) is str:
74+
groupset.add(groups)
7575
groupset.add('all')
76-
for g in groupset:
77-
self.groups[g].add(f)
76+
77+
self.func_groups[f] = groupset
7878
return f
7979
return decorator
8080

81-
def generate(self, group='all', groups=[], sort=None):
81+
def generate(self, groups='all', sort=None):
8282
"""Return a list of dict describing the routes specified by the
8383
doc() method
8484
@@ -95,6 +95,12 @@ def generate(self, group='all', groups=[], sort=None):
9595
9696
Routes are sorted alphabetically based on the rule.
9797
"""
98+
groups_to_generate = list()
99+
if type(groups) is list:
100+
groups_to_generate = groups
101+
elif type(groups) is str:
102+
groups_to_generate.append(groups)
103+
98104
links = []
99105
for rule in current_app.url_map.iter_rules():
100106

@@ -103,9 +109,9 @@ def generate(self, group='all', groups=[], sort=None):
103109

104110
func = current_app.view_functions[rule.endpoint]
105111
arguments = rule.arguments if rule.arguments else ['None']
112+
func_groups = self.func_groups[func]
106113

107-
if (groups and [True for g in groups if func in self.groups[g]]) \
108-
or (not groups and func in self.groups[group]):
114+
if func_groups.intersection(groups_to_generate):
109115
links.append(
110116
dict(
111117
methods=rule.methods,
@@ -121,7 +127,7 @@ def generate(self, group='all', groups=[], sort=None):
121127
else:
122128
return sorted(links, key=itemgetter('rule'))
123129

124-
def html(self, template=None, group='all', groups=None, **context):
130+
def html(self, groups='all', template=None, **context):
125131
"""Return an html string of the routes specified by the doc() method
126132
127133
A template can be specified. A list of routes is available under the
@@ -134,7 +140,7 @@ def html(self, template=None, group='all', groups=None, **context):
134140
"""
135141
if template:
136142
return render_template(template,
137-
autodoc=self.generate(group),
143+
autodoc=self.generate(groups=groups),
138144
**context)
139145
else:
140146
filename = os.path.join(
@@ -147,5 +153,5 @@ def html(self, template=None, group='all', groups=None, **context):
147153
with current_app.app_context():
148154
return render_template_string(
149155
content,
150-
autodoc=self.generate(group=group, groups=groups),
156+
autodoc=self.generate(groups=groups),
151157
**context)

0 commit comments

Comments
 (0)