Skip to content

Commit

Permalink
Merge branch 'dbca-wa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrutihindocha authored Feb 7, 2024
2 parents 47f6013 + eba8097 commit a0492e8
Show file tree
Hide file tree
Showing 11 changed files with 669 additions and 2 deletions.
100 changes: 100 additions & 0 deletions boranga/components/occurrence/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
LocationAccuracy,
Location,
ObserverDetail,
OccurrenceReportDocument,
)
from boranga.components.occurrence.serializers import(
ListOccurrenceReportSerializer,
Expand All @@ -94,6 +95,8 @@
ListInternalOccurrenceReportSerializer,
OccurrenceReportUserActionSerializer,
OccurrenceReportLogEntrySerializer,
OccurrenceReportDocumentSerializer,
SaveOccurrenceReportDocumentSerializer,
)

from boranga.components.occurrence.utils import (
Expand Down Expand Up @@ -1025,6 +1028,29 @@ def add_comms_log(self, request, *args, **kwargs):
print(traceback.print_exc())
raise serializers.ValidationError(str(e))

@detail_route(methods=['GET',], detail=True)
def documents(self, request, *args, **kwargs):
try:
instance = self.get_object()
qs = instance.documents.all()
if is_internal(self.request):
qs = qs
elif is_customer(self.request):
qs = qs.filter(Q(uploaded_by=request.user.id))
# qs = qs.exclude(input_name='occurrence_report_approval_doc') # TODO do we need/not to show approval doc in cs documents tab
qs = qs.order_by('-uploaded_date')
serializer = OccurrenceReportDocumentSerializer(qs,many=True, context={'request':request})
return Response(serializer.data)
except serializers.ValidationError:
print(traceback.print_exc())
raise
except ValidationError as e:
print(traceback.print_exc())
raise serializers.ValidationError(repr(e.error_dict))
except Exception as e:
print(traceback.print_exc())
raise serializers.ValidationError(str(e))


class ObserverDetailViewSet(viewsets.ModelViewSet):
queryset = ObserverDetail.objects.all().order_by('id')
Expand Down Expand Up @@ -1064,5 +1090,79 @@ def create(self, request, *args, **kwargs):
raise serializers.ValidationError(str(e))


class OccurrenceReportDocumentViewSet(viewsets.ModelViewSet):
queryset = OccurrenceReportDocument.objects.all().order_by('id')
serializer_class = OccurrenceReportDocumentSerializer

@detail_route(methods=['GET',], detail=True)
def discard(self, request, *args, **kwargs):
try:
instance = self.get_object()
instance.visible = False
instance.save()
serializer = self.get_serializer(instance)
return Response(serializer.data)
except serializers.ValidationError:
print(traceback.print_exc())
raise
except ValidationError as e:
print(traceback.print_exc())
raise serializers.ValidationError(repr(e.error_dict))
except Exception as e:
print(traceback.print_exc())
raise serializers.ValidationError(str(e))

@detail_route(methods=['GET',], detail=True)
def reinstate(self, request, *args, **kwargs):
try:
instance = self.get_object()
instance.visible = True
instance.save()
serializer = self.get_serializer(instance)
return Response(serializer.data)
except serializers.ValidationError:
print(traceback.print_exc())
raise
except ValidationError as e:
print(traceback.print_exc())
raise serializers.ValidationError(repr(e.error_dict))
except Exception as e:
print(traceback.print_exc())
raise serializers.ValidationError(str(e))

def update(self, request, *args, **kwargs):
try:
instance = self.get_object()
serializer = SaveOccurrenceReportDocumentSerializer(instance, data=json.loads(request.data.get('data')))
serializer.is_valid(raise_exception=True)
serializer.save()
instance.add_documents(request)
instance.uploaded_by = request.user.id
instance.save()
return Response(serializer.data)
except Exception as e:
print(traceback.print_exc())
raise serializers.ValidationError(str(e))


def create(self, request, *args, **kwargs):
try:
serializer = SaveOccurrenceReportDocumentSerializer(data= json.loads(request.data.get('data')))
serializer.is_valid(raise_exception = True)
instance = serializer.save()
instance.add_documents(request)
instance.uploaded_by = request.user.id
instance.save()
return Response(serializer.data)
except serializers.ValidationError:
print(traceback.print_exc())
raise
except ValidationError as e:
if hasattr(e,'error_dict'):
raise serializers.ValidationError(repr(e.error_dict))
else:
if hasattr(e,'message'):
raise serializers.ValidationError(e.message)
except Exception as e:
print(traceback.print_exc())
raise serializers.ValidationError(str(e))
56 changes: 56 additions & 0 deletions boranga/components/occurrence/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
def update_occurrence_report_comms_log_filename(instance, filename):
return '{}/occurrence_report/{}/communications/{}'.format(settings.MEDIA_APP_DIR, instance.log_entry.occurrence_report.id,filename)

def update_occurrence_report_doc_filename(instance, filename):
return '{}/occurrence_report/{}/documents/{}'.format(settings.MEDIA_APP_DIR, instance.occurrence_report.id,filename)


class OccurrenceReport(models.Model):
"""
Expand Down Expand Up @@ -1224,3 +1227,56 @@ class Meta:

def __str__(self):
return str(self.occurrence_report)


class OccurrenceReportDocument(Document):
document_number = models.CharField(max_length=9, blank=True, default='')
occurrence_report = models.ForeignKey('OccurrenceReport',related_name='documents', on_delete=models.CASCADE)
_file = models.FileField(upload_to=update_occurrence_report_doc_filename, max_length=512, storage=private_storage)
input_name = models.CharField(max_length=255,null=True,blank=True)
can_delete = models.BooleanField(default=True) # after initial submit prevent document from being deleted
can_hide= models.BooleanField(default=False) # after initial submit, document cannot be deleted but can be hidden
hidden=models.BooleanField(default=False) # after initial submit prevent document from being deleted # Priya alternatively used below visible field in boranga
visible = models.BooleanField(default=True) # to prevent deletion on file system, hidden and still be available in history
document_category = models.ForeignKey(DocumentCategory,
null=True,
blank=True,
on_delete=models.SET_NULL)
document_sub_category = models.ForeignKey(DocumentSubCategory,
null=True,
blank=True,
on_delete=models.SET_NULL)
uploaded_by = models.IntegerField(null=True) # EmailUserRO

class Meta:
app_label = 'boranga'
verbose_name = "Occurrence Report Document"

def save(self, *args, **kwargs):
# Prefix "D" char to document_number.
super(OccurrenceReportDocument, self).save(*args,**kwargs)
if self.document_number == '':
new_document_id = 'D{}'.format(str(self.pk))
self.document_number = new_document_id
self.save()

def add_documents(self, request):
with transaction.atomic():
try:
# save the files
data = json.loads(request.data.get('data'))
# if not data.get('update'):
# documents_qs = self.filter(input_name='species_doc', visible=True)
# documents_qs.delete()
for idx in range(data['num_files']):
_file = request.data.get('file-'+str(idx))
self._file=_file
self.name=_file.name
self.input_name = data['input_name']
self.can_delete = True
self.save()
# end save documents
self.save()
except:
raise
return
48 changes: 48 additions & 0 deletions boranga/components/occurrence/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
OccurrenceReportGeometry,
OccurrenceReportLogEntry,
OccurrenceReportUserAction,
OccurrenceReportDocument,
)

from boranga.components.users.serializers import UserSerializer
Expand Down Expand Up @@ -893,3 +894,50 @@ class Meta:

def get_documents(self, obj):
return [[d.name, d._file.url] for d in obj.documents.all()]


class OccurrenceReportDocumentSerializer(serializers.ModelSerializer):
document_category_name = serializers.SerializerMethodField()
document_sub_category_name = serializers.SerializerMethodField()
class Meta:
model = OccurrenceReportDocument
fields = (
'id',
'document_number',
'occurrence_report',
'name',
'_file',
'description',
'input_name',
'uploaded_date',
'document_category',
'document_category_name',
'document_sub_category',
'document_sub_category_name',
'visible',
)
read_only_fields = ('id','document_number')

def get_document_category_name(self,obj):
if obj.document_category:
return obj.document_category.document_category_name

def get_document_sub_category_name(self,obj):
if obj.document_sub_category:
return obj.document_sub_category.document_sub_category_name


class SaveOccurrenceReportDocumentSerializer(serializers.ModelSerializer):
class Meta:
model = OccurrenceReportDocument
fields = (
'id',
'occurrence_report',
'name',
'description',
'input_name',
'uploaded_date',
'document_category',
'document_sub_category',
)

2 changes: 1 addition & 1 deletion boranga/components/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class UserSerializer(serializers.ModelSerializer):
is_department_user = serializers.SerializerMethodField()
#is_payment_admin = serializers.SerializerMethodField()
system_settings= serializers.SerializerMethodField()
is_payment_admin = serializers.SerializerMethodField()
# is_payment_admin = serializers.SerializerMethodField()
is_boranga_admin = serializers.SerializerMethodField()

class Meta:
Expand Down
1 change: 1 addition & 0 deletions boranga/frontend/boranga/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ module.exports = {

occurrence_report:"/api/occurrence_report",
observer_detail:"/api/observer_detail.json",
occurrence_report_documents:"/api/occurrence_report_documents.json",

//approvals_paginated:"/api/approvals/user_list_paginated/?format=datatables",
//compliances_paginated:"/api/compliances/user_list_paginated/?format=datatables",
Expand Down
Loading

0 comments on commit a0492e8

Please sign in to comment.