2023-08-17 02:34:42 +02:00
|
|
|
from typing import List
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2023-08-17 02:34:42 +02:00
|
|
|
from pydantic import Json
|
2020-07-21 18:04:50 +02:00
|
|
|
|
2021-07-24 06:54:31 +02:00
|
|
|
from zerver.lib.drafts import (
|
2023-08-17 02:34:42 +02:00
|
|
|
DraftData,
|
2021-07-24 06:54:31 +02:00
|
|
|
do_create_drafts,
|
|
|
|
do_delete_draft,
|
|
|
|
do_edit_draft,
|
|
|
|
draft_endpoint,
|
|
|
|
)
|
2021-07-04 08:52:23 +02:00
|
|
|
from zerver.lib.response import json_success
|
2023-08-17 02:34:42 +02:00
|
|
|
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
|
2020-07-21 18:04:50 +02:00
|
|
|
from zerver.models import Draft, UserProfile
|
|
|
|
|
2020-08-02 20:35:11 +02:00
|
|
|
|
|
|
|
@draft_endpoint
|
2020-07-23 21:23:17 +02:00
|
|
|
def fetch_drafts(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
|
|
|
user_drafts = Draft.objects.filter(user_profile=user_profile).order_by("last_edit_time")
|
2020-08-06 08:10:35 +02:00
|
|
|
draft_dicts = [draft.to_dict() for draft in user_drafts]
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"count": user_drafts.count(), "drafts": draft_dicts})
|
2020-07-23 21:23:17 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-08-02 20:35:11 +02:00
|
|
|
@draft_endpoint
|
2023-08-17 02:34:42 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def create_drafts(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-17 02:34:42 +02:00
|
|
|
*,
|
|
|
|
drafts: Json[List[DraftData]],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2023-08-17 02:34:42 +02:00
|
|
|
created_draft_objects = do_create_drafts(drafts, user_profile)
|
2020-07-21 18:04:50 +02:00
|
|
|
draft_ids = [draft_object.id for draft_object in created_draft_objects]
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"ids": draft_ids})
|
2020-07-23 14:48:44 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-08-02 20:35:11 +02:00
|
|
|
@draft_endpoint
|
2023-08-17 02:34:42 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def edit_draft(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-17 02:34:42 +02:00
|
|
|
*,
|
|
|
|
draft_id: PathOnly[int],
|
|
|
|
draft: Json[DraftData],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2023-08-17 02:34:42 +02:00
|
|
|
do_edit_draft(draft_id, draft, user_profile)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-07-23 15:09:58 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-08-02 20:35:11 +02:00
|
|
|
@draft_endpoint
|
2023-08-17 02:34:42 +02:00
|
|
|
def delete_draft(request: HttpRequest, user_profile: UserProfile, *, draft_id: int) -> HttpResponse:
|
2021-07-24 06:54:31 +02:00
|
|
|
do_delete_draft(draft_id, user_profile)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|