Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [NR-NMP-149] Added regions and subregions #150

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/apps/animals/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from .models import Animals
from .models import *

admin.site.register(Animals)
admin.site.register(AnimalSubtype)
18 changes: 17 additions & 1 deletion backend/apps/animals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ class Animals(models.Model):

class Meta:
managed = False
db_table = 'animals'
db_table = 'animals'

class AnimalSubtype(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
liquidpergalperanimalperday = models.FloatField()
solidpergalperanimalperday = models.FloatField()
solidperpoundperanimalperday = models.FloatField()
solidliquidseparationpercentage = models.IntegerField()
washwater = models.FloatField()
milkproduction = models.FloatField()
animalid = models.IntegerField()
sortorder = models.IntegerField()

class Meta:
managed = False
db_table = 'animal_subtype'
7 changes: 6 additions & 1 deletion backend/apps/animals/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from rest_framework import serializers
from .models import Animals
from .models import *

class AnimalsSerializer(serializers.ModelSerializer):
class Meta:
model = Animals
fields = '__all__'

class AnimalSubtypeSerializer(serializers.ModelSerializer):
class Meta:
model = AnimalSubtype
fields = '__all__'
9 changes: 9 additions & 0 deletions backend/apps/animals/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from rest_framework import routers
from .views import AnimalsViewset

urlpatterns = [
path('animals/', AnimalsViewset.as_view({'get': 'animals'})),
path('animal_subtypes/', AnimalsViewset.as_view({'get': 'animalSubtypes'})),
path('animal_subtypes/<int:animalId>/', AnimalsViewset.as_view({'get': 'animalSubtypes'})),
]
23 changes: 23 additions & 0 deletions backend/apps/animals/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from .models import *
from .serializers import *

class AnimalsViewset(viewsets.ViewSet):

@action(detail=True, methods=['get'])
def animals(self, request):
animals = Animals.objects.all()
serializer = AnimalsSerializer(animals, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

@action(detail=True, methods=['get'])
def animalSubtypes(self, request, animalId=None):
animals = None
if animalId == None:
animals = AnimalSubtype.objects.all()
else:
animals = AnimalSubtype.objects.filter(animalid=animalId)
serializer = AnimalSubtypeSerializer(animals, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
28 changes: 0 additions & 28 deletions backend/apps/api/urls.py

This file was deleted.

27 changes: 0 additions & 27 deletions backend/apps/api/views.py

This file was deleted.

8 changes: 8 additions & 0 deletions backend/apps/crops/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from rest_framework import routers
from .views import CropsViewset

urlpatterns = [
path('croptypes/', CropsViewset.as_view({'get': 'cropTypes'})),
path('crops/', CropsViewset.as_view({'get': 'crops'}), name='crops'),
]
19 changes: 19 additions & 0 deletions backend/apps/crops/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from .models import *
from .serializers import *

class CropsViewset(viewsets.ViewSet):

@action(detail=True, methods=['get'])
def cropTypes(self, request):
crop_types = CropTypes.objects.all()
serializer = CropTypesSerializer(crop_types, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

@action(detail=True, methods=['get'])
def crops(self, request, pk=None):
crops = Crops.objects.all()
serializer = CropsSerializer(crops, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
File renamed without changes.
Empty file added backend/apps/shared/admin.py
Empty file.
24 changes: 24 additions & 0 deletions backend/apps/shared/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.db import models

class Regions(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
soiltestphosphorousregioncd = models.IntegerField()
soiltestpotassiumregioncd = models.IntegerField()
locationid = models.IntegerField()
sortorder = models.IntegerField()

class Meta:
managed = False
db_table = 'regions'

class Subregion(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
annualprecipitation = models.IntegerField()
annualprecipitationocttomar = models.IntegerField()
regionid = models.IntegerField()

class Meta:
managed = False
db_table = 'subregion'
12 changes: 12 additions & 0 deletions backend/apps/shared/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework import serializers
from .models import *

class RegionsSerializer(serializers.ModelSerializer):
class Meta:
model = Regions
fields = '__all__'

class SubregionSerializer(serializers.ModelSerializer):
class Meta:
model = Subregion
fields = '__all__'
9 changes: 9 additions & 0 deletions backend/apps/shared/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from rest_framework import routers
from .views import SharedViewset

urlpatterns = [
path('regions/', SharedViewset.as_view({'get': 'regions'})),
path('subregions/', SharedViewset.as_view({'get': 'subregions'})),
path('subregions/<int:regionId>/', SharedViewset.as_view({'get': 'subregions'})),
]
23 changes: 23 additions & 0 deletions backend/apps/shared/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from .models import *
from .serializers import *

class SharedViewset(viewsets.ViewSet):

@action(detail=True, methods=['get'])
def regions(self, request):
regions = Regions.objects.all()
serializer = RegionsSerializer(regions, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

@action(detail=True, methods=['get'])
def subregions(self, request, regionId=None):
subregions = None
if regionId == None:
subregions = Subregion.objects.all()
else:
subregions = Subregion.objects.filter(regionid=regionId)
serializer = SubregionSerializer(subregions, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
2 changes: 1 addition & 1 deletion backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
'rest_framework',

# Apps for this project
'apps.api',
'apps.crops',
'apps.animals',
'apps.shared',
]

MIDDLEWARE = [
Expand Down
6 changes: 4 additions & 2 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
path('admin/', admin.site.urls),
# TODO: make a better accounts/login page
path('accounts/', include('allauth.urls')),
path('healthcheck/', health_check, name='health_check'),
path('api/', include('apps.api.urls')),
path('healthcheck/', health_check),
path('api/', include('apps.animals.urls')),
path('api/', include('apps.crops.urls')),
path('api/', include('apps.shared.urls')),
]
Loading
Loading