# 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
DEPRECATED_CONTENT_TYPES = []
for tpe, rows in settings.ALL_SCHEMA_IDX.items():
    if len(rows) > 1:
        for deprecated_version, _ in rows[1:]:
            # stringify version because that is what `flatten_accept` returns
            deprecated = (_ctype(tpe), "version", str(deprecated_version))
            DEPRECATED_CONTENT_TYPES.append(deprecated)


def is_deprecated(accepts_header_str):
    "returns `True` if *any* of the mime types in the parsed 'accepts' header string are deprecated."
    accepts = flatten_accept(accepts_header_str)
    for target in DEPRECATED_CONTENT_TYPES:
        if target in accepts:
            return True


#
# middleware
#


def mark_deprecated(get_response_fn):
    """returns `True` if *any* of the *requested* content types are deprecated.
    lsh@2021-07-6: should probably be changed to test the negotiated content type."""

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

# 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

DEPRECATED_CONTENT_TYPES = []
for tpe, rows in settings.ALL_SCHEMA_IDX.items():
    if len(rows) > 1:
        for deprecated_version, _ in rows[1:]:
            # stringify version because that is what `flatten_accept` returns
            deprecated = (_ctype(tpe), "version", str(deprecated_version))
            DEPRECATED_CONTENT_TYPES.append(deprecated)


def is_deprecated(accepts_header_str):
    "returns `True` if *any* of the mime types in the parsed 'accepts' header string are deprecated."
    accepts = flatten_accept(accepts_header_str)
    for target in DEPRECATED_CONTENT_TYPES:
        if target in accepts:
            return True


#
# middleware
#


def mark_deprecated(get_response_fn):
    """returns `True` if *any* of the *requested* content types are deprecated.
    lsh@2021-07-6: should probably be changed to test the negotiated content type."""

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

            msg = "Deprecation: Support for this Content-Type version will be removed"