mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-24 21:08:30 +00:00
perf: 重构工单处理流程 (#5408)
* perf: 重构工单处理流程 * perf: 重构工单处理流程 (1) * perf: 重构工单处理流程 (2) * perf: 重构工单处理流程 (3) * perf: 重构工单处理流程 (4) * perf: 重构工单处理流程 (5) * perf: 重构工单处理流程 (6) Co-authored-by: Bai <bugatti_it@163.com>
This commit is contained in:
@@ -11,8 +11,9 @@ from django.conf import settings
|
||||
from common.mixins.models import CommonModelMixin
|
||||
from orgs.mixins.models import OrgModelMixin
|
||||
from orgs.utils import tmp_to_root_org, tmp_to_org
|
||||
from tickets import const
|
||||
from .mixin import TicketModelMixin
|
||||
from tickets.const import TicketTypeChoices, TicketActionChoices, TicketStatusChoices
|
||||
from tickets.signals import post_change_ticket_action
|
||||
from tickets.handler import get_ticket_handler
|
||||
|
||||
__all__ = ['Ticket', 'ModelJSONFieldEncoder']
|
||||
|
||||
@@ -30,20 +31,20 @@ class ModelJSONFieldEncoder(json.JSONEncoder):
|
||||
return super().default(obj)
|
||||
|
||||
|
||||
class Ticket(TicketModelMixin, CommonModelMixin, OrgModelMixin):
|
||||
class Ticket(CommonModelMixin, OrgModelMixin):
|
||||
title = models.CharField(max_length=256, verbose_name=_("Title"))
|
||||
type = models.CharField(
|
||||
max_length=64, choices=const.TicketTypeChoices.choices,
|
||||
default=const.TicketTypeChoices.general.value, verbose_name=_("Type")
|
||||
max_length=64, choices=TicketTypeChoices.choices,
|
||||
default=TicketTypeChoices.general.value, verbose_name=_("Type")
|
||||
)
|
||||
meta = models.JSONField(encoder=ModelJSONFieldEncoder, default=dict, verbose_name=_("Meta"))
|
||||
action = models.CharField(
|
||||
choices=const.TicketActionChoices.choices, max_length=16,
|
||||
default=const.TicketActionChoices.open.value, verbose_name=_("Action")
|
||||
choices=TicketActionChoices.choices, max_length=16,
|
||||
default=TicketActionChoices.open.value, verbose_name=_("Action")
|
||||
)
|
||||
status = models.CharField(
|
||||
max_length=16, choices=const.TicketStatusChoices.choices,
|
||||
default=const.TicketStatusChoices.open.value, verbose_name=_("Status")
|
||||
max_length=16, choices=TicketStatusChoices.choices,
|
||||
default=TicketStatusChoices.open.value, verbose_name=_("Status")
|
||||
)
|
||||
# 申请人
|
||||
applicant = models.ForeignKey(
|
||||
@@ -51,7 +52,7 @@ class Ticket(TicketModelMixin, CommonModelMixin, OrgModelMixin):
|
||||
verbose_name=_("Applicant")
|
||||
)
|
||||
applicant_display = models.CharField(
|
||||
max_length=256, default='No', verbose_name=_("Applicant display")
|
||||
max_length=256, default='', verbose_name=_("Applicant display")
|
||||
)
|
||||
# 处理人
|
||||
processor = models.ForeignKey(
|
||||
@@ -59,7 +60,7 @@ class Ticket(TicketModelMixin, CommonModelMixin, OrgModelMixin):
|
||||
verbose_name=_("Processor")
|
||||
)
|
||||
processor_display = models.CharField(
|
||||
max_length=256, blank=True, null=True, default='No', verbose_name=_("Processor display")
|
||||
max_length=256, blank=True, null=True, default='', verbose_name=_("Processor display")
|
||||
)
|
||||
# 受理人列表
|
||||
assignees = models.ManyToManyField(
|
||||
@@ -80,70 +81,71 @@ class Ticket(TicketModelMixin, CommonModelMixin, OrgModelMixin):
|
||||
# type
|
||||
@property
|
||||
def type_apply_asset(self):
|
||||
return self.type == const.TicketTypeChoices.apply_asset.value
|
||||
return self.type == TicketTypeChoices.apply_asset.value
|
||||
|
||||
@property
|
||||
def type_apply_application(self):
|
||||
return self.type == const.TicketTypeChoices.apply_application.value
|
||||
return self.type == TicketTypeChoices.apply_application.value
|
||||
|
||||
@property
|
||||
def type_login_confirm(self):
|
||||
return self.type == const.TicketTypeChoices.login_confirm.value
|
||||
return self.type == TicketTypeChoices.login_confirm.value
|
||||
|
||||
# status
|
||||
@property
|
||||
def status_closed(self):
|
||||
return self.status == const.TicketStatusChoices.closed.value
|
||||
return self.status == TicketStatusChoices.closed.value
|
||||
|
||||
@property
|
||||
def status_open(self):
|
||||
return self.status == const.TicketStatusChoices.open.value
|
||||
return self.status == TicketStatusChoices.open.value
|
||||
|
||||
def set_status_closed(self):
|
||||
self.status = const.TicketStatusChoices.closed.value
|
||||
self.status = TicketStatusChoices.closed.value
|
||||
|
||||
# action
|
||||
@property
|
||||
def action_open(self):
|
||||
return self.action == const.TicketActionChoices.open.value
|
||||
return self.action == TicketActionChoices.open.value
|
||||
|
||||
@property
|
||||
def action_approve(self):
|
||||
return self.action == const.TicketActionChoices.approve.value
|
||||
return self.action == TicketActionChoices.approve.value
|
||||
|
||||
@property
|
||||
def action_reject(self):
|
||||
return self.action == const.TicketActionChoices.reject.value
|
||||
return self.action == TicketActionChoices.reject.value
|
||||
|
||||
@property
|
||||
def action_close(self):
|
||||
return self.action == const.TicketActionChoices.close.value
|
||||
return self.action == TicketActionChoices.close.value
|
||||
|
||||
@property
|
||||
def has_applied(self):
|
||||
return self.action_open
|
||||
# action changed
|
||||
def open(self, applicant):
|
||||
self.applicant = applicant
|
||||
self._change_action(action=TicketActionChoices.open.value)
|
||||
|
||||
@property
|
||||
def has_processed(self):
|
||||
return self.action_approve or self.action_reject or self.action_close
|
||||
def approve(self, processor):
|
||||
self.processor = processor
|
||||
self._change_action(action=TicketActionChoices.approve.value)
|
||||
|
||||
def set_action_close(self):
|
||||
self.action = const.TicketActionChoices.close.value
|
||||
def reject(self, processor):
|
||||
self.processor = processor
|
||||
self._change_action(action=TicketActionChoices.reject.value)
|
||||
|
||||
def close(self, processor):
|
||||
self.processor = processor
|
||||
self.set_action_close()
|
||||
self._change_action(action=TicketActionChoices.close.value)
|
||||
|
||||
def _change_action(self, action):
|
||||
self.action = action
|
||||
self.save()
|
||||
post_change_ticket_action.send(sender=self.__class__, ticket=self, action=action)
|
||||
|
||||
# ticket
|
||||
def has_assignee(self, assignee):
|
||||
return self.assignees.filter(id=assignee.id).exists()
|
||||
|
||||
@classmethod
|
||||
def all(cls):
|
||||
with tmp_to_root_org():
|
||||
return Ticket.objects.all()
|
||||
|
||||
@classmethod
|
||||
def get_user_related_tickets(cls, user):
|
||||
queries = None
|
||||
@@ -176,7 +178,22 @@ class Ticket(TicketModelMixin, CommonModelMixin, OrgModelMixin):
|
||||
tickets = tickets.filter(queries)
|
||||
return tickets.distinct()
|
||||
|
||||
@classmethod
|
||||
def all(cls):
|
||||
with tmp_to_root_org():
|
||||
return Ticket.objects.all()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
""" 确保保存的org_id的是自身的值 """
|
||||
with tmp_to_org(self.org_id):
|
||||
# 确保保存的org_id的是自身的值
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def handler(self):
|
||||
return get_ticket_handler(ticket=self)
|
||||
|
||||
# body
|
||||
@property
|
||||
def body(self):
|
||||
_body = self.handler.get_body()
|
||||
return _body
|
@@ -1 +0,0 @@
|
||||
from .model import *
|
@@ -1 +0,0 @@
|
||||
from .ticket import TicketModelMixin
|
@@ -1,134 +0,0 @@
|
||||
import textwrap
|
||||
from django.utils.translation import ugettext as __
|
||||
|
||||
|
||||
class SetDisplayFieldMixin:
|
||||
|
||||
def set_applicant_display(self):
|
||||
if self.has_applied:
|
||||
self.applicant_display = str(self.applicant)
|
||||
|
||||
def set_assignees_display(self):
|
||||
if self.has_applied:
|
||||
self.assignees_display = [str(assignee) for assignee in self.assignees.all()]
|
||||
|
||||
def set_processor_display(self):
|
||||
if self.has_processed:
|
||||
self.processor_display = str(self.processor)
|
||||
|
||||
def set_meta_display(self):
|
||||
method_name = f'construct_meta_{self.type}_{self.action}_fields_display'
|
||||
meta_display = getattr(self, method_name, lambda: {})()
|
||||
self.meta.update(meta_display)
|
||||
|
||||
def set_display_fields(self):
|
||||
self.set_applicant_display()
|
||||
self.set_processor_display()
|
||||
self.set_meta_display()
|
||||
|
||||
|
||||
class ConstructBodyMixin:
|
||||
# applied body
|
||||
def construct_applied_body(self):
|
||||
construct_method = getattr(self, f'construct_{self.type}_applied_body', lambda: 'No')
|
||||
applied_body = construct_method()
|
||||
body = '''
|
||||
{}:
|
||||
{}
|
||||
'''.format(
|
||||
__('Ticket applied info'),
|
||||
applied_body
|
||||
)
|
||||
return body
|
||||
|
||||
# approved body
|
||||
def construct_approved_body(self):
|
||||
construct_method = getattr(self, f'construct_{self.type}_approved_body', lambda: 'No')
|
||||
approved_body = construct_method()
|
||||
body = '''
|
||||
{}:
|
||||
{}
|
||||
'''.format(
|
||||
__('Ticket approved info'),
|
||||
approved_body
|
||||
)
|
||||
return body
|
||||
|
||||
# meta body
|
||||
def construct_meta_body(self):
|
||||
applied_body = self.construct_applied_body()
|
||||
if not self.action_approve:
|
||||
return applied_body
|
||||
approved_body = self.construct_approved_body()
|
||||
return applied_body + approved_body
|
||||
|
||||
# basic body
|
||||
def construct_basic_body(self):
|
||||
basic_body = '''
|
||||
{}:
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {}
|
||||
'''.format(
|
||||
__("Ticket basic info"),
|
||||
__('Ticket title'), self.title,
|
||||
__('Ticket type'), self.get_type_display(),
|
||||
__('Ticket applicant'), self.applicant_display,
|
||||
__('Ticket assignees'), self.assignees_display,
|
||||
__('Ticket processor'), self.processor_display,
|
||||
__('Ticket action'), self.get_action_display(),
|
||||
__('Ticket status'), self.get_status_display()
|
||||
)
|
||||
return basic_body
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
old_body = self.meta.get('body')
|
||||
if old_body:
|
||||
# 之前版本的body
|
||||
return old_body
|
||||
basic_body = self.construct_basic_body()
|
||||
meta_body = self.construct_meta_body()
|
||||
return basic_body + meta_body
|
||||
|
||||
|
||||
class CreatePermissionMixin:
|
||||
# create permission
|
||||
def create_permission(self):
|
||||
create_method = getattr(self, f'create_{self.type}_permission', lambda: None)
|
||||
permission = create_method()
|
||||
return permission
|
||||
|
||||
|
||||
class CreateCommentMixin:
|
||||
def create_comment(self, comment_body):
|
||||
# 页面展示需要取消缩进
|
||||
comment_body = textwrap.dedent(comment_body)
|
||||
comment_data = {
|
||||
'body': comment_body,
|
||||
'user': self.processor,
|
||||
'user_display': self.processor_display
|
||||
}
|
||||
return self.comments.create(**comment_data)
|
||||
|
||||
def create_applied_comment(self):
|
||||
comment_body = self.construct_applied_body()
|
||||
self.create_comment(comment_body)
|
||||
|
||||
def create_approved_comment(self):
|
||||
comment_body = self.construct_approved_body()
|
||||
self.create_comment(comment_body)
|
||||
|
||||
def create_action_comment(self):
|
||||
if self.has_applied:
|
||||
user_display = self.applicant_display
|
||||
if self.has_processed:
|
||||
user_display = self.processor_display
|
||||
comment_body = __(
|
||||
'User {} {} the ticket'.format(user_display, self.get_action_display())
|
||||
)
|
||||
self.create_comment(comment_body)
|
@@ -1 +0,0 @@
|
||||
from .base import *
|
@@ -1,133 +0,0 @@
|
||||
from django.utils.translation import ugettext as __
|
||||
from orgs.utils import tmp_to_org, tmp_to_root_org
|
||||
from applications.models import Application
|
||||
from applications.const import ApplicationCategoryChoices, ApplicationTypeChoices
|
||||
from assets.models import SystemUser
|
||||
from perms.models import ApplicationPermission
|
||||
from tickets.utils import convert_model_instance_data_field_name_to_verbose_name
|
||||
|
||||
|
||||
class ConstructDisplayFieldMixin:
|
||||
def construct_meta_apply_application_open_fields_display(self):
|
||||
meta_display_fields = ['apply_category_display', 'apply_type_display']
|
||||
apply_category = self.meta.get('apply_category')
|
||||
apply_category_display = ApplicationCategoryChoices.get_label(apply_category)
|
||||
apply_type = self.meta.get('apply_type')
|
||||
apply_type_display = ApplicationTypeChoices.get_label(apply_type)
|
||||
meta_display_values = [apply_category_display, apply_type_display]
|
||||
meta_display = dict(zip(meta_display_fields, meta_display_values))
|
||||
return meta_display
|
||||
|
||||
def construct_meta_apply_application_approve_fields_display(self):
|
||||
meta_display_fields = ['approve_applications_snapshot', 'approve_system_users_snapshot']
|
||||
approve_applications_id = self.meta.get('approve_applications', [])
|
||||
approve_system_users_id = self.meta.get('approve_system_users', [])
|
||||
with tmp_to_org(self.org_id):
|
||||
approve_applications_snapshot = list(
|
||||
Application.objects.filter(id__in=approve_applications_id).values(
|
||||
'name', 'category', 'type'
|
||||
)
|
||||
)
|
||||
approve_system_users_snapshot = list(
|
||||
SystemUser.objects.filter(id__in=approve_system_users_id).values(
|
||||
'name', 'username', 'username_same_with_user', 'protocol',
|
||||
'auto_push', 'sudo', 'home', 'sftp_root'
|
||||
)
|
||||
)
|
||||
meta_display_values = [approve_applications_snapshot, approve_system_users_snapshot]
|
||||
meta_display = dict(zip(meta_display_fields, meta_display_values))
|
||||
return meta_display
|
||||
|
||||
|
||||
class ConstructBodyMixin:
|
||||
|
||||
def construct_apply_application_applied_body(self):
|
||||
apply_category_display = self.meta.get('apply_category_display')
|
||||
apply_type_display = self.meta.get('apply_type_display')
|
||||
apply_application_group = self.meta.get('apply_application_group', [])
|
||||
apply_system_user_group = self.meta.get('apply_system_user_group', [])
|
||||
apply_date_start = self.meta.get('apply_date_start')
|
||||
apply_date_expired = self.meta.get('apply_date_expired')
|
||||
applied_body = '''{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
'''.format(
|
||||
__('Applied category'), apply_category_display,
|
||||
__('Applied type'), apply_type_display,
|
||||
__('Applied application group'), apply_application_group,
|
||||
__('Applied system user group'), apply_system_user_group,
|
||||
__('Applied date start'), apply_date_start,
|
||||
__('Applied date expired'), apply_date_expired,
|
||||
)
|
||||
return applied_body
|
||||
|
||||
def construct_apply_application_approved_body(self):
|
||||
# 审批信息
|
||||
approve_applications_snapshot = self.meta.get('approve_applications_snapshot', [])
|
||||
approve_applications_snapshot_display = convert_model_instance_data_field_name_to_verbose_name(
|
||||
Application, approve_applications_snapshot
|
||||
)
|
||||
approve_system_users_snapshot = self.meta.get('approve_system_users_snapshot', [])
|
||||
approve_system_users_snapshot_display = convert_model_instance_data_field_name_to_verbose_name(
|
||||
SystemUser, approve_system_users_snapshot
|
||||
)
|
||||
approve_date_start = self.meta.get('approve_date_start')
|
||||
approve_date_expired = self.meta.get('approve_date_expired')
|
||||
approved_body = '''{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
'''.format(
|
||||
__('Approved applications'), approve_applications_snapshot_display,
|
||||
__('Approved system users'), approve_system_users_snapshot_display,
|
||||
__('Approved date start'), approve_date_start,
|
||||
__('Approved date expired'), approve_date_expired
|
||||
)
|
||||
return approved_body
|
||||
|
||||
|
||||
class CreatePermissionMixin:
|
||||
|
||||
def create_apply_application_permission(self):
|
||||
with tmp_to_root_org():
|
||||
application_permission = ApplicationPermission.objects.filter(id=self.id).first()
|
||||
if application_permission:
|
||||
return application_permission
|
||||
|
||||
apply_category = self.meta.get('apply_category')
|
||||
apply_type = self.meta.get('apply_type')
|
||||
approved_applications_id = self.meta.get('approve_applications', [])
|
||||
approve_system_users_id = self.meta.get('approve_system_users', [])
|
||||
approve_date_start = self.meta.get('approve_date_start')
|
||||
approve_date_expired = self.meta.get('approve_date_expired')
|
||||
permission_name = '{}({})'.format(
|
||||
__('Created by ticket ({})'.format(self.title)), str(self.id)[:4]
|
||||
)
|
||||
permission_comment = __(
|
||||
'Created by the ticket, '
|
||||
'ticket title: {}, '
|
||||
'ticket applicant: {}, '
|
||||
'ticket processor: {}, '
|
||||
'ticket ID: {}'
|
||||
''.format(self.title, self.applicant_display, self.processor_display, str(self.id))
|
||||
)
|
||||
permissions_data = {
|
||||
'id': self.id,
|
||||
'name': permission_name,
|
||||
'category': apply_category,
|
||||
'type': apply_type,
|
||||
'comment': permission_comment,
|
||||
'created_by': '{}:{}'.format(str(self.__class__.__name__), str(self.id)),
|
||||
'date_start': approve_date_start,
|
||||
'date_expired': approve_date_expired,
|
||||
}
|
||||
with tmp_to_org(self.org_id):
|
||||
application_permission = ApplicationPermission.objects.create(**permissions_data)
|
||||
application_permission.users.add(self.applicant)
|
||||
application_permission.applications.set(approved_applications_id)
|
||||
application_permission.system_users.set(approve_system_users_id)
|
||||
|
||||
return application_permission
|
@@ -1,135 +0,0 @@
|
||||
from django.utils.translation import ugettext as __
|
||||
|
||||
from perms.models import AssetPermission, Action
|
||||
from assets.models import Asset, SystemUser
|
||||
from orgs.utils import tmp_to_org, tmp_to_root_org
|
||||
from tickets.utils import convert_model_instance_data_field_name_to_verbose_name
|
||||
|
||||
|
||||
class ConstructDisplayFieldMixin:
|
||||
def construct_meta_apply_asset_open_fields_display(self):
|
||||
meta_display_fields = ['apply_actions_display']
|
||||
|
||||
apply_actions = self.meta.get('apply_actions', Action.NONE)
|
||||
apply_actions_display = Action.value_to_choices_display(apply_actions)
|
||||
|
||||
meta_display_values = [apply_actions_display]
|
||||
meta_display = dict(zip(meta_display_fields, meta_display_values))
|
||||
return meta_display
|
||||
|
||||
def construct_meta_apply_asset_approve_fields_display(self):
|
||||
meta_display_fields = [
|
||||
'approve_actions_display', 'approve_assets_snapshot', 'approve_system_users_snapshot'
|
||||
]
|
||||
approve_actions = self.meta.get('approve_actions', Action.NONE)
|
||||
approve_actions_display = Action.value_to_choices_display(approve_actions)
|
||||
approve_assets_id = self.meta.get('approve_assets', [])
|
||||
approve_system_users_id = self.meta.get('approve_system_users', [])
|
||||
with tmp_to_org(self.org_id):
|
||||
approve_assets_snapshot = list(
|
||||
Asset.objects.filter(id__in=approve_assets_id).values(
|
||||
'hostname', 'ip', 'protocols', 'platform__name', 'public_ip'
|
||||
)
|
||||
)
|
||||
approve_system_users_snapshot = list(
|
||||
SystemUser.objects.filter(id__in=approve_system_users_id).values(
|
||||
'name', 'username', 'username_same_with_user', 'protocol',
|
||||
'auto_push', 'sudo', 'home', 'sftp_root'
|
||||
)
|
||||
)
|
||||
meta_display_values = [
|
||||
approve_actions_display, approve_assets_snapshot, approve_system_users_snapshot
|
||||
]
|
||||
meta_display = dict(zip(meta_display_fields, meta_display_values))
|
||||
return meta_display
|
||||
|
||||
|
||||
class ConstructBodyMixin:
|
||||
def construct_apply_asset_applied_body(self):
|
||||
apply_ip_group = self.meta.get('apply_ip_group', [])
|
||||
apply_hostname_group = self.meta.get('apply_hostname_group', [])
|
||||
apply_system_user_group = self.meta.get('apply_system_user_group', [])
|
||||
apply_actions_display = self.meta.get('apply_actions_display', [])
|
||||
apply_date_start = self.meta.get('apply_date_start')
|
||||
apply_date_expired = self.meta.get('apply_date_expired')
|
||||
applied_body = '''{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {}
|
||||
'''.format(
|
||||
__('Applied IP group'), apply_ip_group,
|
||||
__("Applied hostname group"), apply_hostname_group,
|
||||
__("Applied system user group"), apply_system_user_group,
|
||||
__("Applied actions"), apply_actions_display,
|
||||
__('Applied date start'), apply_date_start,
|
||||
__('Applied date expired'), apply_date_expired,
|
||||
)
|
||||
return applied_body
|
||||
|
||||
def construct_apply_asset_approved_body(self):
|
||||
approve_assets_snapshot = self.meta.get('approve_assets_snapshot', [])
|
||||
approve_assets_snapshot_display = convert_model_instance_data_field_name_to_verbose_name(
|
||||
Asset, approve_assets_snapshot
|
||||
)
|
||||
approve_system_users_snapshot = self.meta.get('approve_system_users_snapshot', [])
|
||||
approve_system_users_snapshot_display = convert_model_instance_data_field_name_to_verbose_name(
|
||||
SystemUser, approve_system_users_snapshot
|
||||
)
|
||||
approve_actions_display = self.meta.get('approve_actions_display', [])
|
||||
approve_date_start = self.meta.get('approve_date_start')
|
||||
approve_date_expired = self.meta.get('approve_date_expired')
|
||||
approved_body = '''{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {},
|
||||
{}: {}
|
||||
'''.format(
|
||||
__('Approved assets'), approve_assets_snapshot_display,
|
||||
__('Approved system users'), approve_system_users_snapshot_display,
|
||||
__('Approved actions'), ', '.join(approve_actions_display),
|
||||
__('Approved date start'), approve_date_start,
|
||||
__('Approved date expired'), approve_date_expired,
|
||||
)
|
||||
return approved_body
|
||||
|
||||
|
||||
class CreatePermissionMixin:
|
||||
def create_apply_asset_permission(self):
|
||||
with tmp_to_root_org():
|
||||
asset_permission = AssetPermission.objects.filter(id=self.id).first()
|
||||
if asset_permission:
|
||||
return asset_permission
|
||||
|
||||
approve_assets_id = self.meta.get('approve_assets', [])
|
||||
approve_system_users_id = self.meta.get('approve_system_users', [])
|
||||
approve_actions = self.meta.get('approve_actions', Action.NONE)
|
||||
approve_date_start = self.meta.get('approve_date_start')
|
||||
approve_date_expired = self.meta.get('approve_date_expired')
|
||||
permission_name = '{}({})'.format(
|
||||
__('Created by ticket ({})'.format(self.title)), str(self.id)[:4]
|
||||
)
|
||||
permission_comment = __(
|
||||
'Created by the ticket, '
|
||||
'ticket title: {}, '
|
||||
'ticket applicant: {}, '
|
||||
'ticket processor: {}, '
|
||||
'ticket ID: {}'
|
||||
''.format(self.title, self.applicant_display, self.processor_display, str(self.id))
|
||||
)
|
||||
permission_data = {
|
||||
'id': self.id,
|
||||
'name': permission_name,
|
||||
'comment': permission_comment,
|
||||
'created_by': '{}:{}'.format(str(self.__class__.__name__), str(self.id)),
|
||||
'actions': approve_actions,
|
||||
'date_start': approve_date_start,
|
||||
'date_expired': approve_date_expired,
|
||||
}
|
||||
with tmp_to_org(self.org_id):
|
||||
asset_permission = AssetPermission.objects.create(**permission_data)
|
||||
asset_permission.users.add(self.applicant)
|
||||
asset_permission.assets.set(approve_assets_id)
|
||||
asset_permission.system_users.set(approve_system_users_id)
|
||||
|
||||
return asset_permission
|
@@ -1,35 +0,0 @@
|
||||
from . import apply_asset, apply_application, login_confirm
|
||||
|
||||
__all__ = ['ConstructDisplayFieldMixin', 'ConstructBodyMixin', 'CreatePermissionMixin']
|
||||
|
||||
|
||||
modules = (apply_asset, apply_application, login_confirm)
|
||||
|
||||
|
||||
construct_display_field_mixin_cls_name = 'ConstructDisplayFieldMixin'
|
||||
construct_body_mixin_cls_name = 'ConstructBodyMixin'
|
||||
create_permission_mixin_cls_name = 'CreatePermissionMixin'
|
||||
|
||||
|
||||
def get_mixin_base_cls_list(base_cls_name):
|
||||
return [
|
||||
getattr(module, base_cls_name) for module in modules if hasattr(module, base_cls_name)
|
||||
]
|
||||
|
||||
|
||||
class ConstructDisplayFieldMixin(
|
||||
*get_mixin_base_cls_list(construct_display_field_mixin_cls_name)
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class ConstructBodyMixin(
|
||||
*get_mixin_base_cls_list(construct_body_mixin_cls_name)
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class CreatePermissionMixin(
|
||||
*get_mixin_base_cls_list(create_permission_mixin_cls_name)
|
||||
):
|
||||
pass
|
@@ -1,18 +0,0 @@
|
||||
from django.utils.translation import ugettext as __
|
||||
|
||||
|
||||
class ConstructBodyMixin:
|
||||
|
||||
def construct_login_confirm_applied_body(self):
|
||||
apply_login_ip = self.meta.get('apply_login_ip')
|
||||
apply_login_city = self.meta.get('apply_login_city')
|
||||
apply_login_datetime = self.meta.get('apply_login_datetime')
|
||||
applied_body = '''{}: {},
|
||||
{}: {},
|
||||
{}: {}
|
||||
'''.format(
|
||||
__("Applied login IP"), apply_login_ip,
|
||||
__("Applied login city"), apply_login_city,
|
||||
__("Applied login datetime"), apply_login_datetime,
|
||||
)
|
||||
return applied_body
|
@@ -1,30 +0,0 @@
|
||||
from . import base, meta
|
||||
|
||||
__all__ = ['TicketModelMixin']
|
||||
|
||||
|
||||
class TicketSetDisplayFieldMixin(meta.ConstructDisplayFieldMixin, base.SetDisplayFieldMixin):
|
||||
""" 设置 ticket display 字段(只设置,不保存)"""
|
||||
pass
|
||||
|
||||
|
||||
class TicketConstructBodyMixin(meta.ConstructBodyMixin, base.ConstructBodyMixin):
|
||||
""" 构造 ticket body 信息 """
|
||||
pass
|
||||
|
||||
|
||||
class TicketCreatePermissionMixin(meta.CreatePermissionMixin, base.CreatePermissionMixin):
|
||||
""" 创建 ticket 相关授权规则"""
|
||||
pass
|
||||
|
||||
|
||||
class TicketCreateCommentMixin(base.CreateCommentMixin):
|
||||
""" 创建 ticket 备注"""
|
||||
pass
|
||||
|
||||
|
||||
class TicketModelMixin(
|
||||
TicketSetDisplayFieldMixin, TicketConstructBodyMixin, TicketCreatePermissionMixin,
|
||||
TicketCreateCommentMixin
|
||||
):
|
||||
pass
|
Reference in New Issue
Block a user