Skip to content

Commit cd587c5

Browse files
committed
✨(backend) add dummy content to demo documents
We need to content in our demo documents so that we can test indexing.
1 parent 373493b commit cd587c5

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

src/backend/core/models.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -439,32 +439,35 @@ def __init__(self, *args, **kwargs):
439439
def save(self, *args, **kwargs):
440440
"""Write content to object storage only if _content has changed."""
441441
super().save(*args, **kwargs)
442-
443442
if self._content:
444-
file_key = self.file_key
445-
bytes_content = self._content.encode("utf-8")
443+
self.save_content(self._content)
446444

447-
# Attempt to directly check if the object exists using the storage client.
448-
try:
449-
response = default_storage.connection.meta.client.head_object(
450-
Bucket=default_storage.bucket_name, Key=file_key
451-
)
452-
except ClientError as excpt:
453-
# If the error is a 404, the object doesn't exist, so we should create it.
454-
if excpt.response["Error"]["Code"] == "404":
455-
has_changed = True
456-
else:
457-
raise
445+
def save_content(self, content):
446+
"""Save content to object storage."""
447+
448+
file_key = self.file_key
449+
bytes_content = content.encode("utf-8")
450+
451+
# Attempt to directly check if the object exists using the storage client.
452+
try:
453+
response = default_storage.connection.meta.client.head_object(
454+
Bucket=default_storage.bucket_name, Key=file_key
455+
)
456+
except ClientError as excpt:
457+
# If the error is a 404, the object doesn't exist, so we should create it.
458+
if excpt.response["Error"]["Code"] == "404":
459+
has_changed = True
458460
else:
459-
# Compare the existing ETag with the MD5 hash of the new content.
460-
has_changed = (
461-
response["ETag"].strip('"')
462-
!= hashlib.md5(bytes_content).hexdigest() # noqa: S324
463-
)
461+
raise
462+
else:
463+
# Compare the existing ETag with the MD5 hash of the new content.
464+
has_changed = (
465+
response["ETag"].strip('"') != hashlib.md5(bytes_content).hexdigest() # noqa: S324
466+
)
464467

465-
if has_changed:
466-
content_file = ContentFile(bytes_content)
467-
default_storage.save(file_key, content_file)
468+
if has_changed:
469+
content_file = ContentFile(bytes_content)
470+
default_storage.save(file_key, content_file)
468471

469472
def is_leaf(self):
470473
"""

src/backend/demo/management/commands/create_demo.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# ruff: noqa: S311, S106
22
"""create_demo management command"""
33

4+
import base64
45
import logging
56
import math
67
import random
78
import time
89
from collections import defaultdict
10+
from uuid import uuid4
911

1012
from django import db
1113
from django.conf import settings
1214
from django.core.management.base import BaseCommand, CommandError
1315

16+
import pycrdt
1417
from faker import Faker
1518

1619
from core import models
@@ -27,6 +30,16 @@ def random_true_with_probability(probability):
2730
return random.random() < probability
2831

2932

33+
def get_ydoc_for_text(text):
34+
"""Return a ydoc from plain text for demo purposes."""
35+
ydoc = pycrdt.Doc()
36+
paragraph = pycrdt.XmlElement("p", {}, [pycrdt.XmlText(text)])
37+
fragment = pycrdt.XmlFragment([paragraph])
38+
ydoc["document-store"] = fragment
39+
update = ydoc.get_update()
40+
return base64.b64encode(update).decode("utf-8")
41+
42+
3043
class BulkQueue:
3144
"""A utility class to create Django model instances in bulk by just pushing to a queue."""
3245

@@ -48,7 +61,7 @@ def _bulk_create(self, objects):
4861
self.queue[objects[0]._meta.model.__name__] = [] # noqa: SLF001
4962

5063
def push(self, obj):
51-
"""Add a model instance to queue to that it gets created in bulk."""
64+
"""Add a model instance to queue so that it gets created in bulk."""
5265
objects = self.queue[obj._meta.model.__name__] # noqa: SLF001
5366
objects.append(obj)
5467
if len(objects) > self.BATCH_SIZE:
@@ -139,17 +152,19 @@ def create_demo(stdout):
139152
# pylint: disable=protected-access
140153
key = models.Document._int2str(i) # noqa: SLF001
141154
padding = models.Document.alphabet[0] * (models.Document.steplen - len(key))
142-
queue.push(
143-
models.Document(
144-
depth=1,
145-
path=f"{padding}{key}",
146-
creator_id=random.choice(users_ids),
147-
title=fake.sentence(nb_words=4),
148-
link_reach=models.LinkReachChoices.AUTHENTICATED
149-
if random_true_with_probability(0.5)
150-
else random.choice(models.LinkReachChoices.values),
151-
)
155+
title = fake.sentence(nb_words=4)
156+
document = models.Document(
157+
id=uuid4(),
158+
depth=1,
159+
path=f"{padding}{key}",
160+
creator_id=random.choice(users_ids),
161+
title=title,
162+
link_reach=models.LinkReachChoices.AUTHENTICATED
163+
if random_true_with_probability(0.5)
164+
else random.choice(models.LinkReachChoices.values),
152165
)
166+
document.save_content(get_ydoc_for_text(f"Content for {title:s}"))
167+
queue.push(document)
153168

154169
queue.flush()
155170

0 commit comments

Comments
 (0)