# Path: pyas2/models.py
# DEFAULT_ENTRY = ('', "---------")
#     CONTENT_TYPE_CHOICES = (
#         ('application/EDI-X12', 'application/EDI-X12'),
#         ('application/EDIFACT', 'application/EDIFACT'),
#         ('application/edi-consent', 'application/edi-consent'),
#         ('application/XML', 'application/XML'),
#         ('text/CSV', 'text/CSV'),
#     )
#     ENCRYPT_ALG_CHOICES = (
#         ('des_ede3_cbc', '3DES'),
#         ('des_cbc', 'DES'),
#         ('aes_128_cbc', 'AES-128'),
#         ('aes_192_cbc', 'AES-192'),
#         ('aes_256_cbc', 'AES-256'),
#         ('rc2_40_cbc', 'RC2-40'),
#     )
#     SIGN_ALG_CHOICES = (
#         ('sha1', 'SHA-1'),
#     )
#     MDN_TYPE_CHOICES = (
#         ('SYNC', 'Synchronous'),
#         ('ASYNC', 'Asynchronous'),
#     )
#     DIRECTION_CHOICES = (
#         ('IN', _('Inbound')),
#         ('OUT', _('Outbound')),
#     )
#     STATUS_CHOICES = (
#         ('S', _('Success')),
#         ('E', _('Error')),
#         ('W', _('Warning')),
#         ('P', _('Pending')),
#         ('R', _('Retry')),
#         ('IP', _('In Process')),
#     )
#     MODE_CHOICES = (
#         ('SYNC', _('Synchronous')),
#         ('ASYNC', _('Asynchronous')),
#     )
#     STATUS_CHOICES = (
#         ('S', _('Success')),
#         ('E', _('Error')),
#         ('W', _('Warning')),
#     )
#     STATUS_CHOICES = (
#         ('S', _('Sent')),
#         ('R', _('Received')),
#         ('P', _('Pending')),
#     )
# def get_certificate_path(instance, filename):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
# def getorganizations():
# def getpartners():
# def check_odirs(sender, instance, created, **kwargs):
# def check_pdirs(sender, instance, created, **kwargs):
# def update_dirs():
# class PrivateCertificate(models.Model):
# class PublicCertificate(models.Model):
# class Organization(models.Model):
# class Partner(models.Model):
# class Message(models.Model):
# class Payload(models.Model):
# class Log(models.Model):
# class MDN(models.Model):
# 
# Path: pyas2/pyas2init.py
# def initialize():
# def initserverlogging(logname):


from django.core.management.base import BaseCommand
from django.utils.translation import ugettext as _
from pyas2 import models
from pyas2 import pyas2init
from email.parser import HeaderParser
from django.utils import timezone
from datetime import timedelta
import requests
                # Set the ca cert if given in the partner profile
                verify = True
                if pending_mdn.omessage.partner.https_ca_cert:
                    verify = pending_mdn.omessage.partner.https_ca_cert.path

                # Post the MDN message to the url provided on the original as2 message
                with open(pending_mdn.file, 'rb') as payload:
                    requests.post(pending_mdn.return_url,
                                  auth=auth,
                                  verify=verify,
                                  headers=dict(mdn_headers.items()),
                                  data=payload)
                pending_mdn.status = 'S'
                models.Log.objects.create(message=pending_mdn.omessage,
                                          status='S',
                                          text=_(u'Successfully sent asynchronous mdn to partner'))
            except Exception, e:
                models.Log.objects.create(message=pending_mdn.omessage,
                                          status='E',
                                          text=_(
                                              u'Failed to send asynchronous mdn to partner, error is {0:s}'.format(e)))
            finally:
                pending_mdn.save()

        # Second Part of script checks if MDNs have been received for outbound messages to partners
        pyas2init.logger.info(_(u'Marking messages waiting for MDNs for more than {0:d} minutes'.format(
            pyas2init.gsettings['async_mdn_wait'])))

        # Find all messages waiting MDNs for more than the set async mdn wait time
        time_threshold = timezone.now() - timedelta(minutes=pyas2init.gsettings['async_mdn_wait'])

# Path: pyas2/models.py
# DEFAULT_ENTRY = ('', "---------")
#     CONTENT_TYPE_CHOICES = (
#         ('application/EDI-X12', 'application/EDI-X12'),
#         ('application/EDIFACT', 'application/EDIFACT'),
#         ('application/edi-consent', 'application/edi-consent'),
#         ('application/XML', 'application/XML'),
#         ('text/CSV', 'text/CSV'),
#     )
#     ENCRYPT_ALG_CHOICES = (
#         ('des_ede3_cbc', '3DES'),
#         ('des_cbc', 'DES'),
#         ('aes_128_cbc', 'AES-128'),
#         ('aes_192_cbc', 'AES-192'),
#         ('aes_256_cbc', 'AES-256'),
#         ('rc2_40_cbc', 'RC2-40'),
#     )
#     SIGN_ALG_CHOICES = (
#         ('sha1', 'SHA-1'),
#     )
#     MDN_TYPE_CHOICES = (
#         ('SYNC', 'Synchronous'),
#         ('ASYNC', 'Asynchronous'),
#     )
#     DIRECTION_CHOICES = (
#         ('IN', _('Inbound')),
#         ('OUT', _('Outbound')),
#     )
#     STATUS_CHOICES = (
#         ('S', _('Success')),
#         ('E', _('Error')),
#         ('W', _('Warning')),
#         ('P', _('Pending')),
#         ('R', _('Retry')),
#         ('IP', _('In Process')),
#     )
#     MODE_CHOICES = (
#         ('SYNC', _('Synchronous')),
#         ('ASYNC', _('Asynchronous')),
#     )
#     STATUS_CHOICES = (
#         ('S', _('Success')),
#         ('E', _('Error')),
#         ('W', _('Warning')),
#     )
#     STATUS_CHOICES = (
#         ('S', _('Sent')),
#         ('R', _('Received')),
#         ('P', _('Pending')),
#     )
# def get_certificate_path(instance, filename):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
# def getorganizations():
# def getpartners():
# def check_odirs(sender, instance, created, **kwargs):
# def check_pdirs(sender, instance, created, **kwargs):
# def update_dirs():
# class PrivateCertificate(models.Model):
# class PublicCertificate(models.Model):
# class Organization(models.Model):
# class Partner(models.Model):
# class Message(models.Model):
# class Payload(models.Model):
# class Log(models.Model):
# class MDN(models.Model):
# 
# Path: pyas2/pyas2init.py
# def initialize():
# def initserverlogging(logname):


# Path: pyas2/management/commands/sendasyncmdn.py
from django.core.management.base import BaseCommand
from django.utils.translation import ugettext as _
from pyas2 import models
from pyas2 import pyas2init
from email.parser import HeaderParser
from django.utils import timezone
from datetime import timedelta
import requests

                # Set the ca cert if given in the partner profile
                verify = True
                if pending_mdn.omessage.partner.https_ca_cert:
                    verify = pending_mdn.omessage.partner.https_ca_cert.path

                # Post the MDN message to the url provided on the original as2 message
                with open(pending_mdn.file, 'rb') as payload:
                    requests.post(pending_mdn.return_url,
                                  auth=auth,
                                  verify=verify,
                                  headers=dict(mdn_headers.items()),
                                  data=payload)
                pending_mdn.status = 'S'
                models.Log.objects.create(message=pending_mdn.omessage,
                                          status='S',
                                          text=_(u'Successfully sent asynchronous mdn to partner'))
            except Exception, e:
                models.Log.objects.create(message=pending_mdn.omessage,
                                          status='E',
                                          text=_(
                                              u'Failed to send asynchronous mdn to partner, error is {0:s}'.format(e)))
            finally:
                pending_mdn.save()

        # Second Part of script checks if MDNs have been received for outbound messages to partners
        pyas2init.logger.info(_(u'Marking messages waiting for MDNs for more than {0:d} minutes'.format(
            pyas2init.gsettings['async_mdn_wait'])))

        # Find all messages waiting MDNs for more than the set async mdn wait time
        time_threshold = timezone.now() - timedelta(minutes=pyas2init.gsettings['async_mdn_wait'])

        out_pending_msgs = models.Message.objects.filter(status='P', direction='OUT', timestamp__lt=time_threshold)