Skip to content

Commit

Permalink
feat(api): setup API for reading app
Browse files Browse the repository at this point in the history
We can list articles.

See #318
  • Loading branch information
Jenselme committed Nov 26, 2024
1 parent 90de7d4 commit 5d127dc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ repos:
rev: v1.36.1
hooks:
- id: djlint-reformat-django
exclude: "ninja/swagger.html"
- id: djlint-django

- repo: https://github.com/shellcheck-py/shellcheck-py
Expand Down
7 changes: 7 additions & 0 deletions config/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ninja import NinjaAPI
from ninja.security import django_auth

from legadilo.reading.api import reading_api_router

api = NinjaAPI(title="Legadilo API", auth=[django_auth], docs_url="/docs/")
api.add_router("reading/", reading_api_router)
7 changes: 7 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"django.forms",
]
THIRD_PARTY_APPS = [
"ninja",
"django_version_checks",
"extra_checks",
"anymail",
Expand Down Expand Up @@ -607,6 +608,12 @@ def before_send_to_sentry(event, hint):
print("Failed to import sentry_sdk") # noqa: T201 print found


# django-ninja
# ------------------------------------------------------------------------------
# See https://django-ninja.dev/reference/settings/
NINJA_PAGINATION_MAX_LIMIT = 500


# Your stuff...
# ------------------------------------------------------------------------------
ARTICLE_FETCH_TIMEOUT = env.int("LEGADILO_ARTICLE_FETCH_TIMEOUT", default=50)
Expand Down
3 changes: 3 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.urls import include, path
from django.views import defaults as default_views

from config.api import api


def _correct_admin_url(path: str) -> str:
path = path.removeprefix("/")
Expand All @@ -26,6 +28,7 @@ def _correct_admin_url(path: str) -> str:
path("feeds/", include("legadilo.feeds.urls", namespace="feeds")),
path("reading/", include("legadilo.reading.urls", namespace="reading")),
path("import-export/", include("legadilo.import_export.urls", namespace="import_export")),
path("api/", api.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


Expand Down
35 changes: 35 additions & 0 deletions legadilo/reading/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Legadilo
# Copyright (C) 2023-2024 by Legadilo contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from ninja import ModelSchema, Router
from ninja.pagination import paginate

from legadilo.reading.models import Article
from legadilo.users.user_types import AuthenticatedHttpRequest

reading_api_router = Router(tags=["reading"])


class OutArticleSchema(ModelSchema):
class Meta:
model = Article
exclude = ("user", "obj_created_at", "obj_updated_at")


@reading_api_router.get("/articles/", response=list[OutArticleSchema])
@paginate
def list_articles(request: AuthenticatedHttpRequest):
return Article.objects.get_queryset().for_user(request.user).default_order_by()
28 changes: 28 additions & 0 deletions legadilo/templates/ninja/swagger.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
<link type="text/css"
rel="stylesheet"
href="{% static 'ninja/swagger-ui.css' %}"
nonce="{{ request.csp_nonce }}" />
<link rel="shortcut icon" href="{% static 'ninja/favicon.png' %}" />
<title>{{ api.title }}</title>
</head>
<body data-csrf-token="{% if add_csrf %}{{ csrf_token }}{% endif %}"
data-api-csrf="{% if add_csrf %}true{% endif %}">
<script type="application/json" id="swagger-settings">
{% comment %}
Beware: if this file gets reformatted, we can loose the proper syntax below. This
will breaks the swagger template!
{% endcomment %}
{{ swagger_settings | safe }}
</script>
<div id="swagger-ui"></div>
<script src="{% static 'ninja/swagger-ui-bundle.js' %}"
nonce="{{ request.csp_nonce }}"></script>
<script src="{% static 'ninja/swagger-ui-init.js' %}"
nonce="{{ request.csp_nonce }}"></script>
</body>
</html>

0 comments on commit 5d127dc

Please sign in to comment.