chore: issue link model field change (#3852)

This commit is contained in:
Bavisetti Narayan 2024-03-06 14:21:07 +05:30 committed by GitHub
parent 53367a6bc4
commit 87eadc3c5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 2 deletions

View File

@ -1,8 +1,9 @@
from lxml import html from lxml import html
# Django imports # Django imports
from django.utils import timezone from django.utils import timezone
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
# Third party imports # Third party imports
from rest_framework import serializers from rest_framework import serializers
@ -284,6 +285,20 @@ class IssueLinkSerializer(BaseSerializer):
"updated_at", "updated_at",
] ]
def validate_url(self, value):
# Check URL format
validate_url = URLValidator()
try:
validate_url(value)
except ValidationError:
raise serializers.ValidationError("Invalid URL format.")
# Check URL scheme
if not value.startswith(('http://', 'https://')):
raise serializers.ValidationError("Invalid URL scheme.")
return value
# Validation if url already exists # Validation if url already exists
def create(self, validated_data): def create(self, validated_data):
if IssueLink.objects.filter( if IssueLink.objects.filter(
@ -295,6 +310,17 @@ class IssueLinkSerializer(BaseSerializer):
) )
return IssueLink.objects.create(**validated_data) return IssueLink.objects.create(**validated_data)
def update(self, instance, validated_data):
if IssueLink.objects.filter(
url=validated_data.get("url"),
issue_id=instance.issue_id,
).exists():
raise serializers.ValidationError(
{"error": "URL already exists for this Issue"}
)
return super().update(instance, validated_data)
class IssueAttachmentSerializer(BaseSerializer): class IssueAttachmentSerializer(BaseSerializer):
class Meta: class Meta:

View File

@ -1,5 +1,7 @@
# Django imports # Django imports
from django.utils import timezone from django.utils import timezone
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
# Third Party imports # Third Party imports
from rest_framework import serializers from rest_framework import serializers
@ -432,6 +434,20 @@ class IssueLinkSerializer(BaseSerializer):
"issue", "issue",
] ]
def validate_url(self, value):
# Check URL format
validate_url = URLValidator()
try:
validate_url(value)
except ValidationError:
raise serializers.ValidationError("Invalid URL format.")
# Check URL scheme
if not value.startswith(('http://', 'https://')):
raise serializers.ValidationError("Invalid URL scheme.")
return value
# Validation if url already exists # Validation if url already exists
def create(self, validated_data): def create(self, validated_data):
if IssueLink.objects.filter( if IssueLink.objects.filter(
@ -443,6 +459,17 @@ class IssueLinkSerializer(BaseSerializer):
) )
return IssueLink.objects.create(**validated_data) return IssueLink.objects.create(**validated_data)
def update(self, instance, validated_data):
if IssueLink.objects.filter(
url=validated_data.get("url"),
issue_id=instance.issue_id,
).exists():
raise serializers.ValidationError(
{"error": "URL already exists for this Issue"}
)
return super().update(instance, validated_data)
class IssueLinkLiteSerializer(BaseSerializer): class IssueLinkLiteSerializer(BaseSerializer):

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-03-01 07:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('db', '0060_cycle_progress_snapshot'),
]
operations = [
migrations.AlterField(
model_name='issuelink',
name='url',
field=models.TextField(),
),
]

View File

@ -320,7 +320,7 @@ class IssueAssignee(ProjectBaseModel):
class IssueLink(ProjectBaseModel): class IssueLink(ProjectBaseModel):
title = models.CharField(max_length=255, null=True, blank=True) title = models.CharField(max_length=255, null=True, blank=True)
url = models.URLField() url = models.TextField()
issue = models.ForeignKey( issue = models.ForeignKey(
"db.Issue", on_delete=models.CASCADE, related_name="issue_link" "db.Issue", on_delete=models.CASCADE, related_name="issue_link"
) )