* Add cloud connection statuses on user page * Add fixes for oncall hobby docker-compose installation * Fix for links and for cloud user status at User settings * Delete cloud api token on cloud disconnect * Merge branch 'dev' into cloud_connection_statuses_on_user_page * Fix cloud statuses for users * Fix usagestats service * Fix phone verification message in users table * added request after syncing user * Add endpoint to create CloudHeartbeat and polish code * Fix imports * Check token and heartbeat setting in setup_hertbeat_integration * Add macthed_users_count in cloud users * Sync users on token change * Fix query param * Fix tests * Heartbit button logic, tab width fix, coount users fix * Solve problem of existent cloud heartbeat integration * Solve problem of existent cloud heartbeat integration 2 * Solve problem of existent cloud heartbeat integration 3 * fix build * build fix, styles for env variables description Co-authored-by: Ildar Iskhakov <ildar.iskhakov@grafana.com> Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
77 lines
3.1 KiB
Python
77 lines
3.1 KiB
Python
from django.db.models import Count
|
|
from django_filters import rest_framework as filters
|
|
from rest_framework.exceptions import NotFound
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
from apps.alerts.models import AlertReceiveChannel
|
|
from apps.auth_token.auth import ApiTokenAuthentication
|
|
from apps.public_api.serializers import IntegrationSerializer, IntegrationUpdateSerializer
|
|
from apps.public_api.throttlers.user_throttle import UserThrottle
|
|
from apps.user_management.organization_log_creator import OrganizationLogType, create_organization_log
|
|
from common.api_helpers.filters import ByTeamFilter
|
|
from common.api_helpers.mixins import FilterSerializerMixin, RateLimitHeadersMixin, UpdateSerializerMixin
|
|
from common.api_helpers.paginators import FiftyPageSizePaginator
|
|
|
|
from .maintaiable_object_mixin import MaintainableObjectMixin
|
|
|
|
|
|
class IntegrationView(
|
|
RateLimitHeadersMixin,
|
|
FilterSerializerMixin,
|
|
UpdateSerializerMixin,
|
|
MaintainableObjectMixin,
|
|
ModelViewSet,
|
|
):
|
|
authentication_classes = (ApiTokenAuthentication,)
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
throttle_classes = [UserThrottle]
|
|
|
|
model = AlertReceiveChannel
|
|
serializer_class = IntegrationSerializer
|
|
update_serializer_class = IntegrationUpdateSerializer
|
|
|
|
pagination_class = FiftyPageSizePaginator
|
|
|
|
filter_backends = (filters.DjangoFilterBackend,)
|
|
filterset_class = ByTeamFilter
|
|
|
|
def get_queryset(self):
|
|
queryset = AlertReceiveChannel.objects.filter(organization=self.request.auth.organization).order_by(
|
|
"created_at"
|
|
)
|
|
name = self.request.query_params.get("name", None)
|
|
if name is not None:
|
|
queryset = queryset.filter(verbal_name=name)
|
|
queryset = self.filter_queryset(queryset)
|
|
queryset = self.serializer_class.setup_eager_loading(queryset)
|
|
queryset = queryset.annotate(alert_groups_count_annotated=Count("alert_groups", distinct=True))
|
|
return queryset
|
|
|
|
def get_object(self):
|
|
public_primary_key = self.kwargs["pk"]
|
|
|
|
try:
|
|
return self.get_queryset().get(public_primary_key=public_primary_key)
|
|
except AlertReceiveChannel.DoesNotExist:
|
|
raise NotFound
|
|
|
|
def perform_update(self, serializer):
|
|
old_state = serializer.instance.repr_settings_for_client_side_logging
|
|
serializer.save()
|
|
new_state = serializer.instance.repr_settings_for_client_side_logging
|
|
description = f"Integration settings was changed from:\n{old_state}\nto:\n{new_state}"
|
|
create_organization_log(
|
|
serializer.instance.organization,
|
|
self.request.user,
|
|
OrganizationLogType.TYPE_INTEGRATION_CHANGED,
|
|
description,
|
|
)
|
|
|
|
def perform_destroy(self, instance):
|
|
organization = instance.organization
|
|
user = self.request.user
|
|
description = f"Integration {instance.verbal_name} was deleted"
|
|
create_organization_log(organization, user, OrganizationLogType.TYPE_INTEGRATION_DELETED, description)
|
|
instance.delete()
|