Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_arxiv_id(self, mock_index):
"""Query parameter contains an arXiv ID."""
request_data = MultiDict({'query': '1702.00123'})
response_data, code, headers = simple.search(request_data)
self.assertEqual(code, status.HTTP_301_MOVED_PERMANENTLY,
"Response should be a 301 redirect.")
self.assertIn('Location', headers, "Location header should be set")
self.assertEqual(mock_index.search.call_count, 0,
"No search should be attempted")
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}
Returns
-------
redirect_url: str
A `browse.abstract` redirect URL that uses the canonical
arXiv identifier.
"""
if not id or id.ids == id.id or id.ids == id.idv:
return None
arxiv_id = id.idv if id.has_version else id.id
redirect_url: str = url_for('browse.abstract',
arxiv_id=arxiv_id)
return {},\
status.HTTP_301_MOVED_PERMANENTLY,\
{'Location': redirect_url}
def search(archives: Optional[List[str]] = None) -> _Response:
"""Simple search interface."""
data, code, hdrs = simple.search(request.args, archives)
logger.debug(f"controller returned code: {code}")
if code == status.HTTP_200_OK:
content = render_template("search/search.html", pagetitle="Search",
archives=archives, **data)
response: Response = make_response(content)
for key, value in hdrs.items():
response.headers[key] = value
return response
elif (code == status.HTTP_301_MOVED_PERMANENTLY
or code == status.HTTP_303_SEE_OTHER):
return redirect(hdrs['Location'], code=code)
raise InternalServerError('Unexpected error')
def abstract(arxiv_id: str) -> Union[str, Response]:
"""Abstract (abs) page view."""
response, code, headers = abs_page.get_abs_page(arxiv_id, request.args)
if code == status.HTTP_200_OK:
return render_template('abs/abs.html', **response), code, headers
elif code == status.HTTP_301_MOVED_PERMANENTLY:
return redirect(headers['Location'], code=code)
raise InternalServerError('Unexpected error')
def list_articles(context: str, subcontext: str) -> Response:
"""
List articles by context, month etc.
Context might be a context or an archive; Subcontext should be
'recent', 'new' or a string of format YYMM.
"""
response, code, headers = \
list_page.get_listing(context, subcontext)
if code == status.HTTP_200_OK:
# TODO if it is a HEAD request we don't want to render the template
return render_template(response['template'], **response), code, headers # type: ignore
elif code == status.HTTP_301_MOVED_PERMANENTLY:
return redirect(headers['Location'], code=code) # type: ignore
elif code == status.HTTP_304_NOT_MODIFIED:
return '', code, headers # type: ignore
return response, code, headers # type: ignore
if 'query' in request_params:
try:
# first check if the URL includes an arXiv ID
arxiv_id: Optional[str] = identifier.parse_arxiv_id(
request_params['query']
)
# If so, redirect.
logger.debug(f"got arXiv ID: {arxiv_id}")
except ValueError as e:
logger.debug('No arXiv ID detected; fall back to form')
arxiv_id = None
else:
arxiv_id = None
if arxiv_id:
return {}, status.HTTP_301_MOVED_PERMANENTLY,\
{'Location': f'https://arxiv.org/abs/{arxiv_id}'}
# TODO: use URL constructor to generate URL
#{'Location': external_url_builder('browse', 'abstract', arxiv_id=arxiv_id)}
# Fall back to form-based search.
form = SimpleSearchForm(request_params)
q: Optional[Query]
if form.validate():
logger.debug('form is valid')
q = _query_from_form(form)
# Pagination is handled outside of the form.
q = paginate(q, request_params)
try:
# Execute the search. We'll use the results directly in
# template rendering, so they get added directly to the
# response content.
Raises
------
BadRequest
Raised when form option is invalid
InternalServerError
Raised when there was an unexpected problem executing the query.
"""
try:
tb_id = int(trackback_id)
if not re.match(r'^[\da-f]+$', hashed_document_id):
raise ValueError
trackback = get_trackback_ping(trackback_id=tb_id)
if trackback.hashed_document_id == hashed_document_id:
response_status = status.HTTP_301_MOVED_PERMANENTLY
return {}, response_status, {'Location': trackback.url}
except ValueError:
raise TrackbackNotFound()
except Exception as ex:
raise InternalServerError from ex
raise TrackbackNotFound()