Skip to content

Commit

Permalink
Fix enum choice ordering across Python 2.x/3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
lskillen committed Aug 4, 2017
1 parent 523fd61 commit 2306992
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
13 changes: 12 additions & 1 deletion djstripe/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from collections import OrderedDict
import operator

from django.utils.translation import ugettext as _
from django.utils.six import add_metaclass
Expand Down Expand Up @@ -28,9 +29,19 @@ def __new__(self, name, bases, classdict):

for k, v in keys.items():
classdict[v] = k

classdict["__choices__"] = choices
classdict["__members__"] = members
classdict["choices"] = tuple(choices.items())

# Note: Differences between Python 2.x and Python 3.x force us to
# explicitly use unicode here, and to explicitly sort the list. In
# Python 2.x, class members are unordered and so the ordering will
# vary on different systems based on internal hashing. Without this
# Django will continually require new no-op migrations.
classdict["choices"] = tuple(
(unicode(k), unicode(v))
for k, v in sorted(choices.items(), key=operator.itemgetter(0))
)

return type.__new__(self, name, bases, classdict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class Migration(migrations.Migration):
('address_state', djstripe.fields.StripeTextField(help_text='Billing address state.', null=True)),
('address_zip', djstripe.fields.StripeTextField(help_text='Billing address zip code.', null=True)),
('address_zip_check', djstripe.fields.StripeCharField(choices=[('pass', 'Pass'), ('fail', 'Fail'), ('unavailable', 'Unavailable'), ('unknown', 'Unknown')], help_text='If ``address_zip`` was provided, results of the check.', max_length=11, null=True)),
('brand', djstripe.fields.StripeCharField(choices=[('Visa', 'Visa'), ('American Express', 'American Express'), ('MasterCard', 'MasterCard'), ('Discover', 'Discover'), ('JCB', 'JCB'), ('Diners Club', 'Diners Club'), ('Unknown', 'Unknown')], help_text='Card brand.', max_length=16)),
('brand', djstripe.fields.StripeCharField(choices=[('American Express', 'American Express'), ('Diners Club', 'Diners Club'), ('Discover', 'Discover'), ('JCB', 'JCB'), ('MasterCard', 'MasterCard'), ('Unknown', 'Unknown'), ('Visa', 'Visa')], help_text='Card brand.', max_length=16)),
('country', djstripe.fields.StripeCharField(help_text='Two-letter ISO code representing the country of the card.', max_length=2)),
('cvc_check', djstripe.fields.StripeCharField(choices=[('pass', 'Pass'), ('fail', 'Fail'), ('unavailable', 'Unavailable'), ('unknown', 'Unknown')], help_text='If a CVC was provided, results of the check.', max_length=11, null=True)),
('dynamic_last4', djstripe.fields.StripeCharField(help_text='(For tokenized numbers only.) The last four digits of the device account number.', max_length=4, null=True)),
Expand All @@ -317,7 +317,7 @@ class Migration(migrations.Migration):
('funding', djstripe.fields.StripeCharField(choices=[('credit', 'Credit'), ('debit', 'Debit'), ('prepaid', 'Prepaid'), ('unknown', 'Unknown')], help_text='Card funding type.', max_length=7)),
('last4', djstripe.fields.StripeCharField(help_text='Last four digits of Card number.', max_length=4)),
('name', djstripe.fields.StripeTextField(help_text='Cardholder name.', null=True)),
('tokenization_method', djstripe.fields.StripeCharField(choices=[('apple_pay', 'Apple Pay'), ('android_pay', 'Android Pay')], help_text='If the card number is tokenized, this is the method that was used.', max_length=11, null=True)),
('tokenization_method', djstripe.fields.StripeCharField(choices=[('android_pay', 'Android Pay'), ('apple_pay', 'Apple Pay')], help_text='If the card number is tokenized, this is the method that was used.', max_length=11, null=True)),
],
options={
'abstract': False,
Expand Down Expand Up @@ -395,7 +395,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='charge',
name='failure_code',
field=djstripe.fields.StripeCharField(choices=[('invalid_number', 'Invalid Number'), ('invalid_expiry_month', 'Invalid Expiry Month'), ('invalid_expiry_year', 'Invalid Expiry Year'), ('invalid_cvc', 'Invalid Cvc'), ('incorrect_number', 'Incorrect Number'), ('expired_card', 'Expired Card'), ('incorrect_cvc', 'Incorrect Cvc'), ('incorrect_zip', 'Incorrect Zip'), ('card_declined', 'Card Declined'), ('missing', 'Missing'), ('processing_error', 'Processing Error'), ('rate_limit', 'Rate Limit')], help_text='Error code explaining reason for charge failure if available.', max_length=30, null=True),
field=djstripe.fields.StripeCharField(choices=[('card_declined', 'Card was declined'), ('expired_card', 'Expired card'), ('incorrect_cvc', 'Incorrect security code'), ('incorrect_number', 'Incorrect number'), ('incorrect_zip', 'ZIP code failed validation'), ('invalid_cvc', 'Invalid security code'), ('invalid_expiry_month', 'Invalid expiration month'), ('invalid_expiry_year', 'Invalid expiration year'), ('invalid_number', 'Invalid number'), ('invalid_swipe_data', 'Invalid swipe data'), ('missing', 'No card being charged'), ('processing_error', 'Processing error')], help_text='Error code explaining reason for charge failure if available.', max_length=30, null=True),
),
migrations.AddField(
model_name='charge',
Expand Down Expand Up @@ -435,7 +435,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='charge',
name='source_type',
field=djstripe.fields.StripeCharField(help_text='The payment source type. If the payment source is supported by dj-stripe, a corresponding model is attached to this Charge via a foreign key matching this field.', max_length=20, null=True),
field=djstripe.fields.StripeCharField(choices=[('alipay_account', 'Alipay account'), ('bank_account', 'Bank account'), ('bitcoin_receiver', 'Bitcoin receiver'), ('card', 'Card')], help_text='The payment source type. If the payment source is supported by dj-stripe, a corresponding model is attached to this Charge via a foreign key matching this field.', max_length=20, null=True),
),
migrations.AddField(
model_name='charge',
Expand Down Expand Up @@ -779,7 +779,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='transfer',
name='source_type',
field=djstripe.fields.StripeCharField(choices=[('card', 'Card'), ('bank_account', 'Bank Account'), ('bitcoin_reciever', 'Bitcoin Reciever'), ('alipay_account', 'Alipay Account')], default='unknown', help_text='The source balance from which this transfer came.', max_length=16),
field=djstripe.fields.StripeCharField(choices=[('alipay_account', 'Alipay account'), ('bank_account', 'Bank account'), ('bitcoin_receiver', 'Bitcoin receiver'), ('card', 'Card')], default='unknown', help_text='The source balance from which this transfer came.', max_length=16),
preserve_default=False,
),
migrations.AddField(
Expand Down Expand Up @@ -1004,7 +1004,7 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='plan',
name='interval',
field=djstripe.fields.StripeCharField(choices=[('day', 'Day'), ('week', 'Week'), ('month', 'Month'), ('year', 'Year')], help_text='The frequency with which a subscription should be billed.', max_length=5),
field=djstripe.fields.StripeCharField(choices=[('day', 'Day'), ('month', 'Month'), ('week', 'Week'), ('year', 'Year')], help_text='The frequency with which a subscription should be billed.', max_length=5),
),
migrations.AlterField(
model_name='plan',
Expand Down Expand Up @@ -2154,42 +2154,42 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='card',
name='address_line1_check',
field=djstripe.fields.StripeCharField(choices=[('pass', 'Pass'), ('fail', 'Fail'), ('unavailable', 'Unavailable'), ('unchecked', 'Unchecked')], help_text='If ``address_line1`` was provided, results of the check.', max_length=11, null=True),
field=djstripe.fields.StripeCharField(choices=[('fail', 'Fail'), ('pass', 'Pass'), ('unavailable', 'Unavailable'), ('unchecked', 'Unchecked')], help_text='If ``address_line1`` was provided, results of the check.', max_length=11, null=True),
),
migrations.AlterField(
model_name='card',
name='address_zip_check',
field=djstripe.fields.StripeCharField(choices=[('pass', 'Pass'), ('fail', 'Fail'), ('unavailable', 'Unavailable'), ('unchecked', 'Unchecked')], help_text='If ``address_zip`` was provided, results of the check.', max_length=11, null=True),
field=djstripe.fields.StripeCharField(choices=[('fail', 'Fail'), ('pass', 'Pass'), ('unavailable', 'Unavailable'), ('unchecked', 'Unchecked')], help_text='If ``address_zip`` was provided, results of the check.', max_length=11, null=True),
),
migrations.AlterField(
model_name='card',
name='cvc_check',
field=djstripe.fields.StripeCharField(choices=[('pass', 'Pass'), ('fail', 'Fail'), ('unavailable', 'Unavailable'), ('unchecked', 'Unchecked')], help_text='If a CVC was provided, results of the check.', max_length=11, null=True),
field=djstripe.fields.StripeCharField(choices=[('fail', 'Fail'), ('pass', 'Pass'), ('unavailable', 'Unavailable'), ('unchecked', 'Unchecked')], help_text='If a CVC was provided, results of the check.', max_length=11, null=True),
),
migrations.AlterField(
model_name='charge',
name='failure_code',
field=djstripe.fields.StripeCharField(choices=[('invalid_number', 'Invalid number'), ('invalid_expiry_month', 'Invalid expiration month'), ('invalid_expiry_year', 'Invalid expiration year'), ('invalid_cvc', 'Invalid security code'), ('invalid_swipe_data', 'Invalid swipe data'), ('incorrect_number', 'Incorrect number'), ('expired_card', 'Expired card'), ('incorrect_cvc', 'Incorrect security code'), ('incorrect_zip', 'ZIP code failed validation'), ('card_declined', 'Card was declined'), ('missing', 'No card being charged'), ('processing_error', 'Processing error')], help_text='Error code explaining reason for charge failure if available.', max_length=30, null=True),
field=djstripe.fields.StripeCharField(choices=[('card_declined', 'Card was declined'), ('expired_card', 'Expired card'), ('incorrect_cvc', 'Incorrect security code'), ('incorrect_number', 'Incorrect number'), ('incorrect_zip', 'ZIP code failed validation'), ('invalid_cvc', 'Invalid security code'), ('invalid_expiry_month', 'Invalid expiration month'), ('invalid_expiry_year', 'Invalid expiration year'), ('invalid_number', 'Invalid number'), ('invalid_swipe_data', 'Invalid swipe data'), ('missing', 'No card being charged'), ('processing_error', 'Processing error')], help_text='Error code explaining reason for charge failure if available.', max_length=30, null=True),
),
migrations.AlterField(
model_name='charge',
name='source_type',
field=djstripe.fields.StripeCharField(choices=[('card', 'Card'), ('bank_account', 'Bank account'), ('bitcoin_receiver', 'Bitcoin receiver'), ('alipay_account', 'Alipay account')], help_text='The payment source type. If the payment source is supported by dj-stripe, a corresponding model is attached to this Charge via a foreign key matching this field.', max_length=20, null=True),
field=djstripe.fields.StripeCharField(choices=[('alipay_account', 'Alipay account'), ('bank_account', 'Bank account'), ('bitcoin_receiver', 'Bitcoin receiver'), ('card', 'Card')], help_text='The payment source type. If the payment source is supported by dj-stripe, a corresponding model is attached to this Charge via a foreign key matching this field.', max_length=20, null=True),
),
migrations.AlterField(
model_name='charge',
name='status',
field=djstripe.fields.StripeCharField(choices=[('succeeded', 'Succeeded'), ('pending', 'Pending'), ('failed', 'Failed')], help_text='The status of the payment.', max_length=10),
field=djstripe.fields.StripeCharField(choices=[('failed', 'Failed'), ('pending', 'Pending'), ('succeeded', 'Succeeded')], help_text='The status of the payment.', max_length=10),
),
migrations.AlterField(
model_name='coupon',
name='duration',
field=djstripe.fields.StripeCharField(choices=[('once', 'Once'), ('repeating', 'Multi-month'), ('forever', 'Forever')], help_text='Describes how long a customer who applies this coupon will get the discount.', max_length=9),
field=djstripe.fields.StripeCharField(choices=[('forever', 'Forever'), ('once', 'Once'), ('repeating', 'Multi-month')], help_text='Describes how long a customer who applies this coupon will get the discount.', max_length=9),
),
migrations.AlterField(
model_name='subscription',
name='status',
field=djstripe.fields.StripeCharField(choices=[('trialing', 'Trialing'), ('active', 'Active'), ('past_due', 'Past due'), ('canceled', 'Canceled'), ('unpaid', 'Unpaid')], help_text='The status of this subscription.', max_length=8),
field=djstripe.fields.StripeCharField(choices=[('active', 'Active'), ('canceled', 'Canceled'), ('past_due', 'Past due'), ('trialing', 'Trialing'), ('unpaid', 'Unpaid')], help_text='The status of this subscription.', max_length=8),
),
migrations.AlterField(
model_name='transfer',
Expand All @@ -2199,11 +2199,11 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='transfer',
name='source_type',
field=djstripe.fields.StripeCharField(choices=[('card', 'Card'), ('bank_account', 'Bank account'), ('bitcoin_receiver', 'Bitcoin receiver'), ('alipay_account', 'Alipay account')], help_text='The source balance from which this transfer came.', max_length=16),
field=djstripe.fields.StripeCharField(choices=[('alipay_account', 'Alipay account'), ('bank_account', 'Bank account'), ('bitcoin_receiver', 'Bitcoin receiver'), ('card', 'Card')], help_text='The source balance from which this transfer came.', max_length=16),
),
migrations.AlterField(
model_name='transfer',
name='status',
field=djstripe.fields.StripeCharField(choices=[('paid', 'Paid'), ('pending', 'Pending'), ('in_transit', 'In transit'), ('canceled', 'Canceled'), ('failed', 'Failed')], help_text='The current status of the transfer. A transfer will be pending until it is submitted to the bank, at which point it becomes in_transit. It will then change to paid if the transaction goes through. If it does not go through successfully, its status will change to failed or canceled.', max_length=10),
field=djstripe.fields.StripeCharField(choices=[('canceled', 'Canceled'), ('failed', 'Failed'), ('in_transit', 'In transit'), ('paid', 'Paid'), ('pending', 'Pending')], help_text='The current status of the transfer. A transfer will be pending until it is submitted to the bank, at which point it becomes in_transit. It will then change to paid if the transaction goes through. If it does not go through successfully, its status will change to failed or canceled.', max_length=10),
),
]

0 comments on commit 2306992

Please sign in to comment.