Skip to content

Commit

Permalink
Add relation of leadattachment in entryattachment model
Browse files Browse the repository at this point in the history
Update testcases
  • Loading branch information
sudan45 committed Jun 13, 2024
1 parent f1d4615 commit f9e973b
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 265 deletions.
2 changes: 1 addition & 1 deletion apps/ary/export/affected_groups_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def get_affected_groups_info(assessment):
affected_group_type_dict = {choice.value: choice.label for choice in AssessmentRegistry.AffectedGroupType}
affected_groups = [affected_group_type_dict.get(group) for group in assessment.affected_groups if group]
max_level = max([len(v.split('/')) for k, v in AssessmentRegistry.AffectedGroupType.choices])
levels = [f'Level {i+1}' for i in range(max_level)]
levels = [f'Level {i + 1}' for i in range(max_level)]
affected_grp_list = []
for group in affected_groups:
group = group.split("/")
Expand Down
52 changes: 33 additions & 19 deletions apps/deepl_integration/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,38 +655,52 @@ def save_data(
# Save extracted images as LeadPreviewAttachment instances
# TODO: The logic is same for unified_connector leads as well. Maybe have a single func?

attachement_base_path = f'{lead.pk}'
attachment_base_path = f'{lead.pk}'
for image_uri in images_uri:
for image in image_uri['images']:
lead_attachement = LeadPreviewAttachment(lead=lead)
lead_attachment = LeadPreviewAttachment(lead=lead)
image_obj = RequestHelper(url=image, ignore_error=True).get_file()
if image_obj:
lead_attachement.file.save(
os.path.join(attachement_base_path, os.path.basename(urlparse(image).path)),
lead_attachment.file.save(
os.path.join(
attachment_base_path,
os.path.basename(
urlparse(image).path
)
),
image_obj
)
lead_attachement.page_number = image_uri['page_number']
lead_attachement.type = LeadPreviewAttachment.AttachementFileType.IMAGE
lead_attachement.file_preview = lead_attachement.file

lead_attachement.save()
lead_attachment.page_number = image_uri['page_number']
lead_attachment.type = LeadPreviewAttachment.AttachmentFileType.IMAGE
lead_attachment.file_preview = lead_attachment.file
lead_attachment.save()

for table in table_uri:
lead_attachement = LeadPreviewAttachment(lead=lead)
lead_attachment = LeadPreviewAttachment(lead=lead)
table_img = RequestHelper(url=table['image_link'], ignore_error=True).get_file()
table_attahcment = RequestHelper(url=table['content_link'], ignore_error=True).get_file()
table_attachment = RequestHelper(url=table['content_link'], ignore_error=True).get_file()
if table_img:
lead_attachement.file_preview.save(
os.path.join(attachement_base_path, os.path.basename(urlparse(table['image_link']).path)),
lead_attachment.file_preview.save(
os.path.join(
attachment_base_path,
os.path.basename(
urlparse(table['image_link']).path
)
),
table_img
)
lead_attachement.page_number = table['page_number']
lead_attachement.type = LeadPreviewAttachment.AttachementFileType.XLSX
lead_attachement.file.save(
os.path.join(attachement_base_path, os.path.basename(urlparse(table['content_link']).path)),
table_attahcment
lead_attachment.page_number = table['page_number']
lead_attachment.type = LeadPreviewAttachment.AttachmentFileType.XLSX
lead_attachment.file.save(
os.path.join(
attachment_base_path,
os.path.basename(
urlparse(table['content_link']).path
)
),
table_attachment
)
lead_attachement.save()
lead_attachment.save()

lead.update_extraction_status(Lead.ExtractionStatus.SUCCESS)
return lead
Expand Down
10 changes: 8 additions & 2 deletions apps/deepl_integration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ class LeadExtractCallbackSerializer(DeeplServerBaseCallbackSerializer):
"""
url = serializers.CharField(required=False)
# Data fields
images_path = serializers.ListSerializer(child=ImagePathSerializer(required=False))
tables_path = serializers.ListSerializer(child=TablePathSerializer(required=False))
images_path = serializers.ListSerializer(
child=ImagePathSerializer(required=True),
required=False
)
tables_path = serializers.ListSerializer(
child=TablePathSerializer(required=True),
required=False
)
text_path = serializers.CharField(required=False, allow_null=True)
total_words_count = serializers.IntegerField(required=False, default=0, allow_null=True)
total_pages = serializers.IntegerField(required=False, default=0, allow_null=True)
Expand Down
7 changes: 5 additions & 2 deletions apps/entry/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class EntryAdmin(VersionAdmin):
)
autocomplete_fields = (
'lead', 'project', 'created_by', 'modified_by', 'analysis_framework', 'tabular_field',
'image', 'controlled_changed_by', 'verified_by',
'image', 'controlled_changed_by', 'verified_by', 'entry_attachment',
)
ordering = ('project', 'created_by', 'created_at')

Expand All @@ -87,6 +87,9 @@ class ProjectEntryLabelAdmin(VersionAdmin):
list_display = ('__str__', 'color')


admin.site.register(EntryAttachment)
@admin.register(EntryAttachment)
class EntryAttachmentAdmin(VersionAdmin):
search_fields = ['entry_file_type',]


reversion.register(LeadEntryGroup)
7 changes: 6 additions & 1 deletion apps/entry/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
get_enum_name_from_django_field,
)

from .models import Entry
from .models import Entry, EntryAttachment

EntryTagTypeEnum = convert_enum_to_graphene_enum(Entry.TagType, name='EntryTagTypeEnum')
EntryAttachmentTypeEnum = convert_enum_to_graphene_enum(
EntryAttachment.EntryFileType,
name='EntryFileType'
)

enum_map = {
get_enum_name_from_django_field(field): enum
for field, enum in (
(Entry.entry_type, EntryTagTypeEnum),
(EntryAttachment.entry_file_type, EntryAttachmentTypeEnum),
)
}
23 changes: 23 additions & 0 deletions apps/entry/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from factory.django import DjangoModelFactory

from gallery.factories import FileFactory
from django.core.files.base import ContentFile


from .models import (
Entry,
Attribute,
EntryComment,
EntryAttachment
)


Expand Down Expand Up @@ -48,3 +51,23 @@ class Meta:
class EntryCommentFactory(DjangoModelFactory):
class Meta:
model = EntryComment


class EntryAttachmentFactory(DjangoModelFactory):
class Meta:
model = EntryAttachment

file = factory.LazyAttribute(
lambda _: ContentFile(
factory.django.ImageField()._make_data(
{'width': 1024, 'height': 768}
), 'example.jpg'
)
)
file_preview = factory.LazyAttribute(
lambda _: ContentFile(
factory.django.ImageField()._make_data(
{'width': 1024, 'height': 768}
), 'example.jpg'
)
)
25 changes: 25 additions & 0 deletions apps/entry/migrations/0041_auto_20240611_0810.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.25 on 2024-06-11 08:10

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


class Migration(migrations.Migration):

dependencies = [
('lead', '0053_alter_leadpreviewattachment_type'),
('entry', '0040_alter_entryattachment_entry_file_type'),
]

operations = [
migrations.AddField(
model_name='entryattachment',
name='lead_attachment',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='lead.leadpreviewattachment'),
),
migrations.AlterField(
model_name='entryattachment',
name='entry_file_type',
field=models.PositiveSmallIntegerField(choices=[(1, 'XLSX'), (2, 'Image')], default=1),
),
]
13 changes: 9 additions & 4 deletions apps/entry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from gallery.models import File
from user.models import User
from user_resource.models import UserResource
from lead.models import Lead
from lead.models import Lead, LeadPreviewAttachment
from notification.models import Assignment
from analysis_framework.models import (
AnalysisFramework,
Expand All @@ -21,17 +21,22 @@
from assisted_tagging.models import DraftEntry


class EntryAttachment(models.Model):
class EntryAttachment(models.Model): # The entry will make reference to it as entry_attachment.
class EntryFileType(models.IntegerChoices):
XLSX = 1, 'XLSX'
XLSX = 1, 'XLSX',
IMAGE = 2, 'Image',

lead_attachment = models.ForeignKey(LeadPreviewAttachment, on_delete=models.SET_NULL, null=True)
entry_file_type = models.PositiveSmallIntegerField(
choices=EntryFileType.choices,
default=EntryFileType.XLSX
default=EntryFileType.XLSX,
)
file = models.FileField(upload_to='entry/attachment/')
file_preview = models.FileField(upload_to='entry/attachment-preview')

def __str__(self):
return f'{self.file}'


class Entry(UserResource, ProjectEntityMixin):
"""
Expand Down
13 changes: 9 additions & 4 deletions apps/entry/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Attribute,
EntryAttachment,
)
from .enums import EntryTagTypeEnum
from .enums import EntryAttachmentTypeEnum, EntryTagTypeEnum
from .filter_set import EntryGQFilterSet


Expand Down Expand Up @@ -86,11 +86,16 @@ def resolve_geo_selected_options(root, info, **_):


class EntryAttachmentType(DjangoObjectType):
file = graphene.Field(FileFieldType)
file_preview = graphene.Field(FileFieldType)
lead_attachment_id = graphene.ID(required=True)
file = graphene.Field(FileFieldType, required=True)
file_preview = graphene.Field(FileFieldType, required=True)
entry_file_type = graphene.Field(EntryAttachmentTypeEnum, required=True)

class Meta:
model = EntryAttachment
only_fields = (
'id',
)


class EntryType(UserResourceMixin, ClientIdMixin, DjangoObjectType):
Expand All @@ -101,7 +106,7 @@ class Meta:
'lead', 'project', 'analysis_framework', 'information_date', 'order',
'excerpt', 'dropped_excerpt', 'image', 'tabular_field', 'highlight_hidden',
'controlled', 'controlled_changed_by',
'client_id', 'entry_attachment'
'client_id',
)

entry_type = graphene.Field(EntryTagTypeEnum, required=True)
Expand Down
20 changes: 8 additions & 12 deletions apps/entry/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from .models import (
Attribute,
Entry,
EntryAttachment,
EntryComment,
EntryCommentText,
ExportData,
Expand All @@ -39,7 +38,7 @@
LeadEntryGroup,
EntryGroupLabel,
)
from .utils import base64_to_deep_image
from .utils import base64_to_deep_image, leadattachment_to_entryattachment

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -598,7 +597,6 @@ class EntryGqSerializer(ProjectPropertySerializerMixin, TempClientIdMixin, UserR
queryset=LeadPreviewAttachment.objects.all(),
help_text=(
'This is used to add attachment from Lead Preview Attachment.'
' This will be changed into gallery image and supplied back in image field.'
)
)

Expand Down Expand Up @@ -636,6 +634,11 @@ def validate_lead(self, lead):
raise serializers.ValidationError('Changing lead is not allowed')
return lead

def validate_lead_attachment(self, lead_attachment):
if int(self.initial_data.get('lead')) != lead_attachment.lead.id:
raise serializers.ValidationError("Don't have access to this lead")
return lead_attachment

def validate(self, data):
"""
- Lead image is copied to deep gallery files
Expand Down Expand Up @@ -687,20 +690,13 @@ def validate(self, data):
})
# If lead image is provided make sure lead are same
elif lead_attachment:
data.pop('excerpt', None) # removing excerpt when lead attachment is send
if lead_attachment.lead != lead:
raise serializers.ValidationError({
'lead_attachment': f'You don\'t have permission to attach lead attachment: {lead_attachment}',
})

if lead_attachment.type == LeadPreviewAttachment.AttachementFileType.XLSX:
data['entry_attachment'] = EntryAttachment.objects.create(
file=lead_attachment.file,
file_preview=lead_attachment.file_preview
)
data['entry_type'] = Entry.TagType.ATTACHMENT
else:
data['image'] = lead_attachment.clone_as_deep_file(request.user)
data['entry_type'] = Entry.TagType.IMAGE
data['entry_attachment'] = leadattachment_to_entryattachment(lead_attachment)
elif image_raw:
generated_image = base64_to_deep_image(image_raw, lead, request.user)
if isinstance(generated_image, File):
Expand Down
Loading

0 comments on commit f9e973b

Please sign in to comment.