Skip to content

Commit 29a956d

Browse files
authored
Merge pull request #5868 from readthedocs/humitos/apiv3-fields-refactor
APIv3 refactor some fields
2 parents a0271d1 + ea3cade commit 29a956d

6 files changed

+47
-23
lines changed

readthedocs/api/v3/serializers.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,26 @@ def get_name(self, programming_language):
301301
return 'Unknown'
302302

303303

304-
class ProjectURLsSerializer(serializers.Serializer):
304+
class ProjectURLsSerializer(BaseLinksSerializer, serializers.Serializer):
305+
306+
"""Serializer with all the user-facing URLs under Read the Docs."""
307+
305308
documentation = serializers.CharField(source='get_docs_url')
306-
project_homepage = serializers.SerializerMethodField()
309+
home = serializers.SerializerMethodField()
310+
builds = serializers.SerializerMethodField()
311+
versions = serializers.SerializerMethodField()
307312

308-
def get_project_homepage(self, obj):
309-
# Overridden only to return ``None`` when the description is ``''``
310-
return obj.project_url or None
313+
def get_home(self, obj):
314+
path = reverse('projects_detail', kwargs={'project_slug': obj.slug})
315+
return self._absolute_url(path)
316+
317+
def get_builds(self, obj):
318+
path = reverse('builds_project_list', kwargs={'project_slug': obj.slug})
319+
return self._absolute_url(path)
320+
321+
def get_versions(self, obj):
322+
path = reverse('project_version_list', kwargs={'project_slug': obj.slug})
323+
return self._absolute_url(path)
311324

312325

313326
class RepositorySerializer(serializers.Serializer):
@@ -409,6 +422,7 @@ class Meta:
409422

410423
class ProjectSerializer(FlexFieldsModelSerializer):
411424

425+
homepage = serializers.SerializerMethodField()
412426
language = LanguageSerializer()
413427
programming_language = ProgrammingLanguageSerializer()
414428
repository = RepositorySerializer(source='*')
@@ -420,8 +434,6 @@ class ProjectSerializer(FlexFieldsModelSerializer):
420434
tags = serializers.StringRelatedField(many=True)
421435
users = UserSerializer(many=True)
422436

423-
description = serializers.SerializerMethodField()
424-
425437
_links = ProjectLinksSerializer(source='*')
426438

427439
# TODO: adapt these fields with the proper names in the db and then remove
@@ -447,11 +459,11 @@ class Meta:
447459
'id',
448460
'name',
449461
'slug',
450-
'description',
451462
'created',
452463
'modified',
453464
'language',
454465
'programming_language',
466+
'homepage',
455467
'repository',
456468
'default_version',
457469
'default_branch',
@@ -470,9 +482,9 @@ class Meta:
470482
'_links',
471483
]
472484

473-
def get_description(self, obj):
474-
# Overridden only to return ``None`` when the description is ``''``
475-
return obj.description or None
485+
def get_homepage(self, obj):
486+
# Overridden only to return ``None`` when the project_url is ``''``
487+
return obj.project_url or None
476488

477489
def get_translation_of(self, obj):
478490
if obj.main_language_project:

readthedocs/api/v3/tests/responses/projects-detail.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"created": "2019-04-29T10:00:00Z",
5252
"default_branch": "master",
5353
"default_version": "latest",
54-
"description": "Project description",
5554
"id": 1,
5655
"language": {
5756
"code": "en",
@@ -76,6 +75,7 @@
7675
"code": "words",
7776
"name": "Only Words"
7877
},
78+
"homepage": "http://project.com",
7979
"repository": {
8080
"type": "git",
8181
"url": "https://github.com/rtfd/project"
@@ -89,8 +89,10 @@
8989
],
9090
"translation_of": null,
9191
"urls": {
92+
"builds": "https://readthedocs.org/projects/project/builds/",
9293
"documentation": "http://readthedocs.org/docs/project/en/latest/",
93-
"project_homepage": "http://project.com"
94+
"home": "https://readthedocs.org/projects/project/",
95+
"versions": "https://readthedocs.org/projects/project/versions/"
9496
},
9597
"users": [
9698
{

readthedocs/api/v3/tests/responses/projects-list.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"id": 1,
88
"name": "project",
99
"slug": "project",
10-
"description": "Project description",
1110
"created": "2019-04-29T10:00:00Z",
1211
"modified": "2019-04-29T12:00:00Z",
1312
"language": {
@@ -18,6 +17,7 @@
1817
"code": "words",
1918
"name": "Only Words"
2019
},
20+
"homepage": "http://project.com",
2121
"repository": {
2222
"url": "https://github.com/rtfd/project",
2323
"type": "git"
@@ -31,8 +31,10 @@
3131
"subproject_of": null,
3232
"translation_of": null,
3333
"urls": {
34+
"builds": "https://readthedocs.org/projects/project/builds/",
3435
"documentation": "http://readthedocs.org/docs/project/en/latest/",
35-
"project_homepage": "http://project.com"
36+
"home": "https://readthedocs.org/projects/project/",
37+
"versions": "https://readthedocs.org/projects/project/versions/"
3638
},
3739
"tags": [
3840
"tag",

readthedocs/api/v3/tests/responses/projects-subprojects-list.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"id": 2,
88
"name": "subproject",
99
"slug": "subproject",
10-
"description": "SubProject description",
1110
"created": "2019-04-29T10:00:00Z",
1211
"modified": "2019-04-29T12:00:00Z",
1312
"language": {
@@ -18,6 +17,7 @@
1817
"code": "words",
1918
"name": "Only Words"
2019
},
20+
"homepage": "http://subproject.com",
2121
"repository": {
2222
"url": "https://github.com/rtfd/subproject",
2323
"type": "git"
@@ -32,7 +32,6 @@
3232
"id": 1,
3333
"name": "project",
3434
"slug": "project",
35-
"description": "Project description",
3635
"created": "2019-04-29T10:00:00Z",
3736
"modified": "2019-04-29T12:00:00Z",
3837
"language": {
@@ -43,6 +42,7 @@
4342
"code": "words",
4443
"name": "Only Words"
4544
},
45+
"homepage": "http://project.com",
4646
"repository": {
4747
"url": "https://github.com/rtfd/project",
4848
"type": "git"
@@ -56,8 +56,10 @@
5656
"subproject_of": null,
5757
"translation_of": null,
5858
"urls": {
59+
"builds": "https://readthedocs.org/projects/project/builds/",
5960
"documentation": "http://readthedocs.org/docs/project/en/latest/",
60-
"project_homepage": "http://project.com"
61+
"home": "https://readthedocs.org/projects/project/",
62+
"versions": "https://readthedocs.org/projects/project/versions/"
6163
},
6264
"tags": [
6365
"tag",
@@ -82,8 +84,10 @@
8284
},
8385
"translation_of": null,
8486
"urls": {
87+
"builds": "https://readthedocs.org/projects/subproject/builds/",
8588
"documentation": "http://readthedocs.org/docs/project/projects/subproject/en/latest/",
86-
"project_homepage": "http://subproject.com"
89+
"home": "https://readthedocs.org/projects/subproject/",
90+
"versions": "https://readthedocs.org/projects/subproject/versions/"
8791
},
8892
"tags": [],
8993
"users": [],

readthedocs/api/v3/tests/responses/projects-superproject.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"created": "2019-04-29T10:00:00Z",
33
"default_branch": "master",
44
"default_version": "latest",
5-
"description": "Project description",
65
"id": 1,
76
"language": {
87
"code": "en",
@@ -27,6 +26,7 @@
2726
"code": "words",
2827
"name": "Only Words"
2928
},
29+
"homepage": "http://project.com",
3030
"repository": {
3131
"type": "git",
3232
"url": "https://github.com/rtfd/project"
@@ -40,8 +40,10 @@
4040
],
4141
"translation_of": null,
4242
"urls": {
43+
"builds": "https://readthedocs.org/projects/project/builds/",
4344
"documentation": "http://readthedocs.org/docs/project/en/latest/",
44-
"project_homepage": "http://project.com"
45+
"home": "https://readthedocs.org/projects/project/",
46+
"versions": "https://readthedocs.org/projects/project/versions/"
4547
},
4648
"users": [
4749
{

readthedocs/api/v3/tests/responses/projects-versions-builds-list_POST.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"created": "2019-04-29T10:00:00Z",
2424
"default_branch": "master",
2525
"default_version": "latest",
26-
"description": "Project description",
2726
"id": 1,
2827
"language": {
2928
"code": "en",
@@ -48,6 +47,7 @@
4847
"code": "words",
4948
"name": "Only Words"
5049
},
50+
"homepage": "http://project.com",
5151
"repository": {
5252
"type": "git",
5353
"url": "https://github.com/rtfd/project"
@@ -61,8 +61,10 @@
6161
],
6262
"translation_of": null,
6363
"urls": {
64+
"builds": "https://readthedocs.org/projects/project/builds/",
6465
"documentation": "http://readthedocs.org/docs/project/en/latest/",
65-
"project_homepage": "http://project.com"
66+
"home": "https://readthedocs.org/projects/project/",
67+
"versions": "https://readthedocs.org/projects/project/versions/"
6668
},
6769
"users": [
6870
{

0 commit comments

Comments
 (0)