<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . translation import pgettext_lazy <EOL> from django_prices . forms import PriceField <EOL> from payments import PaymentError <EOL> from payments . models import PAYMENT_STATUS_CHOICES <EOL> from satchless . item import InsufficientStock <EOL> from ... cart . forms import QuantityField <EOL> from ... discount . models import Voucher <EOL> from ... order import Status <EOL> from ... order . models import DeliveryGroup , Order , OrderedItem , OrderNote <EOL> from ... product . models import ProductVariant , Stock <EOL> class OrderNoteForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = OrderNote <EOL> fields = [ '<STR_LIT:content>' ] <EOL> widgets = { '<STR_LIT:content>' : forms . Textarea ( { <EOL> '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( OrderNoteForm , self ) . __init__ ( * args , ** kwargs ) <EOL> class ManagePaymentForm ( forms . Form ) : <EOL> amount = PriceField ( max_digits = <NUM_LIT:12> , decimal_places = <NUM_LIT:2> , <EOL> currency = settings . DEFAULT_CURRENCY ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . payment = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( ManagePaymentForm , self ) . __init__ ( * args , ** kwargs ) <EOL> class CapturePaymentForm ( ManagePaymentForm ) : <EOL> def clean ( self ) : <EOL> if self . payment . status != '<STR_LIT>' : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) ) <EOL> def capture ( self ) : <EOL> amount = self . cleaned_data [ '<STR_LIT>' ] <EOL> try : <EOL> self . payment . capture ( amount . gross ) <EOL> except ( PaymentError , ValueError ) as e : <EOL> self . add_error ( None , _ ( '<STR_LIT>' ) % e . message ) <EOL> return False <EOL> return True <EOL> class RefundPaymentForm ( ManagePaymentForm ) : <EOL> def clean ( self ) : <EOL> if self . payment . status != '<STR_LIT>' : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) ) <EOL> def refund ( self ) : <EOL> amount = self . cleaned_data [ '<STR_LIT>' ] <EOL> try : <EOL> self . payment . refund ( amount . gross ) <EOL> except ( PaymentError , ValueError ) as e : <EOL> self . add_error ( None , _ ( '<STR_LIT>' ) % e . message ) <EOL> return False <EOL> return True <EOL> class ReleasePaymentForm ( forms . Form ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . payment = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( ReleasePaymentForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self ) : <EOL> if self . payment . status != '<STR_LIT>' : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) ) <EOL> def release ( self ) : <EOL> try : <EOL> self . payment . release ( ) <EOL> except ( PaymentError , ValueError ) as e : <EOL> self . add_error ( None , _ ( '<STR_LIT>' ) % e . message ) <EOL> return False <EOL> return True <EOL> class MoveItemsForm ( forms . Form ) : <EOL> quantity = QuantityField ( label = _ ( '<STR_LIT>' ) ) <EOL> target_group = forms . ChoiceField ( label = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . item = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( MoveItemsForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs . update ( { <EOL> '<STR_LIT>' : self . item . quantity , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . fields [ '<STR_LIT>' ] . choices = self . get_delivery_group_choices ( ) <EOL> def get_delivery_group_choices ( self ) : <EOL> group = self . item . delivery_group <EOL> groups = group . order . groups . exclude ( pk = group . pk ) . exclude ( <EOL> status = '<STR_LIT>' ) <EOL> choices = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] <EOL> choices . extend ( [ ( g . pk , str ( g ) ) for g in groups ] ) <EOL> return choices <EOL> def move_items ( self ) : <EOL> how_many = self . cleaned_data [ '<STR_LIT>' ] <EOL> choice = self . cleaned_data [ '<STR_LIT>' ] <EOL> old_group = self . item . delivery_group <EOL> if choice == '<STR_LIT>' : <EOL> target_group = old_group . order . groups . create ( <EOL> status = old_group . status , <EOL> shipping_method_name = old_group . shipping_method_name ) <EOL> else : <EOL> target_group = DeliveryGroup . objects . get ( pk = choice ) <EOL> OrderedItem . objects . move_to_group ( self . item , target_group , how_many ) <EOL> return target_group <EOL> class CancelItemsForm ( forms . Form ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . item = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( CancelItemsForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def cancel_item ( self ) : <EOL> if self . item . stock : <EOL> Stock . objects . deallocate_stock ( self . item . stock , self . item . quantity ) <EOL> order = self . item . delivery_group . order <EOL> OrderedItem . objects . remove_empty_groups ( self . item , force = True ) <EOL> Order . objects . recalculate_order ( order ) <EOL> class ChangeQuantityForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = OrderedItem <EOL> fields = [ '<STR_LIT>' ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ChangeQuantityForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . initial_quantity = self . instance . quantity <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs . update ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . fields [ '<STR_LIT>' ] . initial = self . initial_quantity <EOL> def clean_quantity ( self ) : <EOL> quantity = self . cleaned_data [ '<STR_LIT>' ] <EOL> variant = get_object_or_404 ( <EOL> ProductVariant , sku = self . instance . product_sku ) <EOL> try : <EOL> variant . check_quantity ( quantity ) <EOL> except InsufficientStock as e : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) % { <EOL> '<STR_LIT>' : e . item . get_stock_quantity ( ) } ) <EOL> return quantity <EOL> def save ( self ) : <EOL> quantity = self . cleaned_data [ '<STR_LIT>' ] <EOL> stock = self . instance . stock <EOL> if stock is not None : <EOL> delta = quantity - self . initial_quantity <EOL> Stock . objects . allocate_stock ( stock , delta ) <EOL> self . instance . change_quantity ( quantity ) <EOL> Order . objects . recalculate_order ( self . instance . delivery_group . order ) <EOL> class ShipGroupForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = DeliveryGroup <EOL> fields = [ '<STR_LIT>' ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ShipGroupForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs . update ( <EOL> { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> def clean ( self ) : <EOL> if self . instance . status != '<STR_LIT>' : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) , <EOL> code = '<STR_LIT>' ) <EOL> def save ( self ) : <EOL> order = self . instance . order <EOL> for line in self . instance . items . all ( ) : <EOL> stock = line . stock <EOL> if stock is not None : <EOL> Stock . objects . decrease_stock ( stock , line . quantity ) <EOL> self . instance . change_status ( '<STR_LIT>' ) <EOL> statuses = [ g . status for g in order . groups . all ( ) ] <EOL> if '<STR_LIT>' in statuses and '<STR_LIT>' not in statuses : <EOL> order . change_status ( '<STR_LIT>' ) <EOL> class CancelGroupForm ( forms . Form ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . delivery_group = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( CancelGroupForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def cancel_group ( self ) : <EOL> for line in self . delivery_group : <EOL> if line . stock : <EOL> Stock . objects . deallocate_stock ( line . stock , line . quantity ) <EOL> self . delivery_group . status = Status . CANCELLED <EOL> self . delivery_group . save ( ) <EOL> other_groups = self . delivery_group . order . groups . all ( ) <EOL> statuses = set ( other_groups . values_list ( '<STR_LIT:status>' , flat = True ) ) <EOL> if statuses == { Status . CANCELLED } : <EOL> self . delivery_group . order . status = Status . CANCELLED <EOL> self . delivery_group . order . save ( update_fields = [ '<STR_LIT:status>' ] ) <EOL> class CancelOrderForm ( forms . Form ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . order = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( CancelOrderForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self ) : <EOL> data = super ( CancelOrderForm , self ) . clean ( ) <EOL> if not self . order . can_cancel ( ) : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> return data <EOL> def cancel_order ( self ) : <EOL> for group in self . order . groups . all ( ) : <EOL> group_form = CancelGroupForm ( delivery_group = group ) <EOL> group_form . cancel_group ( ) <EOL> class RemoveVoucherForm ( forms . Form ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . order = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( RemoveVoucherForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self ) : <EOL> data = super ( RemoveVoucherForm , self ) . clean ( ) <EOL> if not self . order . voucher : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> return data <EOL> def remove_voucher ( self ) : <EOL> self . order . discount_amount = <NUM_LIT:0> <EOL> self . order . discount_name = '<STR_LIT>' <EOL> voucher = self . order . voucher <EOL> Voucher . objects . decrease_usage ( voucher ) <EOL> self . order . voucher = None <EOL> Order . objects . recalculate_order ( self . order ) <EOL> ORDER_STATUS_CHOICES = [ ( '<STR_LIT>' , pgettext_lazy ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) ] + Status . CHOICES <EOL> PAYMENT_STATUS_CHOICES = ( ( '<STR_LIT>' , pgettext_lazy ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) , ) + PAYMENT_STATUS_CHOICES <EOL> class OrderFilterForm ( forms . Form ) : <EOL> status = forms . ChoiceField ( choices = ORDER_STATUS_CHOICES ) <EOL> class PaymentFilterForm ( forms . Form ) : <EOL> status = forms . ChoiceField ( choices = PAYMENT_STATUS_CHOICES ) </s>
