Skip to content

Commit

Permalink
config: support for ignoring endpoints
Browse files Browse the repository at this point in the history
* Adds support for ignoring certain endpoints through
  SITEMAP_IGNORE_ENDPOINTS configuration option.  (closes #2)

Signed-off-by: Jiri Kuncar <jiri.kuncar@cern.ch>
  • Loading branch information
jirikuncar committed Nov 10, 2014
1 parent 5e45faf commit dc17655
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flask_sitemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ def _routes_without_params(self):
if self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS']:
for rule in self.app.url_map.iter_rules():
if 'GET' in rule.methods and len(rule.arguments) == 0:
print rule
print dir(rule)
yield rule.endpoint, {}

def _generate_all_urls(self):
"""Run all generators and yield (url, enpoint) tuples."""
ignore = set(self.app.config['SITEMAP_IGNORE_ENDPOINTS'] or [])
kwargs = dict(
_external=True,
_scheme=self.app.config.get('SITEMAP_URL_SCHEME')
Expand Down Expand Up @@ -139,6 +138,10 @@ def _generate_all_urls(self):
result[key] = left[0]
left = left[1:]

# Check if the endpoint should be skipped
if endpoint in ignore:
continue

values.update(kwargs)
result['loc'] = url_for(endpoint, **values)
yield result
Expand Down
6 changes: 6 additions & 0 deletions flask_sitemap/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
Default: ``False``.
SITEMAP_IGNORE_ENDPOINTS
------------------------
Default: ``None``.
"""

SITEMAP_BLUEPRINT = 'flask_sitemap'
Expand All @@ -48,3 +52,5 @@
SITEMAP_URL_SCHEME = 'http'

SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS = False

SITEMAP_IGNORE_ENDPOINTS = None
35 changes: 35 additions & 0 deletions tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,38 @@ def user():
assert 'http://www.example.com/second' in results
assert 'http://www.example.com/third' in results
assert 'http://www.example.com/fourth' in results

def test_ignore_endpoints(self):
self.app.config['SERVER_NAME'] = 'www.example.com'
self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS'] = True
self.app.config['SITEMAP_IGNORE_ENDPOINTS'] = ['first', 'user']
sitemap = Sitemap(app=self.app)
now = datetime.now().isoformat()

@self.app.route('/')
def index():
pass

@self.app.route('/first')
def first():
pass

@self.app.route('/second')
def second():
pass

@self.app.route('/<username>')
def user(username):
pass

@sitemap.register_generator
def user():
yield 'user', {'username': 'third'}
yield 'user', {'username': 'fourth'}, now

results = [result['loc'] for result in sitemap._generate_all_urls()]
assert 'http://www.example.com/' in results
assert 'http://www.example.com/first' not in results
assert 'http://www.example.com/second' in results
assert 'http://www.example.com/third' not in results
assert 'http://www.example.com/fourth' not in results

0 comments on commit dc17655

Please sign in to comment.