Skip to content

Commit

Permalink
add API for tagged turles as JSON, rename dbdump
Browse files Browse the repository at this point in the history
  • Loading branch information
ehallein committed Oct 24, 2023
1 parent e8ddf95 commit 642c3f2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion observations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class LineTransectEncounterDetail(DetailViewBreadcrumbMixin, DetailView):


#This just dumps the database as json for use by external tools such as PowerBI or Shiny
def dbdump(request):
def nestAndTracks(request):
query = '''
SELECT
e."id",
Expand Down
78 changes: 78 additions & 0 deletions turtle_tags/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from django.views.generic import ListView, DetailView
import json
from wastd.utils import search_filter, Breadcrumb
from django.db import connection
from django.http import StreamingHttpResponse
import json
import datetime

from .models import (
Turtle,
Expand Down Expand Up @@ -75,3 +79,77 @@ def get_context_data(self, **kwargs):
context["encounters_points"] = None

return context


#get the tagged turtles
def taggedTurtles(request):
query = '''
SELECT
t.id AS turtle_id,
-- TO_CHAR(t.created AT TIME ZONE \'Australia/Perth\', \'YYYY-MM-DD\') AS "created",
t.species,
t.sex,
t.name AS turtle_name,
t.comments AS turtle_comments,
tobs.tag_type,
tobs.name AS tag_name,
tobs.status AS tag_status,
tobs.tag_location,
tobs.comments AS tag_comments,
TO_CHAR(enc.when AT TIME ZONE \'Australia/Perth\', \'YYYY-MM-DD\') AS "encounter_date",
ST_Y(enc."where") as latitude,
ST_X(enc."where") as longitude,
enc.name AS encouter_name,
enc.encounter_type,
site."name" AS "site_name",
area."name" AS "area_name"
FROM
turtle_tags_turtle AS t
JOIN
turtle_tags_turtletag AS tt ON t.id = tt.turtle_id
JOIN
observations_tagobservation AS tobs ON tt.tag_type = tobs.tag_type AND tt.serial = tobs.name
LEFT JOIN
"observations_observation" obs ON (obs."id" = tobs."observation_ptr_id")
LEFT JOIN
"observations_encounter" enc ON (enc."id" = obs."encounter_id")
LEFT JOIN
"observations_area" site ON (enc."site_id" = site."id")
LEFT JOIN
"observations_area" area ON (enc."area_id" = area."id")
ORDER BY
t.id;
'''
response = StreamingHttpResponse(stream_data(query), content_type="application/json")
return response

class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
return super(DateTimeEncoder, self).default(obj)

def stream_data(query):
with connection.cursor() as cursor:
cursor.execute(query)

# Get column names from cursor.description
columns = [col[0] for col in cursor.description]

yield '[' # Start of JSON array
first_row = True
row = cursor.fetchone()
while row:
if not first_row:
yield ','
else:
first_row = False

# Convert row data to dictionary with column names as keys
row_dict = dict(zip(columns, row))

# Convert the dictionary to JSON and yield
yield json.dumps(row_dict, cls=DateTimeEncoder)

row = cursor.fetchone()
yield ']' # End of JSON array
4 changes: 3 additions & 1 deletion wastd/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
)
from users.api import UserListResource, UserDetailResource
from observations import views
from turtle_tags import views as tagviews

urlpatterns = [
# observations
path('dbdump/', views.dbdump, name='dbdump'),
path('nestAndTracks/', views.nestAndTracks, name='nestAndTracks'),
path('taggedTurtles/', tagviews.taggedTurtles, name='taggedTurtles'),
path("users/", UserListResource.as_view(), name="user_list_resource"),
path("users/<int:pk>/", UserDetailResource.as_view(), name="user_detail_resource"),
path("areas/", AreaListResource.as_view(), name="area_list_resource"),
Expand Down

0 comments on commit 642c3f2

Please sign in to comment.