management: Use Realm.objects.get instead of get_realm.

The Realm.DoesNotExist exception will never be raised if we pass a
string_id as in that case zerver.models.get_realm function would be
used for fetching. realm.zerver.models.get_realm uses filter query
so the exception will not be raised even if there are no realm which
matches the query.

Tests added by tabbott.
This commit is contained in:
Vishnu Ks 2017-07-08 03:09:55 +05:30 committed by Tim Abbott
parent d5517bae36
commit 8954c0c726
2 changed files with 4 additions and 2 deletions

View File

@ -7,7 +7,7 @@ from django.core.exceptions import MultipleObjectsReturned
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from typing import Any, Dict, Optional, Text from typing import Any, Dict, Optional, Text
from zerver.models import get_realm, Realm, UserProfile from zerver.models import Realm, UserProfile
def is_integer_string(val): def is_integer_string(val):
# type: (str) -> bool # type: (str) -> bool
@ -39,7 +39,7 @@ class ZulipBaseCommand(BaseCommand):
try: try:
if is_integer_string(val): if is_integer_string(val):
return Realm.objects.get(id=val) return Realm.objects.get(id=val)
return get_realm(val) return Realm.objects.get(string_id=val)
except Realm.DoesNotExist: except Realm.DoesNotExist:
raise CommandError("The is no realm with id '%s'. Aborting." % raise CommandError("The is no realm with id '%s'. Aborting." %
(options["realm_id"],)) (options["realm_id"],))

View File

@ -25,6 +25,8 @@ class TestZulipBaseCommand(ZulipTestCase):
self.assertEqual(command.get_realm(dict(realm_id='1')), get_realm("zulip")) self.assertEqual(command.get_realm(dict(realm_id='1')), get_realm("zulip"))
with self.assertRaisesRegex(CommandError, "The is no realm with id"): with self.assertRaisesRegex(CommandError, "The is no realm with id"):
command.get_realm(dict(realm_id='17')) command.get_realm(dict(realm_id='17'))
with self.assertRaisesRegex(CommandError, "The is no realm with id"):
command.get_realm(dict(realm_id='mit'))
def test_get_user(self): def test_get_user(self):
# type: () -> None # type: () -> None