diff --git a/boranga/components/occurrence/api.py b/boranga/components/occurrence/api.py index 063b93f9..628342e3 100644 --- a/boranga/components/occurrence/api.py +++ b/boranga/components/occurrence/api.py @@ -75,6 +75,7 @@ LocationAccuracy, Location, ObserverDetail, + OccurrenceReportDocument, ) from boranga.components.occurrence.serializers import( ListOccurrenceReportSerializer, @@ -94,6 +95,8 @@ ListInternalOccurrenceReportSerializer, OccurrenceReportUserActionSerializer, OccurrenceReportLogEntrySerializer, + OccurrenceReportDocumentSerializer, + SaveOccurrenceReportDocumentSerializer, ) from boranga.components.occurrence.utils import ( @@ -1051,6 +1054,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') @@ -1090,5 +1116,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)) diff --git a/boranga/components/occurrence/models.py b/boranga/components/occurrence/models.py index f5a883bf..cd3c9e9f 100644 --- a/boranga/components/occurrence/models.py +++ b/boranga/components/occurrence/models.py @@ -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): """ @@ -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 diff --git a/boranga/components/occurrence/serializers.py b/boranga/components/occurrence/serializers.py index f15ab361..d4383e6d 100644 --- a/boranga/components/occurrence/serializers.py +++ b/boranga/components/occurrence/serializers.py @@ -28,6 +28,7 @@ OccurrenceReportGeometry, OccurrenceReportLogEntry, OccurrenceReportUserAction, + OccurrenceReportDocument, ) from boranga.components.users.serializers import UserSerializer @@ -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', + ) + diff --git a/boranga/components/users/serializers.py b/boranga/components/users/serializers.py index 44d63627..b5ce9cae 100755 --- a/boranga/components/users/serializers.py +++ b/boranga/components/users/serializers.py @@ -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: diff --git a/boranga/frontend/boranga/src/api.js b/boranga/frontend/boranga/src/api.js index 5c7401ff..c9c9510f 100644 --- a/boranga/frontend/boranga/src/api.js +++ b/boranga/frontend/boranga/src/api.js @@ -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", diff --git a/boranga/frontend/boranga/src/components/common/occurrence/ocr_documents.vue b/boranga/frontend/boranga/src/components/common/occurrence/ocr_documents.vue new file mode 100644 index 00000000..950ab794 --- /dev/null +++ b/boranga/frontend/boranga/src/components/common/occurrence/ocr_documents.vue @@ -0,0 +1,361 @@ + + + + + diff --git a/boranga/frontend/boranga/src/components/form_occurrence_report.vue b/boranga/frontend/boranga/src/components/form_occurrence_report.vue index 92ff843d..aa46c2ad 100644 --- a/boranga/frontend/boranga/src/components/form_occurrence_report.vue +++ b/boranga/frontend/boranga/src/components/form_occurrence_report.vue @@ -42,6 +42,19 @@ Observation +