# Path: src/publisher/utils.py
# def isint(v):
#     try:
#         int(v)
#         return True
#     except (ValueError, TypeError):
#         return False
# 
# Path: src/publisher/api_v2_views.py
# def flatten_accept(accepts_header_str):
#     "returns a list of triples like [(mime, 'version', version), ...]"
#     lst = []
#     if not accepts_header_str:
#         return lst
#     for mime in accepts_header_str.split(","):
#         # ('application/vnd.elife.article-vor+json', {'version': 2})
#         parsed_mime, parsed_params = parse_header(mime.encode())
#         # ('*/*', 'version', None)
#         # ('application/json', 'version', None)
#         # ('application/vnd.elife.article-poa+json', 'version', 2)
#         version = parsed_params.pop("version", b"").decode("utf-8")
#         lst.append((parsed_mime, "version", version or None))
#     return lst
# 
# def _ctype(content_type_key):
#     "returns a content type for the given `content_type_key`."
#     assert content_type_key in settings.CONTENT_TYPES
#     return "application/vnd.elife.article-%s+json" % content_type_key
# 
# def http_406():
#     "returns a HTTP 406 json error response (couldn't negotiate content type with request)."
#     title = "not acceptable"
#     detail = "could not negotiate an acceptable response type"
#     return error_response(406, title, detail)


from django.conf import settings
from .utils import isint
from .api_v2_views import flatten_accept, _ctype, http_406
import json
import logging
            row for row in client_accepts_list if row[0] == poa_ctype
        ]

        # [1, 2, 3, ...]
        client_accepts_poa_versions = [
            int(row[-1]) for row in client_accepts_poa_list if isint(row[-1])
        ]

        if not client_accepts_poa_versions:
            # no specific version specified, return latest version
            return response

        max_accepted_poa = max(client_accepts_poa_versions)
        current_poa_version = settings.ALL_SCHEMA_IDX["poa"][0][0]

        if max_accepted_poa == current_poa_version:
            # user requested the current latest POA version
            return response

        if max_accepted_poa == 2:
            # client specifically accepts a v2 POA only (deprecated)
            # we might be ok if the content is valid under v3 and v2
            body = json.loads(response.content.decode("utf-8"))
            if poa_valid_under_v2(body):
                # all good, drop content-type returned to POA v2
                new_content_type = "application/vnd.elife.article-poa+json; version=2"
                response["Content-Type"] = new_content_type
                return response

        # an unsupported POA version was requested

# Path: src/publisher/utils.py
# def isint(v):
#     try:
#         int(v)
#         return True
#     except (ValueError, TypeError):
#         return False
# 
# Path: src/publisher/api_v2_views.py
# def flatten_accept(accepts_header_str):
#     "returns a list of triples like [(mime, 'version', version), ...]"
#     lst = []
#     if not accepts_header_str:
#         return lst
#     for mime in accepts_header_str.split(","):
#         # ('application/vnd.elife.article-vor+json', {'version': 2})
#         parsed_mime, parsed_params = parse_header(mime.encode())
#         # ('*/*', 'version', None)
#         # ('application/json', 'version', None)
#         # ('application/vnd.elife.article-poa+json', 'version', 2)
#         version = parsed_params.pop("version", b"").decode("utf-8")
#         lst.append((parsed_mime, "version", version or None))
#     return lst
# 
# def _ctype(content_type_key):
#     "returns a content type for the given `content_type_key`."
#     assert content_type_key in settings.CONTENT_TYPES
#     return "application/vnd.elife.article-%s+json" % content_type_key
# 
# def http_406():
#     "returns a HTTP 406 json error response (couldn't negotiate content type with request)."
#     title = "not acceptable"
#     detail = "could not negotiate an acceptable response type"
#     return error_response(406, title, detail)


# Path: src/publisher/middleware.py
from django.conf import settings
from .utils import isint
from .api_v2_views import flatten_accept, _ctype, http_406
import json
import logging

            row for row in client_accepts_list if row[0] == poa_ctype
        ]

        # [1, 2, 3, ...]
        client_accepts_poa_versions = [
            int(row[-1]) for row in client_accepts_poa_list if isint(row[-1])
        ]

        if not client_accepts_poa_versions:
            # no specific version specified, return latest version
            return response

        max_accepted_poa = max(client_accepts_poa_versions)
        current_poa_version = settings.ALL_SCHEMA_IDX["poa"][0][0]

        if max_accepted_poa == current_poa_version:
            # user requested the current latest POA version
            return response

        if max_accepted_poa == 2:
            # client specifically accepts a v2 POA only (deprecated)
            # we might be ok if the content is valid under v3 and v2
            body = json.loads(response.content.decode("utf-8"))
            if poa_valid_under_v2(body):
                # all good, drop content-type returned to POA v2
                new_content_type = "application/vnd.elife.article-poa+json; version=2"
                response["Content-Type"] = new_content_type
                return response

        # an unsupported POA version was requested

        return http_406()