<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . db . models import Q <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> class CustomerSearchForm ( forms . Form ) : <EOL> email = forms . CharField ( required = False , label = _ ( '<STR_LIT>' ) ) <EOL> name = forms . CharField ( required = False , label = _ ( '<STR_LIT:Name>' ) ) <EOL> order_status = forms . BooleanField ( required = False , initial = True , <EOL> label = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . queryset = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( CustomerSearchForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self ) : <EOL> data = self . cleaned_data <EOL> for field in [ '<STR_LIT:email>' , '<STR_LIT:name>' ] : <EOL> data [ field ] = data [ field ] . strip ( ) <EOL> return data <EOL> def search ( self ) : <EOL> queryset = self . queryset <EOL> data = self . cleaned_data <EOL> if data [ '<STR_LIT:email>' ] : <EOL> queryset = queryset . filter ( email__icontains = data [ '<STR_LIT:email>' ] ) <EOL> if data [ '<STR_LIT:name>' ] : <EOL> parts = data [ '<STR_LIT:name>' ] . split ( ) <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> query = ( ( Q ( addresses__first_name__icontains = parts [ <NUM_LIT:0> ] ) | <EOL> Q ( addresses__last_name__icontains = parts [ <NUM_LIT:1> ] ) ) | <EOL> ( Q ( addresses__first_name__icontains = parts [ <NUM_LIT:1> ] ) | <EOL> Q ( addresses__last_name__icontains = parts [ <NUM_LIT:0> ] ) ) ) <EOL> else : <EOL> query = ( Q ( addresses__first_name__icontains = data [ '<STR_LIT:name>' ] ) | <EOL> Q ( addresses__last_name__icontains = data [ '<STR_LIT:name>' ] ) ) <EOL> queryset = queryset . filter ( query ) . distinct ( ) <EOL> if data [ '<STR_LIT>' ] : <EOL> open_order = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> queryset = queryset . filter ( orders__status__in = open_order ) <EOL> return queryset </s>
