Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 27533c6

Browse files
07.02 code root API view
1 parent 1c6ffba commit 27533c6

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

‎src/config/urls.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
from blog.routers import urlpatterns as blog_api_urls
77
from organizer import urls as organizer_urls
88
from organizer.routers import (
9-
urlpatterns as organizer_api_urls
9+
urlpatterns as organizer_api_urls,
1010
)
1111

12-
api_urls = blog_api_urls + organizer_api_urls
12+
from .views import RootApiView
13+
14+
root_api_url = [
15+
path("", RootApiView.as_view(), name="api-root")
16+
]
17+
api_urls = root_api_url + blog_api_urls + organizer_api_urls
1318

1419
urlpatterns = [
1520
path("admin/", admin.site.urls),

‎src/config/views.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Site Views
2+
3+
(Views that do not belong in any app but are needed by site.)
4+
5+
I'm breaking the rules here for simplicity: this should not
6+
be in config, as this is not configuration for the project.
7+
However, introducing an app specifically for a single view
8+
would overcomplicate the work we're doing, so it's going
9+
here instead.
10+
"""
11+
12+
from rest_framework.response import Response
13+
from rest_framework.reverse import reverse
14+
from rest_framework.status import HTTP_200_OK
15+
from rest_framework.views import APIView
16+
17+
18+
class RootApiView(APIView):
19+
"""Direct users to other API endpoints"""
20+
21+
def get(self, request, *args, **kwargs):
22+
"""Build & display links to other endpoints"""
23+
api_endpoints = [
24+
# (name, url_name),
25+
("tag", "api-tag-list"),
26+
("startup", "api-startup-list"),
27+
("newslink", "api-newslink-list"),
28+
("blog", "api-post-list"),
29+
]
30+
data = {
31+
name: reverse(
32+
url_name,
33+
request=request,
34+
format=kwargs.get("format", None),
35+
)
36+
for (name, url_name) in api_endpoints
37+
}
38+
return Response(data=data, status=HTTP_200_OK)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /