mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-03 12:47:51 +02:00
Add REST API pagination and mke REST API optional (#43)
* Implement Rest API pagination * REST API must be enabled in settings * Report REST API status in the admin site settings page
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
||||||
from rest_framework.schemas.openapi import AutoSchema
|
from rest_framework.schemas.openapi import AutoSchema
|
||||||
|
|
||||||
|
from ram.views import CustomLimitOffsetPagination
|
||||||
from bookshelf.models import Book, Catalog
|
from bookshelf.models import Book, Catalog
|
||||||
from bookshelf.serializers import BookSerializer, CatalogSerializer
|
from bookshelf.serializers import BookSerializer, CatalogSerializer
|
||||||
|
|
||||||
|
|
||||||
class BookList(ListAPIView):
|
class BookList(ListAPIView):
|
||||||
serializer_class = BookSerializer
|
serializer_class = BookSerializer
|
||||||
|
pagination_class = CustomLimitOffsetPagination
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Book.objects.get_published(self.request.user)
|
return Book.objects.get_published(self.request.user)
|
||||||
@@ -23,6 +25,7 @@ class BookGet(RetrieveAPIView):
|
|||||||
|
|
||||||
class CatalogList(ListAPIView):
|
class CatalogList(ListAPIView):
|
||||||
serializer_class = CatalogSerializer
|
serializer_class = CatalogSerializer
|
||||||
|
pagination_class = CustomLimitOffsetPagination
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Catalog.objects.get_published(self.request.user)
|
return Catalog.objects.get_published(self.request.user)
|
||||||
|
@@ -1,11 +1,13 @@
|
|||||||
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
||||||
|
|
||||||
|
from ram.views import CustomLimitOffsetPagination
|
||||||
from consist.models import Consist
|
from consist.models import Consist
|
||||||
from consist.serializers import ConsistSerializer
|
from consist.serializers import ConsistSerializer
|
||||||
|
|
||||||
|
|
||||||
class ConsistList(ListAPIView):
|
class ConsistList(ListAPIView):
|
||||||
serializer_class = ConsistSerializer
|
serializer_class = ConsistSerializer
|
||||||
|
pagination_class = CustomLimitOffsetPagination
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Consist.objects.get_published(self.request.user)
|
return Consist.objects.get_published(self.request.user)
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
from django.conf import settings
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from solo.admin import SingletonModelAdmin
|
from solo.admin import SingletonModelAdmin
|
||||||
|
|
||||||
@@ -7,7 +8,7 @@ from portal.models import SiteConfiguration, Flatpage
|
|||||||
|
|
||||||
@admin.register(SiteConfiguration)
|
@admin.register(SiteConfiguration)
|
||||||
class SiteConfigurationAdmin(SingletonModelAdmin):
|
class SiteConfigurationAdmin(SingletonModelAdmin):
|
||||||
readonly_fields = ("site_name",)
|
readonly_fields = ("site_name", "rest_api")
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(
|
(
|
||||||
None,
|
None,
|
||||||
@@ -21,6 +22,7 @@ class SiteConfigurationAdmin(SingletonModelAdmin):
|
|||||||
"currency",
|
"currency",
|
||||||
"footer",
|
"footer",
|
||||||
"footer_extended",
|
"footer_extended",
|
||||||
|
"rest_api",
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -37,6 +39,10 @@ class SiteConfigurationAdmin(SingletonModelAdmin):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@admin.display(description="REST API enabled", boolean=True)
|
||||||
|
def rest_api(self, obj):
|
||||||
|
return settings.REST_ENABLED
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Flatpage)
|
@admin.register(Flatpage)
|
||||||
class FlatpageAdmin(admin.ModelAdmin):
|
class FlatpageAdmin(admin.ModelAdmin):
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
from ram.utils import git_suffix
|
from ram.utils import git_suffix
|
||||||
|
|
||||||
__version__ = "0.15.5"
|
__version__ = "0.15.6"
|
||||||
__version__ += git_suffix(__file__)
|
__version__ += git_suffix(__file__)
|
||||||
|
@@ -142,6 +142,12 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
|||||||
MEDIA_URL = "media/"
|
MEDIA_URL = "media/"
|
||||||
MEDIA_ROOT = STORAGE_DIR / "media"
|
MEDIA_ROOT = STORAGE_DIR / "media"
|
||||||
|
|
||||||
|
REST_ENABLED = False # Set to True to enable the REST API
|
||||||
|
REST_FRAMEWORK = {
|
||||||
|
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
|
||||||
|
"PAGE_SIZE": 5,
|
||||||
|
}
|
||||||
|
|
||||||
TINYMCE_DEFAULT_CONFIG = {
|
TINYMCE_DEFAULT_CONFIG = {
|
||||||
"height": "500px",
|
"height": "500px",
|
||||||
"menubar": False,
|
"menubar": False,
|
||||||
|
@@ -32,9 +32,6 @@ urlpatterns = [
|
|||||||
path("tinymce/upload_image", UploadImage.as_view(), name="upload_image"),
|
path("tinymce/upload_image", UploadImage.as_view(), name="upload_image"),
|
||||||
path("portal/", include("portal.urls")),
|
path("portal/", include("portal.urls")),
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
path("api/v1/consist/", include("consist.urls")),
|
|
||||||
path("api/v1/roster/", include("roster.urls")),
|
|
||||||
path("api/v1/bookshelf/", include("bookshelf.urls")),
|
|
||||||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
|
|
||||||
# Enable the "/dcc" routing only if the "driver" app is active
|
# Enable the "/dcc" routing only if the "driver" app is active
|
||||||
@@ -43,30 +40,37 @@ if apps.is_installed("driver"):
|
|||||||
path("api/v1/dcc/", include("driver.urls")),
|
path("api/v1/dcc/", include("driver.urls")),
|
||||||
]
|
]
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.REST_ENABLED:
|
||||||
from django.views.generic import TemplateView
|
|
||||||
from rest_framework.schemas import get_schema_view
|
|
||||||
|
|
||||||
urlpatterns += [
|
urlpatterns += [
|
||||||
path(
|
path("api/v1/consist/", include("consist.urls")),
|
||||||
"swagger/",
|
path("api/v1/roster/", include("roster.urls")),
|
||||||
TemplateView.as_view(
|
path("api/v1/bookshelf/", include("bookshelf.urls")),
|
||||||
template_name="swagger.html",
|
|
||||||
extra_context={"schema_url": "openapi-schema"},
|
|
||||||
),
|
|
||||||
name="swagger",
|
|
||||||
),
|
|
||||||
path(
|
|
||||||
"openapi",
|
|
||||||
get_schema_view(
|
|
||||||
title="RAM - Railroad Assets Manager",
|
|
||||||
description="RAM API",
|
|
||||||
version="1.0.0",
|
|
||||||
),
|
|
||||||
name="openapi-schema",
|
|
||||||
),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if settings.DEBUG:
|
||||||
if apps.is_installed("debug_toolbar"):
|
if apps.is_installed("debug_toolbar"):
|
||||||
urlpatterns += [
|
urlpatterns += [
|
||||||
path("__debug__/", include("debug_toolbar.urls")),
|
path("__debug__/", include("debug_toolbar.urls")),
|
||||||
]
|
]
|
||||||
|
if settings.REST_ENABLED:
|
||||||
|
from django.views.generic import TemplateView
|
||||||
|
from rest_framework.schemas import get_schema_view
|
||||||
|
urlpatterns += [
|
||||||
|
path(
|
||||||
|
"swagger/",
|
||||||
|
TemplateView.as_view(
|
||||||
|
template_name="swagger.html",
|
||||||
|
extra_context={"schema_url": "openapi-schema"},
|
||||||
|
),
|
||||||
|
name="swagger",
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
"openapi",
|
||||||
|
get_schema_view(
|
||||||
|
title="RAM - Railroad Assets Manager",
|
||||||
|
description="RAM API",
|
||||||
|
version="1.0.0",
|
||||||
|
),
|
||||||
|
name="openapi-schema",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
@@ -16,6 +16,13 @@ from django.utils.text import slugify as slugify
|
|||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
|
from rest_framework.pagination import LimitOffsetPagination
|
||||||
|
|
||||||
|
|
||||||
|
class CustomLimitOffsetPagination(LimitOffsetPagination):
|
||||||
|
default_limit = 10
|
||||||
|
max_limit = 25
|
||||||
|
|
||||||
|
|
||||||
@method_decorator(csrf_exempt, name="dispatch")
|
@method_decorator(csrf_exempt, name="dispatch")
|
||||||
class UploadImage(View):
|
class UploadImage(View):
|
||||||
|
@@ -1,12 +1,14 @@
|
|||||||
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
||||||
from rest_framework.schemas.openapi import AutoSchema
|
from rest_framework.schemas.openapi import AutoSchema
|
||||||
|
|
||||||
|
from ram.views import CustomLimitOffsetPagination
|
||||||
from roster.models import RollingStock
|
from roster.models import RollingStock
|
||||||
from roster.serializers import RollingStockSerializer
|
from roster.serializers import RollingStockSerializer
|
||||||
|
|
||||||
|
|
||||||
class RosterList(ListAPIView):
|
class RosterList(ListAPIView):
|
||||||
serializer_class = RollingStockSerializer
|
serializer_class = RollingStockSerializer
|
||||||
|
pagination_class = CustomLimitOffsetPagination
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return RollingStock.objects.get_published(self.request.user)
|
return RollingStock.objects.get_published(self.request.user)
|
||||||
@@ -23,6 +25,7 @@ class RosterGet(RetrieveAPIView):
|
|||||||
|
|
||||||
class RosterAddress(ListAPIView):
|
class RosterAddress(ListAPIView):
|
||||||
serializer_class = RollingStockSerializer
|
serializer_class = RollingStockSerializer
|
||||||
|
pagination_class = CustomLimitOffsetPagination
|
||||||
schema = AutoSchema(operation_id_base="retrieveRollingStockByAddress")
|
schema = AutoSchema(operation_id_base="retrieveRollingStockByAddress")
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
@@ -34,6 +37,7 @@ class RosterAddress(ListAPIView):
|
|||||||
|
|
||||||
class RosterClass(ListAPIView):
|
class RosterClass(ListAPIView):
|
||||||
serializer_class = RollingStockSerializer
|
serializer_class = RollingStockSerializer
|
||||||
|
pagination_class = CustomLimitOffsetPagination
|
||||||
|
|
||||||
schema = AutoSchema(operation_id_base="retrieveRollingStockByClass")
|
schema = AutoSchema(operation_id_base="retrieveRollingStockByClass")
|
||||||
|
|
||||||
|
@@ -3,4 +3,7 @@ pdbpp
|
|||||||
ipython
|
ipython
|
||||||
flake8
|
flake8
|
||||||
pyinstrument
|
pyinstrument
|
||||||
|
pyyaml
|
||||||
|
uritemplate
|
||||||
|
inflection
|
||||||
django-debug-toolbar
|
django-debug-toolbar
|
||||||
|
@@ -9,7 +9,6 @@ django-health-check
|
|||||||
django-admin-sortable2
|
django-admin-sortable2
|
||||||
django-tinymce
|
django-tinymce
|
||||||
# Optional: # psycopg2-binary
|
# Optional: # psycopg2-binary
|
||||||
# Optional: # pySerial
|
|
||||||
# Required by django-countries and not always installed
|
# Required by django-countries and not always installed
|
||||||
# by default on modern venvs (like Python 3.12 on Fedora 39)
|
# by default on modern venvs (like Python 3.12 on Fedora 39)
|
||||||
setuptools
|
setuptools
|
||||||
|
Reference in New Issue
Block a user