Skip to content

Commit

Permalink
Initial working serializer and view for a restful endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiky30 committed Jan 24, 2024
1 parent f323079 commit 2d9a578
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build==0.10.0
cfgv==3.4.0
distlib==0.3.8
Django==5.0.1
djangorestframework==3.14.0
exceptiongroup==1.2.0
factory-boy==3.3.0
Faker==22.5.0
Expand All @@ -21,6 +22,7 @@ pyproject_hooks==1.0.0
pytest==7.4.4
pytest-django==4.7.0
python-dateutil==2.8.2
pytz==2023.3.post1
PyYAML==6.0.1
referencing==0.32.1
rpds-py==0.17.1
Expand Down
9 changes: 9 additions & 0 deletions shedpi_hub_dashboard/serlializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from .models import DeviceModuleReading


class DeviceModuleReadingSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = DeviceModuleReading
fields = "__all__"
Empty file.
9 changes: 9 additions & 0 deletions shedpi_hub_dashboard/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from django.template.response import TemplateResponse
from rest_framework import viewsets

from .models import DeviceModuleReading
from .serlializers import DeviceModuleReadingSerializer


def index(request):
response = TemplateResponse(request, "shedpi_hub_dashboard/index.html", {})
return response


class DeviceModuleReadingViewSet(viewsets.ModelViewSet):
queryset = DeviceModuleReading.objects.all()
serializer_class = DeviceModuleReadingSerializer
9 changes: 9 additions & 0 deletions shedpi_hub_example_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"shedpi_hub_dashboard.apps.ShedpiHubDashboardConfig",
"rest_framework",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -126,3 +127,11 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly"
]
}
8 changes: 8 additions & 0 deletions shedpi_hub_example_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from rest_framework import routers

from shedpi_hub_dashboard.views import DeviceModuleReadingViewSet

router = routers.DefaultRouter()
router.register(r"device_module_readings", DeviceModuleReadingViewSet)

urlpatterns = [
*[
path("admin/", admin.site.urls),
path("", include("shedpi_hub_dashboard.urls")),
path("api-auth/", include("rest_framework.urls")),
path("api/v1/", include(router.urls)),
],
*static(settings.STATIC_URL, document_root=settings.STATIC_ROOT),
]

0 comments on commit 2d9a578

Please sign in to comment.