<s> from django . db import transaction <EOL> from django . http import Http404 <EOL> from django . shortcuts import redirect <EOL> from django . core . urlresolvers import reverse , resolve <EOL> from sellmo . core import chaining <EOL> from sellmo . core . exceptions import ViewNotImplemented <EOL> from sellmo . core . process . exceptions import ProcessStepNotFound , ProcessError <EOL> from . process import CheckoutProcess <EOL> from . routines import order_from_request , completed_order_from_request <EOL> @ chaining . define ( provides = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def checkout ( request , step = None , ** kwargs ) : <EOL> order = order_from_request ( request ) <EOL> if not order : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> data = request . POST if request . method == '<STR_LIT:POST>' else None <EOL> process = CheckoutProcess ( request , order ) <EOL> if step : <EOL> try : <EOL> process . step_to ( step ) <EOL> except ProcessStepNotFound : <EOL> step = None <EOL> except ProcessError as error : <EOL> raise Http404 ( error ) <EOL> if not step : <EOL> try : <EOL> process . step_to_latest ( ) <EOL> except ProcessError as error : <EOL> raise Http404 ( error ) <EOL> yield redirect ( <EOL> reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : process . current_step . key } , <EOL> current_app = resolve ( request . path ) . namespace <EOL> ) <EOL> ) <EOL> elif data : <EOL> with transaction . atomic ( ) : <EOL> success = process . feed ( data ) <EOL> if order . may_change : <EOL> order . calculate ( ) <EOL> if success : <EOL> order . save ( ) <EOL> if not process . is_completed ( ) : <EOL> yield redirect ( <EOL> reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : process . current_step . key } , <EOL> current_app = resolve ( request . path ) . namespace <EOL> ) <EOL> ) <EOL> if process . is_completed ( ) : <EOL> request . session [ '<STR_LIT>' ] = order . pk <EOL> yield redirect ( <EOL> reverse ( <EOL> '<STR_LIT>' , <EOL> current_app = resolve ( request . path ) . namespace <EOL> ) <EOL> ) <EOL> yield chaining . update ( <EOL> order = order , <EOL> process = process ) <EOL> if ( yield chaining . forward ) . result is None : <EOL> context = { '<STR_LIT>' : order } <EOL> try : <EOL> result = process . render ( request , context = context ) <EOL> except ProcessError as error : <EOL> raise Http404 ( error ) <EOL> if result is None : <EOL> raise ViewNotImplemented <EOL> @ chaining . define ( provides = [ '<STR_LIT>' ] ) <EOL> def complete ( request , order = None , context = None , ** kwargs ) : <EOL> order = completed_order_from_request ( request ) <EOL> if order is None : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> yield chaining . update ( order = order ) <EOL> result = ( yield chaining . forward ) . result <EOL> if result is None : <EOL> raise ViewNotImplemented </s>
