Skip to content

Commit 28fb13f

Browse files
committed
Add new link field
1 parent a4edbad commit 28fb13f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

readthedocs/search/api.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import logging
2+
import os
23
from pprint import pformat
34

4-
from rest_framework import generics
5-
from rest_framework import serializers
5+
from rest_framework import generics, serializers
66
from rest_framework.exceptions import ValidationError
77
from rest_framework.pagination import PageNumberPagination
88

99
from readthedocs.search.faceted_search import PageSearch
1010
from readthedocs.search.utils import get_project_list_or_404
1111

12+
1213
log = logging.getLogger(__name__)
1314

1415

@@ -23,10 +24,29 @@ class PageSearchSerializer(serializers.Serializer):
2324
version = serializers.CharField()
2425
title = serializers.CharField()
2526
path = serializers.CharField()
27+
# Doc url without extension
2628
link = serializers.SerializerMethodField()
29+
# Doc url with extension
30+
url = serializers.SerializerMethodField()
2731
highlight = serializers.SerializerMethodField()
2832

2933
def get_link(self, obj):
34+
"""
35+
Gets the url without extension.
36+
37+
.. warning::
38+
This is only used to keep compatibility with
39+
the previous search implementation.
40+
Use `url` instead.
41+
"""
42+
projects_url = self.context.get('projects_url')
43+
if projects_url:
44+
docs_url = projects_url[obj.project]
45+
path = os.path.splitext(obj.path)[0]
46+
return docs_url + path
47+
48+
def get_url(self, obj):
49+
"""Gets the full url."""
3050
projects_url = self.context.get('projects_url')
3151
if projects_url:
3252
docs_url = projects_url[obj.project]

readthedocs/search/parse_json.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ def process_file(fjson_filename, filename):
7171
sections = []
7272
title = ''
7373
body_content = ''
74-
if 'body' in data and data['body']:
74+
75+
if data.get('body'):
7576
body = PyQuery(data['body'])
7677
body_content = body.text().replace('¶', '')
7778
sections.extend(generate_sections_from_pyquery(body))
7879
else:
7980
log.info('Unable to index content for: %s', fjson_filename)
81+
8082
if 'title' in data:
8183
title = data['title']
8284
if title.startswith('<'):

0 commit comments

Comments
 (0)