# 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
    "returns True if the given article-json is valid POA v2 (no structured abstract)"
    return not has_structured_abstract(ajson)


def downgrade_poa_content_type(get_response_fn):
    """if a content-type less than the current VOR version is requested, downgrade content-type if possible or return a 406"""

    def middleware(request):
        request_accept_header = request.META.get("HTTP_ACCEPT", "*/*")
        response = get_response_fn(request)

        if response.status_code != 200:
            # unsuccessful response, ignore
            return response

        # are we returning POA content?
        poa_ctype = "application/vnd.elife.article-poa+json"
        resp_ctype = get_content_type(response)

        if poa_ctype not in resp_ctype:
            # this isn't a poa
            return response

        client_accepts_list = flatten_accept(request_accept_header)
        client_accepts_poa_list = [
            row for row in client_accepts_list if row[0] == poa_ctype
        ]

        # [1, 2, 3, ...]
        client_accepts_poa_versions = [

# 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

    "returns True if the given article-json is valid POA v2 (no structured abstract)"
    return not has_structured_abstract(ajson)


def downgrade_poa_content_type(get_response_fn):
    """if a content-type less than the current VOR version is requested, downgrade content-type if possible or return a 406"""

    def middleware(request):
        request_accept_header = request.META.get("HTTP_ACCEPT", "*/*")
        response = get_response_fn(request)

        if response.status_code != 200:
            # unsuccessful response, ignore
            return response

        # are we returning POA content?
        poa_ctype = "application/vnd.elife.article-poa+json"
        resp_ctype = get_content_type(response)

        if poa_ctype not in resp_ctype:
            # this isn't a poa
            return response

        client_accepts_list = flatten_accept(request_accept_header)
        client_accepts_poa_list = [
            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])