mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
models: Replace is_guest and is_realm_admin with UserProfile.role.
This new data model will be more extensible for future work on features like a primary administrator.
This commit is contained in:
49
zerver/migrations/0248_userprofile_role_start.py
Normal file
49
zerver/migrations/0248_userprofile_role_start.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.24 on 2019-10-03 22:27
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
|
||||
from django.db.migrations.state import StateApps
|
||||
|
||||
# The values at the time of this migration
|
||||
ROLE_REALM_ADMINISTRATOR = 200
|
||||
ROLE_MEMBER = 400
|
||||
ROLE_GUEST = 600
|
||||
|
||||
def update_role(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
|
||||
UserProfile = apps.get_model('zerver', 'UserProfile')
|
||||
for user in UserProfile.objects.all():
|
||||
if user.is_realm_admin:
|
||||
user.role = ROLE_REALM_ADMINISTRATOR
|
||||
elif user.is_guest:
|
||||
user.role = ROLE_GUEST
|
||||
else:
|
||||
user.role = ROLE_MEMBER
|
||||
user.save(update_fields=['role'])
|
||||
|
||||
def reverse_code(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
|
||||
UserProfile = apps.get_model('zerver', 'UserProfile')
|
||||
for user in UserProfile.objects.all():
|
||||
if user.role == ROLE_REALM_ADMINISTRATOR:
|
||||
user.is_realm_admin = True
|
||||
user.save(update_fields=['is_realm_admin'])
|
||||
elif user.role == ROLE_GUEST:
|
||||
user.is_guest = True
|
||||
user.save(update_fields=['is_guest'])
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('zerver', '0247_realmauditlog_event_type_to_int'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='userprofile',
|
||||
name='role',
|
||||
field=models.PositiveSmallIntegerField(null=True),
|
||||
),
|
||||
|
||||
migrations.RunPython(update_role, reverse_code=reverse_code),
|
||||
]
|
||||
Reference in New Issue
Block a user