2016-10-24 18:32:09 +02:00
|
|
|
import os
|
2017-11-02 06:58:36 +01:00
|
|
|
from typing import Any, Iterator
|
2016-10-24 18:32:09 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2016-10-24 18:32:09 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
|
|
|
from django.db.models import QuerySet
|
|
|
|
|
|
|
|
from zerver.lib.message import render_markdown
|
|
|
|
from zerver.models import Message
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def queryset_iterator(queryset: QuerySet, chunksize: int=5000) -> Iterator[Any]:
|
2016-10-24 18:32:09 +02:00
|
|
|
queryset = queryset.order_by('id')
|
|
|
|
while queryset.exists():
|
|
|
|
for row in queryset[:chunksize]:
|
|
|
|
msg_id = row.id
|
|
|
|
yield row
|
|
|
|
queryset = queryset.filter(id__gt=msg_id)
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """
|
|
|
|
Render messages to a file.
|
2016-11-22 01:44:16 +01:00
|
|
|
Usage: ./manage.py render_messages <destination> [--amount=10000]
|
2016-10-24 18:32:09 +02:00
|
|
|
"""
|
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2016-10-24 18:32:09 +02:00
|
|
|
parser.add_argument('destination', help='Destination file path')
|
|
|
|
parser.add_argument('--amount', default=100000, help='Number of messages to render')
|
|
|
|
parser.add_argument('--latest_id', default=0, help="Last message id to render")
|
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2016-10-24 18:32:09 +02:00
|
|
|
dest_dir = os.path.realpath(os.path.dirname(options['destination']))
|
|
|
|
amount = int(options['amount'])
|
|
|
|
latest = int(options['latest_id']) or Message.objects.latest('id').id
|
2020-06-09 00:25:09 +02:00
|
|
|
self.stdout.write(f'Latest message id: {latest}')
|
2016-10-24 18:32:09 +02:00
|
|
|
if not os.path.exists(dest_dir):
|
|
|
|
os.makedirs(dest_dir)
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(options['destination'], 'wb') as result:
|
|
|
|
result.write(b'[')
|
2016-10-24 18:32:09 +02:00
|
|
|
messages = Message.objects.filter(id__gt=latest - amount, id__lte=latest).order_by('id')
|
|
|
|
for message in queryset_iterator(messages):
|
2016-11-14 10:06:58 +01:00
|
|
|
content = message.content
|
|
|
|
# In order to ensure that the output of this tool is
|
|
|
|
# consistent across the time, even if messages are
|
|
|
|
# edited, we always render the original content
|
|
|
|
# version, extracting it from the edit history if
|
|
|
|
# necessary.
|
|
|
|
if message.edit_history:
|
2020-08-07 01:09:47 +02:00
|
|
|
history = orjson.loads(message.edit_history)
|
2016-11-14 10:06:58 +01:00
|
|
|
history = sorted(history, key=lambda i: i['timestamp'])
|
2017-02-19 07:33:26 +01:00
|
|
|
for entry in history:
|
|
|
|
if 'prev_content' in entry:
|
|
|
|
content = entry['prev_content']
|
|
|
|
break
|
2020-08-07 01:09:47 +02:00
|
|
|
result.write(orjson.dumps({
|
2016-10-24 18:32:09 +02:00
|
|
|
'id': message.id,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
'content': render_markdown(message, content),
|
2016-10-24 18:32:09 +02:00
|
|
|
}))
|
|
|
|
if message.id != latest:
|
2020-08-07 01:09:47 +02:00
|
|
|
result.write(b',')
|
|
|
|
result.write(b']')
|