migrations: Refactor the enum type fields.

Accessing attributes is the preferred design
pattern, as it is more readable.
This commit is contained in:
Ryan Rehman
2020-02-17 21:43:57 +05:30
committed by Tim Abbott
parent a2efe3ab64
commit b4ade7b6d8
16 changed files with 54 additions and 36 deletions

View File

@@ -2,6 +2,7 @@
from django.db import migrations, models
CORPORATE = 1
class Migration(migrations.Migration):
dependencies = [
@@ -12,6 +13,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='realm',
name='org_type',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=CORPORATE),
),
]

View File

@@ -2,6 +2,7 @@
from django.db import migrations, models
COMMUNITY = 2
class Migration(migrations.Migration):
dependencies = [
@@ -17,7 +18,7 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='realm',
name='org_type',
field=models.PositiveSmallIntegerField(default=2),
field=models.PositiveSmallIntegerField(default=COMMUNITY),
),
migrations.AlterField(
model_name='realm',

View File

@@ -3,6 +3,7 @@
from django.db import migrations, models
CORPORATE = 1
class Migration(migrations.Migration):
dependencies = [
@@ -13,6 +14,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='realm',
name='org_type',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=CORPORATE),
),
]

View File

@@ -7,19 +7,23 @@ from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
BOT_CREATION_EVERYONE = 1
def set_initial_value_for_bot_creation_policy(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model("zerver", "Realm")
Realm.BOT_CREATION_EVERYONE = 1
Realm.BOT_CREATION_LIMIT_GENERIC_BOTS = 2
for realm in Realm.objects.all():
if realm.create_generic_bot_by_admins_only:
realm.bot_creation_policy = 2 # BOT_CREATION_LIMIT_GENERIC_BOTS
realm.bot_creation_policy = Realm.BOT_CREATION_LIMIT_GENERIC_BOTS
else:
realm.bot_creation_policy = 1 # BOT_CREATION_EVERYONE
realm.bot_creation_policy = Realm.BOT_CREATION_EVERYONE
realm.save(update_fields=["bot_creation_policy"])
def reverse_code(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model("zerver", "Realm")
Realm.BOT_CREATION_EVERYONE = 1
for realm in Realm.objects.all():
if realm.bot_creation_policy == 1: # BOT_CREATION_EVERYONE
if realm.bot_creation_policy == Realm.BOT_CREATION_EVERYONE:
realm.create_generic_bot_by_admins_only = False
else:
realm.create_generic_bot_by_admins_only = True
@@ -35,7 +39,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='realm',
name='bot_creation_policy',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=BOT_CREATION_EVERYONE),
),
migrations.RunPython(set_initial_value_for_bot_creation_policy,
reverse_code=reverse_code),

View File

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
SELF_HOSTED = 1
class Migration(migrations.Migration):
dependencies = [
@@ -16,6 +17,6 @@ class Migration(migrations.Migration):
model_name='realm',
name='plan_type',
# Realm.SELF_HOSTED
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=SELF_HOSTED),
),
]

View File

@@ -4,7 +4,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
EMAIL_ADDRESS_VISIBILITY_EVERYONE = 1
class Migration(migrations.Migration):
dependencies = [
@@ -15,6 +15,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='realm',
name='email_address_visibility',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=EMAIL_ADDRESS_VISIBILITY_EVERYONE),
),
]

View File

@@ -7,10 +7,12 @@ from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
INVITE_TO_STREAM_POLICY_MEMBERS = 1
def handle_waiting_period(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model('zerver', 'Realm')
Realm.INVITE_TO_STREAM_POLICY_WAITING_PERIOD = 3
Realm.objects.filter(waiting_period_threshold__gt=0).update(
invite_to_stream_policy=3) # INVITE_TO_STREAM_POLICY_WAITING_PERIOD
invite_to_stream_policy=Realm.INVITE_TO_STREAM_POLICY_WAITING_PERIOD)
class Migration(migrations.Migration):
@@ -22,7 +24,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='realm',
name='invite_to_stream_policy',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=INVITE_TO_STREAM_POLICY_MEMBERS),
),
migrations.RunPython(handle_waiting_period,
reverse_code=migrations.RunPython.noop),

View File

@@ -9,14 +9,17 @@ from django.db.migrations.state import StateApps
def upgrade_create_stream_policy(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model('zerver', 'Realm')
Realm.CREATE_STREAM_POLICY_MEMBERS = 1
Realm.CREATE_STREAM_POLICY_ADMINS = 2
Realm.CREATE_STREAM_POLICY_WAITING_PERIOD = 3
Realm.objects.filter(waiting_period_threshold__exact=0) \
.filter(create_stream_by_admins_only=False) \
.update(create_stream_policy=1) # CREATE_STREAM_POLICY_MEMBERS
.update(create_stream_policy=Realm.CREATE_STREAM_POLICY_MEMBERS)
Realm.objects.filter(create_stream_by_admins_only=True) \
.update(create_stream_policy=2) # CREATE_STREAM_POLICY_ADMINS
.update(create_stream_policy=Realm.CREATE_STREAM_POLICY_ADMINS)
Realm.objects.filter(waiting_period_threshold__gt=0) \
.filter(create_stream_by_admins_only=False) \
.update(create_stream_policy=3) # CREATE_STREAM_POLICY_WAITING_PERIOD
.update(create_stream_policy=Realm.CREATE_STREAM_POLICY_WAITING_PERIOD)
class Migration(migrations.Migration):

View File

@@ -68,7 +68,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='realm',
name='video_chat_provider',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=VIDEO_CHAT_PROVIDERS['jitsi_meet']['id']),
),
migrations.RunPython(update_existing_video_chat_provider_values,
reverse_code=reverse_code),

View File

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
DEMOTE_STREAMS_AUTOMATIC = 1
class Migration(migrations.Migration):
dependencies = [
@@ -15,6 +16,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='userprofile',
name='demote_inactive_streams',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=DEMOTE_STREAMS_AUTOMATIC),
),
]

View File

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
DESKTOP_ICON_COUNT_DISPLAY_MESSAGES = 1
class Migration(migrations.Migration):
dependencies = [
@@ -15,6 +16,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='userprofile',
name='desktop_icon_count_display',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=DESKTOP_ICON_COUNT_DISPLAY_MESSAGES),
),
]

View File

@@ -6,29 +6,30 @@ 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')
# The values at the time of this migration
UserProfile.ROLE_REALM_ADMINISTRATOR = 200
UserProfile.ROLE_MEMBER = 400
UserProfile.ROLE_GUEST = 600
for user in UserProfile.objects.all():
if user.is_realm_admin:
user.role = ROLE_REALM_ADMINISTRATOR
user.role = UserProfile.ROLE_REALM_ADMINISTRATOR
elif user.is_guest:
user.role = ROLE_GUEST
user.role = UserProfile.ROLE_GUEST
else:
user.role = ROLE_MEMBER
user.role = UserProfile.ROLE_MEMBER
user.save(update_fields=['role'])
def reverse_code(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserProfile = apps.get_model('zerver', 'UserProfile')
UserProfile.ROLE_REALM_ADMINISTRATOR = 200
UserProfile.ROLE_GUEST = 600
for user in UserProfile.objects.all():
if user.role == ROLE_REALM_ADMINISTRATOR:
if user.role == UserProfile.ROLE_REALM_ADMINISTRATOR:
user.is_realm_admin = True
user.save(update_fields=['is_realm_admin'])
elif user.role == ROLE_GUEST:
elif user.role == UserProfile.ROLE_GUEST:
user.is_guest = True
user.save(update_fields=['is_guest'])

View File

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
USER_GROUP_EDIT_POLICY_MEMBERS = 1
class Migration(migrations.Migration):
dependencies = [
@@ -15,6 +16,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='realm',
name='user_group_edit_policy',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=USER_GROUP_EDIT_POLICY_MEMBERS),
),
]

View File

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
PRIVATE_MESSAGE_POLICY_UNLIMITED = 1
class Migration(migrations.Migration):
dependencies = [
@@ -15,6 +16,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='realm',
name='private_message_policy',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=PRIVATE_MESSAGE_POLICY_UNLIMITED),
),
]

View File

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
STREAM_POST_POLICY_EVERYONE = 1
class Migration(migrations.Migration):
dependencies = [
@@ -15,6 +16,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='stream',
name='stream_post_policy',
field=models.PositiveSmallIntegerField(default=1),
field=models.PositiveSmallIntegerField(default=STREAM_POST_POLICY_EVERYONE),
),
]

View File

@@ -8,14 +8,13 @@ from django.db.migrations.state import StateApps
def upgrade_stream_post_policy(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
STREAM_POST_POLICY_EVERYONE = 1
STREAM_POST_POLICY_ADMINS = 2
Stream = apps.get_model('zerver', 'Stream')
Stream.STREAM_POST_POLICY_EVERYONE = 1
Stream.STREAM_POST_POLICY_ADMINS = 2
Stream.objects.filter(is_announcement_only=False) \
.update(stream_post_policy=STREAM_POST_POLICY_EVERYONE)
.update(stream_post_policy=Stream.STREAM_POST_POLICY_EVERYONE)
Stream.objects.filter(is_announcement_only=True) \
.update(stream_post_policy=STREAM_POST_POLICY_ADMINS)
.update(stream_post_policy=Stream.STREAM_POST_POLICY_ADMINS)
class Migration(migrations.Migration):