@@ -147,6 +147,41 @@ def validate_url(self, value):
147147 return value
148148
149149
150+ def create (self , validated_data ):
151+ # Filtering the WorkspaceUserLink with the given url to check if the link already exists.
152+
153+ url = validated_data .get ("url" )
154+
155+ workspace_user_link = WorkspaceUserLink .objects .filter (
156+ url = url ,
157+ workspace_id = validated_data .get ("workspace_id" ),
158+ owner = validated_data .get ("owner" )
159+ )
160+
161+ if workspace_user_link .exists ():
162+ raise serializers .ValidationError (
163+ {"error" : "URL already exists for this workspace and owner" }
164+ )
165+ return WorkspaceUserLink .objects .create (** validated_data )
166+
167+ def update (self , instance , validated_data ):
168+ # Filtering the WorkspaceUserLink with the given url to check if the link already exists.
169+
170+ url = validated_data .get ("url" )
171+
172+ workspace_user_link = WorkspaceUserLink .objects .filter (
173+ url = url ,
174+ workspace_id = instance .workspace_id ,
175+ owner = instance .owner
176+ )
177+
178+ if workspace_user_link .exclude (pk = instance .id ).exists ():
179+ raise serializers .ValidationError (
180+ {"error" : "URL already exists for this workspace and owner" }
181+ )
182+
183+ return super ().update (instance , validated_data )
184+
150185class IssueRecentVisitSerializer (serializers .ModelSerializer ):
151186 project_identifier = serializers .SerializerMethodField ()
152187
0 commit comments