<s> from __future__ import unicode_literals <EOL> from django . db . models import Count , Max <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . forms import CustomerSearchForm <EOL> from . . views import staff_member_required <EOL> from ... core . utils import get_paginator_items <EOL> from ... userprofile . models import User <EOL> @ staff_member_required <EOL> def customer_list ( request ) : <EOL> customers = ( <EOL> User . objects <EOL> . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> . select_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> . annotate ( <EOL> num_orders = Count ( '<STR_LIT>' , distinct = True ) , <EOL> last_order = Max ( '<STR_LIT>' , distinct = True ) ) ) <EOL> form = CustomerSearchForm ( request . GET or None , queryset = customers ) <EOL> form_values = [ ( field . name , field . value ( ) or '<STR_LIT>' ) for field in form ] <EOL> if form . is_valid ( ) : <EOL> customers = form . search ( ) <EOL> else : <EOL> customers = customers . filter ( <EOL> orders__status__in = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> title = _ ( '<STR_LIT>' ) % len ( customers ) <EOL> customers = get_paginator_items ( customers , <NUM_LIT:30> , request . GET . get ( '<STR_LIT>' ) ) <EOL> ctx = { '<STR_LIT>' : customers , '<STR_LIT>' : form , '<STR_LIT:title>' : title , <EOL> '<STR_LIT>' : form_values } <EOL> return TemplateResponse ( request , '<STR_LIT>' , ctx ) <EOL> @ staff_member_required <EOL> def customer_details ( request , pk ) : <EOL> queryset = User . objects . prefetch_related ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) . select_related ( '<STR_LIT>' ) <EOL> customer = get_object_or_404 ( queryset , pk = pk ) <EOL> customer_orders = customer . orders . all ( ) <EOL> ctx = { '<STR_LIT>' : customer , '<STR_LIT>' : customer_orders } <EOL> return TemplateResponse ( request , '<STR_LIT>' , ctx ) </s>
