Change X.realm.id to X.realm_id across codebase.

This makes it more clearly the pattern in the Zulip codebase, and thus
decreases the risk of accidentally doing database queries.
This commit is contained in:
Rishi Gupta
2017-01-03 12:04:55 -08:00
committed by Tim Abbott
parent caccf8ee17
commit cf762eaf84
11 changed files with 18 additions and 18 deletions

View File

@@ -338,7 +338,7 @@ def process_new_human_user(user_profile, prereg_user=None, newsletter_data=None)
'email_address': user_profile.email, 'email_address': user_profile.email,
'merge_fields': { 'merge_fields': {
'NAME': user_profile.full_name, 'NAME': user_profile.full_name,
'REALM_ID': user_profile.realm.id, 'REALM_ID': user_profile.realm_id,
'OPTIN_IP': newsletter_data["IP"], 'OPTIN_IP': newsletter_data["IP"],
'OPTIN_TIME': datetime.datetime.isoformat(now().replace(microsecond=0)), 'OPTIN_TIME': datetime.datetime.isoformat(now().replace(microsecond=0)),
}, },
@@ -918,7 +918,7 @@ def do_send_messages(messages):
if message['stream'] is None: if message['stream'] is None:
message['stream'] = Stream.objects.select_related("realm").get(id=message['message'].recipient.type_id) message['stream'] = Stream.objects.select_related("realm").get(id=message['message'].recipient.type_id)
if message['stream'].is_public(): if message['stream'].is_public():
event['realm_id'] = message['stream'].realm.id event['realm_id'] = message['stream'].realm_id
event['stream_name'] = message['stream'].name event['stream_name'] = message['stream'].name
if message['stream'].invite_only: if message['stream'].invite_only:
event['invite_only'] = True event['invite_only'] = True

View File

@@ -930,7 +930,7 @@ def export_uploads_from_local(realm, local_dir, output_dir):
mkdir_p(os.path.dirname(output_path)) mkdir_p(os.path.dirname(output_path))
subprocess.check_call(["cp", "-a", local_path, output_path]) subprocess.check_call(["cp", "-a", local_path, output_path])
stat = os.stat(local_path) stat = os.stat(local_path)
record = dict(realm_id=attachment.realm.id, record = dict(realm_id=attachment.realm_id,
user_profile_id=attachment.owner.id, user_profile_id=attachment.owner.id,
user_profile_email=attachment.owner.email, user_profile_email=attachment.owner.email,
s3_path=attachment.path_id, s3_path=attachment.path_id,
@@ -1358,7 +1358,7 @@ def import_uploads_s3(bucket_name, import_dir, processing_avatars=False):
user_profile_id = id_maps["user_profile"][user_profile_id] user_profile_id = id_maps["user_profile"][user_profile_id]
user_profile = get_user_profile_by_id(user_profile_id) user_profile = get_user_profile_by_id(user_profile_id)
key.set_metadata("user_profile_id", str(user_profile.id)) key.set_metadata("user_profile_id", str(user_profile.id))
key.set_metadata("realm_id", str(user_profile.realm.id)) key.set_metadata("realm_id", str(user_profile.realm_id))
key.set_metadata("orig_last_modified", record['last_modified']) key.set_metadata("orig_last_modified", record['last_modified'])
headers = {'Content-Type': record['content_type']} headers = {'Content-Type': record['content_type']}

View File

@@ -72,7 +72,7 @@ class MessageDict(object):
rendered_content_version = message.rendered_content_version, rendered_content_version = message.rendered_content_version,
sender_id = message.sender.id, sender_id = message.sender.id,
sender_email = message.sender.email, sender_email = message.sender.email,
sender_realm_id = message.sender.realm.id, sender_realm_id = message.sender.realm_id,
sender_realm_domain = message.sender.realm.domain, sender_realm_domain = message.sender.realm.domain,
sender_full_name = message.sender.full_name, sender_full_name = message.sender.full_name,
sender_short_name = message.sender.short_name, sender_short_name = message.sender.short_name,
@@ -316,7 +316,7 @@ def render_markdown(message, content, realm_id=None, realm_alert_words=None, mes
message.links_for_preview = set() message.links_for_preview = set()
if realm_id is None: if realm_id is None:
realm_id = message.sender.realm.id realm_id = message.sender.realm_id
if message.sending_client.name == "zephyr_mirror" and message.sender.realm.is_zephyr_mirror_realm: if message.sending_client.name == "zephyr_mirror" and message.sender.realm.is_zephyr_mirror_realm:
# Use slightly customized Markdown processor for content # Use slightly customized Markdown processor for content
# delivered via zephyr_mirror # delivered via zephyr_mirror

View File

@@ -145,7 +145,7 @@ def upload_image_to_s3(
key = Key(bucket) key = Key(bucket)
key.key = force_str(file_name) key.key = force_str(file_name)
key.set_metadata("user_profile_id", str(user_profile.id)) key.set_metadata("user_profile_id", str(user_profile.id))
key.set_metadata("realm_id", str(user_profile.realm.id)) key.set_metadata("realm_id", str(user_profile.realm_id))
if content_type is not None: if content_type is not None:
headers = {'Content-Type': force_str(content_type)} headers = {'Content-Type': force_str(content_type)}
@@ -184,14 +184,14 @@ def get_realm_for_filename(path):
if key is None: if key is None:
# This happens if the key does not exist. # This happens if the key does not exist.
return None return None
return get_user_profile_by_id(key.metadata["user_profile_id"]).realm.id return get_user_profile_by_id(key.metadata["user_profile_id"]).realm_id
class S3UploadBackend(ZulipUploadBackend): class S3UploadBackend(ZulipUploadBackend):
def upload_message_image(self, uploaded_file_name, content_type, file_data, user_profile, target_realm=None): def upload_message_image(self, uploaded_file_name, content_type, file_data, user_profile, target_realm=None):
# type: (Text, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text # type: (Text, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text
bucket_name = settings.S3_AUTH_UPLOADS_BUCKET bucket_name = settings.S3_AUTH_UPLOADS_BUCKET
s3_file_name = "/".join([ s3_file_name = "/".join([
str(target_realm.id if target_realm is not None else user_profile.realm.id), str(target_realm.id if target_realm is not None else user_profile.realm_id),
random_name(18), random_name(18),
sanitize_name(uploaded_file_name) sanitize_name(uploaded_file_name)
]) ])
@@ -315,7 +315,7 @@ class LocalUploadBackend(ZulipUploadBackend):
# type: (Text, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text # type: (Text, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text
# Split into 256 subdirectories to prevent directories from getting too big # Split into 256 subdirectories to prevent directories from getting too big
path = "/".join([ path = "/".join([
str(user_profile.realm.id), str(user_profile.realm_id),
format(random.randint(0, 255), 'x'), format(random.randint(0, 255), 'x'),
random_name(18), random_name(18),
sanitize_name(uploaded_file_name) sanitize_name(uploaded_file_name)

View File

@@ -456,7 +456,7 @@ def all_realm_filters():
def flush_realm_filter(sender, **kwargs): def flush_realm_filter(sender, **kwargs):
# type: (Any, **Any) -> None # type: (Any, **Any) -> None
realm_id = kwargs['instance'].realm.id realm_id = kwargs['instance'].realm_id
cache_delete(get_realm_filters_cache_key(realm_id)) cache_delete(get_realm_filters_cache_key(realm_id))
try: try:
per_request_realm_filters_cache.pop(realm_id) per_request_realm_filters_cache.pop(realm_id)

View File

@@ -495,9 +495,9 @@ class BugdownTest(TestCase):
directly for testing is kind of awkward directly for testing is kind of awkward
''' '''
class Instance(object): class Instance(object):
realm = None # type: Optional[Realm] realm_id = None # type: Optional[int]
instance = Instance() instance = Instance()
instance.realm = realm instance.realm_id = realm.id
flush_realm_filter(sender=None, instance=instance) flush_realm_filter(sender=None, instance=instance)
def save_new_realm_filter(): def save_new_realm_filter():

View File

@@ -274,7 +274,7 @@ class EventsRegisterTest(ZulipTestCase):
client = allocate_client_descriptor( client = allocate_client_descriptor(
dict(user_profile_id = self.user_profile.id, dict(user_profile_id = self.user_profile.id,
user_profile_email = self.user_profile.email, user_profile_email = self.user_profile.email,
realm_id = self.user_profile.realm.id, realm_id = self.user_profile.realm_id,
event_types = event_types, event_types = event_types,
client_type_name = "website", client_type_name = "website",
apply_markdown = True, apply_markdown = True,

View File

@@ -130,7 +130,7 @@ class TornadoTestCase(WebSocketBaseTestCase):
'narrow': [], 'narrow': [],
'user_profile_email': user_profile.email, 'user_profile_email': user_profile.email,
'all_public_streams': False, 'all_public_streams': False,
'realm_id': user_profile.realm.id, 'realm_id': user_profile.realm_id,
'client_type_name': 'website', 'client_type_name': 'website',
'event_types': None, 'event_types': None,
'user_profile_id': user_profile.id, 'user_profile_id': user_profile.id,

View File

@@ -69,7 +69,7 @@ def get_events_backend(request, user_profile, handler,
if queue_id is None: if queue_id is None:
events_query['new_queue_data'] = dict( events_query['new_queue_data'] = dict(
user_profile_id = user_profile.id, user_profile_id = user_profile.id,
realm_id = user_profile.realm.id, realm_id = user_profile.realm_id,
user_profile_email = user_profile.email, user_profile_email = user_profile.email,
event_types = event_types, event_types = event_types,
client_type_name = user_client.name, client_type_name = user_client.name,

View File

@@ -952,7 +952,7 @@ def render_message_backend(request, user_profile, content=REQ()):
message.content = content message.content = content
message.sending_client = request.client message.sending_client = request.client
rendered_content = render_markdown(message, content, realm_id=user_profile.realm.id) rendered_content = render_markdown(message, content, realm_id=user_profile.realm_id)
return json_success({"rendered": rendered_content}) return json_success({"rendered": rendered_content})
@authenticated_json_post_view @authenticated_json_post_view

View File

@@ -28,7 +28,7 @@ def serve_s3(request, user_profile, realm_id_str, filename):
realm_id = int(realm_id_str) realm_id = int(realm_id_str)
# Internal users can access all uploads so we can receive attachments in cross-realm messages # Internal users can access all uploads so we can receive attachments in cross-realm messages
if user_profile.realm.id == realm_id or user_profile.realm.domain == 'zulip.com': if user_profile.realm_id == realm_id or user_profile.realm.domain == 'zulip.com':
uri = get_signed_upload_url(url_path) uri = get_signed_upload_url(url_path)
return redirect(uri) return redirect(uri)
else: else: