Skip to content

Commit

Permalink
Add support for Bird observation creation (with provided species)
Browse files Browse the repository at this point in the history
  • Loading branch information
eleurent committed Jan 12, 2024
1 parent d6f08bd commit 28a3c8c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
Binary file modified backend/nature_go/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion backend/nature_go/observation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ class Meta:

class Observation(models.Model):
ORGAN_CHOICES = [
('whole', 'whole'),
('leaf', 'leaf'),
('flower', 'flower'),
('fruit', 'fruit'),
('bark', 'bark'),
('whole', 'whole'),
]

user = models.ForeignKey(User, on_delete=models.CASCADE)
Expand Down
2 changes: 1 addition & 1 deletion backend/nature_go/observation/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ObservationSerializer(serializers.ModelSerializer):

class Meta:
model = Observation
fields = ['id', 'user', 'image', 'organ', 'species', 'location', 'datetime', 'identification_response', 'xp']
fields = ['id', 'user', 'image', 'type', 'organ', 'species', 'location', 'datetime', 'identification_response', 'xp']


class SpeciesSerializer(serializers.ModelSerializer):
Expand Down
4 changes: 2 additions & 2 deletions backend/nature_go/observation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

urlpatterns = format_suffix_patterns([
path('', views.SpeciesList.as_view(), name='species-list'),
path('plant', views.PlantSpeciesList.as_view(), name='plant-species-list'),
path('bird', views.BirdSpeciesList.as_view(), name='bird-species-list'),
path('plant/', views.PlantSpeciesList.as_view(), name='plant-species-list'),
path('bird/', views.BirdSpeciesList.as_view(), name='bird-species-list'),
path('all/', views.SpeciesAllList.as_view(), name='species-all-list'),
path('labeled/', views.SpeciesLabeledList.as_view(), name='species-labeled-list'),
path('<int:pk>/', views.SpeciesDetail.as_view(), name='species-detail'),
Expand Down
16 changes: 12 additions & 4 deletions backend/nature_go/observation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def get_queryset(self):
user = self.request.user
return Species.objects.filter(type=Species.PLANT_TYPE, observation__user=user).distinct()


class BirdSpeciesList(SpeciesList):
def get_queryset(self):
user = self.request.user
Expand Down Expand Up @@ -122,6 +123,7 @@ def get_queryset(self):

class ObservationCreate(generics.CreateAPIView):
serializer_class = ObservationSerializer
queryset = Observation.objects.none() # dummy queryset

def create(self, request, *args, **kwargs):
serializer = ObservationSerializer(data=request.data)
Expand All @@ -132,10 +134,16 @@ def create(self, request, *args, **kwargs):
raise e
# Save the serializer first so we can access the image
observation = serializer.save(user=self.request.user)
observation.identification_response = identification.plantnet_identify(
image_path=observation.image.path, organ=observation.organ)
# observation.location, observation.datetime = identification.read_exif(observation.image.path)
observation.save()

# Run identification service
if observation.type == Species.PLANT_TYPE:
observation.identification_response = identification.plantnet_identify(
image_path=observation.image.path, organ=observation.organ)
observation.save()
elif observation.type == Species.BIRD_TYPE:
if not (observation.species and observation.species.type == Species.BIRD_TYPE):
raise serializers.ValidationError('For bird observations, a bird species must be provided')

serializer = ObservationSerializer(instance=observation)
return Response(serializer.data)

Expand Down

0 comments on commit 28a3c8c

Please sign in to comment.