From 7b03c64837c400ca0c5951963b6c6669db1d6794 Mon Sep 17 00:00:00 2001 From: sudan45 Date: Fri, 5 Jul 2024 11:14:00 +0545 Subject: [PATCH] Add Export in ocr integration --- apps/entry/models.py | 18 ++++++++++++++++++ apps/export/entries/excel_exporter.py | 2 ++ apps/export/entries/report_exporter.py | 2 ++ apps/export/models.py | 1 + apps/gallery/enums.py | 6 ++++++ apps/gallery/views.py | 21 ++++++++++++++++++++- deep/urls.py | 6 ++++++ schema.graphql | 1 + 8 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 apps/gallery/enums.py diff --git a/apps/entry/models.py b/apps/entry/models.py index 4ae3209e22..bf7b8a1a9d 100644 --- a/apps/entry/models.py +++ b/apps/entry/models.py @@ -3,6 +3,10 @@ from django.contrib.postgres.aggregates.general import ArrayAgg from django.contrib.postgres.fields import ArrayField from django.db import models +from django.conf import settings +from django.urls import reverse +from django.utils.encoding import force_bytes +from django.utils.http import urlsafe_base64_encode from deep.middleware import get_current_user from unified_connector.models import ConnectorLeadPreviewAttachment @@ -21,6 +25,7 @@ Exportable, ) from assisted_tagging.models import DraftEntry +from gallery.enums import ModuleTypeEnum class EntryAttachment(models.Model): @@ -66,6 +71,19 @@ def clone_from_lead_attachment(cls, lead_attachment: LeadPreviewAttachment) -> ' entry_attachment.save() return entry_attachment + def get_file_url(self): + return '{protocol}://{domain}{url}'.format( + protocol=settings.HTTP_PROTOCOL, + domain=settings.DJANGO_API_HOST, + url=reverse( + 'external_private_url', + kwargs={ + 'module': ModuleTypeEnum.ENTRY_ATTACHMENT.value, + 'identifier': urlsafe_base64_encode(force_bytes(self.id)) + } + ) + ) + class Entry(UserResource, ProjectEntityMixin): """ diff --git a/apps/export/entries/excel_exporter.py b/apps/export/entries/excel_exporter.py index ec8d0452a8..649241311f 100644 --- a/apps/export/entries/excel_exporter.py +++ b/apps/export/entries/excel_exporter.py @@ -272,6 +272,8 @@ def add_entries_from_excel_data_for_static_column( if self.modified_excerpt_exists: return [entry_excerpt, entry.dropped_excerpt] return entry_excerpt + elif exportable == Export.StaticColumn.LEAD_ENTRY_ENTRY_ATTACHMENT_FILE_PREVIEW: + return f'{entry.entry_attachment.get_file_url()}' def add_entries_from_excel_data(self, rows, data, export_data): export_type = data.get('type') diff --git a/apps/export/entries/report_exporter.py b/apps/export/entries/report_exporter.py index 745919a6bd..f49a63df68 100644 --- a/apps/export/entries/report_exporter.py +++ b/apps/export/entries/report_exporter.py @@ -592,6 +592,8 @@ def _generate_for_entry(self, entry): if h_stats.get(key): image_text += f', {key.title()} values: {h_stats.get(key)}' if h_stats.get(key) else '' + if entry.entry_type == Entry.TagType.ATTACHMENT: + image = entry.entry_attachment.file_preview if image: self.doc.add_image(image) if image_text: diff --git a/apps/export/models.py b/apps/export/models.py index 25bf7680dc..925748d3cf 100644 --- a/apps/export/models.py +++ b/apps/export/models.py @@ -146,6 +146,7 @@ class StaticColumn(models.TextChoices): ENTRY_ID = 'entry_id', 'Entry Id' LEAD_ENTRY_ID = 'lead_entry_id', 'Source-Entry Id' ENTRY_EXCERPT = 'entry_excerpt', 'Modified Excerpt, Original Excerpt' + LEAD_ENTRY_ENTRY_ATTACHMENT_FILE_PREVIEW = 'lead_entry_entry_attachment_file_preview', 'EntryAttachment Url' # Used by extra options for Report class CitationStyle(models.IntegerChoices): diff --git a/apps/gallery/enums.py b/apps/gallery/enums.py new file mode 100644 index 0000000000..9ac6b501c7 --- /dev/null +++ b/apps/gallery/enums.py @@ -0,0 +1,6 @@ +import graphene + + +class ModuleTypeEnum(graphene.Enum): + ENTRY_ATTACHMENT = 'entry-attachment' + LEAD_PREVIEW_ATTACHMENT = 'lead-preview-attachment' diff --git a/apps/gallery/views.py b/apps/gallery/views.py index 79d8b21439..1977e83beb 100644 --- a/apps/gallery/views.py +++ b/apps/gallery/views.py @@ -7,6 +7,7 @@ from django.utils.http import urlsafe_base64_decode from django.shortcuts import redirect, get_object_or_404 +from gallery.enums import ModuleTypeEnum from rest_framework import ( views, viewsets, @@ -24,7 +25,7 @@ from deep.permalinks import Permalink from project.models import Project from lead.models import Lead -from entry.models import Entry +from entry.models import Entry, EntryAttachment from user_resource.filters import UserResourceFilterSet from utils.extractor.formats import ( @@ -82,6 +83,24 @@ def get(self, request, uuid=None, filename=None): ) +class AttachmentFileView(views.APIView): + permission_classes = [permissions.IsAuthenticated] + + def get(self, request, module=None, identifier=None): + + if module == ModuleTypeEnum.ENTRY_ATTACHMENT.value: + id = force_text(urlsafe_base64_decode(identifier)) + qs = get_object_or_404(EntryAttachment, id=id) + if qs: + return redirect(request.build_absolute_uri(qs.file.url)) + return response.Response({ + 'error': 'File doesn\'t exists', + }, status=status.HTTP_404_NOT_FOUND) + return response.Response({ + 'error': 'Access Forbidden, Contact Admin', + }, status=status.HTTP_403_FORBIDDEN) + + class DeprecatedPrivateFileView(views.APIView): permission_classes = [permissions.IsAuthenticated] diff --git a/deep/urls.py b/deep/urls.py index 88b1f0ca7b..b63c873fb6 100644 --- a/deep/urls.py +++ b/deep/urls.py @@ -26,6 +26,7 @@ unsubscribe_email, ) from gallery.views import ( + AttachmentFileView, FileView, FileViewSet, GoogleDriveFileViewSet, @@ -435,6 +436,11 @@ def get_api_path(path): DeprecatedPrivateFileView.as_view(), name='deprecated_gallery_private_url', ), + path( + 'external/private-file//', + AttachmentFileView.as_view(), + name='external_private_url', + ), re_path( r'^public-file/(?P[0-9A-Za-z]+)/(?P.+)/(?P.*)$', PublicFileView.as_view(), diff --git a/schema.graphql b/schema.graphql index 3aa54dd23a..420a742f55 100644 --- a/schema.graphql +++ b/schema.graphql @@ -4263,6 +4263,7 @@ enum ExportExcelSelectedStaticColumnEnum { ENTRY_ID LEAD_ENTRY_ID ENTRY_EXCERPT + LEAD_ENTRY_ENTRY_ATTACHMENT_FILE_PREVIEW } enum ExportExportTypeEnum {