Skip to content

Commit

Permalink
Add mutation of EntryAttachment
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Jun 10, 2024
1 parent 6459b96 commit 2c39255
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 16 deletions.
3 changes: 3 additions & 0 deletions apps/entry/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from entry.models import (
Entry,
Attribute,
EntryAttachment,
FilterData,
ExportData,
EntryComment,
Expand Down Expand Up @@ -86,4 +87,6 @@ class ProjectEntryLabelAdmin(VersionAdmin):
list_display = ('__str__', 'color')


admin.site.register(EntryAttachment)

reversion.register(LeadEntryGroup)
29 changes: 29 additions & 0 deletions apps/entry/migrations/0038_auto_20240609_0904.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.25 on 2024-06-09 09:04

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


class Migration(migrations.Migration):

dependencies = [
('entry', '0037_merge_20220401_0527'),
]

operations = [
migrations.AlterField(
model_name='entry',
name='entry_type',
field=models.CharField(choices=[('excerpt', 'Excerpt'), ('image', 'Image'), ('attachment', 'Attachment'), ('dataSeries', 'Data Series')], default='excerpt', max_length=10),
),
migrations.CreateModel(
name='EntryAttachment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('entry_file_type', models.PositiveSmallIntegerField(choices=[('1', 'XLSX')], default='1')),
('file', models.FileField(upload_to='entry/attachment/')),
('file_preview', models.FileField(upload_to='entry/attachment-preview')),
('entry', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='entry.entry')),
],
),
]
23 changes: 23 additions & 0 deletions apps/entry/migrations/0039_auto_20240609_0933.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.25 on 2024-06-09 09:33

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


class Migration(migrations.Migration):

dependencies = [
('entry', '0038_auto_20240609_0904'),
]

operations = [
migrations.RemoveField(
model_name='entryattachment',
name='entry',
),
migrations.AddField(
model_name='entry',
name='entry_attachment',
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='entry.entryattachment'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.25 on 2024-06-09 10:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('entry', '0039_auto_20240609_0933'),
]

operations = [
migrations.AlterField(
model_name='entryattachment',
name='entry_file_type',
field=models.PositiveSmallIntegerField(choices=[(1, 'XLSX')], default=1),
),
]
14 changes: 14 additions & 0 deletions apps/entry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
from assisted_tagging.models import DraftEntry


class EntryAttachment(models.Model):
class EntryFileType(models.IntegerChoices):
XLSX = 1, 'XLSX'

entry_file_type = models.PositiveSmallIntegerField(
choices=EntryFileType.choices,
default=EntryFileType.XLSX
)
file = models.FileField(upload_to='entry/attachment/')
file_preview = models.FileField(upload_to='entry/attachment-preview')


class Entry(UserResource, ProjectEntityMixin):
"""
Entry belonging to a lead
Expand All @@ -32,6 +44,7 @@ class Entry(UserResource, ProjectEntityMixin):
class TagType(models.TextChoices):
EXCERPT = 'excerpt', 'Excerpt',
IMAGE = 'image', 'Image',
ATTACHMENT = 'attachment', 'Attachment',
DATA_SERIES = 'dataSeries', 'Data Series' # NOTE: data saved as tabular_field id

lead = models.ForeignKey(Lead, on_delete=models.CASCADE)
Expand All @@ -47,6 +60,7 @@ class TagType(models.TextChoices):
image = models.ForeignKey(File, on_delete=models.SET_NULL, null=True, blank=True)
image_raw = models.TextField(blank=True)
tabular_field = models.ForeignKey('tabular.Field', on_delete=models.CASCADE, null=True, blank=True)
entry_attachment = models.OneToOneField(EntryAttachment, on_delete=models.CASCADE, null=True, blank=True)

dropped_excerpt = models.TextField(blank=True) # NOTE: Original Exceprt. Modified version is stored in excerpt
excerpt_modified = models.BooleanField(default=False)
Expand Down
14 changes: 12 additions & 2 deletions apps/entry/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from utils.common import has_prefetched
from utils.graphene.enums import EnumDescription
from utils.graphene.types import CustomDjangoListObjectType, ClientIdMixin
from utils.graphene.types import CustomDjangoListObjectType, ClientIdMixin, FileFieldType
from utils.graphene.fields import DjangoPaginatedListObjectField, DjangoListField
from user_resource.schema import UserResourceMixin
from deep.permissions import ProjectPermissions as PP
Expand All @@ -20,6 +20,7 @@
from .models import (
Entry,
Attribute,
EntryAttachment,
)
from .enums import EntryTagTypeEnum
from .filter_set import EntryGQFilterSet
Expand Down Expand Up @@ -84,6 +85,14 @@ def resolve_geo_selected_options(root, info, **_):
)


class EntryAttachmentType(DjangoObjectType):
file = graphene.Field(FileFieldType)
file_preview = graphene.Field(FileFieldType)

class Meta:
model = EntryAttachment


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

entry_type = graphene.Field(EntryTagTypeEnum, required=True)
Expand All @@ -103,6 +112,7 @@ class Meta:
verified_by_count = graphene.Int(required=True)
review_comments_count = graphene.Int(required=True)
draft_entry = graphene.ID(source="draft_entry_id")
entry_attachment = graphene.Field(EntryAttachmentType, required=False)

# project_labels TODO:
# tabular_field TODO:
Expand Down
26 changes: 18 additions & 8 deletions apps/entry/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .models import (
Attribute,
Entry,
EntryAttachment,
EntryComment,
EntryCommentText,
ExportData,
Expand Down Expand Up @@ -591,12 +592,12 @@ class EntryGqSerializer(ProjectPropertySerializerMixin, TempClientIdMixin, UserR
' This will be changed into gallery image and supplied back in image field.'
)
)
lead_image = serializers.PrimaryKeyRelatedField(
lead_attachment = serializers.PrimaryKeyRelatedField(
required=False,
write_only=True,
queryset=LeadPreviewAttachment.objects.all(),
help_text=(
'This is used to add images from Lead Preview Images.'
'This is used to add attachment from Lead Preview Attachment.'
' This will be changed into gallery image and supplied back in image field.'
)
)
Expand All @@ -611,7 +612,7 @@ class Meta:
'entry_type',
'image',
'image_raw',
'lead_image',
'lead_attachment',
'tabular_field',
'excerpt',
'dropped_excerpt',
Expand Down Expand Up @@ -643,7 +644,7 @@ def validate(self, data):
request = self.context['request']
image = data.get('image')
image_raw = data.pop('image_raw', None)
lead_image = data.pop('lead_image', None)
lead_attachment = data.pop('lead_attachment', None)

# ---------------- Lead
lead = data['lead']
Expand Down Expand Up @@ -685,12 +686,21 @@ def validate(self, data):
'image': f'You don\'t have permission to attach image: {image}',
})
# If lead image is provided make sure lead are same
elif lead_image:
if lead_image.lead != lead:
elif lead_attachment:
if lead_attachment.lead != lead:
raise serializers.ValidationError({
'lead_image': f'You don\'t have permission to attach lead image: {lead_image}',
'lead_attachment': f'You don\'t have permission to attach lead attachment: {lead_attachment}',
})
data['image'] = lead_image.clone_as_deep_file(request.user)

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
elif image_raw:
generated_image = base64_to_deep_image(image_raw, lead, request.user)
if isinstance(generated_image, File):
Expand Down
18 changes: 18 additions & 0 deletions apps/lead/migrations/0053_alter_leadpreviewattachment_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.25 on 2024-06-09 10:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('lead', '0052_alter_leadpreviewattachment_type'),
]

operations = [
migrations.AlterField(
model_name='leadpreviewattachment',
name='type',
field=models.PositiveSmallIntegerField(choices=[(1, 'XLSX'), (2, 'Image')], default=1),
),
]
2 changes: 1 addition & 1 deletion apps/lead/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class LeadPreviewAttachment(models.Model):
"""
NOTE: File can be only used by gallery (when attached to a entry)
"""
class AttachementFileType(models.TextChoices):
class AttachementFileType(models.IntegerChoices):
XLSX = 1, 'XLSX'
IMAGE = 2, 'Image'

Expand Down
1 change: 1 addition & 0 deletions apps/lead/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class LeadPreviewAttachmentType(DjangoObjectType):
class Meta:
model = LeadPreviewAttachment
only_fields = (
'id',
'page_number',
'order',
)
Expand Down
16 changes: 13 additions & 3 deletions apps/lead/tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,11 +1802,18 @@ def test_extractor_callback_url(self, get_file_mock, get_text_mock, index_lead_f
'page_number': 1,
'images': [
'http://random.com/image1.jpeg',
'http://random.com/image1.jpeg'
'http://random.com/image2.jpeg'
],
}
],
'tables_path': [],
'tables_path': [
{
"page_number": 1,
"order": 0,
"image_link": "http://random.com/timetable.png",
"content_link": "http://random.com/table_timetable.xlsx"
}
],
'text_path': 'http://random.com/extracted_file.txt',
'url': 'http://random.com/pdf_file.pdf',
'total_words_count': 300,
Expand Down Expand Up @@ -1835,7 +1842,10 @@ def test_extractor_callback_url(self, get_file_mock, get_text_mock, index_lead_f
self.assertEqual(lead_preview.text_extract, 'Extracted text')
self.assertEqual(lead_preview.word_count, 300)
self.assertEqual(lead_preview.page_count, 4)
self.assertEqual(LeadPreviewAttachment.objects.filter(lead=self.lead).count(), 2)
self.assertEqual(LeadPreviewAttachment.objects.filter(lead=self.lead).count(), 3)
self.assertEqual(LeadPreviewAttachment.objects.filter(
lead=self.lead, type=LeadPreviewAttachment.AttachementFileType.IMAGE).count(), 2
)

index_lead_func.assert_called_once_with(self.lead.id)

Expand Down
19 changes: 17 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3268,7 +3268,7 @@ input BulkEntryInputType {
entryType: EntryTagTypeEnum
image: ID
imageRaw: String
leadImage: ID
leadAttachment: ID
tabularField: ID
excerpt: String
droppedExcerpt: String
Expand Down Expand Up @@ -3925,6 +3925,18 @@ type EntriesFilterDataType {
filterableData: [EntryFilterDataType!]
}

enum EntryAttachmentEntryFileType {
A_1
}

type EntryAttachmentType {
id: ID!
entryFileType: EntryAttachmentEntryFileType!
file: FileFieldType
filePreview: FileFieldType
entry: EntryType
}

input EntryFilterDataInputType {
filterKey: ID!
value: String
Expand Down Expand Up @@ -3968,7 +3980,7 @@ input EntryInputType {
entryType: EntryTagTypeEnum
image: ID
imageRaw: String
leadImage: ID
leadAttachment: ID
tabularField: ID
excerpt: String
droppedExcerpt: String
Expand Down Expand Up @@ -4050,6 +4062,7 @@ enum EntryReviewCommentTypeEnum {
enum EntryTagTypeEnum {
EXCERPT
IMAGE
ATTACHMENT
DATA_SERIES
}

Expand All @@ -4063,6 +4076,7 @@ type EntryType {
informationDate: Date
excerpt: String!
image: GalleryFileType
entryAttachment: EntryAttachmentType
droppedExcerpt: String!
highlightHidden: Boolean!
controlled: Boolean
Expand Down Expand Up @@ -4694,6 +4708,7 @@ enum LeadOrderingEnum {
}

type LeadPreviewAttachmentType {
id: ID!
order: Int!
pageNumber: Int!
file: FileFieldType
Expand Down

0 comments on commit 2c39255

Please sign in to comment.