[PATCH] models: Add State.slug field

Stephen Finucane stephen at that.guru
Wed Dec 11 02:32:45 AEDT 2019


On Tue, 2019-12-10 at 15:43 +1100, Andrew Donnellan wrote:
> On 9/12/19 3:00 am, Stephen Finucane wrote:
> > --- /dev/null
> > +++ b/patchwork/migrations/0038_state_slug.py
> > @@ -0,0 +1,68 @@
> > +# -*- coding: utf-8 -*-
> > +
> > +from __future__ import unicode_literals
> > +
> > +from django.db import migrations, models, transaction
> > +from django.utils.text import slugify
> > +
> > +
> > +def validate_uniqueness(apps, schema_editor):
> > +    """Ensure all State.name entries are unique.
> > +
> > +    We need to do this before enforcing a uniqueness constraint.
> > +    """
> > +
> > +    State = apps.get_model('patchwork', 'State')
> > +
> > +    unique_count = len(State.objects.order_by().values_list(
> > +        'name', flat=True).distinct())
> > +    total_count = State.objects.count()
> > +
> > +    if unique_count != total_count:
> > +        raise Exception(
> > +            'You have non-unique States entries that need to be combined '
> > +            'before you can run this migration. This migration must be done '
> > +            'by hand. If you need assistance, please contact '
> > +            'patchwork at ozlabs.org')
> 
> I think the migration can still fail without printing a helpful 
> exception message if two states have names that are different but still 
> slugify to the same thing?

Good point. I can fix this if we think it's worth it. Personally, I
think the uniqueness check itself is overkill and could probably be
removed.

Stephen



More information about the Patchwork mailing list