def test_email_from(self): sla = SLA.objects.create(name="RoadMap", start_date="2012-01-01", end_date="2012-12-31") service = sla.service_set.create( response_time=2, solution_time=4, priority="normal") contact = Contact.objects.create(email="*****@*****.**") contact.save() contact.sla.add(sla) issue = Issue(title="broken stuff", contact=contact, text="Well, it's broken") issue.save() self.assertEquals(settings.DEFAULT_FROM_ADDR, issue.email_from) issue.sla = sla self.assertEquals(settings.DEFAULT_FROM_ADDR, issue.email_from) sla.email_from = "*****@*****.**" self.assertEquals("*****@*****.**", issue.email_from)
def test_create_issue(self): contact = Contact.objects.create(email="*****@*****.**") contact.save() issue = Issue(title="broken stuff", contact=contact, text="Well, it's broken", created=datetime.now()) issue.save() connector = ITConnector(name="jira", connector_type="jira", uri="http://issues-pg.pythonunited.com", usr="******", pwd="Wagner01") jira_connector = JiraConnector()
def test_time_on_hold(self): contact = Contact.objects.create(email="*****@*****.**") contact.save() issue = Issue(title="broken stuff", contact=contact, text="Well, it's broken") issue.save() self.assertEquals(0, issue._time_on_hold) status = issue.status_history.create(name=settings.ISSUE_STATUS_HOLD, issue=issue, comment="no comment") status.date = timezone.make_aware( datetime(2000, 1, 1, 16, 33), timezone.get_default_timezone()) status.save() status = issue.status_history.create(name=settings.ISSUE_STATUS_OPEN, issue=issue, comment="no comment") status.date = timezone.make_aware( datetime(2000, 1, 1, 16, 37), timezone.get_default_timezone()) status.save() status = issue.status_history.create(name=settings.ISSUE_STATUS_HOLD, issue=issue, comment="no comment") status.date = timezone.make_aware( datetime(2000, 1, 1, 17, 33), timezone.get_default_timezone()) status.save() status = issue.status_history.create(name=settings.ISSUE_STATUS_OPEN, issue=issue, comment="no comment") status.date = timezone.make_aware( datetime(2000, 1, 1, 18, 37), timezone.get_default_timezone()) status.save() self.assertEquals(60 * 68, issue._time_on_hold)
def test_in_time(self): sla = SLA.objects.create(name="RoadMap", start_date="2012-01-01", end_date="2012-12-31") service = sla.service_set.create( response_time=2, solution_time=4, priority="normal") contact = Contact.objects.create(email="*****@*****.**") contact.save() contact.sla.add(sla) issue = Issue(title="broken stuff", contact=contact, service=service, text="Well, it's broken", created=timezone.now(), sla=sla) issue.save() self.assertTrue(issue.in_time) issue.created = timezone.make_aware( datetime(1966, 1, 1, 12), timezone.get_default_timezone()) self.assertFalse(issue.in_time) issue.status = settings.ISSUE_STATUS_CLOSED status = issue.status_history.create(name=settings.ISSUE_STATUS_CLOSED, issue=issue, comment="solved") status.date = timezone.make_aware( datetime(1966, 1, 1, 14), timezone.get_default_timezone()) status.save() self.assertTrue(issue.in_time)
def handle_message(message): sla = determine_sla(message) sender = None from_addr = message['from'] send_from = settings.DEFAULT_FROM_ADDR if re.search('<[^>]*>', from_addr): from_addr = re.search('<([^>]*)>', from_addr).groups(0)[0] if not Contact.objects.filter(email=from_addr).exists(): if settings.ALLOW_NON_CONTACTS: sender = Contact.objects.create(email=from_addr) else: notify("issue_bounced", {}, send_from, from_addr) return False else: sender = Contact.objects.filter(email=from_addr)[0] if sla and not sla.is_contact(sender) and not \ settings.ALLOW_NON_CONTACTS: notify("issue_bounced", {}, send_from, from_addr) return False # Is this a reply to an existing issue? # match = ISSUE_SUBJECT_MATCH.search(message['subject']) # Parse message payload # body = [] html = [] attachments = [] counter = 0 for part in message.walk(): # multipart/* are just containers if part.get_content_maintype() == 'multipart': continue # Applications should really sanitize the given filename so that an # email message can't be used to overwrite important files # if part.get_content_type() == "text/plain": body.append(unicode( part.get_payload(decode=True), part.get_content_charset() or "utf8", 'ignore' )) elif part.get_content_type() == "text/html": if part.get_content_charset() is None: charset = str(chardet.detect(str(part))['encoding']) else: charset = str(part.get_content_charset()) text = unicode(part.get_payload(decode=True), charset, "ignore") html.append(html2text(text).replace(" _place_holder;", " ")) elif part.get_content_type() == "application/ms-tnef": tnef = TNEF(part.get_payload(decode=True), do_checksum=False) for tnef_att in tnef.attachments: att = Attachment() att._file = ContentFile(tnef_att.data) att._file.name = tnef_att.long_filename() att.mimetype = mimetypes.guess_type(tnef_att.long_filename()) attachments.append(att) else: filename = part.get_filename() if not filename: ext = mimetypes.guess_extension(part.get_content_type()) if not ext: # Use a generic bag-of-bits extension ext = '.bin' filename = 'part-%03d%s' % (counter, ext) counter += 1 att = Attachment() att._file = ContentFile(part.get_payload(decode=True)) att._file.name = filename att.mimetype = part.get_content_type() attachments.append(att) # Some emails contain both html and plain. We assume that in this case, # we only need the html part. # if html: body = "\n\n".join(html) else: body = "\n\n".join(body) issue = None comment = None send_notification_tpl = None if match: issue_id = int(match.groups()[0]) try: issue = Issue.objects.get(pk=issue_id) comment = issue.comments.create(comment=body, comment_by=sender) send_notification_tpl = "comment_received" if issue.status == settings.ISSUE_STATUS_WAIT: issue.set_status(settings.ISSUE_STATUS_OPEN) except Issue.DoesNotExist: pass if not issue: issue = Issue(title=message['subject'], contact=sender, text=body, sla=sla) send_notification_tpl = "issue_received" if sla and sla.default_service: issue.service = sla.default_service issue.save() for att in attachments: att.issue = issue att.save() if send_notification_tpl: to_addr = from_addr notify(send_notification_tpl, {"issue": issue, "comment": comment}, issue.email_from, to_addr) return True
def handle_message(message): sla = determine_sla(message) sender = None from_addr = message['from'] send_from = settings.DEFAULT_FROM_ADDR if re.search('<[^>]*>', from_addr): from_addr = re.search('<([^>]*)>', from_addr).groups(0)[0] if not Contact.objects.filter(email=from_addr).exists(): if settings.ALLOW_NON_CONTACTS: sender = Contact.objects.create(email=from_addr) else: notify("issue_bounced", {}, send_from, from_addr) return False else: sender = Contact.objects.filter(email=from_addr)[0] if sla and not sla.is_contact(sender) and not \ settings.ALLOW_NON_CONTACTS: notify("issue_bounced", {}, send_from, from_addr) return False # Is this a reply to an existing issue? # match = ISSUE_SUBJECT_MATCH.search(message['subject']) # Parse message payload # body = [] html = [] attachments = [] counter = 0 for part in message.walk(): # multipart/* are just containers if part.get_content_maintype() == 'multipart': continue # Applications should really sanitize the given filename so that an # email message can't be used to overwrite important files # if part.get_content_type() == "text/plain": body.append(unicode( part.get_payload(decode=True), part.get_content_charset() or "utf8", 'ignore' )) elif part.get_content_type() == "text/html": if part.get_content_charset() is None: charset = str(chardet.detect(str(part))['encoding']) else: charset = str(part.get_content_charset()) text = unicode(part.get_payload(decode=True), charset, "ignore") html.append(html2text(text).replace(" _place_holder;", " ")) elif part.get_content_type() == "application/ms-tnef": tnef = TNEF(part.get_payload(decode=True), do_checksum=False) for tnef_att in tnef.attachments: att = Attachment() att._file = ContentFile(tnef_att.data) att._file.name = tnef_att.long_filename() att.mimetype = mimetypes.guess_type(tnef_att.long_filename()) attachments.append(att) else: filename = part.get_filename() if not filename: ext = mimetypes.guess_extension(part.get_content_type()) if not ext: # Use a generic bag-of-bits extension ext = '.bin' filename = 'part-%03d%s' % (counter, ext) counter += 1 att = Attachment() att._file = ContentFile(part.get_payload(decode=True)) att._file.name = filename att.mimetype = part.get_content_type() attachments.append(att) # Some emails contain both html and plain. We assume that in this case, # we only need the html part. # if html: body = "\n\n".join(html) else: body = "\n\n".join(body) issue = None comment = None send_notification_tpl = None if match: issue_id = int(match.groups()[0]) try: issue = Issue.objects.get(pk=issue_id) comment = issue.comments.create(comment=body, comment_by=sender) send_notification_tpl = "comment_received" if issue.status == settings.ISSUE_STATUS_WAIT: issue.set_status(settings.ISSUE_STATUS_OPEN) except Issue.DoesNotExist: pass if not issue: issue = Issue(title=message['subject'][100:], contact=sender, text=body, sla=sla) send_notification_tpl = "issue_received" if sla and sla.default_service: issue.service = sla.default_service issue.save() for att in attachments: att.issue = issue att.save() if send_notification_tpl: to_addr = from_addr notify(send_notification_tpl, {"issue": issue, "comment": comment}, issue.email_from, to_addr) return True