We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Enable enter to send in dev env
In dev environment every time we fresh login we have to check ```Enter to send``` to true. We should enable it by default for dev env only to increase testing speed.

```git grep enter_sends``` will help you.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `zerver/lib/create_user.py`
Content:
```
1 from __future__ import absolute_import
2 
3 from django.contrib.auth.models import UserManager
4 from django.utils import timezone
5 from zerver.models import UserProfile, Recipient, Subscription, Realm, Stream
6 import base64
7 import ujson
8 import os
9 import string
10 from six.moves import range
11 
12 from typing import Optional, Text
13 
14 def random_api_key():
15     # type: () -> Text
16     choices = string.ascii_letters + string.digits
17     altchars = ''.join([choices[ord(os.urandom(1)) % 62] for _ in range(2)]).encode("utf-8")
18     return base64.b64encode(os.urandom(24), altchars=altchars).decode("utf-8")
19 
20 # create_user_profile is based on Django's User.objects.create_user,
21 # except that we don't save to the database so it can used in
22 # bulk_creates
23 #
24 # Only use this for bulk_create -- for normal usage one should use
25 # create_user (below) which will also make the Subscription and
26 # Recipient objects
27 def create_user_profile(realm, email, password, active, bot_type, full_name,
28                         short_name, bot_owner, is_mirror_dummy, tos_version,
29                         tutorial_status=UserProfile.TUTORIAL_WAITING):
30     # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text]) -> UserProfile
31     now = timezone.now()
32     email = UserManager.normalize_email(email)
33 
34     user_profile = UserProfile(email=email, is_staff=False, is_active=active,
35                                full_name=full_name, short_name=short_name,
36                                last_login=now, date_joined=now, realm=realm,
37                                pointer=-1, is_bot=bool(bot_type), bot_type=bot_type,
38                                bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy,
39                                tos_version=tos_version,
40                                tutorial_status=tutorial_status,
41                                onboarding_steps=ujson.dumps([]),
42                                default_language=realm.default_language)
43 
44     if bot_type or not active:
45         password = None
46 
47     user_profile.set_password(password)
48 
49     user_profile.api_key = random_api_key()
50     return user_profile
51 
52 def create_user(email, password, realm, full_name, short_name,
53                 active=True, bot_type=None, bot_owner=None, tos_version=None,
54                 avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,
55                 is_mirror_dummy=False, default_sending_stream=None,
56                 default_events_register_stream=None,
57                 default_all_public_streams=None, user_profile_id=None):
58     # type: (Text, Text, Realm, Text, Text, bool, Optional[int], Optional[UserProfile], Optional[Text], Text, bool, Optional[Stream], Optional[Stream], Optional[bool], Optional[int]) -> UserProfile
59     user_profile = create_user_profile(realm, email, password, active, bot_type,
60                                        full_name, short_name, bot_owner,
61                                        is_mirror_dummy, tos_version)
62     user_profile.avatar_source = avatar_source
63     user_profile.default_sending_stream = default_sending_stream
64     user_profile.default_events_register_stream = default_events_register_stream
65     # Allow the ORM default to be used if not provided
66     if default_all_public_streams is not None:
67         user_profile.default_all_public_streams = default_all_public_streams
68 
69     if user_profile_id is not None:
70         user_profile.id = user_profile_id
71 
72     user_profile.save()
73     recipient = Recipient.objects.create(type_id=user_profile.id,
74                                          type=Recipient.PERSONAL)
75     Subscription.objects.create(user_profile=user_profile, recipient=recipient)
76     return user_profile
77
```

Path: `zerver/lib/bulk_create.py`
Content:
```
1 from __future__ import absolute_import
2 from typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple, Text
3 
4 from zerver.lib.initial_password import initial_password
5 from zerver.models import Realm, Stream, UserProfile, Huddle, \
6     Subscription, Recipient, Client, RealmAuditLog, get_huddle_hash
7 from zerver.lib.create_user import create_user_profile
8 
9 def bulk_create_users(realm, users_raw, bot_type=None, tos_version=None):
10     # type: (Realm, Set[Tuple[Text, Text, Text, bool]], Optional[int], Optional[Text]) -> None
11     """
12     Creates and saves a UserProfile with the given email.
13     Has some code based off of UserManage.create_user, but doesn't .save()
14     """
15     existing_users = frozenset(UserProfile.objects.values_list('email', flat=True))
16     users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users])
17 
18     # Now create user_profiles
19     profiles_to_create = [] # type: List[UserProfile]
20     for (email, full_name, short_name, active) in users:
21         profile = create_user_profile(realm, email,
22                                       initial_password(email), active, bot_type,
23                                       full_name, short_name, None, False, tos_version,
24                                       tutorial_status=UserProfile.TUTORIAL_FINISHED)
25         profiles_to_create.append(profile)
26     UserProfile.objects.bulk_create(profiles_to_create)
27 
28     RealmAuditLog.objects.bulk_create(
29         [RealmAuditLog(realm=profile_.realm, modified_user=profile_,
30                        event_type='user_created', event_time=profile_.date_joined)
31          for profile_ in profiles_to_create])
32 
33     profiles_by_email = {} # type: Dict[Text, UserProfile]
34     profiles_by_id = {} # type: Dict[int, UserProfile]
35     for profile in UserProfile.objects.select_related().all():
36         profiles_by_email[profile.email] = profile
37         profiles_by_id[profile.id] = profile
38 
39     recipients_to_create = [] # type: List[Recipient]
40     for (email, full_name, short_name, active) in users:
41         recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id,
42                                               type=Recipient.PERSONAL))
43     Recipient.objects.bulk_create(recipients_to_create)
44 
45     recipients_by_email = {} # type: Dict[Text, Recipient]
46     for recipient in Recipient.objects.filter(type=Recipient.PERSONAL):
47         recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient
48 
49     subscriptions_to_create = [] # type: List[Subscription]
50     for (email, full_name, short_name, active) in users:
51         subscriptions_to_create.append(
52             Subscription(user_profile_id=profiles_by_email[email].id,
53                          recipient=recipients_by_email[email]))
54     Subscription.objects.bulk_create(subscriptions_to_create)
55 
56 def bulk_create_streams(realm, stream_dict):
57     # type: (Realm, Dict[Text, Dict[Text, Any]]) -> None
58     existing_streams = frozenset([name.lower() for name in
59                                   Stream.objects.filter(realm=realm)
60                                   .values_list('name', flat=True)])
61     streams_to_create = [] # type: List[Stream]
62     for name, options in stream_dict.items():
63         if name.lower() not in existing_streams:
64             streams_to_create.append(
65                 Stream(
66                     realm=realm, name=name, description=options["description"],
67                     invite_only=options["invite_only"]
68                 )
69             )
70     Stream.objects.bulk_create(streams_to_create)
71 
72     recipients_to_create = [] # type: List[Recipient]
73     for stream in Stream.objects.filter(realm=realm).values('id', 'name'):
74         if stream['name'].lower() not in existing_streams:
75             recipients_to_create.append(Recipient(type_id=stream['id'],
76                                                   type=Recipient.STREAM))
77     Recipient.objects.bulk_create(recipients_to_create)
78 
79 def bulk_create_clients(client_list):
80     # type: (Iterable[Text]) -> None
81     existing_clients = set(client.name for client in Client.objects.select_related().all()) # type: Set[Text]
82 
83     clients_to_create = [] # type: List[Client]
84     for name in client_list:
85         if name not in existing_clients:
86             clients_to_create.append(Client(name=name))
87             existing_clients.add(name)
88     Client.objects.bulk_create(clients_to_create)
89 
90 def bulk_create_huddles(users, huddle_user_list):
91     # type: (Dict[Text, UserProfile], Iterable[Iterable[Text]]) -> None
92     huddles = {} # type: Dict[Text, Huddle]
93     huddles_by_id = {} # type: Dict[int, Huddle]
94     huddle_set = set() # type: Set[Tuple[Text, Tuple[int, ...]]]
95     existing_huddles = set() # type: Set[Text]
96     for huddle in Huddle.objects.all():
97         existing_huddles.add(huddle.huddle_hash)
98     for huddle_users in huddle_user_list:
99         user_ids = [users[email].id for email in huddle_users] # type: List[int]
100         huddle_hash = get_huddle_hash(user_ids)
101         if huddle_hash in existing_huddles:
102             continue
103         huddle_set.add((huddle_hash, tuple(sorted(user_ids))))
104 
105     huddles_to_create = [] # type: List[Huddle]
106     for (huddle_hash, _) in huddle_set:
107         huddles_to_create.append(Huddle(huddle_hash=huddle_hash))
108     Huddle.objects.bulk_create(huddles_to_create)
109 
110     for huddle in Huddle.objects.all():
111         huddles[huddle.huddle_hash] = huddle
112         huddles_by_id[huddle.id] = huddle
113 
114     recipients_to_create = [] # type: List[Recipient]
115     for (huddle_hash, _) in huddle_set:
116         recipients_to_create.append(Recipient(type_id=huddles[huddle_hash].id, type=Recipient.HUDDLE))
117     Recipient.objects.bulk_create(recipients_to_create)
118 
119     huddle_recipients = {} # type: Dict[Text, Recipient]
120     for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):
121         huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient
122 
123     subscriptions_to_create = [] # type: List[Subscription]
124     for (huddle_hash, huddle_user_ids) in huddle_set:
125         for user_id in huddle_user_ids:
126             subscriptions_to_create.append(Subscription(active=True, user_profile_id=user_id,
127                                                         recipient=huddle_recipients[huddle_hash]))
128     Subscription.objects.bulk_create(subscriptions_to_create)
129
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/zerver/lib/bulk_create.py b/zerver/lib/bulk_create.py
--- a/zerver/lib/bulk_create.py
+++ b/zerver/lib/bulk_create.py
@@ -21,7 +21,8 @@
         profile = create_user_profile(realm, email,
                                       initial_password(email), active, bot_type,
                                       full_name, short_name, None, False, tos_version,
-                                      tutorial_status=UserProfile.TUTORIAL_FINISHED)
+                                      tutorial_status=UserProfile.TUTORIAL_FINISHED,
+                                      enter_sends=True)
         profiles_to_create.append(profile)
     UserProfile.objects.bulk_create(profiles_to_create)
 
diff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.py
--- a/zerver/lib/create_user.py
+++ b/zerver/lib/create_user.py
@@ -26,8 +26,9 @@
 # Recipient objects
 def create_user_profile(realm, email, password, active, bot_type, full_name,
                         short_name, bot_owner, is_mirror_dummy, tos_version,
-                        tutorial_status=UserProfile.TUTORIAL_WAITING):
-    # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text]) -> UserProfile
+                        tutorial_status=UserProfile.TUTORIAL_WAITING,
+                        enter_sends=False):
+    # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text], bool) -> UserProfile
     now = timezone.now()
     email = UserManager.normalize_email(email)
 
@@ -38,6 +39,7 @@
                                bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy,
                                tos_version=tos_version,
                                tutorial_status=tutorial_status,
+                               enter_sends=enter_sends,
                                onboarding_steps=ujson.dumps([]),
                                default_language=realm.default_language)

{"golden_diff": "diff --git a/zerver/lib/bulk_create.py b/zerver/lib/bulk_create.py\n--- a/zerver/lib/bulk_create.py\n+++ b/zerver/lib/bulk_create.py\n@@ -21,7 +21,8 @@\n         profile = create_user_profile(realm, email,\n                                       initial_password(email), active, bot_type,\n                                       full_name, short_name, None, False, tos_version,\n-                                      tutorial_status=UserProfile.TUTORIAL_FINISHED)\n+                                      tutorial_status=UserProfile.TUTORIAL_FINISHED,\n+                                      enter_sends=True)\n         profiles_to_create.append(profile)\n     UserProfile.objects.bulk_create(profiles_to_create)\n \ndiff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.py\n--- a/zerver/lib/create_user.py\n+++ b/zerver/lib/create_user.py\n@@ -26,8 +26,9 @@\n # Recipient objects\n def create_user_profile(realm, email, password, active, bot_type, full_name,\n                         short_name, bot_owner, is_mirror_dummy, tos_version,\n-                        tutorial_status=UserProfile.TUTORIAL_WAITING):\n-    # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text]) -> UserProfile\n+                        tutorial_status=UserProfile.TUTORIAL_WAITING,\n+                        enter_sends=False):\n+    # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text], bool) -> UserProfile\n     now = timezone.now()\n     email = UserManager.normalize_email(email)\n \n@@ -38,6 +39,7 @@\n                                bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy,\n                                tos_version=tos_version,\n                                tutorial_status=tutorial_status,\n+                               enter_sends=enter_sends,\n                                onboarding_steps=ujson.dumps([]),\n                                default_language=realm.default_language)\n", "issue": "Enable enter to send in dev env\nIn dev environment every time we fresh login we have to check ```Enter to send``` to true. We should enable it by default for dev env only to increase testing speed.\r\n\r\n```git grep enter_sends``` will help you.\n", "before_files": [{"content": "from __future__ import absolute_import\n\nfrom django.contrib.auth.models import UserManager\nfrom django.utils import timezone\nfrom zerver.models import UserProfile, Recipient, Subscription, Realm, Stream\nimport base64\nimport ujson\nimport os\nimport string\nfrom six.moves import range\n\nfrom typing import Optional, Text\n\ndef random_api_key():\n    # type: () -> Text\n    choices = string.ascii_letters + string.digits\n    altchars = ''.join([choices[ord(os.urandom(1)) % 62] for _ in range(2)]).encode(\"utf-8\")\n    return base64.b64encode(os.urandom(24), altchars=altchars).decode(\"utf-8\")\n\n# create_user_profile is based on Django's User.objects.create_user,\n# except that we don't save to the database so it can used in\n# bulk_creates\n#\n# Only use this for bulk_create -- for normal usage one should use\n# create_user (below) which will also make the Subscription and\n# Recipient objects\ndef create_user_profile(realm, email, password, active, bot_type, full_name,\n                        short_name, bot_owner, is_mirror_dummy, tos_version,\n                        tutorial_status=UserProfile.TUTORIAL_WAITING):\n    # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text]) -> UserProfile\n    now = timezone.now()\n    email = UserManager.normalize_email(email)\n\n    user_profile = UserProfile(email=email, is_staff=False, is_active=active,\n                               full_name=full_name, short_name=short_name,\n                               last_login=now, date_joined=now, realm=realm,\n                               pointer=-1, is_bot=bool(bot_type), bot_type=bot_type,\n                               bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy,\n                               tos_version=tos_version,\n                               tutorial_status=tutorial_status,\n                               onboarding_steps=ujson.dumps([]),\n                               default_language=realm.default_language)\n\n    if bot_type or not active:\n        password = None\n\n    user_profile.set_password(password)\n\n    user_profile.api_key = random_api_key()\n    return user_profile\n\ndef create_user(email, password, realm, full_name, short_name,\n                active=True, bot_type=None, bot_owner=None, tos_version=None,\n                avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,\n                is_mirror_dummy=False, default_sending_stream=None,\n                default_events_register_stream=None,\n                default_all_public_streams=None, user_profile_id=None):\n    # type: (Text, Text, Realm, Text, Text, bool, Optional[int], Optional[UserProfile], Optional[Text], Text, bool, Optional[Stream], Optional[Stream], Optional[bool], Optional[int]) -> UserProfile\n    user_profile = create_user_profile(realm, email, password, active, bot_type,\n                                       full_name, short_name, bot_owner,\n                                       is_mirror_dummy, tos_version)\n    user_profile.avatar_source = avatar_source\n    user_profile.default_sending_stream = default_sending_stream\n    user_profile.default_events_register_stream = default_events_register_stream\n    # Allow the ORM default to be used if not provided\n    if default_all_public_streams is not None:\n        user_profile.default_all_public_streams = default_all_public_streams\n\n    if user_profile_id is not None:\n        user_profile.id = user_profile_id\n\n    user_profile.save()\n    recipient = Recipient.objects.create(type_id=user_profile.id,\n                                         type=Recipient.PERSONAL)\n    Subscription.objects.create(user_profile=user_profile, recipient=recipient)\n    return user_profile\n", "path": "zerver/lib/create_user.py"}, {"content": "from __future__ import absolute_import\nfrom typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple, Text\n\nfrom zerver.lib.initial_password import initial_password\nfrom zerver.models import Realm, Stream, UserProfile, Huddle, \\\n    Subscription, Recipient, Client, RealmAuditLog, get_huddle_hash\nfrom zerver.lib.create_user import create_user_profile\n\ndef bulk_create_users(realm, users_raw, bot_type=None, tos_version=None):\n    # type: (Realm, Set[Tuple[Text, Text, Text, bool]], Optional[int], Optional[Text]) -> None\n    \"\"\"\n    Creates and saves a UserProfile with the given email.\n    Has some code based off of UserManage.create_user, but doesn't .save()\n    \"\"\"\n    existing_users = frozenset(UserProfile.objects.values_list('email', flat=True))\n    users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users])\n\n    # Now create user_profiles\n    profiles_to_create = [] # type: List[UserProfile]\n    for (email, full_name, short_name, active) in users:\n        profile = create_user_profile(realm, email,\n                                      initial_password(email), active, bot_type,\n                                      full_name, short_name, None, False, tos_version,\n                                      tutorial_status=UserProfile.TUTORIAL_FINISHED)\n        profiles_to_create.append(profile)\n    UserProfile.objects.bulk_create(profiles_to_create)\n\n    RealmAuditLog.objects.bulk_create(\n        [RealmAuditLog(realm=profile_.realm, modified_user=profile_,\n                       event_type='user_created', event_time=profile_.date_joined)\n         for profile_ in profiles_to_create])\n\n    profiles_by_email = {} # type: Dict[Text, UserProfile]\n    profiles_by_id = {} # type: Dict[int, UserProfile]\n    for profile in UserProfile.objects.select_related().all():\n        profiles_by_email[profile.email] = profile\n        profiles_by_id[profile.id] = profile\n\n    recipients_to_create = [] # type: List[Recipient]\n    for (email, full_name, short_name, active) in users:\n        recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id,\n                                              type=Recipient.PERSONAL))\n    Recipient.objects.bulk_create(recipients_to_create)\n\n    recipients_by_email = {} # type: Dict[Text, Recipient]\n    for recipient in Recipient.objects.filter(type=Recipient.PERSONAL):\n        recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient\n\n    subscriptions_to_create = [] # type: List[Subscription]\n    for (email, full_name, short_name, active) in users:\n        subscriptions_to_create.append(\n            Subscription(user_profile_id=profiles_by_email[email].id,\n                         recipient=recipients_by_email[email]))\n    Subscription.objects.bulk_create(subscriptions_to_create)\n\ndef bulk_create_streams(realm, stream_dict):\n    # type: (Realm, Dict[Text, Dict[Text, Any]]) -> None\n    existing_streams = frozenset([name.lower() for name in\n                                  Stream.objects.filter(realm=realm)\n                                  .values_list('name', flat=True)])\n    streams_to_create = [] # type: List[Stream]\n    for name, options in stream_dict.items():\n        if name.lower() not in existing_streams:\n            streams_to_create.append(\n                Stream(\n                    realm=realm, name=name, description=options[\"description\"],\n                    invite_only=options[\"invite_only\"]\n                )\n            )\n    Stream.objects.bulk_create(streams_to_create)\n\n    recipients_to_create = [] # type: List[Recipient]\n    for stream in Stream.objects.filter(realm=realm).values('id', 'name'):\n        if stream['name'].lower() not in existing_streams:\n            recipients_to_create.append(Recipient(type_id=stream['id'],\n                                                  type=Recipient.STREAM))\n    Recipient.objects.bulk_create(recipients_to_create)\n\ndef bulk_create_clients(client_list):\n    # type: (Iterable[Text]) -> None\n    existing_clients = set(client.name for client in Client.objects.select_related().all()) # type: Set[Text]\n\n    clients_to_create = [] # type: List[Client]\n    for name in client_list:\n        if name not in existing_clients:\n            clients_to_create.append(Client(name=name))\n            existing_clients.add(name)\n    Client.objects.bulk_create(clients_to_create)\n\ndef bulk_create_huddles(users, huddle_user_list):\n    # type: (Dict[Text, UserProfile], Iterable[Iterable[Text]]) -> None\n    huddles = {} # type: Dict[Text, Huddle]\n    huddles_by_id = {} # type: Dict[int, Huddle]\n    huddle_set = set() # type: Set[Tuple[Text, Tuple[int, ...]]]\n    existing_huddles = set() # type: Set[Text]\n    for huddle in Huddle.objects.all():\n        existing_huddles.add(huddle.huddle_hash)\n    for huddle_users in huddle_user_list:\n        user_ids = [users[email].id for email in huddle_users] # type: List[int]\n        huddle_hash = get_huddle_hash(user_ids)\n        if huddle_hash in existing_huddles:\n            continue\n        huddle_set.add((huddle_hash, tuple(sorted(user_ids))))\n\n    huddles_to_create = [] # type: List[Huddle]\n    for (huddle_hash, _) in huddle_set:\n        huddles_to_create.append(Huddle(huddle_hash=huddle_hash))\n    Huddle.objects.bulk_create(huddles_to_create)\n\n    for huddle in Huddle.objects.all():\n        huddles[huddle.huddle_hash] = huddle\n        huddles_by_id[huddle.id] = huddle\n\n    recipients_to_create = [] # type: List[Recipient]\n    for (huddle_hash, _) in huddle_set:\n        recipients_to_create.append(Recipient(type_id=huddles[huddle_hash].id, type=Recipient.HUDDLE))\n    Recipient.objects.bulk_create(recipients_to_create)\n\n    huddle_recipients = {} # type: Dict[Text, Recipient]\n    for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):\n        huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient\n\n    subscriptions_to_create = [] # type: List[Subscription]\n    for (huddle_hash, huddle_user_ids) in huddle_set:\n        for user_id in huddle_user_ids:\n            subscriptions_to_create.append(Subscription(active=True, user_profile_id=user_id,\n                                                        recipient=huddle_recipients[huddle_hash]))\n    Subscription.objects.bulk_create(subscriptions_to_create)\n", "path": "zerver/lib/bulk_create.py"}], "after_files": [{"content": "from __future__ import absolute_import\n\nfrom django.contrib.auth.models import UserManager\nfrom django.utils import timezone\nfrom zerver.models import UserProfile, Recipient, Subscription, Realm, Stream\nimport base64\nimport ujson\nimport os\nimport string\nfrom six.moves import range\n\nfrom typing import Optional, Text\n\ndef random_api_key():\n    # type: () -> Text\n    choices = string.ascii_letters + string.digits\n    altchars = ''.join([choices[ord(os.urandom(1)) % 62] for _ in range(2)]).encode(\"utf-8\")\n    return base64.b64encode(os.urandom(24), altchars=altchars).decode(\"utf-8\")\n\n# create_user_profile is based on Django's User.objects.create_user,\n# except that we don't save to the database so it can used in\n# bulk_creates\n#\n# Only use this for bulk_create -- for normal usage one should use\n# create_user (below) which will also make the Subscription and\n# Recipient objects\ndef create_user_profile(realm, email, password, active, bot_type, full_name,\n                        short_name, bot_owner, is_mirror_dummy, tos_version,\n                        tutorial_status=UserProfile.TUTORIAL_WAITING,\n                        enter_sends=False):\n    # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text], bool) -> UserProfile\n    now = timezone.now()\n    email = UserManager.normalize_email(email)\n\n    user_profile = UserProfile(email=email, is_staff=False, is_active=active,\n                               full_name=full_name, short_name=short_name,\n                               last_login=now, date_joined=now, realm=realm,\n                               pointer=-1, is_bot=bool(bot_type), bot_type=bot_type,\n                               bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy,\n                               tos_version=tos_version,\n                               tutorial_status=tutorial_status,\n                               enter_sends=enter_sends,\n                               onboarding_steps=ujson.dumps([]),\n                               default_language=realm.default_language)\n\n    if bot_type or not active:\n        password = None\n\n    user_profile.set_password(password)\n\n    user_profile.api_key = random_api_key()\n    return user_profile\n\ndef create_user(email, password, realm, full_name, short_name,\n                active=True, bot_type=None, bot_owner=None, tos_version=None,\n                avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,\n                is_mirror_dummy=False, default_sending_stream=None,\n                default_events_register_stream=None,\n                default_all_public_streams=None, user_profile_id=None):\n    # type: (Text, Text, Realm, Text, Text, bool, Optional[int], Optional[UserProfile], Optional[Text], Text, bool, Optional[Stream], Optional[Stream], Optional[bool], Optional[int]) -> UserProfile\n    user_profile = create_user_profile(realm, email, password, active, bot_type,\n                                       full_name, short_name, bot_owner,\n                                       is_mirror_dummy, tos_version)\n    user_profile.avatar_source = avatar_source\n    user_profile.default_sending_stream = default_sending_stream\n    user_profile.default_events_register_stream = default_events_register_stream\n    # Allow the ORM default to be used if not provided\n    if default_all_public_streams is not None:\n        user_profile.default_all_public_streams = default_all_public_streams\n\n    if user_profile_id is not None:\n        user_profile.id = user_profile_id\n\n    user_profile.save()\n    recipient = Recipient.objects.create(type_id=user_profile.id,\n                                         type=Recipient.PERSONAL)\n    Subscription.objects.create(user_profile=user_profile, recipient=recipient)\n    return user_profile\n", "path": "zerver/lib/create_user.py"}, {"content": "from __future__ import absolute_import\nfrom typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple, Text\n\nfrom zerver.lib.initial_password import initial_password\nfrom zerver.models import Realm, Stream, UserProfile, Huddle, \\\n    Subscription, Recipient, Client, RealmAuditLog, get_huddle_hash\nfrom zerver.lib.create_user import create_user_profile\n\ndef bulk_create_users(realm, users_raw, bot_type=None, tos_version=None):\n    # type: (Realm, Set[Tuple[Text, Text, Text, bool]], Optional[int], Optional[Text]) -> None\n    \"\"\"\n    Creates and saves a UserProfile with the given email.\n    Has some code based off of UserManage.create_user, but doesn't .save()\n    \"\"\"\n    existing_users = frozenset(UserProfile.objects.values_list('email', flat=True))\n    users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users])\n\n    # Now create user_profiles\n    profiles_to_create = [] # type: List[UserProfile]\n    for (email, full_name, short_name, active) in users:\n        profile = create_user_profile(realm, email,\n                                      initial_password(email), active, bot_type,\n                                      full_name, short_name, None, False, tos_version,\n                                      tutorial_status=UserProfile.TUTORIAL_FINISHED,\n                                      enter_sends=True)\n        profiles_to_create.append(profile)\n    UserProfile.objects.bulk_create(profiles_to_create)\n\n    RealmAuditLog.objects.bulk_create(\n        [RealmAuditLog(realm=profile_.realm, modified_user=profile_,\n                       event_type='user_created', event_time=profile_.date_joined)\n         for profile_ in profiles_to_create])\n\n    profiles_by_email = {} # type: Dict[Text, UserProfile]\n    profiles_by_id = {} # type: Dict[int, UserProfile]\n    for profile in UserProfile.objects.select_related().all():\n        profiles_by_email[profile.email] = profile\n        profiles_by_id[profile.id] = profile\n\n    recipients_to_create = [] # type: List[Recipient]\n    for (email, full_name, short_name, active) in users:\n        recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id,\n                                              type=Recipient.PERSONAL))\n    Recipient.objects.bulk_create(recipients_to_create)\n\n    recipients_by_email = {} # type: Dict[Text, Recipient]\n    for recipient in Recipient.objects.filter(type=Recipient.PERSONAL):\n        recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient\n\n    subscriptions_to_create = [] # type: List[Subscription]\n    for (email, full_name, short_name, active) in users:\n        subscriptions_to_create.append(\n            Subscription(user_profile_id=profiles_by_email[email].id,\n                         recipient=recipients_by_email[email]))\n    Subscription.objects.bulk_create(subscriptions_to_create)\n\ndef bulk_create_streams(realm, stream_dict):\n    # type: (Realm, Dict[Text, Dict[Text, Any]]) -> None\n    existing_streams = frozenset([name.lower() for name in\n                                  Stream.objects.filter(realm=realm)\n                                  .values_list('name', flat=True)])\n    streams_to_create = [] # type: List[Stream]\n    for name, options in stream_dict.items():\n        if name.lower() not in existing_streams:\n            streams_to_create.append(\n                Stream(\n                    realm=realm, name=name, description=options[\"description\"],\n                    invite_only=options[\"invite_only\"]\n                )\n            )\n    Stream.objects.bulk_create(streams_to_create)\n\n    recipients_to_create = [] # type: List[Recipient]\n    for stream in Stream.objects.filter(realm=realm).values('id', 'name'):\n        if stream['name'].lower() not in existing_streams:\n            recipients_to_create.append(Recipient(type_id=stream['id'],\n                                                  type=Recipient.STREAM))\n    Recipient.objects.bulk_create(recipients_to_create)\n\ndef bulk_create_clients(client_list):\n    # type: (Iterable[Text]) -> None\n    existing_clients = set(client.name for client in Client.objects.select_related().all()) # type: Set[Text]\n\n    clients_to_create = [] # type: List[Client]\n    for name in client_list:\n        if name not in existing_clients:\n            clients_to_create.append(Client(name=name))\n            existing_clients.add(name)\n    Client.objects.bulk_create(clients_to_create)\n\ndef bulk_create_huddles(users, huddle_user_list):\n    # type: (Dict[Text, UserProfile], Iterable[Iterable[Text]]) -> None\n    huddles = {} # type: Dict[Text, Huddle]\n    huddles_by_id = {} # type: Dict[int, Huddle]\n    huddle_set = set() # type: Set[Tuple[Text, Tuple[int, ...]]]\n    existing_huddles = set() # type: Set[Text]\n    for huddle in Huddle.objects.all():\n        existing_huddles.add(huddle.huddle_hash)\n    for huddle_users in huddle_user_list:\n        user_ids = [users[email].id for email in huddle_users] # type: List[int]\n        huddle_hash = get_huddle_hash(user_ids)\n        if huddle_hash in existing_huddles:\n            continue\n        huddle_set.add((huddle_hash, tuple(sorted(user_ids))))\n\n    huddles_to_create = [] # type: List[Huddle]\n    for (huddle_hash, _) in huddle_set:\n        huddles_to_create.append(Huddle(huddle_hash=huddle_hash))\n    Huddle.objects.bulk_create(huddles_to_create)\n\n    for huddle in Huddle.objects.all():\n        huddles[huddle.huddle_hash] = huddle\n        huddles_by_id[huddle.id] = huddle\n\n    recipients_to_create = [] # type: List[Recipient]\n    for (huddle_hash, _) in huddle_set:\n        recipients_to_create.append(Recipient(type_id=huddles[huddle_hash].id, type=Recipient.HUDDLE))\n    Recipient.objects.bulk_create(recipients_to_create)\n\n    huddle_recipients = {} # type: Dict[Text, Recipient]\n    for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):\n        huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient\n\n    subscriptions_to_create = [] # type: List[Subscription]\n    for (huddle_hash, huddle_user_ids) in huddle_set:\n        for user_id in huddle_user_ids:\n            subscriptions_to_create.append(Subscription(active=True, user_profile_id=user_id,\n                                                        recipient=huddle_recipients[huddle_hash]))\n    Subscription.objects.bulk_create(subscriptions_to_create)\n", "path": "zerver/lib/bulk_create.py"}]}