migrate_party_user.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. from django.db import migrations
  3. from django.contrib.auth.models import User
  4. from core.models import Party
  5. import csv
  6. import re
  7. import string
  8. from random import *
  9. def generate_password(length=12):
  10. characters = string.ascii_letters + '#@+-&%()?!*' + string.digits
  11. return ''.join(choice(characters) for x in range(length))
  12. def add_user_for_parties(apps, schema_editor):
  13. parties = Party.objects.all()
  14. for party in parties:
  15. username_raw = re.sub('ö', 'oe', re.sub('ü', 'ue', re.sub('ä', 'ae', party.shortname.encode('utf8').lower())))
  16. username = re.sub('[^a-zA-Z]', '', username_raw)
  17. password = generate_password()
  18. if not User.objects.filter(username=username).exists():
  19. user = User.objects.create_user(username=username, password=password)
  20. user.save()
  21. else:
  22. continue
  23. class Migration(migrations.Migration):
  24. dependencies = [
  25. ('core', '0004_politician_user')
  26. ]
  27. operations = [
  28. migrations.RunPython(add_user_for_parties)
  29. ]