Skip to content

Commit

Permalink
Merge branch 'main' into dev/spectrogram-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
BryonLewis committed Apr 1, 2024
2 parents 24bda46 + 7f21089 commit b563d92
Show file tree
Hide file tree
Showing 24 changed files with 5,167 additions and 4,636 deletions.
1 change: 1 addition & 0 deletions bats_ai/core/admin/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AnnotationsAdmin(admin.ModelAdmin):
'end_time',
'low_freq',
'high_freq',
'type',
'comments',
]
list_select_related = True
Expand Down
17 changes: 17 additions & 0 deletions bats_ai/core/migrations/0009_annotations_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.13 on 2024-03-22 17:23

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('core', '0008_grtscells_recording_recorded_time'),
]

operations = [
migrations.AddField(
model_name='annotations',
name='type',
field=models.TextField(blank=True, null=True),
),
]
1 change: 1 addition & 0 deletions bats_ai/core/models/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class Annotations(models.Model):
end_time = models.IntegerField(blank=True, null=True)
low_freq = models.IntegerField(blank=True, null=True)
high_freq = models.IntegerField(blank=True, null=True)
type = models.TextField(blank=True, null=True)
species = models.ManyToManyField(Species)
comments = models.TextField(blank=True, null=True)
45 changes: 44 additions & 1 deletion bats_ai/core/views/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class AnnotationSchema(Schema):
high_freq: int
species: list[SpeciesSchema]
comments: str
id: int = None
type: str | None = None
id: int | None = None
owner_email: str = None

@classmethod
Expand All @@ -69,6 +70,7 @@ def from_orm(cls, obj, owner_email=None, **kwargs):
species=[SpeciesSchema.from_orm(species) for species in obj.species.all()],
comments=obj.comments,
id=obj.id,
type=obj.type,
owner_email=owner_email, # Include owner_email in the schema
)

Expand All @@ -80,6 +82,7 @@ class UpdateAnnotationsSchema(Schema):
high_freq: int | None
species: list[SpeciesSchema] | None
comments: str | None
type: str | None
id: int | None


Expand Down Expand Up @@ -204,6 +207,39 @@ def get_recordings(request: HttpRequest, public: bool | None = None):
return list(recordings)


@router.get('/{id}/')
def get_recording(request: HttpRequest, id: int):
# Filter recordings based on the owner's id or public=True
try:
recordings = Recording.objects.filter(pk=id).values()
if len(recordings) > 0:
recording = recordings[0]

user = User.objects.get(id=recording['owner_id'])
recording['owner_username'] = user.username
recording['audio_file_presigned_url'] = default_storage.url(recording['audio_file'])
recording['hasSpectrogram'] = Recording.objects.get(id=recording['id']).has_spectrogram
if recording['recording_location']:
recording['recording_location'] = json.loads(recording['recording_location'].json)
unique_users_with_annotations = (
Annotations.objects.filter(recording_id=recording['id'])
.values('owner')
.distinct()
.count()
)
recording['userAnnotations'] = unique_users_with_annotations
user_has_annotations = Annotations.objects.filter(
recording_id=recording['id'], owner=request.user
).exists()
recording['userMadeAnnotations'] = user_has_annotations

return recording
else:
return {'error': 'Recording not found'}
except Recording.DoesNotExist:
return {'error': 'Recording not found'}


@router.get('/{id}/spectrogram')
def get_spectrogram(request: HttpRequest, id: int):
try:
Expand Down Expand Up @@ -461,6 +497,7 @@ def put_annotation(
low_freq=annotation.low_freq,
high_freq=annotation.high_freq,
comments=annotation.comments,
type=annotation.type,
)

# Add species to the annotation based on the provided species_ids
Expand Down Expand Up @@ -508,6 +545,10 @@ def patch_annotation(
annotation_instance.low_freq = annotation.low_freq
if annotation.high_freq:
annotation_instance.high_freq = annotation.high_freq
if annotation.type:
annotation_instance.type = annotation.type
else:
annotation_instance.type = None
if annotation.comments:
annotation_instance.comments = annotation.comments
annotation_instance.save()
Expand Down Expand Up @@ -562,6 +603,8 @@ def patch_temporal_annotation(
annotation_instance.comments = annotation.comments
if annotation.type:
annotation_instance.type = annotation.type
else:
annotation_instance.type = None
annotation_instance.save()

# Clear existing species associations
Expand Down
Loading

0 comments on commit b563d92

Please sign in to comment.