2021-07-24 06:54:31 +02:00
|
|
|
from typing import Any, Dict, List
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2021-07-24 06:54:31 +02:00
|
|
|
from zerver.lib.drafts import (
|
|
|
|
do_create_drafts,
|
|
|
|
do_delete_draft,
|
|
|
|
do_edit_draft,
|
|
|
|
draft_dict_validator,
|
|
|
|
draft_endpoint,
|
|
|
|
)
|
2020-07-21 18:04:50 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-07-04 08:52:23 +02:00
|
|
|
from zerver.lib.response import json_success
|
2021-07-24 06:54:31 +02:00
|
|
|
from zerver.lib.validator import check_list
|
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
|
2020-07-21 18:04:50 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def create_drafts(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-04-07 22:00:44 +02:00
|
|
|
draft_dicts: List[Dict[str, Any]] = REQ(
|
|
|
|
"drafts", json_validator=check_list(draft_dict_validator)
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-07-24 06:54:31 +02:00
|
|
|
created_draft_objects = do_create_drafts(draft_dicts, 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
|
2020-07-23 14:48:44 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def edit_draft(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
draft_id: int,
|
2021-04-07 22:00:44 +02:00
|
|
|
draft_dict: Dict[str, Any] = REQ("draft", json_validator=draft_dict_validator),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-07-24 06:54:31 +02:00
|
|
|
do_edit_draft(draft_id, draft_dict, 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
|
2020-07-23 15:09:58 +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)
|