from django.utils.translation import ugettext as _ from common.utils import get_logger from tickets.utils import ( send_ticket_processed_mail_to_applicant, send_ticket_applied_mail_to_assignees ) logger = get_logger(__name__) class BaseHandler(object): def __init__(self, ticket): self.ticket = ticket # on action def _on_open(self): self.ticket.applicant_display = str(self.ticket.applicant) self.ticket.assignees_display = [str(assignee) for assignee in self.ticket.assignees.all()] meta_display = getattr(self, '_construct_meta_display_of_open', lambda: {})() self.ticket.meta.update(meta_display) self.ticket.save() self._send_applied_mail_to_assignees() def _on_approve(self): meta_display = getattr(self, '_construct_meta_display_of_approve', lambda: {})() self.ticket.meta.update(meta_display) self.__on_process() def _on_reject(self): self.__on_process() def _on_close(self): self.__on_process() def __on_process(self): self.ticket.processor_display = str(self.ticket.processor) self.ticket.set_status_closed() self._send_processed_mail_to_applicant() self.ticket.save() def dispatch(self, action): self._create_comment_on_action() method = getattr(self, f'_on_{action}', lambda: None) return method() # email def _send_applied_mail_to_assignees(self): logger.debug('Send applied email to assignees: {}'.format(self.ticket.assignees_display)) send_ticket_applied_mail_to_assignees(self.ticket) def _send_processed_mail_to_applicant(self): logger.debug('Send processed mail to applicant: {}'.format(self.ticket.applicant_display)) send_ticket_processed_mail_to_applicant(self.ticket) # comments def _create_comment_on_action(self): user = self.ticket.applicant if self.ticket.action_open else self.ticket.processor user_display = str(user) action_display = self.ticket.get_action_display() data = { 'body': _('User {} {} the ticket'.format(user_display, action_display)), 'user': user, 'user_display': user_display } return self.ticket.comments.create(**data) # body body_html_format = ''' {}: