billing: Always transition to Realm.LIMITED via do_change_plan_type.

Fixes a bug in import_realm where secondary attributes like message
visibility weren't being set, and also makes bugs like this less likely in
the future.

Also, putting the plan_type change at the end of import_realm, so that
future restrictions to LIMITED realms don't affect the import process.
This commit is contained in:
Rishi Gupta 2018-12-12 23:19:29 -08:00 committed by Tim Abbott
parent b245c661da
commit 8a95526ced
4 changed files with 12 additions and 10 deletions

View File

@ -3295,9 +3295,6 @@ def do_create_realm(string_id: str, name: str,
kwargs = {} # type: Dict[str, Any]
if emails_restricted_to_domains is not None:
kwargs['emails_restricted_to_domains'] = emails_restricted_to_domains
if settings.BILLING_ENABLED:
kwargs['plan_type'] = Realm.LIMITED
kwargs['message_visibility_limit'] = Realm.MESSAGE_VISIBILITY_LIMITED
realm = Realm(string_id=string_id, name=name, **kwargs)
realm.save()
@ -3312,6 +3309,9 @@ def do_create_realm(string_id: str, name: str,
realm.save(update_fields=['notifications_stream', 'signup_notifications_stream'])
if settings.BILLING_ENABLED:
do_change_plan_type(realm, Realm.LIMITED)
# Log the event
log_event({"type": "realm_created",
"string_id": string_id,

View File

@ -13,7 +13,8 @@ from django.utils.timezone import utc as timezone_utc, now as timezone_now
from typing import Any, Dict, List, Optional, Set, Tuple, \
Iterable, cast
from zerver.lib.actions import UserMessageLite, bulk_insert_ums
from zerver.lib.actions import UserMessageLite, bulk_insert_ums, \
do_change_plan_type
from zerver.lib.avatar_hash import user_avatar_path_from_ids
from zerver.lib.bulk_create import bulk_create_users
from zerver.lib.timestamp import datetime_to_timestamp
@ -683,10 +684,6 @@ def do_import_realm(import_dir: Path, subdomain: str) -> Realm:
update_model_ids(Realm, data, 'realm')
realm = Realm(**data['zerver_realm'][0])
if settings.BILLING_ENABLED:
realm.plan_type = Realm.LIMITED
else:
realm.plan_type = Realm.SELF_HOSTED
if realm.notifications_stream_id is not None:
notifications_stream_id = int(realm.notifications_stream_id) # type: Optional[int]
@ -909,6 +906,9 @@ def do_import_realm(import_dir: Path, subdomain: str) -> Realm:
data = ujson.load(f)
import_attachments(data)
if settings.BILLING_ENABLED:
do_change_plan_type(realm, Realm.LIMITED)
return realm
# create_users and do_import_system_bots differ from their equivalent in

View File

@ -537,7 +537,8 @@ class ImportExportTest(ZulipTestCase):
self._export_realm(original_realm)
with patch('logging.info'):
do_import_realm('var/test-export', 'test-zulip')
with self.settings(BILLING_ENABLED=False):
do_import_realm('var/test-export', 'test-zulip')
# sanity checks

View File

@ -561,7 +561,8 @@ class SlackImporter(ZulipTestCase):
self.assertTrue(os.path.exists(output_dir + '/realm.json'))
# test import of the converted slack data into an existing database
do_import_realm(output_dir, test_realm_subdomain)
with self.settings(BILLING_ENABLED=False):
do_import_realm(output_dir, test_realm_subdomain)
realm = get_realm(test_realm_subdomain)
self.assertTrue(realm.name, test_realm_subdomain)