<s> import django <EOL> from django . db import models <EOL> from django . db . models . sql . query import LOOKUP_SEP <EOL> from django . db . models . deletion import Collector <EOL> from django . db . models . fields . related import ForeignObjectRel <EOL> from django . forms . forms import pretty_name <EOL> from django . utils import formats <EOL> from django . utils . html import escape <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . text import capfirst <EOL> from django . utils . encoding import force_unicode , smart_unicode , smart_str <EOL> from django . utils . translation import ungettext <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf import settings <EOL> from django . forms import Media <EOL> from django . utils . translation import get_language <EOL> from django . contrib . admin . utils import label_for_field , help_text_for_field <EOL> import datetime <EOL> import decimal <EOL> if '<STR_LIT>' in settings . INSTALLED_APPS : <EOL> from django . contrib . staticfiles . templatetags . staticfiles import static <EOL> else : <EOL> from django . templatetags . static import static <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> from django . utils import simplejson as json <EOL> try : <EOL> from django . utils . timezone import template_localtime as tz_localtime <EOL> except ImportError : <EOL> from django . utils . timezone import localtime as tz_localtime <EOL> try : <EOL> from django . contrib . auth import get_user_model <EOL> User = get_user_model ( ) <EOL> username_field = User . USERNAME_FIELD <EOL> except Exception : <EOL> from django . contrib . auth . models import User <EOL> username_field = '<STR_LIT:username>' <EOL> def xstatic ( * tags ) : <EOL> from vendors import vendors <EOL> node = vendors <EOL> fs = [ ] <EOL> lang = get_language ( ) <EOL> for tag in tags : <EOL> try : <EOL> for p in tag . split ( '<STR_LIT:.>' ) : <EOL> node = node [ p ] <EOL> except Exception , e : <EOL> if tag . startswith ( '<STR_LIT>' ) : <EOL> file_type = tag . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> if file_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> node = "<STR_LIT>" % ( file_type , tag ) <EOL> else : <EOL> raise e <EOL> else : <EOL> raise e <EOL> if type ( node ) in ( str , unicode ) : <EOL> files = node <EOL> else : <EOL> mode = '<STR_LIT>' <EOL> if not settings . DEBUG : <EOL> mode = getattr ( settings , '<STR_LIT>' , <EOL> False ) and '<STR_LIT>' or '<STR_LIT>' <EOL> if mode == '<STR_LIT>' and mode not in node : <EOL> mode = '<STR_LIT>' <EOL> if mode == '<STR_LIT>' and mode not in node : <EOL> mode = '<STR_LIT>' <EOL> files = node [ mode ] <EOL> files = type ( files ) in ( list , tuple ) and files or [ files , ] <EOL> fs . extend ( [ f % { '<STR_LIT>' : lang . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) } for f in files ] ) <EOL> return [ f . startswith ( '<STR_LIT>' ) and f or static ( f ) for f in fs ] <EOL> def vendor ( * tags ) : <EOL> media = Media ( ) <EOL> for tag in tags : <EOL> file_type = tag . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> files = xstatic ( tag ) <EOL> if file_type == '<STR_LIT>' : <EOL> media . add_js ( files ) <EOL> elif file_type == '<STR_LIT>' : <EOL> media . add_css ( { '<STR_LIT>' : files } ) <EOL> return media <EOL> def lookup_needs_distinct ( opts , lookup_path ) : <EOL> """<STR_LIT>""" <EOL> field_name = lookup_path . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> field = opts . get_field ( field_name ) <EOL> if ( ( hasattr ( field , '<STR_LIT>' ) and <EOL> isinstance ( field . rel , models . ManyToManyRel ) ) or <EOL> ( is_related_field ( field ) and <EOL> not field . field . unique ) ) : <EOL> return True <EOL> return False <EOL> def prepare_lookup_value ( key , value ) : <EOL> """<STR_LIT>""" <EOL> if key . endswith ( '<STR_LIT>' ) : <EOL> value = value . split ( '<STR_LIT:U+002C>' ) <EOL> if key . endswith ( '<STR_LIT>' ) and type ( value ) == str : <EOL> if value . lower ( ) in ( '<STR_LIT>' , '<STR_LIT:false>' ) : <EOL> value = False <EOL> else : <EOL> value = True <EOL> return value <EOL> def quote ( s ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( s , basestring ) : <EOL> return s <EOL> res = list ( s ) <EOL> for i in range ( len ( res ) ) : <EOL> c = res [ i ] <EOL> if c in """<STR_LIT>""" : <EOL> res [ i ] = '<STR_LIT>' % ord ( c ) <EOL> return '<STR_LIT>' . join ( res ) <EOL> def unquote ( s ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( s , basestring ) : <EOL> return s <EOL> mychr = chr <EOL> myatoi = int <EOL> list = s . split ( '<STR_LIT:_>' ) <EOL> res = [ list [ <NUM_LIT:0> ] ] <EOL> myappend = res . append <EOL> del list [ <NUM_LIT:0> ] <EOL> for item in list : <EOL> if item [ <NUM_LIT:1> : <NUM_LIT:2> ] : <EOL> try : <EOL> myappend ( mychr ( myatoi ( item [ : <NUM_LIT:2> ] , <NUM_LIT:16> ) ) + item [ <NUM_LIT:2> : ] ) <EOL> except ValueError : <EOL> myappend ( '<STR_LIT:_>' + item ) <EOL> else : <EOL> myappend ( '<STR_LIT:_>' + item ) <EOL> return "<STR_LIT>" . join ( res ) <EOL> def flatten_fieldsets ( fieldsets ) : <EOL> """<STR_LIT>""" <EOL> field_names = [ ] <EOL> for name , opts in fieldsets : <EOL> for field in opts [ '<STR_LIT>' ] : <EOL> if type ( field ) == tuple : <EOL> field_names . extend ( field ) <EOL> else : <EOL> field_names . append ( field ) <EOL> return field_names <EOL> class NestedObjects ( Collector ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( NestedObjects , self ) . __init__ ( * args , ** kwargs ) <EOL> self . edges = { } <EOL> self . protected = set ( ) <EOL> def add_edge ( self , source , target ) : <EOL> self . edges . setdefault ( source , [ ] ) . append ( target ) <EOL> def collect ( self , objs , source_attr = None , ** kwargs ) : <EOL> for obj in objs : <EOL> if source_attr and hasattr ( obj , source_attr ) : <EOL> self . add_edge ( getattr ( obj , source_attr ) , obj ) <EOL> else : <EOL> self . add_edge ( None , obj ) <EOL> try : <EOL> return super ( NestedObjects , self ) . collect ( objs , source_attr = source_attr , ** kwargs ) <EOL> except models . ProtectedError , e : <EOL> self . protected . update ( e . protected_objects ) <EOL> def related_objects ( self , related , objs ) : <EOL> qs = super ( NestedObjects , self ) . related_objects ( related , objs ) <EOL> return qs . select_related ( related . field . name ) <EOL> def _nested ( self , obj , seen , format_callback ) : <EOL> if obj in seen : <EOL> return [ ] <EOL> seen . add ( obj ) <EOL> children = [ ] <EOL> for child in self . edges . get ( obj , ( ) ) : <EOL> children . extend ( self . _nested ( child , seen , format_callback ) ) <EOL> if format_callback : <EOL> ret = [ format_callback ( obj ) ] <EOL> else : <EOL> ret = [ obj ] <EOL> if children : <EOL> ret . append ( children ) <EOL> return ret <EOL> def nested ( self , format_callback = None ) : <EOL> """<STR_LIT>""" <EOL> seen = set ( ) <EOL> roots = [ ] <EOL> for root in self . edges . get ( None , ( ) ) : <EOL> roots . extend ( self . _nested ( root , seen , format_callback ) ) <EOL> return roots <EOL> def model_format_dict ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , ( models . Model , models . base . ModelBase ) ) : <EOL> opts = obj . _meta <EOL> elif isinstance ( obj , models . query . QuerySet ) : <EOL> opts = obj . model . _meta <EOL> else : <EOL> opts = obj <EOL> return { <EOL> '<STR_LIT>' : force_unicode ( opts . verbose_name ) , <EOL> '<STR_LIT>' : force_unicode ( opts . verbose_name_plural ) <EOL> } <EOL> def model_ngettext ( obj , n = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , models . query . QuerySet ) : <EOL> if n is None : <EOL> n = obj . count ( ) <EOL> obj = obj . model <EOL> d = model_format_dict ( obj ) <EOL> singular , plural = d [ "<STR_LIT>" ] , d [ "<STR_LIT>" ] <EOL> return ungettext ( singular , plural , n or <NUM_LIT:0> ) <EOL> def is_rel_field ( name , model ) : <EOL> if hasattr ( name , '<STR_LIT>' ) and name . find ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> parts = name . split ( "<STR_LIT>" ) <EOL> if parts [ <NUM_LIT:0> ] in model . _meta . get_all_field_names ( ) : <EOL> return True <EOL> return False <EOL> def lookup_field ( name , obj , model_admin = None ) : <EOL> opts = obj . _meta <EOL> try : <EOL> f = opts . get_field ( name ) <EOL> except models . FieldDoesNotExist : <EOL> if callable ( name ) : <EOL> attr = name <EOL> value = attr ( obj ) <EOL> elif ( model_admin is not None and hasattr ( model_admin , name ) and <EOL> not name == '<STR_LIT>' and not name == '<STR_LIT>' ) : <EOL> attr = getattr ( model_admin , name ) <EOL> value = attr ( obj ) <EOL> else : <EOL> if is_rel_field ( name , obj ) : <EOL> parts = name . split ( "<STR_LIT>" ) <EOL> rel_name , sub_rel_name = parts [ <NUM_LIT:0> ] , "<STR_LIT>" . join ( parts [ <NUM_LIT:1> : ] ) <EOL> rel_obj = getattr ( obj , rel_name ) <EOL> if rel_obj is not None : <EOL> return lookup_field ( sub_rel_name , rel_obj , model_admin ) <EOL> attr = getattr ( obj , name ) <EOL> if callable ( attr ) : <EOL> value = attr ( ) <EOL> else : <EOL> value = attr <EOL> f = None <EOL> else : <EOL> attr = None <EOL> value = getattr ( obj , name ) <EOL> return f , attr , value <EOL> def admin_urlname ( value , arg ) : <EOL> return '<STR_LIT>' % ( value . app_label , value . model_name , arg ) <EOL> def boolean_icon ( field_val ) : <EOL> return mark_safe ( u'<STR_LIT>' % ( <EOL> { True : '<STR_LIT>' , False : '<STR_LIT>' , None : '<STR_LIT>' } [ field_val ] , field_val ) ) <EOL> def display_for_field ( value , field ) : <EOL> from xadmin . views . list import EMPTY_CHANGELIST_VALUE <EOL> if field . flatchoices : <EOL> return dict ( field . flatchoices ) . get ( value , EMPTY_CHANGELIST_VALUE ) <EOL> elif isinstance ( field , models . BooleanField ) or isinstance ( field , models . NullBooleanField ) : <EOL> return boolean_icon ( value ) <EOL> elif value is None : <EOL> return EMPTY_CHANGELIST_VALUE <EOL> elif isinstance ( field , models . DateTimeField ) : <EOL> return formats . localize ( tz_localtime ( value ) ) <EOL> elif isinstance ( field , ( models . DateField , models . TimeField ) ) : <EOL> return formats . localize ( value ) <EOL> elif isinstance ( field , models . DecimalField ) : <EOL> return formats . number_format ( value , field . decimal_places ) <EOL> elif isinstance ( field , models . FloatField ) : <EOL> return formats . number_format ( value ) <EOL> elif isinstance ( field . rel , models . ManyToManyRel ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( [ smart_unicode ( obj ) for obj in value . all ( ) ] ) <EOL> else : <EOL> return smart_unicode ( value ) <EOL> def display_for_value ( value , boolean = False ) : <EOL> from xadmin . views . list import EMPTY_CHANGELIST_VALUE <EOL> if boolean : <EOL> return boolean_icon ( value ) <EOL> elif value is None : <EOL> return EMPTY_CHANGELIST_VALUE <EOL> elif isinstance ( value , datetime . datetime ) : <EOL> return formats . localize ( tz_localtime ( value ) ) <EOL> elif isinstance ( value , ( datetime . date , datetime . time ) ) : <EOL> return formats . localize ( value ) <EOL> elif isinstance ( value , ( decimal . Decimal , float ) ) : <EOL> return formats . number_format ( value ) <EOL> else : <EOL> return smart_unicode ( value ) <EOL> class NotRelationField ( Exception ) : <EOL> pass <EOL> def get_model_from_relation ( field ) : <EOL> if is_related_field ( field ) : <EOL> return field . model <EOL> elif getattr ( field , '<STR_LIT>' ) : <EOL> return field . rel . to <EOL> else : <EOL> raise NotRelationField <EOL> def reverse_field_path ( model , path ) : <EOL> """<STR_LIT>""" <EOL> reversed_path = [ ] <EOL> parent = model <EOL> pieces = path . split ( LOOKUP_SEP ) <EOL> for piece in pieces : <EOL> field = parent . _meta . get_field ( piece ) <EOL> direct = not field . auto_created or field . concrete <EOL> if len ( reversed_path ) == len ( pieces ) - <NUM_LIT:1> : <EOL> try : <EOL> get_model_from_relation ( field ) <EOL> except NotRelationField : <EOL> break <EOL> if direct : <EOL> related_name = field . related_query_name ( ) <EOL> parent = field . rel . to <EOL> else : <EOL> related_name = field . field . name <EOL> parent = field . model <EOL> reversed_path . insert ( <NUM_LIT:0> , related_name ) <EOL> return ( parent , LOOKUP_SEP . join ( reversed_path ) ) <EOL> def get_fields_from_path ( model , path ) : <EOL> """<STR_LIT>""" <EOL> pieces = path . split ( LOOKUP_SEP ) <EOL> fields = [ ] <EOL> for piece in pieces : <EOL> if fields : <EOL> parent = get_model_from_relation ( fields [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> parent = model <EOL> fields . append ( parent . _meta . get_field ( piece ) ) <EOL> return fields <EOL> def remove_trailing_data_field ( fields ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> get_model_from_relation ( fields [ - <NUM_LIT:1> ] ) <EOL> except NotRelationField : <EOL> fields = fields [ : - <NUM_LIT:1> ] <EOL> return fields <EOL> def get_limit_choices_to_from_path ( model , path ) : <EOL> """<STR_LIT>""" <EOL> fields = get_fields_from_path ( model , path ) <EOL> fields = remove_trailing_data_field ( fields ) <EOL> limit_choices_to = ( <EOL> fields and hasattr ( fields [ - <NUM_LIT:1> ] , '<STR_LIT>' ) and <EOL> getattr ( fields [ - <NUM_LIT:1> ] . rel , '<STR_LIT>' , None ) ) <EOL> if not limit_choices_to : <EOL> return models . Q ( ) <EOL> elif isinstance ( limit_choices_to , models . Q ) : <EOL> return limit_choices_to <EOL> else : <EOL> return models . Q ( ** limit_choices_to ) <EOL> def sortkeypicker ( keynames ) : <EOL> negate = set ( ) <EOL> for i , k in enumerate ( keynames ) : <EOL> if k [ : <NUM_LIT:1> ] == '<STR_LIT:->' : <EOL> keynames [ i ] = k [ <NUM_LIT:1> : ] <EOL> negate . add ( k [ <NUM_LIT:1> : ] ) <EOL> def getit ( adict ) : <EOL> composite = [ adict [ k ] for k in keynames ] <EOL> for i , ( k , v ) in enumerate ( zip ( keynames , composite ) ) : <EOL> if k in negate : <EOL> composite [ i ] = - v <EOL> return composite <EOL> return getit <EOL> def is_related_field ( field ) : <EOL> return isinstance ( field , ForeignObjectRel ) <EOL> def is_related_field2 ( field ) : <EOL> return ( hasattr ( field , '<STR_LIT>' ) and field . rel != None ) or is_related_field ( field ) </s>
