Python pyramid.response.FileResponse() Examples

The following are 6 code examples of pyramid.response.FileResponse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyramid.response , or try the search function .
Example #1
Source File: views.py    From kotori with GNU Affero General Public License v3.0 5 votes vote down vote up
def favicon_view(request):
    """
    http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/assets.html#registering-a-view-callable-to-serve-a-static-asset
    """
    icon = resource_filename('kotori.frontend', 'static/favicon.ico')
    if os.path.isfile(icon):
        return FileResponse(icon, request=request)
    else:
        return HTTPNotFound() 
Example #2
Source File: views.py    From patzilla with GNU Affero General Public License v3.0 5 votes vote down vote up
def favicon_view(request):
    # http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/assets.html#registering-a-view-callable-to-serve-a-static-asset
    icon = resource_filename('patzilla.navigator', 'static/favicon.ico')
    if os.path.isfile(icon):
        return FileResponse(icon, request=request)
    else:
        return HTTPNotFound() 
Example #3
Source File: uploads.py    From travelcrm with GNU General Public License v3.0 5 votes vote down vote up
def download(self):
        upload = Upload.get(self.request.params.get('id'))
        response = FileResponse(
            self.request.storage.base_path + '/' + upload.path,
            request=self.request,
        )
        return response 
Example #4
Source File: views.py    From muesli with GNU General Public License v3.0 5 votes vote down vote up
def favicon_view(request):
    here = os.path.dirname(__file__)
    icon = os.path.join(here, "static", "favicon.ico")
    return FileResponse(icon, request=request) 
Example #5
Source File: __init__.py    From pyramid_openapi3 with MIT License 4 votes vote down vote up
def add_spec_view(
    config: Configurator,
    filepath: str,
    route: str = "/openapi.yaml",
    route_name: str = "pyramid_openapi3.spec",
) -> None:
    """Serve and register OpenApi 3.0 specification file.

    :param filepath: absolute/relative path to the specification file
    :param route: URL path where to serve specification file
    :param route_name: Route name under which specification file will be served
    """

    def register() -> None:
        if hupper.is_active():  # pragma: no cover
            hupper.get_reloader().watch_files([filepath])
        spec_dict = read_yaml_file(filepath)

        validate_spec(spec_dict)
        spec = create_spec(spec_dict)

        def spec_view(request: Request) -> FileResponse:
            return FileResponse(filepath, request=request, content_type="text/yaml")

        config.add_route(route_name, route)
        config.add_view(route_name=route_name, view=spec_view)

        custom_formatters = config.registry.settings.get("pyramid_openapi3_formatters")

        config.registry.settings["pyramid_openapi3"] = {
            "filepath": filepath,
            "spec_route_name": route_name,
            "spec": spec,
            "request_validator": RequestValidator(
                spec, custom_formatters=custom_formatters
            ),
            "response_validator": ResponseValidator(
                spec, custom_formatters=custom_formatters
            ),
        }

    config.action(("pyramid_openapi3_spec",), register, order=PHASE0_CONFIG) 
Example #6
Source File: pdf.py    From thinkhazard with GNU General Public License v3.0 4 votes vote down vote up
def create_pdf_report(request):
    """View to create an asynchronous print job.
    """
    publication_date = request.publication_date
    locale = request.locale_name
    division_code = request.matchdict.get("divisioncode")
    force = "force" in request.params

    filename = "{:s}-{:s}.pdf".format(division_code, locale)
    s3_path = "reports/{:%Y-%m-%d}/{}".format(publication_date, filename)
    local_path = os.path.join(tempfile.gettempdir(), filename)

    if force or not request.s3_helper.download_file(s3_path, local_path):
        categories = (
            request.dbsession.query(HazardCategory)
            .options(joinedload(HazardCategory.hazardtype))
            .join(HazardCategoryAdministrativeDivisionAssociation)
            .join(AdministrativeDivision)
            .join(HazardLevel)
            .filter(AdministrativeDivision.code == division_code)
            .order_by(HazardLevel.order)
        )
        query_args = {"_query": {"_LOCALE_": request.locale_name}}
        pages = [
            request.route_url("pdf_cover", divisioncode=division_code, **query_args),
            request.route_url("pdf_about", **query_args),
        ]
        for cat in categories:
            pages.append(
                request.route_url(
                    "report_print",
                    divisioncode=division_code,
                    hazardtype=cat.hazardtype.mnemonic,
                    **query_args,
                )
            )
        run(create_and_upload_pdf(local_path, pages, s3_path, request.s3_helper))

    response = FileResponse(local_path, request=request, content_type="application/pdf")
    response.headers["Content-Disposition"] = (
        'attachment; filename="ThinkHazard.pdf"'
    )
    return response