<s> """<STR_LIT>""" <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import signals <EOL> from django . db import models <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . html import strip_tags <EOL> from treeio . core . conf import settings <EOL> from treeio . identities . models import Contact <EOL> from treeio . core . models import User , Object , ModuleSetting , UpdateRecord <EOL> from treeio . core . mail import BaseEmail <EOL> from treeio . core . rendering import render_to_string , render_string_template <EOL> from treeio . messaging . models import Message , MessageStream <EOL> class TicketStatus ( Object ) : <EOL> "<STR_LIT>" <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> details = models . TextField ( blank = True , null = True ) <EOL> active = models . BooleanField ( default = True ) <EOL> hidden = models . BooleanField ( default = False ) <EOL> searchable = False <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def get_absolute_url ( self ) : <EOL> "<STR_LIT>" <EOL> return reverse ( '<STR_LIT>' , args = [ self . id ] ) <EOL> class Meta : <EOL> "<STR_LIT>" <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' ) <EOL> class Service ( Object ) : <EOL> "<STR_LIT>" <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> parent = models . ForeignKey ( <EOL> '<STR_LIT>' , blank = True , null = True , related_name = '<STR_LIT>' ) <EOL> details = models . TextField ( blank = True , null = True ) <EOL> access_inherit = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def get_absolute_url ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return reverse ( '<STR_LIT>' , args = [ self . id ] ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> class Meta : <EOL> "<STR_LIT>" <EOL> ordering = [ '<STR_LIT:name>' ] <EOL> class ServiceLevelAgreement ( Object ) : <EOL> "<STR_LIT>" <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> service = models . ForeignKey ( Service ) <EOL> default = models . BooleanField ( default = False ) <EOL> response_time = models . PositiveIntegerField ( blank = True , null = True ) <EOL> uptime_rate = models . FloatField ( blank = True , null = True ) <EOL> available_from = models . TimeField ( blank = True , null = True ) <EOL> available_to = models . TimeField ( blank = True , null = True ) <EOL> client = models . ForeignKey ( <EOL> Contact , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> provider = models . ForeignKey ( Contact , related_name = "<STR_LIT>" ) <EOL> access_inherit = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Meta : <EOL> "<STR_LIT>" <EOL> ordering = ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def get_absolute_url ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return reverse ( '<STR_LIT>' , args = [ self . id ] ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> class ServiceAgent ( Object ) : <EOL> "<STR_LIT>" <EOL> related_user = models . ForeignKey ( User ) <EOL> active = models . BooleanField ( default = True ) <EOL> occupied = models . BooleanField ( default = False ) <EOL> available_from = models . TimeField ( blank = True , null = True ) <EOL> available_to = models . TimeField ( blank = True , null = True ) <EOL> class Meta : <EOL> "<STR_LIT>" <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . related_user ) <EOL> def get_absolute_url ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return reverse ( '<STR_LIT>' , args = [ self . id ] ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> class TicketQueue ( Object ) : <EOL> "<STR_LIT>" <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> active = models . BooleanField ( default = True ) <EOL> parent = models . ForeignKey ( <EOL> '<STR_LIT>' , blank = True , null = True , related_name = '<STR_LIT>' ) <EOL> default_ticket_status = models . ForeignKey ( <EOL> TicketStatus , blank = True , null = True , on_delete = models . SET_NULL ) <EOL> default_ticket_priority = models . IntegerField ( default = <NUM_LIT:3> , <EOL> choices = ( ( <NUM_LIT:5> , '<STR_LIT>' ) , ( <NUM_LIT:4> , '<STR_LIT>' ) , ( <NUM_LIT:3> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) , ( <NUM_LIT:1> , '<STR_LIT>' ) ) ) <EOL> default_service = models . ForeignKey ( <EOL> Service , blank = True , null = True , on_delete = models . SET_NULL ) <EOL> waiting_time = models . PositiveIntegerField ( blank = True , null = True ) <EOL> next_queue = models . ForeignKey ( <EOL> '<STR_LIT>' , blank = True , null = True , related_name = '<STR_LIT>' , on_delete = models . SET_NULL ) <EOL> ticket_code = models . CharField ( <EOL> max_length = <NUM_LIT:8> , blank = True , null = True , default = '<STR_LIT>' ) <EOL> message_stream = models . ForeignKey ( <EOL> MessageStream , blank = True , null = True , on_delete = models . SET_NULL ) <EOL> details = models . TextField ( blank = True , null = True ) <EOL> class Meta : <EOL> "<STR_LIT>" <EOL> ordering = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def get_absolute_url ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return reverse ( '<STR_LIT>' , args = [ self . id ] ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> class Ticket ( Object ) : <EOL> "<STR_LIT>" <EOL> reference = models . CharField ( max_length = <NUM_LIT> ) <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> caller = models . ForeignKey ( <EOL> Contact , blank = True , null = True , on_delete = models . SET_NULL ) <EOL> urgency = models . IntegerField ( default = <NUM_LIT:3> , <EOL> choices = ( ( <NUM_LIT:5> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:4> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:3> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:2> , _ ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) ) ) <EOL> priority = models . IntegerField ( default = <NUM_LIT:3> , <EOL> choices = ( ( <NUM_LIT:5> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:4> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:3> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:2> , _ ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) ) ) <EOL> status = models . ForeignKey ( TicketStatus ) <EOL> service = models . ForeignKey ( <EOL> Service , blank = True , null = True , on_delete = models . SET_NULL ) <EOL> sla = models . ForeignKey ( <EOL> ServiceLevelAgreement , blank = True , null = True , on_delete = models . SET_NULL ) <EOL> queue = models . ForeignKey ( TicketQueue , blank = True , null = True ) <EOL> assigned = models . ManyToManyField ( ServiceAgent , blank = True , null = True ) <EOL> message = models . ForeignKey ( <EOL> Message , blank = True , null = True , on_delete = models . SET_NULL ) <EOL> details = models . TextField ( blank = True , null = True ) <EOL> resolution = models . TextField ( blank = True , null = True ) <EOL> class Meta : <EOL> "<STR_LIT>" <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def priority_human ( self ) : <EOL> "<STR_LIT>" <EOL> choices = ( ( <NUM_LIT:5> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:4> , _ ( '<STR_LIT>' ) ) , ( <EOL> <NUM_LIT:3> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:2> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) ) <EOL> for choice in choices : <EOL> if choice [ <NUM_LIT:0> ] == self . priority : <EOL> return choice [ <NUM_LIT:1> ] <EOL> def urgency_human ( self ) : <EOL> "<STR_LIT>" <EOL> choices = ( ( <NUM_LIT:5> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:4> , _ ( '<STR_LIT>' ) ) , ( <EOL> <NUM_LIT:3> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:2> , _ ( '<STR_LIT>' ) ) , ( <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) ) <EOL> for choice in choices : <EOL> if choice [ <NUM_LIT:0> ] == self . priority : <EOL> return choice [ <NUM_LIT:1> ] <EOL> def save ( self , * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> super ( Ticket , self ) . save ( * args , ** kwargs ) <EOL> if not self . reference : <EOL> if self . queue : <EOL> self . reference = self . queue . ticket_code + str ( self . id ) <EOL> else : <EOL> self . reference = str ( self . id ) <EOL> self . save ( ) <EOL> def get_absolute_url ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return reverse ( '<STR_LIT>' , args = [ self . id ] ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> class TicketRecord ( UpdateRecord ) : <EOL> "<STR_LIT>" <EOL> message = models . ForeignKey ( Message , blank = True , null = True ) <EOL> notify = models . BooleanField ( default = False , blank = True ) <EOL> """<STR_LIT>""" <EOL> def email_caller_on_new_ticket ( sender , instance , created , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> if created : <EOL> send_email_to_caller = False <EOL> try : <EOL> conf = ModuleSetting . get_for_module ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> send_email_to_caller = conf . value <EOL> except : <EOL> send_email_to_caller = getattr ( <EOL> settings , '<STR_LIT>' , True ) <EOL> if send_email_to_caller : <EOL> creator_contact = None <EOL> if instance . creator : <EOL> creator_contact = instance . creator . get_contact ( ) <EOL> if instance . caller and instance . caller != creator_contact : <EOL> if not instance . reference : <EOL> if instance . queue : <EOL> instance . reference = instance . queue . ticket_code + str ( instance . id ) <EOL> else : <EOL> instance . reference = str ( instance . id ) <EOL> instance . save ( ) <EOL> subject = "<STR_LIT>" % ( instance . reference , instance . name ) <EOL> context = { '<STR_LIT>' : instance } <EOL> try : <EOL> conf = ModuleSetting . get_for_module ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> send_email_template = conf . value <EOL> html = render_string_template ( send_email_template , context ) <EOL> except : <EOL> html = render_to_string ( <EOL> '<STR_LIT>' , context , response_format = '<STR_LIT:html>' ) <EOL> body = strip_tags ( html ) <EOL> if instance . queue and instance . queue . message_stream : <EOL> stream = instance . queue . message_stream <EOL> if stream . outgoing_server_name : <EOL> try : <EOL> caller_email = instance . caller . get_email ( ) <EOL> if caller_email : <EOL> toaddr = caller_email <EOL> ssl = False <EOL> if stream . outgoing_server_type == '<STR_LIT>' : <EOL> ssl = True <EOL> email = BaseEmail ( stream . outgoing_server_name , <EOL> stream . outgoing_server_username , <EOL> stream . outgoing_password , <EOL> stream . outgoing_email , <EOL> toaddr , subject , body , html = html , <EOL> ssl = ssl ) <EOL> email . process_email ( ) <EOL> except : <EOL> pass <EOL> signals . post_save . connect ( email_caller_on_new_ticket , sender = Ticket ) <EOL> def create_ticket_from_message ( sender , instance , created , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if created and getattr ( instance , '<STR_LIT>' , True ) : <EOL> if instance . reply_to : <EOL> tickets = instance . reply_to . ticket_set . all ( ) <EOL> for ticket in tickets : <EOL> record = TicketRecord ( ) <EOL> record . sender = instance . author <EOL> record . record_type = '<STR_LIT>' <EOL> record . body = instance . body <EOL> record . save ( ) <EOL> record . about . add ( ticket ) <EOL> ticket . set_last_updated ( ) <EOL> else : <EOL> stream = instance . stream <EOL> queues = TicketQueue . objects . filter ( message_stream = stream ) <EOL> if stream and queues : <EOL> queue = queues [ <NUM_LIT:0> ] <EOL> ticket = Ticket ( ) <EOL> try : <EOL> conf = ModuleSetting . get_for_module ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> ticket . status = TicketStatus . objects . get ( <EOL> pk = long ( conf . value ) ) <EOL> except : <EOL> statuses = TicketStatus . objects . all ( ) <EOL> ticket . status = statuses [ <NUM_LIT:0> ] <EOL> ticket . queue = queue <EOL> ticket . caller = instance . author <EOL> ticket . details = instance . body <EOL> ticket . message = instance <EOL> ticket . name = instance . title <EOL> ticket . auto_notify = False <EOL> ticket . save ( ) <EOL> try : <EOL> if stream . creator : <EOL> ticket . set_user ( stream . creator ) <EOL> elif queue . creator : <EOL> ticket . set_user ( queue . creator ) <EOL> else : <EOL> ticket . copy_permissions ( queue ) <EOL> except : <EOL> pass <EOL> instance . title = "<STR_LIT>" + ticket . reference + "<STR_LIT>" + instance . title <EOL> instance . save ( ) <EOL> signals . post_save . connect ( create_ticket_from_message , sender = Message ) </s>
