<s> from django import forms <EOL> from django . contrib import messages <EOL> from django . core . exceptions import ValidationError <EOL> from django . db . models import ObjectDoesNotExist <EOL> from django . shortcuts import get_object_or_404 , redirect , render_to_response <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views import generic <EOL> from plata . contact . models import Contact <EOL> from plata . discount . models import Discount <EOL> from plata . shop . views import Shop <EOL> from plata . shop . models import Order <EOL> from staggered . models import Product <EOL> shop = Shop ( Contact , Order , Discount ) <EOL> product_list = generic . ListView . as_view ( <EOL> queryset = Product . objects . filter ( is_active = True ) , <EOL> template_name = '<STR_LIT>' , <EOL> ) <EOL> class OrderItemForm ( forms . Form ) : <EOL> quantity = forms . IntegerField ( label = _ ( '<STR_LIT>' ) , initial = <NUM_LIT:1> , <EOL> min_value = <NUM_LIT:1> , max_value = <NUM_LIT:100> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . product = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( OrderItemForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self ) : <EOL> data = super ( OrderItemForm , self ) . clean ( ) <EOL> try : <EOL> self . product . get_price ( ) <EOL> except ObjectDoesNotExist : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> return data <EOL> def product_detail ( request , object_id ) : <EOL> product = get_object_or_404 ( Product . objects . filter ( is_active = True ) , pk = object_id ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = OrderItemForm ( request . POST , product = product ) <EOL> if form . is_valid ( ) : <EOL> order = shop . order_from_request ( request , create = True ) <EOL> try : <EOL> order . modify_item ( product , <EOL> relative = form . cleaned_data . get ( '<STR_LIT>' ) , <EOL> ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> except ValidationError , e : <EOL> if e . code == '<STR_LIT>' : <EOL> [ messages . error ( request , msg ) for msg in e . messages ] <EOL> else : <EOL> raise <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = OrderItemForm ( product = product ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT:object>' : product , <EOL> '<STR_LIT>' : form , <EOL> } , context_instance = RequestContext ( request ) ) </s>
