Skip to content

Commit

Permalink
Merge pull request #32 from akirachix/dev
Browse files Browse the repository at this point in the history
Staging to dev
  • Loading branch information
Ndichu-shee authored Sep 26, 2024
2 parents fee4251 + 7776e1e commit ad3066f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 38 deletions.
10 changes: 7 additions & 3 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from rest_framework import serializers
from user.models import User, ROLE_CHOICES
from django.contrib.auth.models import User as DjangoUser
from django.contrib.auth.models import User
from datamonitoring.models import MonitoringData
from drainagesystem.models import DrainageSystem
from sensor.models import Sensor
from map.models import Device
from notification.models import Notification



class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=True)

Expand Down Expand Up @@ -43,8 +44,11 @@ def create(self, validated_data):

class RoleSerializer(serializers.Serializer):
user_id = serializers.IntegerField()
# role = serializers.ChoiceField(choices=User.ROLE_CHOICES)

class MonitoringDataSerializer(serializers.ModelSerializer):
class Meta:
model = MonitoringData
fields = ['monitoring_id', 'user_id', 'drainage_id', 'timestamp', 'water_level', 'water_pressure']

class MonitoringDataSerializer(serializers.ModelSerializer):
class Meta:
Expand Down Expand Up @@ -76,8 +80,8 @@ class Meta:
model = Sensor
fields = ["Sensor_ID", "Type", "Location", "Status", "Time_Date"]


class NotificationSerializer(serializers.ModelSerializer):
class Meta:
model = Notification
fields = ["id", "title", "message", "type", "created_at"]

45 changes: 23 additions & 22 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,41 @@
router.register(r"monitoring-data", MonitoringDataViewSet)
router = DefaultRouter()
router.register(r"notifications", NotificationViewSet)

urlpatterns = router.urls
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import (
UserListView, UserDetailView, RegisterView, LoginView, RoleBasedView,
MonitoringDataViewSet, DrainageSystemList, DrainageSystemDetail,
SensorListCreateView, SensorDetailView, NotificationViewSet
)
from map.views import map_view, MapDeviceListView, DeviceSearchView


router = DefaultRouter()
router.register(r"monitoring-data", MonitoringDataViewSet)
router.register(r"notifications", NotificationViewSet)


urlpatterns = [

path("", include(router.urls)),
path("users/", UserListView.as_view(), name="user-list"),
path("users/<int:id>/", UserDetailView.as_view(), name="user-detail"),
path("users/register/", RegisterView.as_view(), name="user-register"),
path("users/login/", LoginView.as_view(), name="user-login"),
path("users/role-based/", RoleBasedView.as_view(), name="role-based"),
path("", include(router.urls)),
path(
"drainage-systems/", DrainageSystemList.as_view(), name="drainage-system-list"
),
path(
"drainage-systems/<int:pk>/",
DrainageSystemDetail.as_view(),
name="drainage-system-detail",
),
path("drainage-systems/", DrainageSystemList.as_view(), name="DrainageSystemList"),
path("drainage-systems/<int:pk>/", DrainageSystemDetail.as_view(), name="DrainageSystemDetail"),
path("api", map_view, name="map_view"),
path("map/", map_view, name="map_view"),
path("devices/", MapDeviceListView.as_view(), name="device-list"),
path("search/", DeviceSearchView.as_view(), name="device-search"),
path("sensors/", SensorListCreateView.as_view(), name="sensor-list-create"),
path("sensors/<int:pk>/", SensorDetailView.as_view(), name="sensor-detail"),
path(
"notifications/", NotificationViewSet.as_view({"get": "list", "post": "create"})
),
path(
"datamonitoring/",
MonitoringDataViewSet.as_view({"get": "list", "post": "create"}),
),
path(
"notifications/<int:pk>/",
NotificationViewSet.as_view(
{"get": "retrieve", "put": "update", "delete": "destroy"}
),
),
path("notifications/", NotificationViewSet.as_view({"get": "list", "post": "create"})),
path("datamonitoring/", MonitoringDataViewSet.as_view({"get": "list", "post": "create"})),
path("notifications/<int:pk>/", NotificationViewSet.as_view({"get": "retrieve", "put": "update", "delete": "destroy"})),
]

22 changes: 13 additions & 9 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.shortcuts import render
import logging
from rest_framework import status
from rest_framework import status,generics
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated, IsAdminUser
Expand All @@ -11,22 +11,19 @@
from rest_framework import viewsets
from datamonitoring.models import MonitoringData
from .serializers import MonitoringDataSerializer
from rest_framework import generics, status
from rest_framework.response import Response
from drainagesystem.models import DrainageSystem
from .serializers import DrainageSystemSerializer
from .serializers import DeviceSerializer
from sensor.models import Sensor
from .serializers import SensorSerializer
from rest_framework import viewsets
from notification.models import Notification
from .serializers import NotificationSerializer


logger = logging.getLogger(__name__)


class UserListView(APIView):
permission_classes = [IsAuthenticated]

def get(self, request):
users = User.objects.all()
Expand All @@ -35,8 +32,6 @@ def get(self, request):


class UserDetailView(APIView):
permission_classes = [IsAuthenticated]

def get(self, request, id):
try:
user = User.objects.get(id=id)
Expand All @@ -48,15 +43,15 @@ def get(self, request, id):
logger.info(f"User with ID {id} retrieved successfully.")
return Response(serializer.data)


class RegisterView(APIView):
def post(self, request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
# hashing password
serializer.validated_data['password'] = make_password(serializer.validated_data['password'])
serializer.validated_data["password"] = make_password(
serializer.validated_data["password"]
)

user = serializer.save()
logger.info(f"User registered successfully: {user.email}")
return Response(UserSerializer(user).data, status=status.HTTP_201_CREATED)
Expand Down Expand Up @@ -127,6 +122,13 @@ class MonitoringDataViewSet(viewsets.ModelViewSet):
queryset = MonitoringData.objects.all()
serializer_class = MonitoringDataSerializer


class DrainageSystemList(APIView):
def get(self, request):
drainagesystems = DrainageSystem.objects.all()
serializer = DrainageSystemSerializer(drainagesystems, many=True)
return Response(serializer.data)


class DrainageSystemList(generics.ListCreateAPIView):
queryset = DrainageSystem.objects.all()
Expand All @@ -140,6 +142,7 @@ def get(self, request, *args, **kwargs):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)



class DrainageSystemDetail(APIView):
def post(self, request, *args, **kwargs):
serializer = DrainageSystemSerializer(data=request.data)
Expand Down Expand Up @@ -190,3 +193,4 @@ def delete(self, request, id):
class NotificationViewSet(viewsets.ModelViewSet):
queryset = Notification.objects.all()
serializer_class = NotificationSerializer

13 changes: 11 additions & 2 deletions aquasens/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True



ALLOWED_HOSTS = [ '*']





# Application definition

INSTALLED_APPS = [
Expand All @@ -44,6 +53,7 @@
"notification",
"corsheaders",
"django_extensions",

]

MIDDLEWARE = [
Expand Down Expand Up @@ -93,8 +103,7 @@
]

ENV_FILE = find_dotenv()
if ENV_FILE:
load_dotenv(ENV_FILE)
if ENV_FILE: load_dotenv(ENV_FILE)

WSGI_APPLICATION = "aquasens.wsgi.application"

Expand Down
1 change: 1 addition & 0 deletions authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def callback(request):


def logout(request):

request.session.clear()

return redirect(
Expand Down
6 changes: 5 additions & 1 deletion drainagesystem/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class DrainageSystem(models.Model):
waterlevel = models.DecimalField(max_digits=10, decimal_places=2)
waterpressure = models.DecimalField(max_digits=10, decimal_places=2)
Status = models.CharField(max_length=100)
Timestamp = models.DateTimeField(auto_now_add=True)
Timestamp=models.DateTimeField(auto_now_add=True)




def __str__(self):
return f"Drainagesystem {self.Drainage_ID}"

1 change: 1 addition & 0 deletions drainagesystem/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from django.shortcuts import render

2 changes: 1 addition & 1 deletion map/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models


# Create your models here.

class Device(models.Model):
address = models.CharField(max_length=255)
latitude = models.FloatField()
Expand Down

0 comments on commit ad3066f

Please sign in to comment.