-
Notifications
You must be signed in to change notification settings - Fork 56
/
__init__.py
73 lines (59 loc) · 2.21 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Handle requests to support sequential navigation between arXiv IDs."""
from flask import url_for, escape
from typing import Tuple, Dict, Any
from werkzeug.exceptions import BadRequest
from browse.domain.identifier import Identifier, IdentifierException
from browse.services.database import get_sequential_id
from arxiv import status
from arxiv.taxonomy.definitions import ARCHIVES, CATEGORIES_ACTIVE
from arxiv.base import logging
Response = Tuple[Dict[str, Any], int, Dict[str, Any]]
logger = logging.getLogger(__name__)
def get_prevnext(id: str, function: str, context: str) -> Response:
"""
Get the next or previous arXiv ID in the browse context.
The 'site' parameter from the classic prevnext is no longer supported.
Parameters
----------
id
arxiv id
function
prev or next
context
which archive or category to browse
Returns
-------
dict
Result response data.
int
HTTP status code.
dict
Headers to add to the response.
Raises
------
BadRequest
Raised when request parameters are missing, invalid, or when an ID
redirect cannot be returned even when the request parameters are valid.
"""
if id is None or not id:
raise BadRequest('Missing article identifier')
if function not in ['prev', 'next']:
raise BadRequest('Missing or invalid function request')
if context is None or not context:
raise BadRequest('Missing context')
if not (context in CATEGORIES_ACTIVE
or context in ARCHIVES or context == 'all'):
raise BadRequest('Invalid context')
try:
arxiv_id = Identifier(id)
except IdentifierException:
raise BadRequest(escape(f"Invalid article identifier {id}"))
seq_id = get_sequential_id(paper_id=arxiv_id,
is_next=function == 'next',
context=context)
if not seq_id:
raise BadRequest(
escape(f'No {function} article found for '
f'{arxiv_id.id} in {context}'))
redirect_url = url_for('browse.abstract', arxiv_id=seq_id, context=context)
return {}, status.HTTP_301_MOVED_PERMANENTLY, {'Location': redirect_url}