Skip to content

Commit c764af6

Browse files
authored
Merge pull request #4780 from rtfd/humitos/import/notslug
Validate the slug generated is valid before importing a project
2 parents df85fef + 2726df4 commit c764af6

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

readthedocs/projects/forms.py

+6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def clean_name(self):
118118
if Project.objects.filter(slug=potential_slug).exists():
119119
raise forms.ValidationError(
120120
_('Invalid project name, a project already exists with that name')) # yapf: disable # noqa
121+
if not potential_slug:
122+
# Check the generated slug won't be empty
123+
raise forms.ValidationError(
124+
_('Invalid project name'),
125+
)
126+
121127
return name
122128

123129
def clean_remote_repository(self):

readthedocs/projects/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def save(self, *args, **kwargs): # pylint: disable=arguments-differ
316316
if not self.slug:
317317
# Subdomains can't have underscores in them.
318318
self.slug = slugify(self.name)
319-
if self.slug == '':
319+
if not self.slug:
320320
raise Exception(_('Model must have slug'))
321321
if self.documentation_type == 'auto':
322322
# This used to determine the type and automatically set the

readthedocs/rtd_tests/tests/test_project_forms.py

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def test_import_repo_url(self):
9393
form = ProjectBasicsForm(initial)
9494
self.assertEqual(form.is_valid(), valid, msg=url)
9595

96+
def test_empty_slug(self):
97+
initial = {
98+
'name': "''",
99+
'repo_type': 'git',
100+
'repo': 'https://github.com/user/repository',
101+
}
102+
form = ProjectBasicsForm(initial)
103+
self.assertFalse(form.is_valid())
104+
self.assertIn('name', form.errors)
105+
96106

97107
class TestTranslationForms(TestCase):
98108

0 commit comments

Comments
 (0)