the-deep/server

Implement graphql query node for assignment

Closed this issue · 1 comments

Problem Statement

The 'Assignment' features uses a Rest API.

Acceptance Criteria

The 'Assignment' feature works with a graphql query.

Additional Information

serializers

class AssignmentSerializer(serializers.ModelSerializer):
content_object_details = GenericRelatedField({
Lead: AssignmentLeadSerializer(),
EntryComment: AssignmentEntryCommentSerializer(),
EntryReviewComment: AssignmentEntryReviewCommentSerializer(),
}, read_only=True, source='content_object')
project_details = SimpleProjectSerializer(source='project', read_only=True)
created_by_details = SimpleUserSerializer(source='created_by', read_only=True)
class Meta:
model = Assignment
read_only_fields = [
'id',
'created_at',
'project_details', 'created_by_details', 'content_object_details', 'content_type'
]
fields = read_only_fields + ['is_done']
def to_representation(self, instance):
data = super().to_representation(instance)
data['content_object_type'] = instance.content_type.model
return data

views

class AssignmentViewSet(viewsets.ModelViewSet):
serializer_class = AssignmentSerializer
permission_classes = [permissions.IsAuthenticated]
filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
filterset_class = AssignmentFilterSet
pagination_class = AssignmentPagination
def get_queryset(self):
return Assignment.get_for(self.request.user).select_related(
'project', 'created_by', 'content_type',
).order_by('-created_at')
@action(
detail=False,
methods=['POST'],
permission_classes=[permissions.IsAuthenticated],
url_path='bulk-mark-as-done'
)
def status(self, request, version=None):
queryset = self.filter_queryset(self.get_queryset()).filter(is_done=False)
updated_rows_count = queryset.update(is_done=True)
return response.Response({
'assignment_updated': updated_rows_count,
})

testcases

class TestAssignmentApi(TestCase):
""" Api test for assignment model"""
def test_get_assignments_lead(self):
project = self.create_project()
project1 = self.create_project()
user1 = self.create(User)
user2 = self.create(User)
url = '/api/v1/assignments/'
self.authenticate(user1)
response = self.client.get(url)
self.assert_200(response)
data = response.data
assert data['count'] == 0, "No Assignments till now"
# try creating lead
lead = self.create_lead(project=project, assignee=[user1])
self.create(Lead, project=project1, assignee=[user2])
self.authenticate(user1)
params = {'project': project.id}
response = self.client.get(url, params)
self.assert_200(response)
self.assertEqual(response.data['count'], 1)
self.assertEqual(response.data['results'][0]['project_details']['id'], project.id)
self.assertEqual(response.data['results'][0]['content_object_type'], 'lead')
self.assertEqual(response.data['results'][0]['content_object_details']['id'], lead.id)

Rest Framework urls

method: GET
url: /api/v1/assignments/
request params : ?is_done=3&offset=0&limit=5
response: [
        {
            "id": ID,
            "createdAt": Datetime,
            "projectDetails": ProjectDetails
            "createdByDetails": UserDetails
            "contentObjectDetails": ContentDetails(ie lead, entry details)
            "contentType": ID,
            "isDone": Boolean,
            "contentObjectType": str
        }
]

Expected graphql

enum AssignmentContentTypeEnum {
  LEAD
  ENTRY_REVIEW_COMMENT
}

type AssignmentContentDataType {
  contentType: AssignmentContentTypeEnum
  lead: AssignmentLeadDetailType
  entryReviewComment: AssignmentEntryReviewCommentDetailType
}

input AssignmentInputType {
  isDone: Boolean
}

type AssignmentLeadDetailType {
  id: ID!
  title: String!
}

type AssignmentProjectDetailType {
  id: ID!
  title: String!
}

type AssignmentType {
  id: ID!
  createdAt: DateTime!
  createdBy: UserType
  createdFor: UserType!
  project: AssignmentProjectDetailType
  isDone: Boolean!
  objectId: Int!
  contentData: AssignmentContentDataType
}

type AssignmentListType {
  results: [AssignmentType!]
  totalCount: Int
  page: Int
  pageSize: Int
}

type AssignmentUpdate {
  errors: [GenericScalar!]
  ok: Boolean
  result: AssignmentType
}

@puranban @AdityaKhatri @subinasr
Note: The client needs to review the lead and entry content types. If contenttype is lead, map data with LeadType; if not, map with EntryType.