comments.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from typing import List, Literal
  2. from django.http import HttpResponse
  3. from ninja import Schema
  4. from ninja.errors import HttpError
  5. from ninja.pagination import paginate
  6. from glitchtip.api.authentication import AuthHttpRequest
  7. from glitchtip.api.permissions import has_permission
  8. from ..models import Comment, Issue
  9. from ..schema import CommentSchema
  10. from . import router
  11. def get_queryset(request: AuthHttpRequest, issue_id: int):
  12. user_id = request.auth.user_id
  13. return Comment.objects.select_related("user").filter(
  14. issue__project__organization__users=user_id, issue__id=issue_id
  15. )
  16. @router.get(
  17. "/issues/{int:issue_id}/comments/",
  18. response=List[CommentSchema],
  19. by_alias=True,
  20. )
  21. @has_permission(["event:read", "event:admin"])
  22. @paginate
  23. async def list_comments(request: AuthHttpRequest, response: HttpResponse, issue_id: int):
  24. return get_queryset(request, issue_id)
  25. class PostCommentSchema(Schema):
  26. data: dict[Literal["text"], str]
  27. @router.post(
  28. "/issues/{int:issue_id}/comments/",
  29. response={201: CommentSchema},
  30. by_alias=True,
  31. )
  32. @has_permission(["event:write", "event:admin"])
  33. async def add_comment(
  34. request: AuthHttpRequest,
  35. issue_id: int,
  36. payload: PostCommentSchema,
  37. ):
  38. try:
  39. issue = await Issue.objects.aget(
  40. id=issue_id, project__organization__users=request.auth.user_id
  41. )
  42. except Issue.DoesNotExist:
  43. raise HttpError(400, "Issue does not exist")
  44. user_id = request.auth.user_id
  45. comment = await Comment.objects.acreate(
  46. text=payload.data["text"],
  47. issue=issue,
  48. user_id=user_id,
  49. )
  50. return 201, await Comment.objects.select_related("user").aget(id=comment.id)
  51. @router.put(
  52. "/issues/{int:issue_id}/comments/{int:comment_id}/",
  53. response=CommentSchema,
  54. by_alias=True,
  55. )
  56. @has_permission(["event:write", "event:admin"])
  57. async def update_comment(
  58. request: AuthHttpRequest,
  59. issue_id: int,
  60. comment_id: int,
  61. payload: PostCommentSchema,
  62. ):
  63. try:
  64. comment = await get_queryset(request, issue_id).aget(id=comment_id)
  65. except Comment.DoesNotExist:
  66. raise HttpError(400, "Comment does not exist")
  67. comment.text = payload.data["text"]
  68. await comment.asave()
  69. return comment
  70. @router.delete(
  71. "/issues/{int:issue_id}/comments/{int:comment_id}/",
  72. response={204: None},
  73. by_alias=True,
  74. )
  75. @has_permission(["event:admin"])
  76. async def delete_comment(
  77. request: AuthHttpRequest,
  78. issue_id: int,
  79. comment_id: int,
  80. ):
  81. try:
  82. comment = await get_queryset(request, issue_id).aget(id=comment_id)
  83. except Comment.DoesNotExist:
  84. raise HttpError(400, "Comment does not exist")
  85. await comment.adelete()
  86. return 204, None