Skip to content

Commit

Permalink
PR fixes on file upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Aug 2, 2023
1 parent ed7fabd commit 07f071b
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/assessment_registry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class DocumentType(models.IntegerChoices):
)
external_link = models.URLField(max_length=500, blank=True)


class ScoreRating(UserResource):
class ScoreType(models.IntegerChoices):
RELEVANCE = 0, "Fit for purpose -> Relevance"
Expand Down
5 changes: 2 additions & 3 deletions apps/assessment_registry/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from graphene_django import DjangoObjectType
from graphene_django_extras import DjangoObjectField

from utils.commons import render_string_for_graphql
from utils.common import render_string_for_graphql
from utils.graphene.types import ClientIdMixin, CustomDjangoListObjectType
from utils.graphene.fields import DjangoPaginatedListObjectField
from utils.graphene.pagination import NoOrderingPageGraphqlPagination
Expand Down Expand Up @@ -264,14 +264,13 @@ class Meta:


class AdditionalDocumentType(DjangoObjectType, UserResourceMixin, ClientIdMixin):
external_link = graphene.String(required=False)

class Meta:
model = AdditionalDocument
fields = ("id", "file")

document_type = graphene.Field(AssessmentRegistryDocumentTypeEnum, required=True)
document_type_display = EnumDescription(source='get_document_type_display', required=True)
external_link = graphene.String(required=False)

def resolve_external_link(root, info, **kwargs):
return render_string_for_graphql(root.external_link)
Expand Down
31 changes: 31 additions & 0 deletions apps/gallery/mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import graphene

from utils.graphene.mutation import (
generate_input_type_for_serializer,
PsGrapheneMutation,
)

from .models import File
from .schema import GalleryFileType
from .serializers import (
FileSerializer
)

FileUploadInputType = generate_input_type_for_serializer(
'FileUploadInputType',
serializer_class=FileSerializer
)


class UploadFile(PsGrapheneMutation):
class Arguments:
data = FileUploadInputType(required=True)

result = graphene.Field(GalleryFileType)
serializer_class = FileSerializer
model = File
permissions = []


class Mutation():
file_upload = UploadFile.Field()
61 changes: 61 additions & 0 deletions apps/gallery/tests/test_mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from utils.graphene.tests import GraphQLTestCase
import json

from graphene_file_upload.django.testing import GraphQLFileUploadTestCase
from django.core.files.temp import NamedTemporaryFile

from user.factories import UserFactory


class TestUploadFileMutation(GraphQLFileUploadTestCase, GraphQLTestCase):
UPLOAD_FILE = '''
mutation MyMutation ($data: FileUploadInputType!) {
fileUpload(data: $data) {
ok
errors
result {
id
file {
name
url
}
}
}
}
'''

def setUp(self):
super().setUp()
self.variables = {
"data": {"title": 'test', "file": None}
}
self.user = UserFactory.create()
self.force_login(self.user)

def test_upload_preview_image(self):
file_text = b'preview image text'
with NamedTemporaryFile(suffix='.png') as t_file:
t_file.write(file_text)
t_file.seek(0)
response = self._client.post(
'/graphql',
data={
'operations': json.dumps({
'query': self.UPLOAD_FILE,
'variables': self.variables
}),
't_file': t_file,
'map': json.dumps({
't_file': ['variables.data.file']
})
}
)
content = response.json()
self.assertResponseNoErrors(response)

self.assertTrue(content['data']['fileUpload']['ok'], content)
self.assertTrue(content['data']['fileUpload']['result']['file']["name"])
file_name = content['data']['fileUpload']['result']['file']["name"]
file_url = content['data']['fileUpload']['result']['file']["url"]
self.assertTrue(file_name.endswith('.png'))
self.assertTrue(file_url.endswith(file_name))
6 changes: 5 additions & 1 deletion apps/geo/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from promise import Promise
from django.utils.functional import cached_property
from django.db.models import Prefetch

from utils.graphene.dataloaders import DataLoaderWithContext, WithContextMixin
from geo.schema import get_geo_area_queryset_for_project_geo_area_type

from .models import (
AdminLevel,
Expand All @@ -23,7 +25,9 @@ def batch_load_fn(self, keys):
class GeoAreaLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
ary_geo_area_qs = AssessmentRegistry.locations.through.objects\
.filter(assessmentregistry__in=keys).prefetch_related('geoarea')
.filter(assessmentregistry__in=keys).prefetch_related(
Prefetch('geoarea', queryset=get_geo_area_queryset_for_project_geo_area_type())
)
_map = defaultdict(list)
for ary_geo_area in ary_geo_area_qs.all():
_map[ary_geo_area.assessmentregistry_id].append(ary_geo_area.geoarea)
Expand Down
7 changes: 0 additions & 7 deletions apps/geo/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ class Meta:
region_title = graphene.String(required=True)
admin_level_title = graphene.String(required=True)

@staticmethod
def resolve_region_title(root, info, **kwargs):
return root.admin_level.region.title

def resolve_admin_level_title(root, info, **kwargs):
return root.admin_level.title


class ProjectGeoAreaListType(CustomDjangoListObjectType):
class Meta:
Expand Down
2 changes: 2 additions & 0 deletions deep/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from unified_connector import schema as unified_connector_schema
from export import schema as export_schema, mutation as export_mutation
from deep_explore import schema as deep_explore_schema
from gallery import mutations as gallery_mutation
from deep.enums import CustomEnum


Expand Down Expand Up @@ -61,6 +62,7 @@ class Mutation(
pj_mutation.Mutation,
notification_mutation.Mutation,
export_mutation.Mutation,
gallery_mutation.Mutation,
# --
graphene.ObjectType
):
Expand Down
16 changes: 16 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,15 @@ type FileFieldType {
url: String
}

input FileUploadInputType {
clientId: String
file: Upload!
title: String!
isPublic: Boolean
createdBy: ID
projects: [ID!]
}

type GalleryFileType {
id: ID!
title: String!
Expand Down Expand Up @@ -2508,6 +2517,7 @@ type MissingPredictionReviewType {
}

type Mutation {
fileUpload(data: FileUploadInputType!): UploadFile
genericExportCreate(data: GenericExportCreateInputType!): CreateUserGenericExport
genericExportCancel(id: ID!): CancelUserGenericExport
notificationStatusUpdate(data: NotificationStatusInputType!): NotificationStatusUpdate
Expand Down Expand Up @@ -3661,6 +3671,12 @@ type UpdateUserGroup {

scalar Upload

type UploadFile {
errors: [GenericScalar!]
ok: Boolean
result: GalleryFileType
}

type UserDelete {
errors: [GenericScalar!]
ok: Boolean
Expand Down

0 comments on commit 07f071b

Please sign in to comment.