Skip to content

Commit

Permalink
Add Export in ocr integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Jul 5, 2024
1 parent 925f373 commit 0eb1030
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 1 deletion.
19 changes: 19 additions & 0 deletions apps/entry/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from base64 import urlsafe_b64encode
import os
from django.contrib.contenttypes.fields import GenericRelation
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
Expand All @@ -21,6 +26,7 @@
Exportable,
)
from assisted_tagging.models import DraftEntry
from gallery.enums import ModuleTypeEnum


class EntryAttachment(models.Model):
Expand Down Expand Up @@ -66,6 +72,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):
"""
Expand Down
2 changes: 2 additions & 0 deletions apps/export/entries/excel_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions apps/export/entries/report_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions apps/export/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions apps/gallery/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import graphene


class ModuleTypeEnum(graphene.Enum):
ENTRY_ATTACHMENT = 'entry-attachment'
LEAD_PREVIEW_ATTACHMENT = 'lead-preview-attachment'
21 changes: 20 additions & 1 deletion apps/gallery/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down Expand Up @@ -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]

Expand Down
6 changes: 6 additions & 0 deletions deep/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
unsubscribe_email,
)
from gallery.views import (
AttachmentFileView,
FileView,
FileViewSet,
GoogleDriveFileViewSet,
Expand Down Expand Up @@ -435,6 +436,11 @@ def get_api_path(path):
DeprecatedPrivateFileView.as_view(),
name='deprecated_gallery_private_url',
),
path(
'external/private-file/<str:module>/<str:identifier>',
AttachmentFileView.as_view(),
name='external_private_url',
),
re_path(
r'^public-file/(?P<fidb64>[0-9A-Za-z]+)/(?P<token>.+)/(?P<filename>.*)$',
PublicFileView.as_view(),
Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4263,6 +4263,7 @@ enum ExportExcelSelectedStaticColumnEnum {
ENTRY_ID
LEAD_ENTRY_ID
ENTRY_EXCERPT
LEAD_ENTRY_ENTRY_ATTACHMENT_FILE_PREVIEW
}

enum ExportExportTypeEnum {
Expand Down

0 comments on commit 0eb1030

Please sign in to comment.