Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
populate_db: Add attachments in messages.
Dhruv Shettyfdf7a75a6ce9
diff --git a/tools/rebuild-test-database b/tools/rebuild-test-databaseindex a4457516018..c09dbe04d60 100755--- a/tools/rebuild-test-database+++ b/tools/rebuild-test-database@@ -33,7 +33,8 @@ create_zulip_test# This next line can be simplified to "-n0" once we fix our app (and tests) with 0 messages../manage.py populate_db --test-suite -n30 --threads=1 \--max-topics=3 \- --direct-message-groups=0 --personals=0 --percent-direct-message-groups=0 --percent-personals=0+ --direct-message-groups=0 --personals=0 --percent-direct-message-groups=0 --percent-personals=0 \+ --percent-attachments=0./manage.py dumpdata \zerver.UserProfile zerver.Stream zerver.Recipient \diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.pyindex d23eddf659b..642b17a37d8 100644--- a/zilencer/management/commands/populate_db.py+++ b/zilencer/management/commands/populate_db.py@@ -1,3 +1,4 @@+import ioimport itertoolsimport osimport random@@ -11,6 +12,7 @@ import orjsonfrom django.conf import settingsfrom django.contrib.sessions.models import Sessionfrom django.core.files.base import File+from django.core.files.uploadedfile import InMemoryUploadedFilefrom django.core.management import call_commandfrom django.core.management.base import CommandParserfrom django.core.validators import validate_email@@ -53,6 +55,7 @@ from zerver.lib.storage import static_pathfrom zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORSfrom zerver.lib.stream_subscription import bulk_create_stream_subscriptionsfrom zerver.lib.types import AnalyticsDataUploadLevel, ProfileFieldData+from zerver.lib.upload import upload_message_attachment_from_requestfrom zerver.lib.users import add_servicefrom zerver.lib.utils import generate_api_keyfrom zerver.models import (@@ -197,6 +200,31 @@ def subscribe_users_to_streams(realm: Realm, stream_dict: dict[str, dict[str, AnRealmAuditLog.objects.bulk_create(all_subscription_logs)+def create_attachment(user: UserProfile) -> tuple[str, str]:+ if random.random() < 0.5:+ filename = "attachment.txt"+ content_type = "text/plain"+ content = b"temporary test attachment file. Hello, World!"+ else:+ filename = "README.md"+ content_type = "text/markdown"+ content = (+ b"# Project README\n\nThis is a **sample** markdown file.\n\n"+ b"## Getting started\n\n- Step 1\n- Step 2\n- Step 3\n"+ )+ file_obj = io.BytesIO(content)+ upload = InMemoryUploadedFile(+ file=file_obj,+ field_name=None,+ name=filename,+ content_type=content_type,+ size=len(content),+ charset=None,+ )+ locator, _ = upload_message_attachment_from_request(upload, user)+ return locator, filename++def create_alert_words(realm_id: int) -> None:user_ids = UserProfile.objects.filter(realm_id=realm_id,@@ -308,6 +336,13 @@ class Command(ZulipBaseCommand):help="The percent of topics with links in them.",)+ parser.add_argument(+ "--percent-attachments",+ type=float,+ default=3,+ help="The percent of messages to have attachments.",+ )+parser.add_argument("--stickiness",type=float,@@ -1333,6 +1368,10 @@ def generate_and_send_messages(recipients: dict[int, tuple[str, int, dict[str, Any]]] = {}messages: list[Message] = []while num_messages < tot_messages:+ has_attachment = (+ options["percent_attachments"] > 0+ and random.random() < options["percent_attachments"] / 100.0+ )saved_data: dict[str, Any] = {}message = Message(realm=realm)message.sending_client = get_client("ZulipDataImport")@@ -1389,6 +1428,10 @@ def generate_and_send_messages(message.subject = random.choice(possible_topic_names[message.recipient.id])saved_data["subject"] = message.subject+ if has_attachment:+ locator, filename = create_attachment(message.sender)+ message.content += f"\n[{filename}]({locator})"+message.is_channel_message = msg_type == MSG_TYPE_STREAMmessage.date_sent = choose_date_sent(num_messages, tot_messages, options["oldest_message_days"], options["threads"]