|
| 1 | +# Copyright (c) 2026 LINE Corporation |
| 2 | +# These sources are released under the terms of the MIT license: see LICENSE |
| 3 | + |
| 4 | +from django.contrib.contenttypes.models import ContentType |
| 5 | +from django.db.models import Q |
| 6 | +from django.urls import re_path |
| 7 | +from drf_spectacular.utils import extend_schema, extend_schema_view |
| 8 | +from drf_spectacular.views import SpectacularAPIView |
| 9 | +from rest_framework import mixins, pagination, routers, viewsets |
| 10 | +from rest_framework.authtoken.models import Token |
| 11 | +from rest_framework.renderers import TemplateHTMLRenderer |
| 12 | +from rest_framework.response import Response |
| 13 | +from rest_framework.views import APIView |
| 14 | + |
| 15 | +from promgen import filters, models, permissions, serializers |
| 16 | + |
| 17 | + |
| 18 | +class SpectacularRapiDocView(APIView): |
| 19 | + renderer_classes = [TemplateHTMLRenderer] |
| 20 | + template_name = "rest_framework/api_v2.html" |
| 21 | + |
| 22 | + @extend_schema(exclude=True) |
| 23 | + def get(self, request): |
| 24 | + api_token = Token.objects.filter(user=self.request.user).first() |
| 25 | + return Response( |
| 26 | + data={"api_token": api_token}, |
| 27 | + template_name=self.template_name, |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +class Router(routers.DefaultRouter): |
| 32 | + include_root_view = False |
| 33 | + |
| 34 | + def get_urls(self): |
| 35 | + urls = super().get_urls() |
| 36 | + |
| 37 | + urls.append( |
| 38 | + re_path( |
| 39 | + rf"^schema{self.trailing_slash}$", |
| 40 | + SpectacularAPIView.as_view(), |
| 41 | + name="schema", |
| 42 | + ) |
| 43 | + ) |
| 44 | + |
| 45 | + urls.append( |
| 46 | + re_path( |
| 47 | + rf"^docs{self.trailing_slash}$", |
| 48 | + SpectacularRapiDocView.as_view(), |
| 49 | + name="docs", |
| 50 | + ) |
| 51 | + ) |
| 52 | + |
| 53 | + return urls |
| 54 | + |
| 55 | + |
| 56 | +class PromgenPagination(pagination.PageNumberPagination): |
| 57 | + page_query_param = "page_number" |
| 58 | + page_size_query_param = "page_size" |
| 59 | + page_size = 10 |
| 60 | + max_page_size = 1000 |
| 61 | + |
| 62 | + def __init__(self): |
| 63 | + super().__init__() |
| 64 | + self.page_query_description = self.page_query_description + " Starts from 1." |
| 65 | + self.page_size_query_description = self.page_size_query_description + str.format( |
| 66 | + " Defaults to {}.", self.page_size |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@extend_schema_view( |
| 71 | + list=extend_schema(summary="List Audit Logs", description="Retrieve a list of all audit logs."), |
| 72 | +) |
| 73 | +@extend_schema(tags=["Log"]) |
| 74 | +class AuditViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): |
| 75 | + queryset = models.Audit.objects.all().order_by("-created") |
| 76 | + filterset_class = filters.AuditFilter |
| 77 | + serializer_class = serializers.AuditSerializer |
| 78 | + lookup_value_regex = "[^/]+" |
| 79 | + lookup_field = "id" |
| 80 | + pagination_class = PromgenPagination |
| 81 | + permission_classes = [permissions.PromgenGuardianRestPermission] |
| 82 | + |
| 83 | + def get_queryset(self): |
| 84 | + if self.request.user.is_superuser or self.action != "list": |
| 85 | + return self.queryset |
| 86 | + services = permissions.get_accessible_services_for_user(self.request.user) |
| 87 | + projects = permissions.get_accessible_projects_for_user(self.request.user) |
| 88 | + groups = permissions.get_accessible_groups_for_user(self.request.user) |
| 89 | + service_ct = ContentType.objects.get_for_model(models.Service) |
| 90 | + project_ct = ContentType.objects.get_for_model(models.Project) |
| 91 | + group_ct = ContentType.objects.get_for_model(models.Group) |
| 92 | + return self.queryset.filter( |
| 93 | + Q( |
| 94 | + content_type__model="service", |
| 95 | + content_type__app_label="promgen", |
| 96 | + object_id__in=services, |
| 97 | + ) |
| 98 | + | Q( |
| 99 | + content_type__model="project", |
| 100 | + content_type__app_label="promgen", |
| 101 | + object_id__in=projects, |
| 102 | + ) |
| 103 | + | Q( |
| 104 | + content_type__model="group", |
| 105 | + content_type__app_label="auth", |
| 106 | + object_id__in=groups, |
| 107 | + ) |
| 108 | + | Q(parent_content_type_id=service_ct.id, parent_object_id__in=services) |
| 109 | + | Q(parent_content_type_id=project_ct.id, parent_object_id__in=projects) |
| 110 | + | Q(parent_content_type_id=group_ct.id, parent_object_id__in=groups) |
| 111 | + ) |
0 commit comments