<s> import types , datetime <EOL> from django import forms <EOL> from django . contrib . localflavor . us . forms import USZipCodeField <EOL> from django . utils . translation import ugettext as _ <EOL> from . settings import STRIPE_PLAN_CHOICES , STRIPE_CUSTOMER_DESCRIPTION <EOL> from . shortcuts import stripe <EOL> FORM_PREIX = '<STR_LIT>' <EOL> CURRENT_YEAR = datetime . date . today ( ) . year <EOL> MONTH_CHOICES = [ ( i , '<STR_LIT>' % i ) for i in xrange ( <NUM_LIT:1> , <NUM_LIT> ) ] <EOL> YEAR_CHOICES = [ ( i , i ) for i in range ( CURRENT_YEAR , CURRENT_YEAR + <NUM_LIT:10> ) ] <EOL> def make_widget_anonymous ( widget ) : <EOL> def _anonymous_render ( instance , name , value , attrs = None ) : <EOL> return instance . _orig_render ( '<STR_LIT>' , value , attrs ) <EOL> widget . _orig_render = widget . render <EOL> widget . render = types . MethodType ( _anonymous_render , widget ) <EOL> return widget <EOL> class CardForm ( forms . Form ) : <EOL> number = forms . CharField ( label = _ ( "<STR_LIT>" ) ) <EOL> exp_month = forms . CharField ( label = _ ( "<STR_LIT>" ) , widget = forms . Select ( choices = MONTH_CHOICES ) ) <EOL> exp_year = forms . CharField ( label = _ ( "<STR_LIT>" ) , widget = forms . Select ( choices = YEAR_CHOICES ) ) <EOL> def get_cvc_field ( self ) : <EOL> return forms . CharField ( label = _ ( "<STR_LIT>" ) ) <EOL> def get_address_line1_field ( self ) : <EOL> return forms . CharField ( label = _ ( "<STR_LIT>" ) ) <EOL> def get_address_zip_field ( self ) : <EOL> return USZipCodeField ( label = _ ( "<STR_LIT>" ) ) <EOL> def __init__ ( self , validate_cvc = True , validate_address = False , prefix = FORM_PREIX , * args , ** kwargs ) : <EOL> super ( CardForm , self ) . __init__ ( prefix = prefix , * args , ** kwargs ) <EOL> if validate_cvc : <EOL> self . fields [ '<STR_LIT>' ] = self . get_cvc_field ( ) <EOL> if validate_address : <EOL> self . fields [ '<STR_LIT>' ] = self . get_address_line1_field ( ) <EOL> self . fields [ '<STR_LIT>' ] = self . get_address_zip_field ( ) <EOL> class AnonymousCardForm ( CardForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AnonymousCardForm , self ) . __init__ ( * args , ** kwargs ) <EOL> for key in self . fields . keys ( ) : <EOL> make_widget_anonymous ( self . fields [ key ] . widget ) <EOL> class CardTokenForm ( AnonymousCardForm ) : <EOL> last4 = forms . CharField ( min_length = <NUM_LIT:4> , max_length = <NUM_LIT:4> , required = False , widget = forms . HiddenInput ( ) ) <EOL> token = forms . CharField ( required = False , widget = forms . HiddenInput ( ) ) <EOL> def __init__ ( self , prefix = FORM_PREIX , * args , ** kwargs ) : <EOL> super ( CardTokenForm , self ) . __init__ ( prefix = prefix , * args , ** kwargs ) <EOL> def clean ( self ) : <EOL> if not self . cleaned_data [ '<STR_LIT>' ] or not self . cleaned_data [ '<STR_LIT>' ] : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> class CustomerForm ( CardTokenForm ) : <EOL> plan = forms . CharField ( widget = forms . Select ( choices = STRIPE_PLAN_CHOICES ) ) <EOL> def get_customer_description ( self , user ) : <EOL> return STRIPE_CUSTOMER_DESCRIPTION ( user ) <EOL> def save ( self , user ) : <EOL> return stripe . Customer . create ( <EOL> description = self . get_customer_description ( user ) , <EOL> card = self . cleaned_data [ '<STR_LIT>' ] , <EOL> plan = self . cleaned_data [ '<STR_LIT>' ] <EOL> ) </s>
