Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make slug field a valid DNS label #3464

Merged
merged 3 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2018-11-01 20:55
from __future__ import unicode_literals

from django.db import migrations, models
from django.db.models.functions import Length


def forwards_func(apps, schema_editor):
max_length = 63
Project = apps.get_model('projects', 'Project')
projects_invalid_slug = (
Project
.objects
.annotate(slug_length=Length('slug'))
.filter(slug_length__gt=max_length)
)
for project in projects_invalid_slug:
project.slug = project.slug[:max_length]
project.save()


class Migration(migrations.Migration):

dependencies = [
('projects', '0029_add_additional_languages'),
]

operations = [
migrations.RunPython(forwards_func),
migrations.AlterField(
model_name='project',
name='slug',
field=models.SlugField(max_length=63, unique=True, verbose_name='Slug'),
),
]
3 changes: 2 additions & 1 deletion readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class Project(models.Model):
users = models.ManyToManyField(User, verbose_name=_('User'),
related_name='projects')
name = models.CharField(_('Name'), max_length=255)
slug = models.SlugField(_('Slug'), max_length=255, unique=True)
# A DNS label can contain up to 63 characters.
slug = models.SlugField(_('Slug'), max_length=63, unique=True)
description = models.TextField(_('Description'), blank=True,
help_text=_('The reStructuredText '
'description of the project'))
Expand Down