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

Update add site visit api to accept occurrence photos #3477

Merged
merged 2 commits into from
Sep 15, 2023
Merged
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
7 changes: 7 additions & 0 deletions bims/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,11 @@ def get_users(self, obj):
filter_horizontal = ('users',)


class TaxonImageAdmin(admin.ModelAdmin):
list_display = ('taxonomy', 'source', 'date')
raw_id_fields = ('taxonomy', 'uploader', 'owner', 'survey')


# Re-register GeoNode's Profile page
admin.site.unregister(Profile)
admin.site.register(Profile, CustomUserAdmin)
Expand Down Expand Up @@ -1791,6 +1796,8 @@ def get_users(self, obj):
admin.site.register(RequestLog, RequestLogAdmin)
admin.site.register(IngestedData, IngestedDataAdmin)

admin.site.register(TaxonImage, TaxonImageAdmin)

admin.site.register(LocationContextGroup, LocationContextGroupAdmin)
admin.site.register(
LocationContextFilterGroupOrder, LocationContextFilterGroupOrderAdmin)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.1.10 on 2023-09-15 02:48

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('bims', '0347_locationsite_hydrogeomorphic_type_and_more'),
]

operations = [
migrations.AddField(
model_name='taxonimage',
name='date',
field=models.DateField(blank=True, null=True),
),
migrations.AddField(
model_name='taxonimage',
name='owner',
field=models.ForeignKey(blank=True, help_text='Owner of the taxon image (Optional)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='taxon_image_owner', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='taxonimage',
name='survey',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='bims.survey'),
),
migrations.AddField(
model_name='taxonimage',
name='uploader',
field=models.ForeignKey(blank=True, help_text='User who uploaded the taxon image (Optional)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='taxon_image_uploader', to=settings.AUTH_USER_MODEL),
),
]
26 changes: 26 additions & 0 deletions bims/models/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,32 @@ class TaxonImage(models.Model):
blank=True,
default=''
)
uploader = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
null=True,
blank=True,
help_text='User who uploaded the taxon image (Optional)',
related_name='taxon_image_uploader'
)
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
null=True,
blank=True,
help_text='Owner of the taxon image (Optional)',
related_name='taxon_image_owner'
)
date = models.DateField(
null=True,
blank=True
)
survey = models.ForeignKey(
'bims.Survey',
null=True,
blank=True,
on_delete=models.CASCADE
)


def check_taxa_duplicates(taxon_name, taxon_rank):
Expand Down
25 changes: 25 additions & 0 deletions mobile/api_views/add_site_visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from bims.models.chemical_record import ChemicalRecord

from bims.models.survey import Survey
from bims.models.taxonomy import TaxonImage
from django.core.files.base import ContentFile
from django.http import Http404
from rest_framework.permissions import IsAuthenticated
Expand Down Expand Up @@ -77,6 +78,30 @@ def post(self, request, *args, **kwargs):
survey.mobile = True
survey.save()

occurrence_photos = post_data.get('occurrence_photos', None)
if occurrence_photos:
for occurrence_photo in occurrence_photos:
occurrence_photo_file_name = (
f'{post_data["date"]}_{request.user.id}_'
f'{occurrence_photo["id"]}.jpeg'
)
base_64_string = occurrence_photo['base64Image']
header, image_data = base_64_string.split(",")[0], base_64_string.split(",")[1]
occurrence_photo_file = ContentFile(
base64.b64decode(
image_data,
),
name=occurrence_photo_file_name
)
TaxonImage.objects.create(
taxonomy_id=occurrence_photo['id'],
survey_id=survey.id,
taxon_image=occurrence_photo_file,
date=post_data.get('date'),
uploader=request.user,
source='Mobile App'
)

process_abiotic_data(
survey,
post_data.get('abiotic', [])
Expand Down
6 changes: 4 additions & 2 deletions mobile/api_views/river.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from guardian.mixins import LoginRequiredMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from bims.location_site.river import fetch_river_name
from sass.models import River


class FetchRiverName(LoginRequiredMixin, APIView):
class FetchRiverName(APIView):
permission_classes = (IsAuthenticated,)

def get(self, request, *args):
lat = request.GET.get('lat', '')
lon = request.GET.get('lon', '')
Expand Down
4 changes: 3 additions & 1 deletion mobile/api_views/wetland.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union, Dict

from guardian.mixins import LoginRequiredMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.response import Response

Expand Down Expand Up @@ -46,7 +47,8 @@ def fetch_wetland_data(latitude: float, longitude: float) -> Union[Dict, None]:
return wetland


class FetchWetland(LoginRequiredMixin, APIView):
class FetchWetland(APIView):
permission_classes = (IsAuthenticated,)

def get(self, request, *args):
lat = float(request.GET.get('lat', '0'))
Expand Down
5 changes: 5 additions & 0 deletions mobile/tests/test_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ def setUp(self):

@patch('mobile.api_views.river.fetch_river_name')
def test_fetch_river_name(self, mock_fetch_river_name):
user = UserF.create(is_superuser=True)
self.client.login(
username=user.username,
password='password'
)
mock_fetch_river_name.return_value = self.river.name
response = self.client.get(
reverse('fetch-river-name'),
Expand Down
Loading