<s> from django . contrib . admin . filterspecs import FilterSpec <EOL> from django . contrib . admin . options import IncorrectLookupParameters <EOL> from django . contrib . admin . util import quote , get_fields_from_path <EOL> from django . core . exceptions import SuspiciousOperation <EOL> from django . core . paginator import InvalidPage <EOL> from django . db import models <EOL> from django . utils . encoding import force_unicode , smart_str <EOL> from django . utils . translation import ugettext , ugettext_lazy <EOL> from django . utils . http import urlencode <EOL> import operator <EOL> MAX_SHOW_ALL_ALLOWED = <NUM_LIT:200> <EOL> ALL_VAR = '<STR_LIT:all>' <EOL> ORDER_VAR = '<STR_LIT:o>' <EOL> ORDER_TYPE_VAR = '<STR_LIT>' <EOL> PAGE_VAR = '<STR_LIT:p>' <EOL> SEARCH_VAR = '<STR_LIT:q>' <EOL> TO_FIELD_VAR = '<STR_LIT:t>' <EOL> IS_POPUP_VAR = '<STR_LIT>' <EOL> ERROR_FLAG = '<STR_LIT:e>' <EOL> EMPTY_CHANGELIST_VALUE = ugettext_lazy ( '<STR_LIT>' ) <EOL> def field_needs_distinct ( field ) : <EOL> if ( ( hasattr ( field , '<STR_LIT>' ) and <EOL> isinstance ( field . rel , models . ManyToManyRel ) ) or <EOL> ( isinstance ( field , models . related . RelatedObject ) and <EOL> not field . field . unique ) ) : <EOL> return True <EOL> return False <EOL> class ChangeList ( object ) : <EOL> def __init__ ( self , request , model , list_display , list_display_links , list_filter , date_hierarchy , search_fields , list_select_related , list_per_page , list_editable , model_admin ) : <EOL> self . model = model <EOL> self . opts = model . _meta <EOL> self . lookup_opts = self . opts <EOL> self . root_query_set = model_admin . queryset ( request ) <EOL> self . list_display = list_display <EOL> self . list_display_links = list_display_links <EOL> self . list_filter = list_filter <EOL> self . date_hierarchy = date_hierarchy <EOL> self . search_fields = search_fields <EOL> self . list_select_related = list_select_related <EOL> self . list_per_page = list_per_page <EOL> self . model_admin = model_admin <EOL> try : <EOL> self . page_num = int ( request . GET . get ( PAGE_VAR , <NUM_LIT:0> ) ) <EOL> except ValueError : <EOL> self . page_num = <NUM_LIT:0> <EOL> self . show_all = ALL_VAR in request . GET <EOL> self . is_popup = IS_POPUP_VAR in request . GET <EOL> self . to_field = request . GET . get ( TO_FIELD_VAR ) <EOL> self . params = dict ( request . GET . items ( ) ) <EOL> if PAGE_VAR in self . params : <EOL> del self . params [ PAGE_VAR ] <EOL> if ERROR_FLAG in self . params : <EOL> del self . params [ ERROR_FLAG ] <EOL> if self . is_popup : <EOL> self . list_editable = ( ) <EOL> else : <EOL> self . list_editable = list_editable <EOL> self . order_field , self . order_type = self . get_ordering ( ) <EOL> self . query = request . GET . get ( SEARCH_VAR , '<STR_LIT>' ) <EOL> self . query_set = self . get_query_set ( ) <EOL> self . get_results ( request ) <EOL> self . title = ( self . is_popup and ugettext ( '<STR_LIT>' ) % force_unicode ( self . opts . verbose_name ) or ugettext ( '<STR_LIT>' ) % force_unicode ( self . opts . verbose_name ) ) <EOL> self . filter_specs , self . has_filters = self . get_filters ( request ) <EOL> self . pk_attname = self . lookup_opts . pk . attname <EOL> def get_filters ( self , request ) : <EOL> filter_specs = [ ] <EOL> if self . list_filter : <EOL> for filter_name in self . list_filter : <EOL> field = get_fields_from_path ( self . model , filter_name ) [ - <NUM_LIT:1> ] <EOL> spec = FilterSpec . create ( field , request , self . params , <EOL> self . model , self . model_admin , <EOL> field_path = filter_name ) <EOL> if spec and spec . has_output ( ) : <EOL> filter_specs . append ( spec ) <EOL> return filter_specs , bool ( filter_specs ) <EOL> def get_query_string ( self , new_params = None , remove = None ) : <EOL> if new_params is None : new_params = { } <EOL> if remove is None : remove = [ ] <EOL> p = self . params . copy ( ) <EOL> for r in remove : <EOL> for k in p . keys ( ) : <EOL> if k . startswith ( r ) : <EOL> del p [ k ] <EOL> for k , v in new_params . items ( ) : <EOL> if v is None : <EOL> if k in p : <EOL> del p [ k ] <EOL> else : <EOL> p [ k ] = v <EOL> return '<STR_LIT>' % urlencode ( p ) <EOL> def get_results ( self , request ) : <EOL> paginator = self . model_admin . get_paginator ( request , self . query_set , self . list_per_page ) <EOL> result_count = paginator . count <EOL> if not self . query_set . query . where : <EOL> full_result_count = result_count <EOL> else : <EOL> full_result_count = self . root_query_set . count ( ) <EOL> can_show_all = result_count <= MAX_SHOW_ALL_ALLOWED <EOL> multi_page = result_count > self . list_per_page <EOL> if ( self . show_all and can_show_all ) or not multi_page : <EOL> result_list = self . query_set . _clone ( ) <EOL> else : <EOL> try : <EOL> result_list = paginator . page ( self . page_num + <NUM_LIT:1> ) . object_list <EOL> except InvalidPage : <EOL> raise IncorrectLookupParameters <EOL> self . result_count = result_count <EOL> self . full_result_count = full_result_count <EOL> self . result_list = result_list <EOL> self . can_show_all = can_show_all <EOL> self . multi_page = multi_page <EOL> self . paginator = paginator <EOL> def get_ordering ( self ) : <EOL> lookup_opts , params = self . lookup_opts , self . params <EOL> ordering = self . model_admin . ordering or lookup_opts . ordering or [ '<STR_LIT:->' + lookup_opts . pk . name ] <EOL> if ordering [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:->' ) : <EOL> order_field , order_type = ordering [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] , '<STR_LIT>' <EOL> else : <EOL> order_field , order_type = ordering [ <NUM_LIT:0> ] , '<STR_LIT>' <EOL> if ORDER_VAR in params : <EOL> try : <EOL> field_name = self . list_display [ int ( params [ ORDER_VAR ] ) ] <EOL> try : <EOL> f = lookup_opts . get_field ( field_name ) <EOL> except models . FieldDoesNotExist : <EOL> try : <EOL> if callable ( field_name ) : <EOL> attr = field_name <EOL> elif hasattr ( self . model_admin , field_name ) : <EOL> attr = getattr ( self . model_admin , field_name ) <EOL> else : <EOL> attr = getattr ( self . model , field_name ) <EOL> order_field = attr . admin_order_field <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> order_field = f . name <EOL> except ( IndexError , ValueError ) : <EOL> pass <EOL> if ORDER_TYPE_VAR in params and params [ ORDER_TYPE_VAR ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> order_type = params [ ORDER_TYPE_VAR ] <EOL> return order_field , order_type <EOL> def get_query_set ( self ) : <EOL> use_distinct = False <EOL> qs = self . root_query_set <EOL> lookup_params = self . params . copy ( ) <EOL> for i in ( ALL_VAR , ORDER_VAR , ORDER_TYPE_VAR , SEARCH_VAR , IS_POPUP_VAR , TO_FIELD_VAR ) : <EOL> if i in lookup_params : <EOL> del lookup_params [ i ] <EOL> for key , value in lookup_params . items ( ) : <EOL> if not isinstance ( key , str ) : <EOL> del lookup_params [ key ] <EOL> lookup_params [ smart_str ( key ) ] = value <EOL> if not use_distinct : <EOL> field_name = key . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> try : <EOL> f = self . lookup_opts . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] <EOL> except models . FieldDoesNotExist : <EOL> raise IncorrectLookupParameters <EOL> use_distinct = field_needs_distinct ( f ) <EOL> if key . endswith ( '<STR_LIT>' ) : <EOL> value = value . split ( '<STR_LIT:U+002C>' ) <EOL> lookup_params [ key ] = value <EOL> if key . endswith ( '<STR_LIT>' ) : <EOL> if value . lower ( ) in ( '<STR_LIT>' , '<STR_LIT:false>' ) : <EOL> value = False <EOL> else : <EOL> value = True <EOL> lookup_params [ key ] = value <EOL> if not self . model_admin . lookup_allowed ( key , value ) : <EOL> raise SuspiciousOperation ( <EOL> "<STR_LIT>" % key <EOL> ) <EOL> try : <EOL> qs = qs . filter ( ** lookup_params ) <EOL> except : <EOL> raise IncorrectLookupParameters <EOL> if not qs . query . select_related : <EOL> if self . list_select_related : <EOL> qs = qs . select_related ( ) <EOL> else : <EOL> for field_name in self . list_display : <EOL> try : <EOL> f = self . lookup_opts . get_field ( field_name ) <EOL> except models . FieldDoesNotExist : <EOL> pass <EOL> else : <EOL> if isinstance ( f . rel , models . ManyToOneRel ) : <EOL> qs = qs . select_related ( ) <EOL> break <EOL> if self . order_field : <EOL> qs = qs . order_by ( '<STR_LIT>' % ( ( self . order_type == '<STR_LIT>' and '<STR_LIT:->' or '<STR_LIT>' ) , self . order_field ) ) <EOL> def construct_search ( field_name ) : <EOL> if field_name . startswith ( '<STR_LIT>' ) : <EOL> return "<STR_LIT>" % field_name [ <NUM_LIT:1> : ] <EOL> elif field_name . startswith ( '<STR_LIT:=>' ) : <EOL> return "<STR_LIT>" % field_name [ <NUM_LIT:1> : ] <EOL> elif field_name . startswith ( '<STR_LIT:@>' ) : <EOL> return "<STR_LIT>" % field_name [ <NUM_LIT:1> : ] <EOL> else : <EOL> return "<STR_LIT>" % field_name <EOL> if self . search_fields and self . query : <EOL> orm_lookups = [ construct_search ( str ( search_field ) ) <EOL> for search_field in self . search_fields ] <EOL> for bit in self . query . split ( ) : <EOL> or_queries = [ models . Q ( ** { orm_lookup : bit } ) <EOL> for orm_lookup in orm_lookups ] <EOL> qs = qs . filter ( reduce ( operator . or_ , or_queries ) ) <EOL> if not use_distinct : <EOL> for search_spec in orm_lookups : <EOL> field_name = search_spec . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> f = self . lookup_opts . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] <EOL> if field_needs_distinct ( f ) : <EOL> use_distinct = True <EOL> break <EOL> if use_distinct : <EOL> return qs . distinct ( ) <EOL> else : <EOL> return qs <EOL> def url_for_result ( self , result ) : <EOL> return "<STR_LIT>" % quote ( getattr ( result , self . pk_attname ) ) </s>
