2022-06-23 05:52:28 +00:00
|
|
|
import json
|
2022-11-18 03:30:31 +00:00
|
|
|
from urllib.parse import urljoin
|
2021-09-13 10:50:29 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
2022-05-05 05:07:48 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.shortcuts import reverse
|
2022-06-23 05:52:28 +00:00
|
|
|
from django.forms import model_to_dict
|
2022-11-18 03:30:31 +00:00
|
|
|
from django.template.loader import render_to_string
|
2021-10-21 12:03:05 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-09-13 10:50:29 +00:00
|
|
|
|
|
|
|
from notifications.notifications import UserMessage
|
2022-05-05 05:07:48 +00:00
|
|
|
from common.utils import get_logger, random_string
|
2022-06-23 05:52:28 +00:00
|
|
|
from common.db.encoder import ModelJSONFieldEncoder
|
2021-10-20 11:45:37 +00:00
|
|
|
from .models import Ticket
|
2022-05-05 05:07:48 +00:00
|
|
|
from . import const
|
2021-09-13 10:50:29 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTicketMessage(UserMessage):
|
2021-10-20 11:45:37 +00:00
|
|
|
title: ''
|
|
|
|
ticket: Ticket
|
|
|
|
content_title: str
|
2021-09-13 10:50:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ticket_detail_url(self):
|
2021-10-22 12:06:16 +00:00
|
|
|
tp = self.ticket.type
|
2022-03-17 03:57:25 +00:00
|
|
|
return urljoin(
|
|
|
|
settings.SITE_URL,
|
|
|
|
const.TICKET_DETAIL_URL.format(
|
|
|
|
id=str(self.ticket.id),
|
|
|
|
type=tp
|
|
|
|
)
|
|
|
|
)
|
2021-09-13 10:50:29 +00:00
|
|
|
|
2021-10-21 12:03:05 +00:00
|
|
|
@property
|
|
|
|
def content_title(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
|
|
|
def subject(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2021-09-13 10:50:29 +00:00
|
|
|
def get_html_msg(self) -> dict:
|
2021-10-20 11:45:37 +00:00
|
|
|
context = dict(
|
2021-09-13 10:50:29 +00:00
|
|
|
title=self.content_title,
|
2022-06-23 05:52:28 +00:00
|
|
|
content=self.content,
|
|
|
|
ticket_detail_url=self.ticket_detail_url
|
2021-09-13 10:50:29 +00:00
|
|
|
)
|
2021-10-20 11:45:37 +00:00
|
|
|
message = render_to_string('tickets/_msg_ticket.html', context)
|
2021-09-13 10:50:29 +00:00
|
|
|
return {
|
|
|
|
'subject': self.subject,
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
@classmethod
|
|
|
|
def gen_test_msg(cls):
|
2021-10-20 12:25:21 +00:00
|
|
|
return None
|
2021-10-20 11:45:37 +00:00
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
@property
|
|
|
|
def content(self):
|
|
|
|
content = [
|
|
|
|
{'title': _('Ticket basic info'), 'content': self.basic_items},
|
|
|
|
{'title': _('Ticket applied info'), 'content': self.spec_items},
|
|
|
|
]
|
|
|
|
return content
|
|
|
|
|
|
|
|
def _get_fields_items(self, item_names):
|
|
|
|
fields = self.ticket._meta._forward_fields_map
|
|
|
|
json_data = json.dumps(model_to_dict(self.ticket), cls=ModelJSONFieldEncoder)
|
|
|
|
data = json.loads(json_data)
|
|
|
|
items = []
|
|
|
|
|
|
|
|
for name in item_names:
|
|
|
|
field = fields[name]
|
|
|
|
item = {'name': name, 'title': field.verbose_name}
|
2022-07-12 07:28:42 +00:00
|
|
|
value = self.ticket.get_field_display(name, field, data)
|
2022-06-23 05:52:28 +00:00
|
|
|
item['value'] = value
|
|
|
|
items.append(item)
|
|
|
|
return items
|
|
|
|
|
|
|
|
@property
|
|
|
|
def basic_items(self):
|
|
|
|
item_names = ['serial_num', 'title', 'type', 'state', 'applicant', 'comment']
|
|
|
|
return self._get_fields_items(item_names)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def spec_items(self):
|
|
|
|
fields = self.ticket._meta.local_fields + self.ticket._meta.local_many_to_many
|
2022-08-16 09:15:21 +00:00
|
|
|
excludes = ['ticket_ptr', 'flow']
|
2022-06-23 05:52:28 +00:00
|
|
|
item_names = [field.name for field in fields if field.name not in excludes]
|
|
|
|
return self._get_fields_items(item_names)
|
|
|
|
|
2021-09-13 10:50:29 +00:00
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
class TicketAppliedToAssigneeMessage(BaseTicketMessage):
|
2021-09-13 10:50:29 +00:00
|
|
|
def __init__(self, user, ticket):
|
2022-11-18 03:30:31 +00:00
|
|
|
self._token = None
|
2021-09-13 10:50:29 +00:00
|
|
|
self.ticket = ticket
|
|
|
|
super().__init__(user)
|
2022-05-05 05:07:48 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def token(self):
|
|
|
|
if self._token is None:
|
|
|
|
self._token = random_string(32)
|
|
|
|
return self._token
|
2021-09-13 10:50:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def content_title(self):
|
2022-07-12 07:28:42 +00:00
|
|
|
return _('Your has a new ticket, applicant - {}').format(self.ticket.applicant)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
2021-10-21 12:03:05 +00:00
|
|
|
@property
|
|
|
|
def subject(self):
|
2022-06-23 05:52:28 +00:00
|
|
|
title = _('{}: New Ticket - {} ({})').format(
|
|
|
|
self.ticket.applicant,
|
|
|
|
self.ticket.title,
|
|
|
|
self.ticket.get_type_display()
|
2021-10-21 12:03:05 +00:00
|
|
|
)
|
|
|
|
return title
|
|
|
|
|
2022-05-05 05:07:48 +00:00
|
|
|
def get_ticket_approval_url(self):
|
|
|
|
url = reverse('tickets:direct-approve', kwargs={'token': self.token})
|
|
|
|
return urljoin(settings.SITE_URL, url)
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
context = dict(
|
|
|
|
title=self.content_title,
|
2022-06-23 05:52:28 +00:00
|
|
|
content=self.content,
|
|
|
|
ticket_detail_url=self.ticket_detail_url
|
2022-05-05 05:07:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ticket_approval_url = self.get_ticket_approval_url()
|
|
|
|
context.update({'ticket_approval_url': ticket_approval_url})
|
|
|
|
message = render_to_string('tickets/_msg_ticket.html', context)
|
2022-06-23 05:52:28 +00:00
|
|
|
cache.set(self.token, {'ticket_id': self.ticket.id, 'content': self.content}, 3600)
|
2022-05-05 05:07:48 +00:00
|
|
|
return {
|
|
|
|
'subject': self.subject,
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
@classmethod
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
from .models import Ticket
|
|
|
|
from users.models import User
|
|
|
|
ticket = Ticket.objects.first()
|
|
|
|
user = User.objects.first()
|
|
|
|
return cls(user, ticket)
|
2021-09-13 10:50:29 +00:00
|
|
|
|
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
class TicketProcessedToApplicantMessage(BaseTicketMessage):
|
2021-09-13 10:50:29 +00:00
|
|
|
def __init__(self, user, ticket, processor):
|
|
|
|
self.ticket = ticket
|
|
|
|
self.processor = processor
|
|
|
|
super().__init__(user)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def content_title(self):
|
|
|
|
return _('Your ticket has been processed, processor - {}').format(str(self.processor))
|
2021-10-20 11:45:37 +00:00
|
|
|
|
2021-10-21 12:03:05 +00:00
|
|
|
@property
|
|
|
|
def subject(self):
|
|
|
|
title = _('Ticket has processed - {} ({})').format(
|
|
|
|
self.ticket.title, self.ticket.get_type_display()
|
|
|
|
)
|
|
|
|
return title
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
@classmethod
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
from .models import Ticket
|
|
|
|
from users.models import User
|
|
|
|
ticket = Ticket.objects.first()
|
|
|
|
user = User.objects.first()
|
|
|
|
processor = User.objects.last()
|
|
|
|
return cls(user, ticket, processor)
|