Skip to content

Commit

Permalink
Merge pull request #7 from chadgates/timezone_in_datetime
Browse files Browse the repository at this point in the history
Prevent RuntimeWarning: DateTimeField received a naive datetime in django-pyas2
  • Loading branch information
abhishek-ram authored Jun 10, 2019
2 parents bca5d62 + f21ee0e commit 2a41029
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
10 changes: 7 additions & 3 deletions pyas2lib/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ def headers_str(self):
return message_header.encode('utf-8')

def build(self, data, filename=None, subject='AS2 Message',
content_type='application/edi-consent', additional_headers=None):
content_type='application/edi-consent', additional_headers=None,
disposition_notification_to='[email protected]'):

"""Function builds the AS2 message. Compresses, signs and encrypts
the payload if applicable.
Expand All @@ -326,6 +327,10 @@ def build(self, data, filename=None, subject='AS2 Message',
:param additional_headers:
Any additional headers to be included as part of the AS2 message.
:param disposition_notification_to:
Email address for disposition-notification-to header entry.
(default "[email protected]")
"""

# Validations
Expand Down Expand Up @@ -441,7 +446,7 @@ def build(self, data, filename=None, subject='AS2 Message',
self.message_id, self.payload.as_string()))

if self.receiver.mdn_mode:
as2_headers['disposition-notification-to'] = '[email protected]'
as2_headers['disposition-notification-to'] = disposition_notification_to
if self.receiver.mdn_digest_alg:
as2_headers['disposition-notification-options'] = \
'signed-receipt-protocol=required, pkcs7-signature; ' \
Expand Down Expand Up @@ -550,7 +555,6 @@ def parse(self, raw_content, find_org_cb, find_partner_cb,
self.enc_alg, decrypted_content = decrypt_message(
encrypted_data, self.receiver.decrypt_key)

raw_content = decrypted_content
self.payload = parse_mime(decrypted_content)

if self.payload.get_content_type() == 'text/plain':
Expand Down
12 changes: 7 additions & 5 deletions pyas2lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from io import BytesIO

from pyas2lib.exceptions import AS2Exception
from datetime import datetime
from datetime import datetime, timezone


def unquote_as2name(quoted_name):
Expand Down Expand Up @@ -196,8 +196,8 @@ def extract_certificate_info(cert):
:param cert: the certificate as byte string in PEM or DER format
:return: a dictionary holding certificate information:
valid_from (datetime)
valid_to (datetime)
valid_from (datetime) - UTC
valid_to (datetime) - UTC
subject (list of name, value tuples)
issuer (list of name, value tuples)
serial (int)
Expand All @@ -224,9 +224,11 @@ def extract_certificate_info(cert):

# on successful load, extract the various fields into the dictionary
cert_info['valid_from'] = datetime.strptime(
certificate.get_notBefore().decode('utf8'), "%Y%m%d%H%M%SZ")
certificate.get_notBefore().decode('utf8'), "%Y%m%d%H%M%SZ").\
replace(tzinfo=timezone.utc)
cert_info['valid_to'] = datetime.strptime(
certificate.get_notAfter().decode('utf8'), "%Y%m%d%H%M%SZ")
certificate.get_notAfter().decode('utf8'), "%Y%m%d%H%M%SZ").\
replace(tzinfo=timezone.utc)
cert_info['subject'] = [
tuple(item.decode('utf8') for item in sets)
for sets in certificate.get_subject().get_components()]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ def test_extract_certificate_info(self):
in PEM or DER format"""

cert_info = {
'valid_from': datetime.datetime(2019, 6, 3, 11, 32, 57),
'valid_to': datetime.datetime(2029, 5, 31, 11, 32, 57),
'valid_from': datetime.datetime(2019, 6, 3, 11, 32, 57, tzinfo=datetime.timezone.utc),
'valid_to': datetime.datetime(2029, 5, 31, 11, 32, 57, tzinfo=datetime.timezone.utc),
'subject': [('C', 'AU'), ('ST', 'Some-State'),
('O', 'pyas2lib'), ('CN', 'test')],
'issuer': [('C', 'AU'), ('ST', 'Some-State'),
Expand Down

0 comments on commit 2a41029

Please sign in to comment.