<s> from django . contrib import admin <EOL> from django . contrib . admin . options import ModelAdmin <EOL> from django . forms . models import BaseInlineFormSet <EOL> from django . template . loader import render_to_string <EOL> from django . utils . safestring import mark_safe <EOL> from . models import Order , OrderedItem , Payment <EOL> def format_address ( address ) : <EOL> address = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT:address>' : address } ) <EOL> return address . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> class OrderModelAdmin ( ModelAdmin ) : <EOL> def get_inline_instances ( self , request , order = None ) : <EOL> inlines = super ( OrderModelAdmin , self ) . get_inline_instances ( request , <EOL> order ) <EOL> if order : <EOL> inlines . extend ( [ <EOL> DeliveryInlineAdmin ( self . model , self . admin_site , group ) <EOL> for group in order ] ) <EOL> return inlines <EOL> class PaymentInlineAdmin ( admin . TabularInline ) : <EOL> model = Payment <EOL> extra = <NUM_LIT:0> <EOL> readonly_fields = [ '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> exclude = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> can_delete = False <EOL> class DeliveryFormSet ( BaseInlineFormSet ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = self . instance_obj <EOL> super ( DeliveryFormSet , self ) . __init__ ( * args , ** kwargs ) <EOL> class DeliveryInlineAdmin ( admin . TabularInline ) : <EOL> model = OrderedItem <EOL> formset = DeliveryFormSet <EOL> def __init__ ( self , parent_model , admin_site , delivery ) : <EOL> self . delivery = delivery <EOL> delivery_class = delivery . __class__ <EOL> if hasattr ( delivery , '<STR_LIT:address>' ) : <EOL> address = format_address ( delivery . address ) <EOL> self . verbose_name_plural = ( <EOL> mark_safe ( <EOL> '<STR_LIT>' % ( <EOL> delivery , <EOL> delivery . price . gross , <EOL> delivery . price . currency , <EOL> address ) ) ) <EOL> if hasattr ( delivery , '<STR_LIT:email>' ) : <EOL> self . verbose_name_plural = ( <EOL> mark_safe ( <EOL> '<STR_LIT>' % ( <EOL> delivery , <EOL> delivery . price . gross , <EOL> delivery . price . currency , <EOL> delivery . email ) ) ) <EOL> super ( DeliveryInlineAdmin , self ) . __init__ ( delivery_class , admin_site ) <EOL> def get_formset ( self , request , obj = None , ** kwargs ) : <EOL> obj = obj if not self . delivery else self . delivery <EOL> formset = super ( DeliveryInlineAdmin , self ) . get_formset ( request , obj , <EOL> ** kwargs ) <EOL> formset . instance_obj = obj <EOL> return formset <EOL> class OrderAdmin ( OrderModelAdmin ) : <EOL> inlines = [ PaymentInlineAdmin ] <EOL> exclude = [ '<STR_LIT>' ] <EOL> readonly_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> list_display = [ '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT:user>' ] <EOL> def customer ( self , obj ) : <EOL> return format_address ( obj . billing_address ) <EOL> customer . allow_tags = True <EOL> def total ( self , obj ) : <EOL> total = obj . get_total ( ) <EOL> return '<STR_LIT>' % ( total . gross , total . currency ) <EOL> total . short_description = '<STR_LIT>' <EOL> def has_add_permission ( self , request , obj = None ) : <EOL> return False <EOL> admin . site . register ( Order , OrderAdmin ) </s>
