0002_copy_sso_subjects.py 1022 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Generated by Django 3.2.15 on 2023-01-04 12:36
  2. from django.db import migrations
  3. from django.utils import timezone
  4. CHUNK_SIZE = 50
  5. def create_settings(apps, _):
  6. Subject = apps.get_model("misago_oauth2", "Subject")
  7. User = apps.get_model("misago_users", "User")
  8. tz_now = timezone.now()
  9. sso_users = User.objects.filter(sso_id__isnull=False)
  10. batch = []
  11. for user in sso_users.iterator(chunk_size=CHUNK_SIZE):
  12. batch.append(
  13. Subject(
  14. sub=str(user.sso_id),
  15. user=user,
  16. created_on=tz_now,
  17. last_used_on=tz_now,
  18. )
  19. )
  20. if len(batch) >= CHUNK_SIZE:
  21. Subject.objects.bulk_create(batch)
  22. batch = []
  23. if batch:
  24. Subject.objects.bulk_create(batch)
  25. class Migration(migrations.Migration):
  26. dependencies = [
  27. ("misago_oauth2", "0001_initial"),
  28. ("misago_users", "0022_deleteduser"),
  29. ]
  30. operations = [migrations.RunPython(create_settings)]