2019-07-02 09:51:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-02-23 08:20:43 +00:00
|
|
|
from django.utils import translation
|
2023-03-27 09:17:20 +00:00
|
|
|
from django.utils.translation import gettext_noop
|
2021-04-08 06:59:14 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
2022-07-04 03:29:39 +00:00
|
|
|
from django.http.response import JsonResponse
|
2021-09-12 13:00:51 +00:00
|
|
|
from rest_framework import permissions
|
2021-10-21 08:16:50 +00:00
|
|
|
from rest_framework.request import Request
|
2019-07-02 09:51:50 +00:00
|
|
|
|
2022-07-04 03:29:39 +00:00
|
|
|
from common.exceptions import UserConfirmRequired
|
2023-03-27 09:17:20 +00:00
|
|
|
from common.utils import i18n_fmt
|
2023-06-14 08:21:46 +00:00
|
|
|
from orgs.utils import current_org
|
2022-11-04 06:22:38 +00:00
|
|
|
from audits.handler import create_or_update_operate_log
|
2023-03-27 09:17:20 +00:00
|
|
|
from audits.const import ActionChoices, ActivityChoices
|
|
|
|
from audits.models import ActivityLog
|
2019-07-02 09:51:50 +00:00
|
|
|
|
2022-11-11 07:11:10 +00:00
|
|
|
__all__ = [
|
|
|
|
"PermissionsMixin",
|
|
|
|
"RecordViewLogMixin",
|
|
|
|
"UserConfirmRequiredExceptionMixin",
|
|
|
|
]
|
2022-07-04 03:29:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserConfirmRequiredExceptionMixin:
|
|
|
|
"""
|
|
|
|
异常处理
|
|
|
|
"""
|
2022-11-11 07:11:10 +00:00
|
|
|
|
2022-07-04 03:29:39 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
except UserConfirmRequired as e:
|
|
|
|
return JsonResponse(e.detail, status=e.status_code)
|
2021-04-08 06:59:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PermissionsMixin(UserPassesTestMixin):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2021-10-21 08:16:50 +00:00
|
|
|
request: Request
|
2021-04-08 06:59:14 +00:00
|
|
|
|
|
|
|
def get_permissions(self):
|
|
|
|
return self.permission_classes
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
permission_classes = self.get_permissions()
|
|
|
|
for permission_class in permission_classes:
|
|
|
|
if not permission_class().has_permission(self.request, self):
|
|
|
|
return False
|
2021-04-29 09:15:46 +00:00
|
|
|
return True
|
2022-05-05 06:42:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RecordViewLogMixin:
|
2022-11-10 06:44:23 +00:00
|
|
|
ACTION = ActionChoices.view
|
2022-05-05 06:42:09 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2023-03-27 09:17:20 +00:00
|
|
|
def _filter_params(params):
|
|
|
|
new_params = {}
|
|
|
|
need_pop_params = ('format', 'order')
|
|
|
|
for key, value in params.items():
|
|
|
|
if key in need_pop_params:
|
|
|
|
continue
|
|
|
|
if isinstance(value, list):
|
|
|
|
value = list(filter(None, value))
|
|
|
|
if value:
|
|
|
|
new_params[key] = value
|
|
|
|
return new_params
|
|
|
|
|
|
|
|
def get_resource_display(self, request):
|
2022-05-05 06:42:09 +00:00
|
|
|
query_params = dict(request.query_params)
|
2023-03-27 09:17:20 +00:00
|
|
|
params = self._filter_params(query_params)
|
|
|
|
|
|
|
|
spm_filter = params.pop("spm", None)
|
|
|
|
|
|
|
|
if not params and not spm_filter:
|
|
|
|
display_message = gettext_noop("Export all")
|
2022-05-05 06:42:09 +00:00
|
|
|
elif spm_filter:
|
2023-03-27 09:17:20 +00:00
|
|
|
display_message = gettext_noop("Export only selected items")
|
2022-05-05 06:42:09 +00:00
|
|
|
else:
|
2022-11-11 07:11:10 +00:00
|
|
|
query = ",".join(
|
2023-03-27 09:17:20 +00:00
|
|
|
["%s=%s" % (key, value) for key, value in params.items()]
|
2022-05-05 06:42:09 +00:00
|
|
|
)
|
2023-03-27 09:17:20 +00:00
|
|
|
display_message = i18n_fmt(gettext_noop("Export filtered: %s"), query)
|
2022-05-05 06:42:09 +00:00
|
|
|
return display_message
|
|
|
|
|
2023-03-27 09:17:20 +00:00
|
|
|
def record_logs(self, ids, **kwargs):
|
|
|
|
resource_type = self.model._meta.verbose_name
|
|
|
|
create_or_update_operate_log(
|
|
|
|
self.ACTION, resource_type, force=True, **kwargs
|
|
|
|
)
|
|
|
|
detail = i18n_fmt(
|
|
|
|
gettext_noop('User %s view/export secret'), self.request.user
|
|
|
|
)
|
|
|
|
activities = [
|
|
|
|
ActivityLog(
|
|
|
|
resource_id=getattr(resource_id, 'pk', resource_id),
|
2023-06-14 08:57:32 +00:00
|
|
|
type=ActivityChoices.operate_log, detail=detail, org_id=current_org,
|
2023-03-27 09:17:20 +00:00
|
|
|
)
|
|
|
|
for resource_id in ids
|
|
|
|
]
|
|
|
|
ActivityLog.objects.bulk_create(activities)
|
|
|
|
|
2022-05-05 06:42:09 +00:00
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
response = super().list(request, *args, **kwargs)
|
2023-02-23 08:20:43 +00:00
|
|
|
with translation.override('en'):
|
|
|
|
resource_display = self.get_resource_display(request)
|
2023-03-27 09:17:20 +00:00
|
|
|
ids = [q.id for q in self.get_queryset()]
|
|
|
|
self.record_logs(ids, resource_display=resource_display)
|
2022-05-05 06:42:09 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
response = super().retrieve(request, *args, **kwargs)
|
2023-02-23 08:20:43 +00:00
|
|
|
with translation.override('en'):
|
2023-03-27 09:17:20 +00:00
|
|
|
resource = self.get_object()
|
|
|
|
self.record_logs([resource.id], resource=resource)
|
2022-05-05 06:42:09 +00:00
|
|
|
return response
|