<s> """<STR_LIT>""" <EOL> from django . db import models <EOL> from django . utils import formats <EOL> from django . utils . text import capfirst <EOL> from django . utils . encoding import smart_unicode , smart_str , iri_to_uri <EOL> from django . utils . safestring import mark_safe <EOL> from django . db . models . query import QuerySet <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> EMPTY_VALUE = '<STR_LIT>' <EOL> DISPLAY_SIZE = <NUM_LIT:100> <EOL> class EasyModel ( object ) : <EOL> def __init__ ( self , site , model ) : <EOL> self . site = site <EOL> self . model = model <EOL> self . model_list = site . registry . keys ( ) <EOL> self . verbose_name = model . _meta . verbose_name <EOL> self . verbose_name_plural = model . _meta . verbose_name_plural <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % smart_str ( self . model . _meta . object_name ) <EOL> def model_databrowse ( self ) : <EOL> "<STR_LIT>" <EOL> return self . site . registry [ self . model ] <EOL> def url ( self ) : <EOL> return mark_safe ( '<STR_LIT>' % ( self . site . root_url , <EOL> self . model . _meta . app_label , <EOL> self . model . _meta . module_name ) ) <EOL> def objects ( self , ** kwargs ) : <EOL> return self . get_query_set ( ) . filter ( ** kwargs ) <EOL> def get_query_set ( self ) : <EOL> easy_qs = self . model . _default_manager . get_query_set ( ) . _clone ( klass = EasyQuerySet ) <EOL> easy_qs . _easymodel = self <EOL> return easy_qs <EOL> def object_by_pk ( self , pk ) : <EOL> return EasyInstance ( self , self . model . _default_manager . get ( pk = pk ) ) <EOL> def sample_objects ( self ) : <EOL> for obj in self . model . _default_manager . all ( ) [ : <NUM_LIT:3> ] : <EOL> yield EasyInstance ( self , obj ) <EOL> def field ( self , name ) : <EOL> try : <EOL> f = self . model . _meta . get_field ( name ) <EOL> except models . FieldDoesNotExist : <EOL> return None <EOL> return EasyField ( self , f ) <EOL> def fields ( self ) : <EOL> return [ EasyField ( self , f ) for f in ( self . model . _meta . fields + <EOL> self . model . _meta . many_to_many ) ] <EOL> class EasyField ( object ) : <EOL> def __init__ ( self , easy_model , field ) : <EOL> self . model , self . field = easy_model , field <EOL> def __repr__ ( self ) : <EOL> return smart_str ( u'<STR_LIT>' % <EOL> ( self . model . model . _meta . object_name , <EOL> self . field . name ) ) <EOL> def choices ( self ) : <EOL> for value , label in self . field . choices : <EOL> yield EasyChoice ( self . model , self , value , label ) <EOL> def url ( self ) : <EOL> if self . field . choices : <EOL> return mark_safe ( '<STR_LIT>' % <EOL> ( self . model . site . root_url , <EOL> self . model . model . _meta . app_label , <EOL> self . model . model . _meta . module_name , <EOL> self . field . name ) ) <EOL> elif self . field . rel : <EOL> return mark_safe ( '<STR_LIT>' % <EOL> ( self . model . site . root_url , <EOL> self . model . model . _meta . app_label , <EOL> self . model . model . _meta . module_name ) ) <EOL> class EasyChoice ( object ) : <EOL> def __init__ ( self , easy_model , field , value , label ) : <EOL> self . model , self . field = easy_model , field <EOL> self . value , self . label = value , label <EOL> def __repr__ ( self ) : <EOL> return smart_str ( u'<STR_LIT>' % <EOL> ( self . model . model . _meta . object_name , <EOL> self . field . name ) ) <EOL> def url ( self ) : <EOL> return mark_safe ( '<STR_LIT>' % <EOL> ( self . model . site . root_url , <EOL> self . model . model . _meta . app_label , <EOL> self . model . model . _meta . module_name , <EOL> self . field . field . name , <EOL> iri_to_uri ( self . value ) ) ) <EOL> class EasyInstance ( object ) : <EOL> def __init__ ( self , easy_model , instance ) : <EOL> self . model , self . instance = easy_model , instance <EOL> def __repr__ ( self ) : <EOL> return smart_str ( u'<STR_LIT>' % <EOL> ( self . model . model . _meta . object_name , <EOL> self . instance . _get_pk_val ( ) ) ) <EOL> def __unicode__ ( self ) : <EOL> val = smart_unicode ( self . instance ) <EOL> if len ( val ) > DISPLAY_SIZE : <EOL> return val [ : DISPLAY_SIZE ] + u'<STR_LIT>' <EOL> return val <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def pk ( self ) : <EOL> return self . instance . _get_pk_val ( ) <EOL> def url ( self ) : <EOL> return mark_safe ( '<STR_LIT>' % <EOL> ( self . model . site . root_url , <EOL> self . model . model . _meta . app_label , <EOL> self . model . model . _meta . module_name , <EOL> iri_to_uri ( self . pk ( ) ) ) ) <EOL> def fields ( self ) : <EOL> """<STR_LIT>""" <EOL> for f in self . model . model . _meta . fields + self . model . model . _meta . many_to_many : <EOL> yield EasyInstanceField ( self . model , self , f ) <EOL> def related_objects ( self ) : <EOL> """<STR_LIT>""" <EOL> for rel_object in self . model . model . _meta . get_all_related_objects ( ) + self . model . model . _meta . get_all_related_many_to_many_objects ( ) : <EOL> if rel_object . model not in self . model . model_list : <EOL> continue <EOL> em = EasyModel ( self . model . site , rel_object . model ) <EOL> try : <EOL> rel_accessor = getattr ( self . instance , rel_object . get_accessor_name ( ) ) <EOL> except ObjectDoesNotExist : <EOL> continue <EOL> if rel_object . field . rel . multiple : <EOL> object_list = [ EasyInstance ( em , i ) for i in rel_accessor . all ( ) ] <EOL> else : <EOL> object_list = [ EasyInstance ( em , rel_accessor ) ] <EOL> yield { <EOL> '<STR_LIT>' : em , <EOL> '<STR_LIT>' : rel_object . field . verbose_name , <EOL> '<STR_LIT>' : object_list , <EOL> } <EOL> class EasyInstanceField ( object ) : <EOL> def __init__ ( self , easy_model , instance , field ) : <EOL> self . model , self . field , self . instance = easy_model , field , instance <EOL> self . raw_value = getattr ( instance . instance , field . name ) <EOL> def __repr__ ( self ) : <EOL> return smart_str ( u'<STR_LIT>' % <EOL> ( self . model . model . _meta . object_name , <EOL> self . field . name ) ) <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . field . rel : <EOL> if isinstance ( self . field . rel , models . ManyToOneRel ) : <EOL> objs = getattr ( self . instance . instance , self . field . name ) <EOL> elif isinstance ( self . field . rel , <EOL> models . ManyToManyRel ) : <EOL> return list ( getattr ( self . instance . instance , <EOL> self . field . name ) . all ( ) ) <EOL> elif self . field . choices : <EOL> objs = dict ( self . field . choices ) . get ( self . raw_value , EMPTY_VALUE ) <EOL> elif isinstance ( self . field , models . DateField ) or isinstance ( self . field , models . TimeField ) : <EOL> if self . raw_value : <EOL> if isinstance ( self . field , models . DateTimeField ) : <EOL> objs = capfirst ( formats . date_format ( self . raw_value , <EOL> '<STR_LIT>' ) ) <EOL> elif isinstance ( self . field , models . TimeField ) : <EOL> objs = capfirst ( formats . time_format ( self . raw_value , <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> objs = capfirst ( formats . date_format ( self . raw_value , <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> objs = EMPTY_VALUE <EOL> elif isinstance ( self . field , models . BooleanField ) or isinstance ( self . field , models . NullBooleanField ) : <EOL> objs = { True : '<STR_LIT>' , False : '<STR_LIT>' , None : '<STR_LIT>' } [ self . raw_value ] <EOL> else : <EOL> objs = self . raw_value <EOL> return [ objs ] <EOL> def urls ( self ) : <EOL> "<STR_LIT>" <EOL> plugin_urls = [ ] <EOL> for plugin_name , plugin in self . model . model_databrowse ( ) . plugins . items ( ) : <EOL> urls = plugin . urls ( plugin_name , self ) <EOL> if urls is not None : <EOL> values = self . values ( ) <EOL> return zip ( self . values ( ) , urls ) <EOL> if self . field . rel : <EOL> m = EasyModel ( self . model . site , self . field . rel . to ) <EOL> if self . field . rel . to in self . model . model_list : <EOL> lst = [ ] <EOL> for value in self . values ( ) : <EOL> if value is None : <EOL> continue <EOL> url = mark_safe ( '<STR_LIT>' % <EOL> ( self . model . site . root_url , <EOL> m . model . _meta . app_label , <EOL> m . model . _meta . module_name , <EOL> iri_to_uri ( value . _get_pk_val ( ) ) ) ) <EOL> lst . append ( ( smart_unicode ( value ) , url ) ) <EOL> else : <EOL> lst = [ ( value , None ) for value in self . values ( ) ] <EOL> elif self . field . choices : <EOL> lst = [ ] <EOL> for value in self . values ( ) : <EOL> url = mark_safe ( '<STR_LIT>' % <EOL> ( self . model . site . root_url , <EOL> self . model . model . _meta . app_label , <EOL> self . model . model . _meta . module_name , <EOL> self . field . name , <EOL> iri_to_uri ( self . raw_value ) ) ) <EOL> lst . append ( ( value , url ) ) <EOL> elif isinstance ( self . field , models . URLField ) : <EOL> val = self . values ( ) [ <NUM_LIT:0> ] <EOL> lst = [ ( val , iri_to_uri ( val ) ) ] <EOL> else : <EOL> lst = [ ( self . values ( ) [ <NUM_LIT:0> ] , None ) ] <EOL> return lst <EOL> class EasyQuerySet ( QuerySet ) : <EOL> """<STR_LIT>""" <EOL> def iterator ( self , * args , ** kwargs ) : <EOL> for obj in super ( EasyQuerySet , self ) . iterator ( * args , ** kwargs ) : <EOL> yield EasyInstance ( self . _easymodel , obj ) <EOL> def _clone ( self , * args , ** kwargs ) : <EOL> c = super ( EasyQuerySet , self ) . _clone ( * args , ** kwargs ) <EOL> c . _easymodel = self . _easymodel <EOL> return c </s>
