Files
zulip/zerver/migrations/0284_convert_realm_admins_to_realm_owners.py
Anders Kaseorg 365fe0b3d5 python: Sort imports with isort.
Fixes #2665.

Regenerated by tabbott with `lint --fix` after a rebase and change in
parameters.

Note from tabbott: In a few cases, this converts technical debt in the
form of unsorted imports into different technical debt in the form of
our largest files having very long, ugly import sequences at the
start.  I expect this change will increase pressure for us to split
those files, which isn't a bad thing.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-06-11 16:45:32 -07:00

69 lines
2.9 KiB
Python

# Generated by Django 2.2.12 on 2020-05-16 18:34
from typing import Any, Dict
import ujson
from django.db import migrations
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db.models import Count
from django.utils.timezone import now as timezone_now
def set_realm_admins_as_realm_owners(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserProfile = apps.get_model('zerver', 'UserProfile')
RealmAuditLog = apps.get_model('zerver', 'RealmAuditLog')
UserProfile.ROLE_REALM_OWNER = 100
UserProfile.ROLE_REALM_ADMINISTRATOR = 200
UserProfile.ROLE_MEMBER = 400
UserProfile.ROLE_GUEST = 600
RealmAuditLog.USER_ROLE_CHANGED = 105
RealmAuditLog.OLD_VALUE = '1'
RealmAuditLog.NEW_VALUE = '2'
RealmAuditLog.ROLE_COUNT = '10'
RealmAuditLog.ROLE_COUNT_HUMANS = '11'
RealmAuditLog.ROLE_COUNT_BOTS = '12'
def realm_user_count_by_role(realm: Any) -> Dict[str, Any]:
human_counts = {UserProfile.ROLE_REALM_ADMINISTRATOR: 0,
UserProfile.ROLE_REALM_OWNER: 0,
UserProfile.ROLE_MEMBER: 0,
UserProfile.ROLE_GUEST: 0}
for value_dict in list(UserProfile.objects.filter(
realm=realm, is_bot=False, is_active=True).values('role').annotate(Count('role'))):
human_counts[value_dict['role']] = value_dict['role__count']
bot_count = UserProfile.objects.filter(realm=realm, is_bot=True, is_active=True).count()
return {
RealmAuditLog.ROLE_COUNT_HUMANS: human_counts,
RealmAuditLog.ROLE_COUNT_BOTS: bot_count,
}
objects_to_create = []
for user in UserProfile.objects.filter(is_active=True, role=UserProfile.ROLE_REALM_ADMINISTRATOR):
user.role = UserProfile.ROLE_REALM_OWNER
user.save(update_fields=['role'])
audit_log_entry = RealmAuditLog(realm=user.realm, modified_user=user,
event_type=RealmAuditLog.USER_ROLE_CHANGED,
event_time=timezone_now(),
extra_data=ujson.dumps({
RealmAuditLog.OLD_VALUE: UserProfile.ROLE_REALM_ADMINISTRATOR,
RealmAuditLog.NEW_VALUE: UserProfile.ROLE_REALM_OWNER,
RealmAuditLog.ROLE_COUNT: realm_user_count_by_role(user.realm),
}))
objects_to_create.append(audit_log_entry)
RealmAuditLog.objects.bulk_create(objects_to_create)
class Migration(migrations.Migration):
dependencies = [
('zerver', '0283_apple_auth'),
]
operations = [
migrations.RunPython(set_realm_admins_as_realm_owners,
reverse_code=migrations.RunPython.noop,
elidable=True),
]