diff --git a/grievance_social_protection/gql_mutations.py b/grievance_social_protection/gql_mutations.py index 14c9b35..dfa9c58 100644 --- a/grievance_social_protection/gql_mutations.py +++ b/grievance_social_protection/gql_mutations.py @@ -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) @@ -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) @@ -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) diff --git a/grievance_social_protection/gql_queries.py b/grievance_social_protection/gql_queries.py index 03f2dab..a3a0a60 100644 --- a/grievance_social_protection/gql_queries.py +++ b/grievance_social_protection/gql_queries.py @@ -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 @@ -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 diff --git a/grievance_social_protection/migrations/0015_auto_20240423_1318.py b/grievance_social_protection/migrations/0015_auto_20240423_1318.py new file mode 100644 index 0000000..d9eb74c --- /dev/null +++ b/grievance_social_protection/migrations/0015_auto_20240423_1318.py @@ -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'), + ), + ] diff --git a/grievance_social_protection/migrations/0016_auto_20240423_1324.py b/grievance_social_protection/migrations/0016_auto_20240423_1324.py new file mode 100644 index 0000000..201443e --- /dev/null +++ b/grievance_social_protection/migrations/0016_auto_20240423_1324.py @@ -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), + ), + ] diff --git a/grievance_social_protection/models.py b/grievance_social_protection/models.py index 2e2d836..a76971f 100644 --- a/grievance_social_protection/models.py +++ b/grievance_social_protection/models.py @@ -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) @@ -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) diff --git a/grievance_social_protection/services.py b/grievance_social_protection/services.py index 6be772b..100897e 100644 --- a/grievance_social_protection/services.py +++ b/grievance_social_protection/services.py @@ -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): @@ -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 diff --git a/grievance_social_protection/validations.py b/grievance_social_protection/validations.py index b305fcd..9438e20 100644 --- a/grievance_social_protection/validations.py +++ b/grievance_social_protection/validations.py @@ -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)