Add DataSource URL validation

This commit is contained in:
jeremystretch 2023-01-27 14:50:12 -05:00
parent b97bf44ed5
commit ee35ea439e

View File

@ -6,6 +6,7 @@ from fnmatch import fnmatchcase
from urllib.parse import quote, urlunparse, urlparse from urllib.parse import quote, urlunparse, urlparse
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
@ -104,6 +105,15 @@ class DataSource(ChangeLoggedModel):
DataSourceStatusChoices.SYNCING DataSourceStatusChoices.SYNCING
) )
def clean(self):
# Ensure URL scheme matches selected type
url_scheme = urlparse(self.url)
if self.type == DataSourceTypeChoices.LOCAL and url_scheme not in ('file', ''):
raise ValidationError({
'url': f"URLs for local sources must start with file:// (or omit the scheme)"
})
def enqueue_sync_job(self, request): def enqueue_sync_job(self, request):
""" """
Enqueue a background job to synchronize the DataSource by calling sync(). Enqueue a background job to synchronize the DataSource by calling sync().