├── docs ├── requirements.txt ├── index.rst ├── Makefile └── conf.py ├── .coveragerc ├── tests ├── __init__.py ├── data │ └── sitemap.xml ├── helpers.py └── test_ext.py ├── AUTHORS ├── flask_sitemap ├── templates │ └── flask_sitemap │ │ ├── sitemapindex.xml │ │ └── sitemap.xml ├── version.py ├── cli.py ├── config.py └── __init__.py ├── tox.ini ├── pytest.ini ├── setup.cfg ├── run-tests.sh ├── .gitignore ├── MANIFEST.in ├── CONTRIBUTING.rst ├── .github └── workflows │ ├── pypi-release.yml │ └── tests.yml ├── README.rst ├── LICENSE ├── CHANGES └── setup.py /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | -e .[docs,tests] 2 | -------------------------------------------------------------------------------- /.coveragerc: -------------------------------------------------------------------------------- 1 | # This file is part of Flask-Sitemap 2 | # Copyright (C) 2014 CERN. 3 | # 4 | # Flask-Sitemap is free software; you can redistribute it and/or modify 5 | # it under the terms of the Revised BSD License; see LICENSE file for 6 | # more details. 7 | 8 | [run] 9 | source = flask_sitemap 10 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | """Tests.""" 11 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Authors 2 | ======= 3 | 4 | Flask-Sitemap is developed for use in `Invenio `_ 5 | digital library software. 6 | 7 | Contact us at `info@inveniosoftware.org `_ 8 | 9 | Contributors 10 | ------------ 11 | 12 | * Jiri Kuncar 13 | * Chris Hawkes 14 | * Albert Wang 15 | -------------------------------------------------------------------------------- /flask_sitemap/templates/flask_sitemap/sitemapindex.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | {%- for sitemap in sitemaps %} 4 | 5 | {{ sitemap.loc }} 6 | {%- if sitemap.lastmod %} 7 | {{ sitemap.lastmod }} 8 | {%- endif %} 9 | 10 | {%- endfor %} 11 | 12 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # This file is part of Flask-Sitemap 2 | # Copyright (C) 2014 CERN. 3 | # 4 | # Flask-Sitemap is free software; you can redistribute it and/or modify 5 | # it under the terms of the Revised BSD License; see LICENSE file for 6 | # more details. 7 | 8 | [tox] 9 | envlist = py26, py27, py33, py34 10 | 11 | [testenv] 12 | deps = pytest 13 | pytest-cov 14 | pytest-pep8 15 | commands = {envpython} setup.py test 16 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | [pytest] 11 | addopts = --isort --pydocstyle --pycodestyle --doctest-glob="*.rst" --doctest-modules --cov=flask_sitemap --cov-report=term-missing 12 | testpaths = docs tests flask_sitemap 13 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014, 2015, 2016 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | [build_sphinx] 11 | source-dir = docs/ 12 | build-dir = docs/_build 13 | all_files = 1 14 | 15 | [bdist_wheel] 16 | universal = 1 17 | 18 | [pydocstyle] 19 | add_ignore = D401 20 | 21 | [pycodestyle] 22 | exclude = docs/conf.py 23 | -------------------------------------------------------------------------------- /tests/data/sitemap.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | http://www.example.com/first 5 | 6 | 7 | http://www.example.com/second 8 | 9 | 10 | http://www.example.com/third 11 | 12 | 13 | http://www.example.com/fourth 14 | {now} 15 | 16 | 17 | -------------------------------------------------------------------------------- /run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # -*- coding: utf-8 -*- 3 | # 4 | # This file is part of Invenio. 5 | # Copyright (C) 2013-2020 CERN. 6 | # 7 | # Flask-Sitemap is free software; you can redistribute it and/or modify 8 | # it under the terms of the Revised BSD License; see LICENSE file for 9 | # more details. 10 | 11 | # Quit on errors 12 | set -o errexit 13 | 14 | # Quit on unbound symbols 15 | set -o nounset 16 | 17 | python -m check_manifest --ignore ".*-requirements.txt,.gitmodules,docs/_themes" 18 | git submodule update --init 19 | python -m sphinx.cmd.build -qnNW docs docs/_build/html 20 | python -m pytest 21 | python -m sphinx.cmd.build -qnNW -b doctest docs docs/_build/doctest 22 | -------------------------------------------------------------------------------- /flask_sitemap/version.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014, 2015, 2016 CERN. 5 | # Copyright (C) 2018 ETH Zurich, Swiss Data Science Center, Jiri Kuncar. 6 | # 7 | # Flask-Sitemap is free software; you can redistribute it and/or modify 8 | # it under the terms of the Revised BSD License; see LICENSE file for 9 | # more details. 10 | 11 | """Version information for Flask-Sitemap. 12 | 13 | This file is imported by ``flask_sitemap.__init__``, and parsed by 14 | ``setup.py`` as well as ``docs/conf.py``. 15 | """ 16 | 17 | # Do not change the format of this next line. Doing so risks breaking 18 | # setup.py and docs/conf.py 19 | 20 | __version__ = "0.4.0" 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | bin/ 10 | build/ 11 | develop-eggs/ 12 | dist/ 13 | eggs/ 14 | lib/ 15 | lib64/ 16 | parts/ 17 | sdist/ 18 | var/ 19 | *.egg-info/ 20 | .eggs/ 21 | .installed.cfg 22 | *.egg 23 | 24 | # Installer logs 25 | pip-log.txt 26 | pip-delete-this-directory.txt 27 | 28 | # Unit test / coverage reports 29 | .tox/ 30 | .coverage 31 | .cache 32 | nosetests.xml 33 | coverage.xml 34 | 35 | # Translations 36 | *.mo 37 | 38 | # Mr Developer 39 | .mr.developer.cfg 40 | .project 41 | .pydevproject 42 | 43 | # Rope 44 | .ropeproject 45 | 46 | # Django stuff: 47 | *.log 48 | *.pot 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | -------------------------------------------------------------------------------- /flask_sitemap/templates/flask_sitemap/sitemap.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | {%- for url in urlset %} 4 | 5 | {{ url.loc }} 6 | {%- if url.lastmod %} 7 | {{ url.lastmod }} 8 | {%- endif %} 9 | {%- if url.changefreq %} 10 | {{ url.changefreq }} 11 | {%- endif %} 12 | {%- if url.priority %} 13 | {{ url.priority }} 14 | {%- endif %} 15 | 16 | {%- endfor %} 17 | 18 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014, 2015, 2016 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | include *.rst 11 | include *.sh 12 | include .coveragerc 13 | include .lgtm MAINTAINERS 14 | include LICENSE AUTHORS CHANGES 15 | include docs/*.rst docs/*.py docs/Makefile docs/requirements.txt 16 | include pytest.ini 17 | include tests/*.py 18 | include tox.ini 19 | recursive-include docs/_templates *.html 20 | recursive-include docs/_themes *.py *.css *.css_t *.conf *.html LICENSE README 21 | recursive-include flask_sitemap *.xml 22 | recursive-include tests *.xml 23 | recursive-include .github/workflows *.yml 24 | -------------------------------------------------------------------------------- /tests/helpers.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | """Test helpers.""" 11 | 12 | from unittest import TestCase 13 | 14 | from flask import Flask 15 | 16 | 17 | class FlaskTestCase(TestCase): 18 | """Mix-in class for creating the Flask application.""" 19 | 20 | def setUp(self): 21 | """Test setup.""" 22 | app = Flask(__name__) 23 | app.config['DEBUG'] = True 24 | app.config['TESTING'] = True 25 | app.logger.disabled = True 26 | self.app = app 27 | 28 | 29 | def dummy_decorator(dummy): 30 | """Dummy decorator.""" 31 | return lambda *args, **kwargs: 'dummy' 32 | -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | Contributing 2 | ============ 3 | 4 | Bug reports, feature requests, and other contributions are welcome. 5 | If you find a demonstrable problem that is caused by the code of this 6 | library, please: 7 | 8 | 1. Search for `already reported problems 9 | `_. 10 | 2. Check if the issue has been fixed or is still reproducible on the 11 | latest `master` branch. 12 | 3. Create an issue with **a test case**. 13 | 14 | If you create a feature branch, you can run the tests to ensure everything is 15 | operating correctly: 16 | 17 | .. code-block:: console 18 | 19 | $ ./run-tests.sh 20 | ... 21 | Ran 8 tests in 0.246s 22 | 23 | OK 24 | Name Stmts Miss Cover Missing 25 | -------------------------------------------------- 26 | flask_sitemap/__init__ 47 0 100% 27 | flask_sitemap/config 4 0 100% 28 | flask_sitemap/version 2 0 100% 29 | -------------------------------------------------- 30 | TOTAL 53 0 100% 31 | -------------------------------------------------------------------------------- /.github/workflows/pypi-release.yml: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Invenio. 4 | # Copyright (C) 2020 CERN. 5 | # 6 | # Invenio is free software; you can redistribute it and/or modify it 7 | # under the terms of the MIT License; see LICENSE file for more details 8 | 9 | name: Publish 10 | 11 | on: 12 | push: 13 | tags: 14 | - v* 15 | 16 | jobs: 17 | Publish: 18 | runs-on: ubuntu-20.04 19 | 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@v2 23 | 24 | - name: Set up Python 25 | uses: actions/setup-python@v2 26 | with: 27 | python-version: 3.8 28 | 29 | - name: Install dependencies 30 | run: | 31 | python -m pip install --upgrade pip 32 | pip install setuptools wheel 33 | 34 | - name: Build package 35 | run: python setup.py sdist bdist_wheel 36 | 37 | - name: Publish on PyPI 38 | uses: pypa/gh-action-pypi-publish@v1.13.0 39 | with: 40 | user: __token__ 41 | # The token is provided by the inveniosoftware organization 42 | password: ${{ secrets.pypi_token }} 43 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | =============== 2 | Flask-Sitemap 3 | =============== 4 | 5 | .. image:: https://github.com/inveniosoftware/flask-sitemap/workflows/CI/badge.svg 6 | :target: https://github.com/inveniosoftware/flask-sitemap/actions 7 | 8 | .. image:: https://img.shields.io/coveralls/inveniosoftware/flask-sitemap.svg 9 | :target: https://coveralls.io/r/inveniosoftware/flask-sitemap 10 | 11 | .. image:: https://img.shields.io/github/tag/inveniosoftware/flask-sitemap.svg 12 | :target: https://github.com/inveniosoftware/flask-sitemap/releases 13 | 14 | .. image:: https://img.shields.io/pypi/dm/flask-sitemap.svg 15 | :target: https://pypi.python.org/pypi/flask-sitemap 16 | 17 | .. image:: https://img.shields.io/github/license/inveniosoftware/flask-sitemap.svg 18 | :target: https://github.com/inveniosoftware/flask-sitemap/blob/master/LICENSE 19 | 20 | 21 | About 22 | ===== 23 | 24 | Flask-Sitemap is a Flask extension helping with sitemap generation. 25 | 26 | Installation 27 | ============ 28 | 29 | Flask-Sitemap is on PyPI so all you need is: :: 30 | 31 | pip install Flask-Sitemap 32 | 33 | Documentation 34 | ============= 35 | 36 | Documentation is readable at http://flask-sitemap.readthedocs.io or can 37 | be built using Sphinx: :: 38 | 39 | git submodule init 40 | git submodule update 41 | pip install Sphinx 42 | python setup.py build_sphinx 43 | 44 | Testing 45 | ======= 46 | 47 | Running the test suite is as simple as: :: 48 | 49 | python setup.py test 50 | 51 | or, to also show code coverage: :: 52 | 53 | ./run-tests.sh 54 | -------------------------------------------------------------------------------- /flask_sitemap/cli.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2018 ETH Zurich, Swiss Data Science Center, Jiri Kuncar. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | """Simple command line interface for sitemap generation. 11 | 12 | .. versionadded:: 0.3.0 13 | Integration with Flask 0.11+ using Click library. 14 | """ 15 | 16 | import codecs 17 | import os 18 | import warnings 19 | 20 | import click 21 | from flask import current_app, url_for 22 | 23 | try: 24 | from flask.cli import with_appcontext 25 | except ImportError: # pragma: no cover 26 | from flask_cli import with_appcontext 27 | 28 | from flask_sitemap import sitemap_page_needed 29 | 30 | 31 | @click.command() 32 | @click.option( 33 | '--output-directory', '-o', default='.', 34 | help='Output directory for sitemap files.' 35 | ) 36 | @click.option('--verbose', '-v', is_flag=True) 37 | @with_appcontext 38 | def sitemap(output_directory, verbose): 39 | """Generate static sitemap to given directory.""" 40 | sitemap = current_app.extensions['sitemap'] 41 | 42 | @sitemap_page_needed.connect 43 | def generate_page(app, page=1, urlset=None): 44 | filename = url_for('flask_sitemap.page', page=page).split('/')[-1] 45 | with codecs.open(os.path.join(output_directory, filename), 'w', 46 | 'utf-8') as f: 47 | f.write(sitemap.render_page(urlset=urlset)) 48 | if verbose: 49 | click.echo(filename) 50 | 51 | filename = url_for('flask_sitemap.sitemap').split('/')[-1] 52 | with codecs.open(os.path.join(output_directory, filename), 'w', 53 | 'utf-8') as f: 54 | f.write(sitemap.sitemap()) 55 | if verbose: 56 | click.echo(filename) 57 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Flask-Sitemap is free software; you can redistribute it and/or modify it 2 | under the terms of the Revised BSD License quoted below. 3 | 4 | Copyright (C) 2013, 2014 CERN. 5 | 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted provided that the following conditions are 10 | met: 11 | 12 | * Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | 15 | * Redistributions in binary form must reproduce the above copyright 16 | notice, this list of conditions and the following disclaimer in the 17 | documentation and/or other materials provided with the distribution. 18 | 19 | * Neither the name of the copyright holder nor the names of its 20 | contributors may be used to endorse or promote products derived from 21 | this software without specific prior written permission. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 30 | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 32 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 33 | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 34 | DAMAGE. 35 | 36 | In applying this license, CERN does not waive the privileges and 37 | immunities granted to it by virtue of its status as an 38 | Intergovernmental Organization or submit itself to any jurisdiction. 39 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Invenio. 4 | # Copyright (C) 2020 CERN. 5 | # 6 | # Invenio is free software; you can redistribute it and/or modify it 7 | # under the terms of the MIT License; see LICENSE file for more details. 8 | 9 | name: CI 10 | 11 | on: 12 | push: 13 | branches: master 14 | pull_request: 15 | branches: master 16 | schedule: 17 | # * is a special character in YAML so you have to quote this string 18 | - cron: '0 3 * * 6' 19 | workflow_dispatch: 20 | inputs: 21 | reason: 22 | description: 'Reason' 23 | required: false 24 | default: 'Manual trigger' 25 | 26 | jobs: 27 | Tests: 28 | runs-on: ubuntu-20.04 29 | strategy: 30 | matrix: 31 | python-version: [3.6, 3.7, 3.8] 32 | requirements-level: [min, pypi] 33 | 34 | steps: 35 | - name: Checkout 36 | uses: actions/checkout@v2 37 | 38 | - name: Set up Python ${{ matrix.python-version }} 39 | uses: actions/setup-python@v2 40 | with: 41 | python-version: ${{ matrix.python-version }} 42 | 43 | - name: Generate dependencies 44 | run: | 45 | python -m pip install --upgrade pip setuptools py wheel requirements-builder 46 | requirements-builder -e all --level=${{ matrix.requirements-level }} setup.py > .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt 47 | 48 | - name: Cache pip 49 | uses: actions/cache@v2 50 | with: 51 | path: ~/.cache/pip 52 | key: ${{ runner.os }}-pip-${{ hashFiles('.${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt') }} 53 | 54 | - name: Install dependencies 55 | run: | 56 | pip install -r .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt 57 | pip install .[all] 58 | pip freeze 59 | 60 | - name: Run tests 61 | run: | 62 | ./run-tests.sh 63 | -------------------------------------------------------------------------------- /flask_sitemap/config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014, 2015 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | """The details of the application settings that can be customized. 11 | 12 | SITEMAP_URL_SCHEME 13 | ------------------ 14 | 15 | Default: ``http``. 16 | 17 | SITEMAP_BLUEPRINT 18 | ----------------- 19 | 20 | If ``None`` or ``False`` then the Blueprint is not registered. 21 | 22 | Default: ``flask_sitemap``. 23 | 24 | SITEMAP_GZIP 25 | ------------ 26 | 27 | Default: ``False``. 28 | 29 | SITEMAP_BLUEPRINT_URL_PREFIX 30 | ---------------------------- 31 | 32 | Default: ``/``. 33 | 34 | SITEMAP_ENDPOINT_URL 35 | -------------------- 36 | 37 | Return sitemap index or sitemap for pages with less than 38 | ``SITEMAP_MAX_URL_COUNT`` urls. 39 | 40 | Default: ``/sitemap.xml``. 41 | 42 | SITEMAP_ENDPOINT_PAGE_URL 43 | ------------------------- 44 | 45 | Return GZipped sitemap for given page range of urls. 46 | 47 | .. note:: It is strongly recommended to provide caching decorator. 48 | 49 | Default: ``/sitemap.xml`` 50 | 51 | SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS 52 | ------------------------------------ 53 | 54 | Default: ``False``. 55 | 56 | SITEMAP_IGNORE_ENDPOINTS 57 | ------------------------ 58 | 59 | Default: ``None``. 60 | 61 | SITEMAP_VIEW_DECORATORS 62 | ----------------------- 63 | 64 | Default: ``[]``. 65 | 66 | SITEMAP_MAX_URL_COUNT 67 | --------------------- 68 | 69 | The maximum number of urls per one sitemap file can be up to 50000, however 70 | there is 10MB limitation for the file. 71 | 72 | Default: ``10000``. 73 | """ 74 | 75 | SITEMAP_BLUEPRINT = 'flask_sitemap' 76 | 77 | SITEMAP_BLUEPRINT_URL_PREFIX = '/' 78 | 79 | SITEMAP_ENDPOINT_URL = '/sitemap.xml' 80 | 81 | SITEMAP_ENDPOINT_PAGE_URL = '/sitemap.xml' 82 | 83 | SITEMAP_GZIP = False 84 | 85 | SITEMAP_URL_SCHEME = 'http' 86 | 87 | SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS = False 88 | 89 | SITEMAP_IGNORE_ENDPOINTS = None 90 | 91 | SITEMAP_VIEW_DECORATORS = [] 92 | 93 | SITEMAP_MAX_URL_COUNT = 10000 94 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | Here you can see the full list of changes between each Flask-Sitemap 5 | release. 6 | 7 | Version 0.4.0 (released 2022-02-16) 8 | ----------------------------------- 9 | 10 | - Adds Python 3.10 support 11 | - Removes Flask-Script library in favor of click. 12 | 13 | Version 0.3.0 (released 2018-05-02) 14 | ----------------------------------- 15 | 16 | New features 17 | ~~~~~~~~~~~~ 18 | 19 | - Adds integration with Click library for Flask 0.11+. 20 | 21 | Improved features 22 | ~~~~~~~~~~~~~~~~~ 23 | 24 | - Improves exclusion of specific URLs without parameters 25 | from to the sitemap. (closes #24) (closes #25) (closes #26) 26 | 27 | Bug fixes 28 | ~~~~~~~~~ 29 | 30 | - Reuses exiting request context if it exits. (closes #35) 31 | - Prepends '/' to endpoint urls for compatibility with Flask 1.0. 32 | - Improves documentation about ``SITEMAP_URL_SCHEME``. 33 | - Fixes typo in ``SITEMAP_VIEW_DECORATORS``. 34 | 35 | Notes 36 | ~~~~~ 37 | 38 | - Removes support for Python 2.6 and 3.3. 39 | 40 | Version 0.2.0 (released 2016-01-05) 41 | ----------------------------------- 42 | 43 | New features 44 | ~~~~~~~~~~~~ 45 | 46 | - Adds new command line interface to generate sitemap. (#13) 47 | 48 | Improved features 49 | ~~~~~~~~~~~~~~~~~ 50 | 51 | - Adds 'application/octet-stream' content type to GZipped response. 52 | (#20) 53 | 54 | Bug fixes 55 | ~~~~~~~~~ 56 | 57 | - Uses pytest-runner as distutils command with dependency resolution. 58 | 59 | Notes 60 | ~~~~~ 61 | 62 | - Improves integration of PyTest and addresses problem with missing 63 | test requirements. (#15) 64 | - If you want to use command line interface install dependencies using 65 | `pip install flask-sitemap[cli]`. 66 | 67 | Version 0.1.0 (released 2015-02-03) 68 | ----------------------------------- 69 | 70 | - Initial public release. (#12) 71 | - Support for configurable gzip response. 72 | - Quickstart example for signals and caching. (#8) 73 | - Support for sitemap pages. (#3) 74 | - Adds an option ``SITEMAP_VIEW_DECORATORS`` for specifying list of view 75 | decorators. (#4) 76 | - Adds support for ignoring certain endpoints through 77 | ``SITEMAP_IGNORE_ENDPOINTS`` configuration option. (#2) 78 | - Adds new option to automatically include all endpoints without 79 | parameters. In order to enable this feature set 80 | ``SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS`` to ``True``. (#2) 81 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014, 2015 CERN. 5 | # Copyright (C) 2018 ETH Zurich, Swiss Data Science Center, Jiri Kuncar. 6 | # 7 | # Flask-Sitemap is free software; you can redistribute it and/or modify 8 | # it under the terms of the Revised BSD License; see LICENSE file for 9 | # more details. 10 | 11 | import os 12 | import re 13 | import sys 14 | 15 | from setuptools import setup 16 | 17 | # Get the version string. Cannot be done with import! 18 | with open(os.path.join('flask_sitemap', 'version.py'), 'rt') as f: 19 | version = re.search( 20 | '__version__\s*=\s*"(?P.*)"\n', 21 | f.read() 22 | ).group('version') 23 | 24 | tests_require = [ 25 | 'check-manifest>=0.42', 26 | 'coverage>=5.3,<6', 27 | 'pytest-cov>=2.10.1', 28 | 'pytest-flask>=1.0.0', 29 | 'pytest-isort>=1.2.0', 30 | 'pytest-pycodestyle>=2.2.0', 31 | 'pytest-pydocstyle>=2.2.0', 32 | 'pytest>=6,<7', 33 | ] 34 | 35 | extras_require = { 36 | 'docs': ['Sphinx==4.2.0'], 37 | 'tests': tests_require, 38 | } 39 | 40 | extras_require['all'] = [] 41 | for reqs in extras_require.values(): 42 | extras_require['all'].extend(reqs) 43 | 44 | setup( 45 | name='Flask-Sitemap', 46 | version=version, 47 | url='http://github.com/inveniosoftware/flask-sitemap/', 48 | license='BSD', 49 | author='Invenio collaboration', 50 | author_email='info@inveniosoftware.org', 51 | description='Flask extension that helps with sitemap generation.', 52 | long_description=open('README.rst').read(), 53 | packages=['flask_sitemap'], 54 | zip_safe=False, 55 | include_package_data=True, 56 | platforms='any', 57 | install_requires=[ 58 | 'Flask>=1.1', 59 | 'blinker>=1.3', 60 | ], 61 | extras_require=extras_require, 62 | tests_require=tests_require, 63 | classifiers=[ 64 | 'Environment :: Web Environment', 65 | 'Intended Audience :: Developers', 66 | 'License :: OSI Approved :: BSD License', 67 | 'Operating System :: OS Independent', 68 | 'Programming Language :: Python', 69 | 'Framework :: Flask', 70 | 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 71 | 'Topic :: Software Development :: Libraries :: Python Modules', 72 | 'Programming Language :: Python :: 3', 73 | 'Programming Language :: Python :: 3.6', 74 | 'Programming Language :: Python :: 3.7', 75 | 'Programming Language :: Python :: 3.8', 76 | 'Programming Language :: Python :: 3.9', 77 | 'Programming Language :: Python :: 3.10', 78 | 'Development Status :: 5 - Production/Stable' 79 | ], 80 | ) 81 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | =============== 2 | Flask-Sitemap 3 | =============== 4 | .. currentmodule:: flask_sitemap 5 | 6 | 7 | .. raw:: html 8 | 9 |

10 | 11 | travis-ci badge 13 | 14 | 15 | coveralls.io badge 17 | 18 |

19 | 20 | 21 | Flask-Sitemap is a Flask extension helping with sitemap generation. 22 | 23 | Contents 24 | ======== 25 | 26 | .. contents:: 27 | :local: 28 | :depth: 1 29 | :backlinks: none 30 | 31 | 32 | Installation 33 | ============ 34 | 35 | Flask-Sitemap is on PyPI so all you need is : 36 | 37 | .. code-block:: console 38 | 39 | $ pip install flask-sitemap 40 | 41 | The development version can be downloaded from `its page at GitHub 42 | `_. 43 | 44 | .. code-block:: console 45 | 46 | $ git clone https://github.com/inveniosoftware/flask-sitemap.git 47 | $ cd flask-sitemap 48 | $ python setup.py develop 49 | $ ./run-tests.sh 50 | 51 | Requirements 52 | ------------ 53 | 54 | Flask-Sitemap has the following dependencies: 55 | 56 | * `Flask `_ 57 | * `blinker `_ 58 | * `six `_ 59 | 60 | Flask-Sitemap requires Python version 2.6, 2.7 or 3.3+ 61 | 62 | 63 | Usage 64 | ===== 65 | 66 | This part of the documentation will show you how to get started in using 67 | Flask-Sitemap with Flask. 68 | 69 | This guide assumes you have successfully installed Flask-Sitemap and a working 70 | understanding of Flask. If not, follow the installation steps and read about 71 | Flask at http://flask.pocoo.org/docs/. 72 | 73 | Simple Example 74 | -------------- 75 | 76 | First, let's create the application and initialise the extension: 77 | 78 | .. code-block:: python 79 | 80 | from flask import Flask, session, redirect 81 | from flask_sitemap import Sitemap 82 | app = Flask("myapp") 83 | ext = Sitemap(app=app) 84 | 85 | @app.route('/') 86 | def index(): 87 | pass 88 | 89 | @ext.register_generator 90 | def index(): 91 | # Not needed if you set SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS=True 92 | yield 'index', {} 93 | 94 | if __name__ == '__main__': 95 | app.run(debug=True) 96 | 97 | If you save the above as ``app.py``, you can run the example 98 | application using your Python interpreter: 99 | 100 | .. code-block:: console 101 | 102 | $ python app.py 103 | * Running on http://127.0.0.1:5000/ 104 | 105 | and you can observe generated sitemap on the example pages: 106 | 107 | .. code-block:: console 108 | 109 | $ firefox http://127.0.0.1:5000/ 110 | $ firefox http://127.0.0.1:5000/sitemap.xml 111 | 112 | You should now be able to emulate this example in your own Flask 113 | applications. For more information, please read the :ref:`indexpage` 114 | guide, the :ref:`caching` guide, and peruse the :ref:`api`. 115 | 116 | 117 | .. _indexpage: 118 | 119 | Index Page 120 | ---------- 121 | 122 | By default, a sitemap contains set of urls up to 123 | ``SITEMAP_MAX_URL_COUNT``. When the limit is reached a sitemap index file 124 | with list of sitemaps is served instead. In order to ease :ref:`caching` 125 | of sitemaps a signal ``sitemap_page_needed`` is fired with current 126 | application object, page number and url generator. 127 | 128 | 129 | .. _caching: 130 | 131 | Caching 132 | ------- 133 | 134 | Large sites should implement caching for their sitemaps. The following 135 | example shows an basic in-memory cache that can be replaced by 136 | *Flask-Cache*. 137 | 138 | .. code-block:: python 139 | 140 | from functools import wraps 141 | from flask_sitemap import Sitemap, sitemap_page_needed 142 | 143 | cache = {} # replace by *Flask-Cache* instance or similar 144 | 145 | @sitemap_page_needed.connect 146 | def create_page(app, page, urlset): 147 | cache[page] = sitemap.render_page(urlset=urlset) 148 | 149 | def load_page(fn): 150 | @wraps(fn) 151 | def loader(*args, **kwargs): 152 | page = kwargs.get('page') 153 | data = cache.get(page) 154 | return data if data else fn(*args, **kwargs) 155 | return loader 156 | 157 | self.app.config['SITEMAP_MAX_URL_COUNT'] = 10 158 | self.app.config['SITEMAP_VIEW_DECORATORS'] = [load_page] 159 | 160 | sitemap = Sitemap(app=self.app) 161 | 162 | 163 | Configuration 164 | ============= 165 | 166 | .. automodule:: flask_sitemap.config 167 | 168 | 169 | .. _api: 170 | 171 | API 172 | === 173 | 174 | This documentation section is automatically generated from Flask-Sitemap's 175 | source code. 176 | 177 | Flask-Sitemap 178 | ------------- 179 | 180 | .. automodule:: flask_sitemap 181 | 182 | .. autoclass:: Sitemap 183 | :members: 184 | 185 | 186 | .. include:: ../CHANGES 187 | 188 | .. include:: ../CONTRIBUTING.rst 189 | 190 | 191 | License 192 | ======= 193 | 194 | .. include:: ../LICENSE 195 | 196 | .. include:: ../AUTHORS 197 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | # You can set these variables from the command line. 11 | SPHINXOPTS = 12 | SPHINXBUILD = sphinx-build 13 | PAPER = 14 | BUILDDIR = _build 15 | 16 | # User-friendly check for sphinx-build 17 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 18 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 19 | endif 20 | 21 | # Internal variables. 22 | PAPEROPT_a4 = -D latex_paper_size=a4 23 | PAPEROPT_letter = -D latex_paper_size=letter 24 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 25 | # the i18n builder cannot share the environment and doctrees with the others 26 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 27 | 28 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 29 | 30 | help: 31 | @echo "Please use \`make ' where is one of" 32 | @echo " html to make standalone HTML files" 33 | @echo " dirhtml to make HTML files named index.html in directories" 34 | @echo " singlehtml to make a single large HTML file" 35 | @echo " pickle to make pickle files" 36 | @echo " json to make JSON files" 37 | @echo " htmlhelp to make HTML files and a HTML help project" 38 | @echo " qthelp to make HTML files and a qthelp project" 39 | @echo " devhelp to make HTML files and a Devhelp project" 40 | @echo " epub to make an epub" 41 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 42 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 43 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 44 | @echo " text to make text files" 45 | @echo " man to make manual pages" 46 | @echo " texinfo to make Texinfo files" 47 | @echo " info to make Texinfo files and run them through makeinfo" 48 | @echo " gettext to make PO message catalogs" 49 | @echo " changes to make an overview of all changed/added/deprecated items" 50 | @echo " xml to make Docutils-native XML files" 51 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 52 | @echo " linkcheck to check all external links for integrity" 53 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 54 | 55 | clean: 56 | rm -rf $(BUILDDIR)/* 57 | 58 | html: 59 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 60 | @echo 61 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 62 | 63 | coverage: 64 | $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage 65 | @echo 66 | @echo "Build finished. The coverage pages are in $(BUILDDIR)/coverage/python.txt." 67 | 68 | dirhtml: 69 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 70 | @echo 71 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 72 | 73 | singlehtml: 74 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 75 | @echo 76 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 77 | 78 | pickle: 79 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 80 | @echo 81 | @echo "Build finished; now you can process the pickle files." 82 | 83 | json: 84 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 85 | @echo 86 | @echo "Build finished; now you can process the JSON files." 87 | 88 | htmlhelp: 89 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 90 | @echo 91 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 92 | ".hhp project file in $(BUILDDIR)/htmlhelp." 93 | 94 | qthelp: 95 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 96 | @echo 97 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 98 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 99 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Flask-Sitemap.qhcp" 100 | @echo "To view the help file:" 101 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Flask-Sitemap.qhc" 102 | 103 | devhelp: 104 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 105 | @echo 106 | @echo "Build finished." 107 | @echo "To view the help file:" 108 | @echo "# mkdir -p $$HOME/.local/share/devhelp/Flask-Sitemap" 109 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Flask-Sitemap" 110 | @echo "# devhelp" 111 | 112 | epub: 113 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 114 | @echo 115 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 116 | 117 | latex: 118 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 119 | @echo 120 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 121 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 122 | "(use \`make latexpdf' here to do that automatically)." 123 | 124 | latexpdf: 125 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 126 | @echo "Running LaTeX files through pdflatex..." 127 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 128 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 129 | 130 | latexpdfja: 131 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 132 | @echo "Running LaTeX files through platex and dvipdfmx..." 133 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 134 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 135 | 136 | text: 137 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 138 | @echo 139 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 140 | 141 | man: 142 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 143 | @echo 144 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 145 | 146 | texinfo: 147 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 148 | @echo 149 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 150 | @echo "Run \`make' in that directory to run these through makeinfo" \ 151 | "(use \`make info' here to do that automatically)." 152 | 153 | info: 154 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 155 | @echo "Running Texinfo files through makeinfo..." 156 | make -C $(BUILDDIR)/texinfo info 157 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 158 | 159 | gettext: 160 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 161 | @echo 162 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 163 | 164 | changes: 165 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 166 | @echo 167 | @echo "The overview file is in $(BUILDDIR)/changes." 168 | 169 | linkcheck: 170 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 171 | @echo 172 | @echo "Link check complete; look for any errors in the above output " \ 173 | "or in $(BUILDDIR)/linkcheck/output.txt." 174 | 175 | doctest: 176 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 177 | @echo "Testing of doctests in the sources finished, look at the " \ 178 | "results in $(BUILDDIR)/doctest/output.txt." 179 | 180 | xml: 181 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 182 | @echo 183 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 184 | 185 | pseudoxml: 186 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 187 | @echo 188 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 189 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | """Sphinx configuration.""" 11 | 12 | import os 13 | 14 | # If extensions (or modules to document with autodoc) are in another directory, 15 | # add these directories to sys.path here. If the directory is relative to the 16 | # documentation root, use os.path.abspath to make it absolute, like shown here. 17 | #sys.path.insert(0, os.path.abspath('.')) 18 | 19 | # -- General configuration ----------------------------------------------------- 20 | 21 | # If your documentation needs a minimal Sphinx version, state it here. 22 | #needs_sphinx = '1.0' 23 | 24 | # Add any Sphinx extension module names here, as strings. They can be extensions 25 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 26 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.coverage', 27 | 'sphinx.ext.doctest'] 28 | 29 | # Add any paths that contain templates here, relative to this directory. 30 | templates_path = ['_templates'] 31 | 32 | # The suffix of source filenames. 33 | source_suffix = '.rst' 34 | 35 | # The encoding of source files. 36 | #source_encoding = 'utf-8-sig' 37 | 38 | # The master toctree document. 39 | master_doc = 'index' 40 | 41 | # General information about the project. 42 | project = u'Flask-Sitemap' 43 | copyright = u'2014, CERN' 44 | 45 | # The version info for the project you're documenting, acts as replacement for 46 | # |version| and |release|, also used in various other places throughout the 47 | # built documents. 48 | # 49 | # The short X.Y version. 50 | 51 | # Get the version string. Cannot be done with import! 52 | g = {} 53 | with open(os.path.join(os.path.dirname(__file__), '..', 54 | 'flask_sitemap', 'version.py'), 55 | 'rt') as fp: 56 | exec(fp.read(), g) 57 | version = g['__version__'] 58 | 59 | # The full version, including alpha/beta/rc tags. 60 | release = version 61 | 62 | # The language for content autogenerated by Sphinx. Refer to documentation 63 | # for a list of supported languages. 64 | #language = None 65 | 66 | # There are two options for replacing |today|: either, you set today to some 67 | # non-false value, then it is used: 68 | #today = '' 69 | # Else, today_fmt is used as the format for a strftime call. 70 | #today_fmt = '%B %d, %Y' 71 | 72 | # List of patterns, relative to source directory, that match files and 73 | # directories to ignore when looking for source files. 74 | exclude_patterns = ['_build'] 75 | 76 | # The reST default role (used for this markup: `text`) to use for all documents. 77 | #default_role = None 78 | 79 | # If true, '()' will be appended to :func: etc. cross-reference text. 80 | #add_function_parentheses = True 81 | 82 | # If true, the current module name will be prepended to all description 83 | # unit titles (such as .. function::). 84 | #add_module_names = True 85 | 86 | # If true, sectionauthor and moduleauthor directives will be shown in the 87 | # output. They are ignored by default. 88 | #show_authors = False 89 | 90 | # The name of the Pygments (syntax highlighting) style to use. 91 | pygments_style = 'sphinx' 92 | 93 | # A list of ignored prefixes for module index sorting. 94 | #modindex_common_prefix = [] 95 | 96 | # If true, keep warnings as "system message" paragraphs in the built documents. 97 | #keep_warnings = False 98 | 99 | 100 | # -- Options for HTML output --------------------------------------------------- 101 | 102 | # The theme to use for HTML and HTML Help pages. See the documentation for 103 | # a list of builtin themes. 104 | # html_theme_path = ['_themes'] 105 | html_theme = 'alabaster' 106 | 107 | 108 | # Theme options are theme-specific and customize the look and feel of a theme 109 | # further. For a list of options available for each theme, see the 110 | # documentation. 111 | html_theme_options = { 112 | 'description': 'Sitemap XML.', 113 | 'github_user': 'inveniosoftware', 114 | 'github_repo': 'flask-sitemap', 115 | 'github_button': False, 116 | 'github_banner': True, 117 | 'show_powered_by': False, 118 | 'extra_nav_links': { 119 | 'flask-sitemap@GitHub': 'https://github.com/inveniosoftware/flask-sitemap', 120 | 'flask-sitemap@PyPI': 'https://pypi.python.org/pypi/flask-sitemap/', 121 | } 122 | } 123 | 124 | # Add any paths that contain custom themes here, relative to this directory. 125 | #html_theme_path = [] 126 | 127 | # The name for this set of Sphinx documents. If None, it defaults to 128 | # " v documentation". 129 | #html_title = None 130 | 131 | # A shorter title for the navigation bar. Default is the same as html_title. 132 | #html_short_title = None 133 | 134 | # The name of an image file (relative to this directory) to place at the top 135 | # of the sidebar. 136 | #html_logo = None 137 | 138 | # The name of an image file (within the static path) to use as favicon of the 139 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 140 | # pixels large. 141 | #html_favicon = None 142 | 143 | # Add any paths that contain custom static files (such as style sheets) here, 144 | # relative to this directory. They are copied after the builtin static files, 145 | # so a file named "default.css" will overwrite the builtin "default.css". 146 | #html_static_path = ['_static'] 147 | 148 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 149 | # using the given strftime format. 150 | #html_last_updated_fmt = '%b %d, %Y' 151 | 152 | # If true, SmartyPants will be used to convert quotes and dashes to 153 | # typographically correct entities. 154 | #html_use_smartypants = True 155 | 156 | # Custom sidebar templates, maps document names to template names. 157 | html_sidebars = { 158 | '**': [ 159 | 'about.html', 160 | 'navigation.html', 161 | 'relations.html', 162 | 'searchbox.html', 163 | 'donate.html', 164 | ] 165 | } 166 | 167 | # Additional templates that should be rendered to pages, maps page names to 168 | # template names. 169 | #html_additional_pages = {} 170 | 171 | # If false, no module index is generated. 172 | #html_domain_indices = True 173 | 174 | # If false, no index is generated. 175 | #html_use_index = True 176 | 177 | # If true, the index is split into individual pages for each letter. 178 | #html_split_index = False 179 | 180 | # If true, links to the reST sources are added to the pages. 181 | #html_show_sourcelink = True 182 | 183 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 184 | #html_show_sphinx = True 185 | 186 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 187 | #html_show_copyright = True 188 | 189 | # If true, an OpenSearch description file will be output, and all pages will 190 | # contain a tag referring to it. The value of this option must be the 191 | # base URL from which the finished HTML is served. 192 | #html_use_opensearch = '' 193 | 194 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 195 | #html_file_suffix = None 196 | 197 | # Output file base name for HTML help builder. 198 | htmlhelp_basename = 'Flask-Sitemapdoc' 199 | 200 | 201 | # -- Options for LaTeX output -------------------------------------------------- 202 | 203 | latex_elements = { 204 | # The paper size ('letterpaper' or 'a4paper'). 205 | #'papersize': 'letterpaper', 206 | 207 | # The font size ('10pt', '11pt' or '12pt'). 208 | #'pointsize': '10pt', 209 | 210 | # Additional stuff for the LaTeX preamble. 211 | #'preamble': '', 212 | } 213 | 214 | # Grouping the document tree into LaTeX files. List of tuples 215 | # (source start file, target name, title, author, documentclass [howto/manual]). 216 | latex_documents = [ 217 | ('index', 'Flask-Sitemap.tex', u'Flask-Sitemap Documentation', 218 | u'CERN', 'manual'), 219 | ] 220 | 221 | # The name of an image file (relative to this directory) to place at the top of 222 | # the title page. 223 | #latex_logo = None 224 | 225 | # For "manual" documents, if this is true, then toplevel headings are parts, 226 | # not chapters. 227 | #latex_use_parts = False 228 | 229 | # If true, show page references after internal links. 230 | #latex_show_pagerefs = False 231 | 232 | # If true, show URL addresses after external links. 233 | #latex_show_urls = False 234 | 235 | # Documents to append as an appendix to all manuals. 236 | #latex_appendices = [] 237 | 238 | # If false, no module index is generated. 239 | #latex_domain_indices = True 240 | 241 | 242 | # -- Options for manual page output -------------------------------------------- 243 | 244 | # One entry per manual page. List of tuples 245 | # (source start file, name, description, authors, manual section). 246 | man_pages = [ 247 | ('index', 'flask-sitemap', u'Flask-Sitemap Documentation', 248 | [u'CERN'], 1) 249 | ] 250 | 251 | # If true, show URL addresses after external links. 252 | #man_show_urls = False 253 | 254 | 255 | # -- Options for Texinfo output ------------------------------------------------ 256 | 257 | # Grouping the document tree into Texinfo files. List of tuples 258 | # (source start file, target name, title, author, 259 | # dir menu entry, description, category) 260 | texinfo_documents = [ 261 | ('index', 'Flask-Sitemap', u'Flask-Sitemap Documentation', 262 | u'CERN', 'Flask-Sitemap', 'One line description of project.', 263 | 'Miscellaneous'), 264 | ] 265 | 266 | # Documents to append as an appendix to all manuals. 267 | #texinfo_appendices = [] 268 | 269 | # If false, no module index is generated. 270 | #texinfo_domain_indices = True 271 | 272 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 273 | #texinfo_show_urls = 'footnote' 274 | 275 | # If true, do not generate a @detailmenu in the "Top" node's menu. 276 | #texinfo_no_detailmenu = False 277 | -------------------------------------------------------------------------------- /flask_sitemap/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014, 2015, 2016, 2017 CERN. 5 | # Copyright (C) 2018 ETH Zurich, Swiss Data Science Center, Jiri Kuncar. 6 | # 7 | # Flask-Sitemap is free software; you can redistribute it and/or modify 8 | # it under the terms of the Revised BSD License; see LICENSE file for 9 | # more details. 10 | 11 | """Flask extension for generating page ``/sitemap.xml``. 12 | 13 | Initialization of the extension: 14 | 15 | >>> from flask import Flask 16 | >>> from flask_sitemap import Sitemap 17 | >>> app = Flask('myapp') 18 | >>> ext = Sitemap(app=app) 19 | 20 | or alternatively using the factory pattern: 21 | 22 | >>> app = Flask('myapp') 23 | >>> ext = Sitemap() 24 | >>> ext.init_app(app) 25 | """ 26 | 27 | import gzip 28 | from collections.abc import Mapping 29 | from functools import wraps 30 | from io import BytesIO 31 | from itertools import islice, zip_longest 32 | 33 | from flask import (Blueprint, Response, current_app, has_request_context, 34 | make_response, render_template, url_for) 35 | from flask.signals import Namespace 36 | from werkzeug.utils import import_string 37 | 38 | from . import config 39 | from .version import __version__ 40 | 41 | # Signals 42 | _signals = Namespace() 43 | 44 | #: Sent when a sitemap index is generated and given page will need to be 45 | #: generated in the future from already calculated url set. 46 | sitemap_page_needed = _signals.signal('sitemap-page-needed') 47 | 48 | 49 | class Sitemap(object): 50 | """Flask extension implementation.""" 51 | 52 | def __init__(self, app=None, command_name='sitemap'): 53 | """Initialize login callback.""" 54 | self.decorators = [] 55 | self.url_generators = [self._routes_without_params] 56 | self.command_name = command_name 57 | 58 | if app is not None: 59 | self.init_app(app) 60 | 61 | def init_app(self, app, command_name=None): 62 | """Initialize a Flask application. 63 | 64 | :param app: Application to register. 65 | :param command_name: Register a Click command with this name, or 66 | skip if ``False``. 67 | 68 | .. versionadded:: 0.3.0 69 | The *command_name* parameter. 70 | """ 71 | self.app = app 72 | # Follow the Flask guidelines on usage of app.extensions 73 | if not hasattr(app, 'extensions'): 74 | app.extensions = {} 75 | if 'sitemap' in app.extensions: 76 | raise RuntimeError("Flask application already initialized") 77 | app.extensions['sitemap'] = self 78 | 79 | # Set default configuration 80 | for k in dir(config): 81 | if k.startswith('SITEMAP_'): 82 | self.app.config.setdefault(k, getattr(config, k)) 83 | 84 | # Set decorators from configuration 85 | for decorator in app.config.get('SITEMAP_VIEW_DECORATORS'): 86 | if isinstance(decorator, str): 87 | decorator = import_string(decorator) 88 | self.decorators.append(decorator) 89 | 90 | # Create and register Blueprint 91 | if app.config.get('SITEMAP_BLUEPRINT'): 92 | # Add custom `template_folder` 93 | self.blueprint = Blueprint(app.config.get('SITEMAP_BLUEPRINT'), 94 | __name__, template_folder='templates') 95 | 96 | self.blueprint.add_url_rule( 97 | app.config.get('SITEMAP_ENDPOINT_URL'), 98 | 'sitemap', 99 | self._decorate(self.sitemap) 100 | ) 101 | self.blueprint.add_url_rule( 102 | app.config.get('SITEMAP_ENDPOINT_PAGE_URL'), 103 | 'page', 104 | self._decorate(self.page) 105 | ) 106 | app.register_blueprint( 107 | self.blueprint, 108 | url_prefix=app.config.get('SITEMAP_BLUEPRINT_URL_PREFIX') 109 | ) 110 | 111 | if (command_name or (command_name is None and self.command_name)) \ 112 | and hasattr(app, 'cli'): 113 | from .cli import sitemap 114 | app.cli.add_command(sitemap, command_name or self.command_name) 115 | 116 | def _decorate(self, view): 117 | """Decorate view with given decorators.""" 118 | response = (self.gzip_response if self.app.config['SITEMAP_GZIP'] else 119 | self.xml_response) 120 | 121 | @wraps(view) 122 | def wrapper(*args, **kwargs): 123 | new_view = view 124 | for decorator in self.decorators: 125 | new_view = decorator(new_view) 126 | return response(new_view(*args, **kwargs)) 127 | return wrapper 128 | 129 | def sitemap(self): 130 | """Generate sitemap.xml.""" 131 | size = self.app.config['SITEMAP_MAX_URL_COUNT'] 132 | args = [iter(self._generate_all_urls())] * size 133 | run = zip_longest(*args) 134 | try: 135 | urlset = next(run) 136 | except StopIteration: 137 | # Special case with empty list of urls. 138 | urlset = [None] 139 | 140 | if urlset[-1] is None: 141 | return render_template('flask_sitemap/sitemap.xml', 142 | urlset=filter(None, urlset)) 143 | 144 | def pages(): 145 | kwargs = dict( 146 | _external=True, 147 | _scheme=self.app.config.get('SITEMAP_URL_SCHEME') 148 | ) 149 | kwargs['page'] = 1 150 | yield {'loc': url_for('flask_sitemap.page', **kwargs)} 151 | sitemap_page_needed.send(current_app._get_current_object(), 152 | page=1, urlset=urlset) 153 | for urlset_ in run: 154 | kwargs['page'] += 1 155 | yield {'loc': url_for('flask_sitemap.page', **kwargs)} 156 | sitemap_page_needed.send(current_app._get_current_object(), 157 | page=kwargs['page'], urlset=urlset_) 158 | 159 | return render_template('flask_sitemap/sitemapindex.xml', 160 | sitemaps=pages()) 161 | 162 | def render_page(self, urlset=None): 163 | """Render GZipped sitemap template with given url set.""" 164 | return render_template('flask_sitemap/sitemap.xml', 165 | urlset=urlset or []) 166 | 167 | def page(self, page): 168 | """Generate sitemap for given range of urls.""" 169 | size = self.app.config['SITEMAP_MAX_URL_COUNT'] 170 | urlset = islice(self._generate_all_urls(), (page-1)*size, page*size) 171 | return self.render_page(urlset=urlset) 172 | 173 | def register_generator(self, generator): 174 | """Register an URL generator. 175 | 176 | The function should return an iterable of URL paths or 177 | ``(endpoint, values)`` tuples to be used as 178 | ``url_for(endpoint, **values)``. 179 | 180 | :return: the original generator function 181 | """ 182 | self.url_generators.append(generator) 183 | # Allow use as a decorator 184 | return generator 185 | 186 | def _routes_without_params(self): 187 | if self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS']: 188 | ignore = set(self.app.config['SITEMAP_IGNORE_ENDPOINTS'] or []) 189 | for rule in self.app.url_map.iter_rules(): 190 | if rule.endpoint not in ignore and 'GET' in rule.methods and \ 191 | len(rule.arguments) == 0: 192 | yield rule.endpoint, {} 193 | 194 | def _generate_all_urls(self): 195 | """Run all generators and yield (url, enpoint) tuples.""" 196 | ignore = set(self.app.config['SITEMAP_IGNORE_ENDPOINTS'] or []) 197 | kwargs = dict( 198 | _external=True, 199 | _scheme=self.app.config.get('SITEMAP_URL_SCHEME') 200 | ) 201 | 202 | def generator(): 203 | for generator in self.url_generators: 204 | for generated in generator(): 205 | result = {} 206 | if isinstance(generated, str): 207 | result['loc'] = generated 208 | else: 209 | if isinstance(generated, Mapping): 210 | values = generated 211 | # The endpoint defaults to the name of the 212 | # generator function, just like with Flask views. 213 | endpoint = generator.__name__ 214 | else: 215 | # Assume a tuple. 216 | endpoint, values = generated[0:2] 217 | # Get optional lastmod, changefreq, and priority 218 | left = generated[2:] 219 | for key in ['lastmod', 'changefreq', 'priority']: 220 | if len(left) == 0: 221 | break 222 | result[key] = left[0] 223 | left = left[1:] 224 | 225 | # Check if the endpoint should be skipped 226 | if endpoint in ignore: 227 | continue 228 | 229 | values.update(kwargs) 230 | result['loc'] = url_for(endpoint, **values) 231 | yield result 232 | 233 | # A request context is required to use url_for 234 | if not has_request_context(): 235 | with self.app.test_request_context(): 236 | for result in generator(): 237 | yield result 238 | else: 239 | for result in generator(): 240 | yield result 241 | 242 | def gzip_response(self, data): 243 | """Gzip response data and create new Response instance.""" 244 | gzip_buffer = BytesIO() 245 | gzip_file = gzip.GzipFile(mode='wb', compresslevel=6, 246 | fileobj=gzip_buffer) 247 | gzip_file.write(data.encode('utf-8')) 248 | gzip_file.close() 249 | response = Response() 250 | response.data = gzip_buffer.getvalue() 251 | response.headers['Content-Type'] = 'application/octet-stream' 252 | response.headers['Content-Encoding'] = 'gzip' 253 | response.headers['Content-Length'] = len(response.data) 254 | 255 | return response 256 | 257 | def xml_response(self, data): 258 | """Return a standard XML response.""" 259 | response = make_response(data) 260 | response.headers["Content-Type"] = "application/xml" 261 | 262 | return response 263 | 264 | 265 | __all__ = ('Sitemap', '__version__', 'sitemap_page_needed') 266 | -------------------------------------------------------------------------------- /tests/test_ext.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # This file is part of Flask-Sitemap 4 | # Copyright (C) 2014, 2015 CERN. 5 | # 6 | # Flask-Sitemap is free software; you can redistribute it and/or modify 7 | # it under the terms of the Revised BSD License; see LICENSE file for 8 | # more details. 9 | 10 | from __future__ import absolute_import 11 | 12 | import os 13 | import shutil 14 | import sys 15 | from contextlib import contextmanager 16 | from datetime import datetime 17 | from tempfile import mkdtemp 18 | 19 | from click.testing import CliRunner 20 | from flask.cli import ScriptInfo 21 | 22 | from flask_sitemap import Sitemap 23 | from flask_sitemap import config as default_config 24 | from flask_sitemap import sitemap_page_needed 25 | 26 | from .helpers import FlaskTestCase 27 | 28 | 29 | class TestSitemap(FlaskTestCase): 30 | 31 | """Test extension creation and functionality.""" 32 | 33 | def test_version(self): 34 | # Assert that version number can be parsed. 35 | from distutils.version import LooseVersion 36 | 37 | from flask_sitemap import __version__ 38 | 39 | LooseVersion(__version__) 40 | 41 | def test_creation(self): 42 | assert 'sitemap' not in self.app.extensions 43 | Sitemap(app=self.app) 44 | assert isinstance(self.app.extensions['sitemap'], Sitemap) 45 | 46 | def test_creation_old_flask(self): 47 | # Simulate old Flask (pre 0.9) 48 | del self.app.extensions 49 | Sitemap(app=self.app) 50 | assert isinstance(self.app.extensions['sitemap'], Sitemap) 51 | 52 | def test_creation_init(self): 53 | assert 'sitemap' not in self.app.extensions 54 | r = Sitemap() 55 | r.init_app(app=self.app) 56 | assert isinstance(self.app.extensions['sitemap'], Sitemap) 57 | 58 | def test_double_creation(self): 59 | Sitemap(app=self.app) 60 | self.assertRaises(RuntimeError, Sitemap, app=self.app) 61 | 62 | def test_default_config(self): 63 | Sitemap(app=self.app) 64 | for k in dir(default_config): 65 | if k.startswith('SITEMAP_'): 66 | assert self.app.config.get(k) == getattr(default_config, k) 67 | 68 | def test_empty_generator(self): 69 | Sitemap(app=self.app) 70 | with self.app.test_client() as c: 71 | assert b'loc' not in c.get('/sitemap.xml').data 72 | 73 | def test_url_generator(self): 74 | self.app.config['SERVER_NAME'] = 'www.example.com' 75 | sitemap = Sitemap(app=self.app) 76 | now = datetime.now().isoformat() 77 | 78 | @self.app.route('/') 79 | def index(): 80 | pass 81 | 82 | @self.app.route('/') 83 | def user(username): 84 | pass 85 | 86 | @sitemap.register_generator 87 | def user(): 88 | yield 'http://www.example.com/first' 89 | yield {'username': 'second'} 90 | yield 'user', {'username': 'third'} 91 | yield 'user', {'username': 'fourth'}, now 92 | 93 | results = sitemap._generate_all_urls() 94 | assert next(results)['loc'] == 'http://www.example.com/first' 95 | assert next(results)['loc'] == 'http://www.example.com/second' 96 | assert next(results)['loc'] == 'http://www.example.com/third' 97 | assert next(results)['loc'] == 'http://www.example.com/fourth' 98 | 99 | with open(os.path.join( 100 | os.path.dirname(__file__), 'data', 'sitemap.xml'), 'r') as f: 101 | out = f.read().format(now=now).strip() 102 | assert out == sitemap.sitemap() 103 | 104 | def test_endpoints_without_arguments(self): 105 | self.app.config['SERVER_NAME'] = 'www.example.com' 106 | self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS'] = True 107 | sitemap = Sitemap(app=self.app) 108 | now = datetime.now().isoformat() 109 | 110 | @self.app.route('/') 111 | def index(): 112 | pass 113 | 114 | @self.app.route('/first') 115 | def first(): 116 | pass 117 | 118 | @self.app.route('/second') 119 | def second(): 120 | pass 121 | 122 | @self.app.route('/') 123 | def user(username): 124 | pass 125 | 126 | @sitemap.register_generator 127 | def user(): 128 | yield 'user', {'username': 'third'} 129 | yield 'user', {'username': 'fourth'}, now 130 | 131 | results = [result['loc'] for result in sitemap._generate_all_urls()] 132 | assert 'http://www.example.com/' in results 133 | assert 'http://www.example.com/first' in results 134 | assert 'http://www.example.com/second' in results 135 | assert 'http://www.example.com/third' in results 136 | assert 'http://www.example.com/fourth' in results 137 | 138 | def test_ignore_endpoints(self): 139 | self.app.config['SERVER_NAME'] = 'www.example.com' 140 | self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS'] = True 141 | self.app.config['SITEMAP_IGNORE_ENDPOINTS'] = ['first', 'user'] 142 | sitemap = Sitemap(app=self.app) 143 | now = datetime.now().isoformat() 144 | 145 | @self.app.route('/') 146 | def index(): 147 | pass 148 | 149 | @self.app.route('/first') 150 | def first(): 151 | pass 152 | 153 | @self.app.route('/second') 154 | def second(): 155 | pass 156 | 157 | @self.app.route('/') 158 | def user(username): 159 | pass 160 | 161 | @sitemap.register_generator 162 | def user(): 163 | yield 'user', {'username': 'third'} 164 | yield 'user', {'username': 'fourth'}, now 165 | 166 | results = [result['loc'] for result in sitemap._generate_all_urls()] 167 | assert 'http://www.example.com/' in results 168 | assert 'http://www.example.com/first' not in results 169 | assert 'http://www.example.com/second' in results 170 | assert 'http://www.example.com/third' not in results 171 | assert 'http://www.example.com/fourth' not in results 172 | 173 | def test_decorators_order(self): 174 | def first(dummy): 175 | return lambda *args, **kwargs: 'first' 176 | 177 | def second(dummy): 178 | return lambda *args, **kwargs: 'second' 179 | 180 | def third(dummy): 181 | return lambda *args, **kwargs: 'third' 182 | 183 | self.app.config['SITEMAP_VIEW_DECORATORS'] = [ 184 | first, second, 'tests.helpers.dummy_decorator'] 185 | sitemap = Sitemap(app=self.app) 186 | 187 | assert first in sitemap.decorators 188 | assert second in sitemap.decorators 189 | 190 | with self.app.test_client() as c: 191 | assert b'dummy' == c.get('/sitemap.xml').data 192 | 193 | sitemap.decorators.append(third) 194 | 195 | with self.app.test_client() as c: 196 | assert b'third' == c.get('/sitemap.xml').data 197 | 198 | def test_pagination(self): 199 | self.app.config['SERVER_NAME'] = 'www.example.com' 200 | self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS'] = True 201 | self.app.config['SITEMAP_MAX_URL_COUNT'] = 10 202 | sitemap = Sitemap(app=self.app) 203 | now = datetime.now().isoformat() 204 | 205 | @self.app.route('/') 206 | def index(): 207 | pass 208 | 209 | @self.app.route('/first') 210 | def first(): 211 | pass 212 | 213 | @self.app.route('/second') 214 | def second(): 215 | pass 216 | 217 | @self.app.route('/') 218 | def user(username): 219 | pass 220 | 221 | @sitemap.register_generator 222 | def user(): 223 | for number in range(20): 224 | yield 'user', {'username': 'test{0}'.format(number)} 225 | 226 | directory = mkdtemp() 227 | runner = CliRunner() 228 | 229 | try: 230 | result = runner.invoke( 231 | self.app.cli, 232 | ['sitemap', '-o', directory, '-v'], 233 | obj=ScriptInfo(create_app=lambda _: self.app), 234 | ) 235 | assert 'sitemap1.xml\nsitemap2.xml\nsitemap3.xml\nsitemap.xml' \ 236 | in result.output 237 | # assert result.exit_code == 0 238 | 239 | with self.app.test_client() as c: 240 | data = c.get('/sitemap.xml').data 241 | data1 = c.get('/sitemap1.xml').data 242 | 243 | assert b'sitemapindex' in data 244 | assert len(data1) > 0 245 | 246 | with open(os.path.join(directory, 'sitemap.xml'), 'rb') as f: 247 | assert f.read() == data 248 | 249 | with open(os.path.join(directory, 'sitemap1.xml'), 'rb') as f: 250 | assert f.read() == data1 251 | finally: 252 | shutil.rmtree(directory) 253 | 254 | def test_signals(self): 255 | now = datetime.now().isoformat() 256 | cache = {} 257 | 258 | @sitemap_page_needed.connect 259 | def create_page(app, page, urlset): 260 | cache[page] = sitemap.render_page(urlset=urlset) 261 | 262 | def load_page(fn): 263 | from functools import wraps 264 | 265 | @wraps(fn) 266 | def loader(*args, **kwargs): 267 | page = kwargs.get('page') 268 | data = cache.get(page) 269 | return data if data else fn(*args, **kwargs) 270 | return loader 271 | 272 | self.app.config['SERVER_NAME'] = 'www.example.com' 273 | self.app.config['SITEMAP_GZIP'] = True 274 | self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS'] = True 275 | self.app.config['SITEMAP_MAX_URL_COUNT'] = 10 276 | self.app.config['SITEMAP_VIEW_DECORATORS'] = [load_page] 277 | sitemap = Sitemap(app=self.app) 278 | 279 | @self.app.route('/') 280 | def index(): 281 | pass 282 | 283 | @self.app.route('/first') 284 | def first(): 285 | pass 286 | 287 | @self.app.route('/second') 288 | def second(): 289 | pass 290 | 291 | @self.app.route('/') 292 | def user(username): 293 | pass 294 | 295 | @sitemap.register_generator 296 | def user(): 297 | for number in range(20): 298 | yield 'user', {'username': 'test{0}'.format(number)} 299 | 300 | with self.app.test_client() as c: 301 | assert 200 == c.get('/sitemap.xml').status_code 302 | assert len(cache) == 3 303 | for page in range(len(cache)): 304 | assert sitemap.gzip_response(cache[page+1]).data == c.get( 305 | '/sitemap{0}.xml'.format(page+1)).data 306 | --------------------------------------------------------------------------------