<s> import datetime <EOL> from django . db import IntegrityError <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response , redirect <EOL> from django . template import RequestContext <EOL> import stripe <EOL> from monospace . forms import * <EOL> from monospace . models import * <EOL> import monospace . settings as settings <EOL> stripe . api_key = settings . STRIPE_SECRET <EOL> def soon ( ) : <EOL> soon = datetime . date . today ( ) + datetime . timedelta ( days = <NUM_LIT:30> ) <EOL> return { '<STR_LIT>' : soon . month , '<STR_LIT>' : soon . year } <EOL> def home ( request ) : <EOL> uid = request . session . get ( '<STR_LIT:user>' ) <EOL> if uid is None : <EOL> return render_to_response ( '<STR_LIT>' ) <EOL> else : <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT:user>' : User . objects . get ( pk = uid ) } ) <EOL> def sign_in ( request ) : <EOL> user = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = SignInForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> results = User . objects . filter ( email = form . cleaned_data [ '<STR_LIT:email>' ] ) <EOL> if len ( results ) == <NUM_LIT:1> : <EOL> if results [ <NUM_LIT:0> ] . check_password ( form . cleaned_data [ '<STR_LIT:password>' ] ) : <EOL> request . session [ '<STR_LIT:user>' ] = results [ <NUM_LIT:0> ] . pk <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> else : <EOL> form . addError ( '<STR_LIT>' ) <EOL> else : <EOL> form . addError ( '<STR_LIT>' ) <EOL> else : <EOL> form = SignInForm ( ) <EOL> print form . non_field_errors ( ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:user>' : user <EOL> } , <EOL> context_instance = RequestContext ( request ) <EOL> ) <EOL> def sign_out ( request ) : <EOL> del request . session [ '<STR_LIT:user>' ] <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> def register ( request ) : <EOL> user = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = UserForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> customer = stripe . Customer . create ( <EOL> description = form . cleaned_data [ '<STR_LIT:email>' ] , <EOL> card = form . cleaned_data [ '<STR_LIT>' ] , <EOL> plan = '<STR_LIT>' <EOL> ) <EOL> user = User ( <EOL> name = form . cleaned_data [ '<STR_LIT:name>' ] , <EOL> email = form . cleaned_data [ '<STR_LIT:email>' ] , <EOL> last_4_digits = form . cleaned_data [ '<STR_LIT>' ] , <EOL> stripe_id = customer . id <EOL> ) <EOL> user . set_password ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> try : <EOL> user . save ( ) <EOL> except IntegrityError : <EOL> form . addError ( user . email + '<STR_LIT>' ) <EOL> else : <EOL> request . session [ '<STR_LIT:user>' ] = user . pk <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> else : <EOL> form = UserForm ( ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : range ( <NUM_LIT:1> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : settings . STRIPE_PUBLISHABLE , <EOL> '<STR_LIT>' : soon ( ) , <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : range ( <NUM_LIT> , <NUM_LIT> ) , <EOL> } , <EOL> context_instance = RequestContext ( request ) <EOL> ) <EOL> def edit ( request ) : <EOL> uid = request . session . get ( '<STR_LIT:user>' ) <EOL> if uid is None : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> user = User . objects . get ( pk = uid ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> customer = stripe . Customer . retrieve ( user . stripe_id ) <EOL> customer . card = form . cleaned_data [ '<STR_LIT>' ] <EOL> customer . save ( ) <EOL> user . last_4_digits = form . cleaned_data [ '<STR_LIT>' ] <EOL> user . stripe_id = customer . id <EOL> user . save ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> else : <EOL> form = CardForm ( ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : settings . STRIPE_PUBLISHABLE , <EOL> '<STR_LIT>' : soon ( ) , <EOL> '<STR_LIT>' : range ( <NUM_LIT:1> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : range ( <NUM_LIT> , <NUM_LIT> ) <EOL> } , <EOL> context_instance = RequestContext ( request ) <EOL> ) </s>
