Exemplo n.º 1
0
    def handle(self, *args: Any, **options: Any) -> None:
        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
        self.stdout.write(f"Latest message id: {latest}")
        if not os.path.exists(dest_dir):
            os.makedirs(dest_dir)

        with open(options["destination"], "wb") as result:
            result.write(b"[")
            messages = Message.objects.filter(id__gt=latest - amount,
                                              id__lte=latest).order_by("id")
            for message in queryset_iterator(messages):
                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:
                    history = orjson.loads(message.edit_history)
                    history = sorted(history, key=lambda i: i["timestamp"])
                    for entry in history:
                        if "prev_content" in entry:
                            content = entry["prev_content"]
                            break
                result.write(
                    orjson.dumps({
                        "id": message.id,
                        "content": render_markdown(message, content),
                    }))
                if message.id != latest:
                    result.write(b",")
            result.write(b"]")
Exemplo n.º 2
0
    def handle(self, *args: Any, **options: Any) -> None:
        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
        self.stdout.write(f'Latest message id: {latest}')
        if not os.path.exists(dest_dir):
            os.makedirs(dest_dir)

        with open(options['destination'], 'wb') as result:
            result.write(b'[')
            messages = Message.objects.filter(id__gt=latest - amount,
                                              id__lte=latest).order_by('id')
            for message in queryset_iterator(messages):
                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:
                    history = orjson.loads(message.edit_history)
                    history = sorted(history, key=lambda i: i['timestamp'])
                    for entry in history:
                        if 'prev_content' in entry:
                            content = entry['prev_content']
                            break
                result.write(
                    orjson.dumps({
                        'id': message.id,
                        'content': render_markdown(message, content),
                    }))
                if message.id != latest:
                    result.write(b',')
            result.write(b']')
Exemplo n.º 3
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        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
        self.stdout.write('Latest message id: {latest}'.format(latest=latest))
        if not os.path.exists(dest_dir):
            os.makedirs(dest_dir)

        with open(options['destination'], 'w') as result:
            result.write('[')
            messages = Message.objects.filter(id__gt=latest - amount, id__lte=latest).order_by('id')
            for message in queryset_iterator(messages):
                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:
                    history = ujson.loads(message.edit_history)
                    history = sorted(history, key=lambda i: i['timestamp'])
                    for entry in history:
                        if 'prev_content' in entry:
                            content = entry['prev_content']
                            break
                result.write(ujson.dumps({
                    'id': message.id,
                    'content': render_markdown(message, content)
                }))
                if message.id != latest:
                    result.write(',')
            result.write(']')
Exemplo n.º 4
0
def render_message_backend(request, user_profile, content=REQ()):
    # type: (HttpRequest, UserProfile, Text) -> HttpResponse
    message = Message()
    message.sender = user_profile
    message.content = content
    message.sending_client = request.client

    rendered_content = render_markdown(message, content, realm=user_profile.realm)
    return json_success({"rendered": rendered_content})
Exemplo n.º 5
0
def render_message_backend(
    request: HttpRequest, user_profile: UserProfile,
    content: str = REQ()) -> HttpResponse:
    message = Message()
    message.sender = user_profile
    message.content = content
    message.sending_client = request.client

    rendering_result = render_markdown(message,
                                       content,
                                       realm=user_profile.realm)
    return json_success({"rendered": rendering_result.rendered_content})
Exemplo n.º 6
0
def render_message_backend(
    request: HttpRequest, user_profile: UserProfile, content: str = REQ()
) -> HttpResponse:
    message = Message()
    message.sender = user_profile
    message.content = content
    client = RequestNotes.get_notes(request).client
    assert client is not None
    message.sending_client = client

    rendering_result = render_markdown(message, content, realm=user_profile.realm)
    return json_success(request, data={"rendered": rendering_result.rendered_content})