api.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from typing import Literal
  2. from django.http import HttpResponse
  3. from django.shortcuts import aget_object_or_404
  4. from ninja import Router
  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 Environment, EnvironmentProject
  9. from .schema import EnvironmentProjectIn, EnvironmentProjectSchema, EnvironmentSchema
  10. router = Router()
  11. """
  12. GET /api/0/organizations/{organization_slug}/environments/
  13. GET /api/0/projects/{organization_slug}/{project_slug}/environments/ (Not documented)
  14. PUT /api/0/projects/{organization_slug}/{project_slug}/environments/{slug}/ (Not documented)
  15. """
  16. Visibility = Literal["all", "hidden", "visible"]
  17. @router.get(
  18. "organizations/{slug:organization_slug}/environments/",
  19. response=list[EnvironmentSchema],
  20. )
  21. @paginate
  22. @has_permission(["org:read", "org:write", "org:admin"])
  23. async def list_environments(
  24. request: AuthHttpRequest,
  25. response: HttpResponse,
  26. organization_slug: str,
  27. visibility: Visibility = "visible",
  28. ):
  29. qs = Environment.objects.filter(
  30. organization__users=request.auth.user_id,
  31. organization__slug=organization_slug,
  32. )
  33. if visibility == "hidden":
  34. qs = qs.filter(environmentproject__is_hidden=True)
  35. if visibility == "visible":
  36. qs = qs.filter(environmentproject__is_hidden=False)
  37. return qs
  38. @router.get(
  39. "projects/{slug:organization_slug}/{slug:project_slug}/environments/",
  40. response=list[EnvironmentProjectSchema],
  41. by_alias=True,
  42. )
  43. @paginate
  44. @has_permission(["project:read", "project:write", "project:admin"])
  45. async def list_environment_projects(
  46. request: AuthHttpRequest,
  47. response: HttpResponse,
  48. organization_slug: str,
  49. project_slug: str,
  50. visibility: Visibility = "visible",
  51. ):
  52. qs = EnvironmentProject.objects.filter(
  53. environment__organization__users=request.auth.user_id,
  54. environment__organization__slug=organization_slug,
  55. project__slug=project_slug,
  56. )
  57. if visibility == "hidden":
  58. qs = qs.filter(is_hidden=True)
  59. if visibility == "visible":
  60. qs = qs.filter(is_hidden=False)
  61. return qs.select_related("environment")
  62. @router.put(
  63. "projects/{slug:organization_slug}/{slug:project_slug}/environments/{str:name}/",
  64. response=EnvironmentProjectSchema,
  65. by_alias=True,
  66. )
  67. @has_permission(["project:write", "project:admin"])
  68. async def update_environment_project(
  69. request: AuthHttpRequest,
  70. organization_slug: str,
  71. project_slug: str,
  72. name: str,
  73. payload: EnvironmentProjectIn,
  74. ):
  75. environment = await aget_object_or_404(
  76. EnvironmentProject,
  77. environment__organization__users=request.auth.user_id,
  78. environment__organization__slug=organization_slug,
  79. project__slug=project_slug,
  80. environment__name=name,
  81. )
  82. for attr, value in payload.dict().items():
  83. setattr(environment, attr, value)
  84. await environment.asave()
  85. return await EnvironmentProject.objects.select_related("environment").aget(
  86. id=environment.id
  87. )