<s> from __future__ import unicode_literals <EOL> from django . db import migrations , models <EOL> from django . utils . text import slugify <EOL> SLUG_LENGTH = <NUM_LIT> <EOL> def add_slug_data ( apps , schema_editor ) : <EOL> NewsLink = apps . get_model ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> query = NewsLink . objects . all ( ) <EOL> for newslink in query : <EOL> expected_slug = slugify ( newslink . title ) <EOL> rivals = ( <EOL> NewsLink . objects . filter ( <EOL> startup = newslink . startup , <EOL> slug__startswith = expected_slug <EOL> ) . count ( ) ) <EOL> if rivals > <NUM_LIT:0> : <EOL> str_len = ( <EOL> SLUG_LENGTH - len ( str ( rivals ) ) ) <EOL> newslink . slug = "<STR_LIT>" . format ( <EOL> expected_slug [ : str_len - <NUM_LIT:1> ] , <EOL> rivals + <NUM_LIT:1> ) <EOL> else : <EOL> newslink . slug = expected_slug <EOL> newslink . save ( ) <EOL> def remove_slug_data ( apps , schema_editor ) : <EOL> NewsLink = apps . get_model ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> NewsLink . objects . update ( slug = '<STR_LIT>' ) <EOL> class Migration ( migrations . Migration ) : <EOL> dependencies = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> operations = [ <EOL> migrations . AddField ( <EOL> model_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> field = models . SlugField ( <EOL> max_length = SLUG_LENGTH , <EOL> default = '<STR_LIT>' ) , <EOL> ) , <EOL> migrations . RunPython ( <EOL> add_slug_data , <EOL> reverse_code = remove_slug_data <EOL> ) , <EOL> migrations . AlterField ( <EOL> model_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> field = models . SlugField ( <EOL> max_length = SLUG_LENGTH ) , <EOL> ) , <EOL> ] </s>
