Created
November 11, 2009 18:21
-
-
Save mpasternacki/232165 to your computer and use it in GitHub Desktop.
Monkey-patch django-paypal for issue #17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/standard/forms.py b/standard/forms.py | |
index 6639146..f13aae4 100644 | |
--- a/standard/forms.py | |
+++ b/standard/forms.py | |
@@ -199,12 +199,52 @@ class PayPalSharedSecretEncryptedPaymentsForm(PayPalEncryptedPaymentsForm): | |
else: | |
self.fields['notify_url'].initial += secret_param | |
+try: | |
+ import dateutil.parser | |
+except ImportError: | |
+ DateTimeField = forms.fields.DateTimeField | |
+else: | |
+ import datetime | |
+ class DateTimeField(forms.fields.Field): | |
+ """Use datetutils parser that is not locale-aware.""" | |
+ EMPTY_VALUES = (None, '') | |
+ def __init__(self, input_formats=None, *args, **kwargs): | |
+ # ignore input_formats for compatibility | |
+ super(DateTimeField, self).__init__(*args, **kwargs) | |
+ | |
+ def clean(self, value): | |
+ value = super(DateTimeField, self).clean(value) | |
+ | |
+ # django.forms.fields.DateTimeField.clean | |
+ if value in forms.fields.EMPTY_VALUES: | |
+ return None | |
+ if isinstance(value, datetime.datetime): | |
+ return value | |
+ if isinstance(value, datetime.date): | |
+ return datetime.datetime(value.year, value.month, value.day) | |
+ if isinstance(value, list): | |
+ # Input comes from a SplitDateTimeWidget, for example. So, it's two | |
+ # components: date and time. | |
+ if len(value) != 2: | |
+ raise ValidationError(self.error_messages['invalid']) | |
+ value = '%s %s' % tuple(value) | |
+ | |
+ try: | |
+ tzdt = dateutil.parser.parse( | |
+ value, | |
+ tzinfos={'PST':-8*3600, 'PDT':-7*3600}, | |
+ ) | |
+ return datetime.datetime(*tzdt.utctimetuple()[:7]) | |
+ except Exception, e: | |
+ raise django.forms.ValidationError( | |
+ 'Invalid date/time: %s' % e) | |
+ | |
class PayPalStandardBaseForm(forms.ModelForm): | |
"""Form used to receive and record PayPal IPN/PDT.""" | |
# PayPal dates have non-standard formats. | |
- time_created = forms.DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
- payment_date = forms.DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
- next_payment_date = forms.DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
- subscr_date = forms.DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
- subscr_effective = forms.DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
\ No newline at end of file | |
+ time_created = DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
+ payment_date = DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
+ next_payment_date = DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
+ subscr_date = DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) | |
+ subscr_effective = DateTimeField(required=False, input_formats=PAYPAL_DATE_FORMAT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment