Skip to content

Commit

Permalink
CM-869: added adjustments to comment mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sniedzielski committed Apr 23, 2024
1 parent 1ac5ea5 commit e7e7256
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 22 deletions.
11 changes: 6 additions & 5 deletions grievance_social_protection/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class TicketStatusEnum(graphene.Enum):
key = graphene.String(required=False)
title = graphene.String(required=False)
description = graphene.String(required=False)
reporter_type = graphene.String(required=True, max_lenght=255)
reporter_id = graphene.String(required=True, max_lenght=255)
reporter_type = graphene.String(required=False, max_lenght=255)
reporter_id = graphene.String(required=False, max_lenght=255)
attending_staff_id = graphene.UUID(required=False)
date_of_incident = graphene.Date(required=False)
status = graphene.Field(TicketStatusEnum, required=False)
Expand All @@ -43,8 +43,8 @@ class UpdateTicketInputType(CreateTicketInputType):

class CreateCommentInputType(OpenIMISMutation.Input):
ticket_id = graphene.UUID(required=True)
commenter_type = graphene.String(required=True, max_lenght=255)
commenter_id = graphene.String(required=True, max_lenght=255)
commenter_type = graphene.String(required=False, max_lenght=255)
commenter_id = graphene.String(required=False, max_lenght=255)
comment = graphene.String(required=True)


Expand Down Expand Up @@ -149,7 +149,8 @@ def _mutate(cls, user, **data):
if "client_mutation_label" in data:
data.pop('client_mutation_label')

data['commenter_type'] = data.get('commenter_type', '').lower()
if "commenter_type" in data:
data['commenter_type'] = data.get('commenter_type', '').lower()
service = CommentService(user)
response = service.create(data)

Expand Down
12 changes: 6 additions & 6 deletions grievance_social_protection/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ class TicketGQLType(DjangoObjectType):
@staticmethod
def resolve_reporter_type(root, info):
check_ticket_perms(info)
return root.reporter_type.id
return root.reporter_type.id if root.reporter_type else None

@staticmethod
def resolve_reporter_type_name(root, info):
check_ticket_perms(info)
return root.reporter_type.name
return root.reporter_type.name if root.reporter_type else None

@staticmethod
def resolve_reporter(root, info):
check_ticket_perms(info)
return model_obj_to_json(root.reporter)
return model_obj_to_json(root.reporter) if root.reporter else None

class Meta:
model = Ticket
Expand Down Expand Up @@ -83,17 +83,17 @@ class CommentGQLType(DjangoObjectType):
@staticmethod
def resolve_commenter_type(root, info):
check_comment_perms(info)
return root.commenter_type.id
return root.commenter_type.id if root.commenter_type else None

@staticmethod
def resolve_commenter_type_name(root, info):
check_comment_perms(info)
return root.commenter_type.name
return root.commenter_type.name if root.commenter_type else None

@staticmethod
def resolve_commenter(root, info):
check_comment_perms(info)
return model_obj_to_json(root.commenter)
return model_obj_to_json(root.commenter) if root.commenter else None

class Meta:
model = Comment
Expand Down
30 changes: 30 additions & 0 deletions grievance_social_protection/migrations/0015_auto_20240423_1318.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.2.19 on 2024-04-23 13:18

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


class Migration(migrations.Migration):

dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('grievance_social_protection', '0014_add_grievance_social_protection_rights_to_admin'),
]

operations = [
migrations.AlterField(
model_name='historicalticket',
name='reporter_id',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AlterField(
model_name='ticket',
name='reporter_id',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AlterField(
model_name='ticket',
name='reporter_type',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='contenttypes.contenttype'),
),
]
30 changes: 30 additions & 0 deletions grievance_social_protection/migrations/0016_auto_20240423_1324.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.2.19 on 2024-04-23 13:24

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


class Migration(migrations.Migration):

dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('grievance_social_protection', '0015_auto_20240423_1318'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='commenter_id',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AlterField(
model_name='comment',
name='commenter_type',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='contenttypes.contenttype'),
),
migrations.AlterField(
model_name='historicalcomment',
name='commenter_id',
field=models.CharField(blank=True, max_length=255, null=True),
),
]
8 changes: 4 additions & 4 deletions grievance_social_protection/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class TicketStatus(models.TextChoices):
description = models.TextField(max_length=255, blank=True, null=True)
code = models.CharField(max_length=16, unique=True, blank=True, null=True)

reporter_type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING, null=False, blank=False)
reporter_id = models.CharField(max_length=255, null=False, blank=False)
reporter_type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING, null=True, blank=True)
reporter_id = models.CharField(max_length=255, null=True, blank=True)
reporter = GenericForeignKey('reporter_type', 'reporter_id')

attending_staff = models.ForeignKey(User, models.DO_NOTHING, blank=True, null=True)
Expand Down Expand Up @@ -87,8 +87,8 @@ class Meta:

class Comment(HistoryModel):
ticket = models.ForeignKey(Ticket, on_delete=models.DO_NOTHING, null=False, blank=False)
commenter_type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING, null=False, blank=False)
commenter_id = models.CharField(max_length=255, null=False, blank=False)
commenter_type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING, null=True, blank=True)
commenter_id = models.CharField(max_length=255, null=True, blank=True)
commenter = GenericForeignKey('commenter_type', 'commenter_id')
comment = models.TextField(blank=False, null=False)

Expand Down
11 changes: 6 additions & 5 deletions grievance_social_protection/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def delete(self, obj_data):
return super().delete(obj_data)

def _get_content_type(self, obj_data):
content_type = ContentType.objects.get(model=obj_data['reporter_type'].lower())
obj_data['reporter_type'] = content_type
if 'reporter_type' in obj_data:
content_type = ContentType.objects.get(model=obj_data['reporter_type'].lower())
obj_data['reporter_type'] = content_type


def _generate_code(self, obj_data):
Expand Down Expand Up @@ -74,6 +75,6 @@ def save_instance(self, obj_):
return output_result_success(dict_representation=dict_repr)

def _get_content_type(self, obj_data):
content_type = ContentType.objects.get(model=obj_data['commenter_type'].lower())
obj_data['commenter_type'] = content_type

if 'commenter_type' in obj_data:
content_type = ContentType.objects.get(model=obj_data['commenter_type'].lower())
obj_data['commenter_type'] = content_type
2 changes: 0 additions & 2 deletions grievance_social_protection/validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class CommentValidation:
def validate_create(cls, user, **data):
errors = [
*validate_ticket_exists(data),
*validate_commenter_exists(data),
*validate_commenter_associated_with_ticket(data)
]
if errors:
raise ValidationError(errors)
Expand Down

0 comments on commit e7e7256

Please sign in to comment.