Skip to content

Commit

Permalink
chore: issue link model field change (#3852)
Browse files Browse the repository at this point in the history
  • Loading branch information
NarayanBavisetti authored Mar 6, 2024
1 parent 53367a6 commit 87eadc3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
28 changes: 27 additions & 1 deletion apiserver/plane/api/serializers/issue.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from lxml import html


# Django imports
from django.utils import timezone
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError

# Third party imports
from rest_framework import serializers
Expand Down Expand Up @@ -284,6 +285,20 @@ class Meta:
"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
def create(self, validated_data):
if IssueLink.objects.filter(
Expand All @@ -295,6 +310,17 @@ def create(self, 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 Meta:
Expand Down
27 changes: 27 additions & 0 deletions apiserver/plane/app/serializers/issue.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Django imports
from django.utils import timezone
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError

# Third Party imports
from rest_framework import serializers
Expand Down Expand Up @@ -432,6 +434,20 @@ class Meta:
"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
def create(self, validated_data):
if IssueLink.objects.filter(
Expand All @@ -443,6 +459,17 @@ def create(self, 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):

Expand Down
18 changes: 18 additions & 0 deletions apiserver/plane/db/migrations/0061_alter_issuelink_url.py
Original file line number Diff line number Diff line change
@@ -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(),
),
]
2 changes: 1 addition & 1 deletion apiserver/plane/db/models/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def __str__(self):

class IssueLink(ProjectBaseModel):
title = models.CharField(max_length=255, null=True, blank=True)
url = models.URLField()
url = models.TextField()
issue = models.ForeignKey(
"db.Issue", on_delete=models.CASCADE, related_name="issue_link"
)
Expand Down

0 comments on commit 87eadc3

Please sign in to comment.