recipe-server: Add migration to change username to emails

This commit is contained in:
Mike Cooper 2017-03-06 11:40:49 -08:00
Родитель 0fcab6a004
Коммит cf69f3050f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 74AB8817639D69C1
2 изменённых файлов: 39 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-06 19:25
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations
def email_to_username(apps, schema_editor):
"""
Copy emails to usernames for all users.
"""
User = apps.get_model('auth', 'User')
for user in User.objects.all():
if user.email:
user.username = user.email
user.save()
def remove_email_from_username(apps, schema_editor):
"""
Copy emails to usernames for all users.
"""
User = apps.get_model('auth', 'User')
for user in User.objects.all():
if '@' in user.username:
user.username = user.username.split('@')[0]
user.save()
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.RunPython(email_to_username, remove_email_from_username),
]

Просмотреть файл