<s> from uliweb . i18n import gettext_lazy as _ <EOL> from uliweb . form import SelectField , BaseField <EOL> import os , sys <EOL> import logging <EOL> import time <EOL> import inspect <EOL> from uliweb . orm import get_model , Model , Result , do_ , Lazy , get_model_property <EOL> import uliweb . orm as orm <EOL> from uliweb import redirect , json , functions , UliwebError , Storage <EOL> from sqlalchemy . sql import select , Select , func <EOL> from sqlalchemy . engine import RowProxy <EOL> from uliweb . contrib . upload import FileServing , FilenameConverter , get_fileserving <EOL> from uliweb . utils . common import safe_unicode , safe_str <EOL> from uliweb . core . html import Builder <EOL> from uliweb . utils . sorteddict import SortedDict <EOL> log = logging . getLogger ( __name__ ) <EOL> __default_fields_builds__ = { } <EOL> max_reference_field_count = <NUM_LIT:100> <EOL> class __default_value__ ( object ) : pass <EOL> from uliweb . form . validators import Validator <EOL> class TEST_DUPLICATE ( Validator ) : <EOL> default_message = _ ( '<STR_LIT>' ) <EOL> def init ( self ) : <EOL> super ( TEST_DUPLICATE , self ) . init ( ) <EOL> self . model = self . args [ '<STR_LIT>' ] <EOL> self . field = self . args [ '<STR_LIT>' ] <EOL> self . condition = self . args . get ( '<STR_LIT>' ) <EOL> def validate ( self , data , all_data = None ) : <EOL> if self . condition is not None : <EOL> if self . model . filter ( self . field == data ) . filter ( self . condition ) . any ( ) : <EOL> return False <EOL> else : <EOL> if self . model . filter ( self . field == data ) . any ( ) : <EOL> return False <EOL> return True <EOL> def get_fileds_builds ( section = '<STR_LIT>' ) : <EOL> if not __default_fields_builds__ : <EOL> from uliweb import settings <EOL> from uliweb . utils . common import import_attr <EOL> import uliweb . form as form <EOL> if settings and section in settings : <EOL> for k , v in settings [ section ] . iteritems ( ) : <EOL> if v . get ( '<STR_LIT>' , None ) : <EOL> v [ '<STR_LIT>' ] = import_attr ( v [ '<STR_LIT>' ] ) <EOL> __default_fields_builds__ [ getattr ( form , k ) ] = v <EOL> return __default_fields_builds__ <EOL> def get_sort_field ( model , sort_field = '<STR_LIT>' , order_name = '<STR_LIT>' ) : <EOL> from uliweb import request <EOL> models = [ ] <EOL> if isinstance ( model , ( list , tuple ) ) : <EOL> for m in model : <EOL> models . append ( get_model ( m ) ) <EOL> else : <EOL> models = [ get_model ( model ) ] <EOL> def get_field ( name ) : <EOL> for m in models : <EOL> if name in m . c : <EOL> return m . c [ name ] <EOL> if request . values . getlist ( '<STR_LIT>' ) : <EOL> sort_fields = request . values . getlist ( '<STR_LIT>' ) <EOL> order_by = [ ] <EOL> orders = request . values . getlist ( '<STR_LIT>' ) <EOL> for i , f in enumerate ( sort_fields ) : <EOL> _order = '<STR_LIT>' <EOL> if not orders : <EOL> if f . endswith ( '<STR_LIT>' ) or f . endswith ( '<STR_LIT>' ) : <EOL> f , _order = f . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> else : <EOL> continue <EOL> field = get_field ( f ) <EOL> if field is not None : <EOL> if orders : <EOL> _order = orders [ i ] <EOL> if _order == '<STR_LIT>' : <EOL> order_by . append ( field ) <EOL> else : <EOL> order_by . append ( field . desc ( ) ) <EOL> else : <EOL> order_by = None <EOL> return order_by <EOL> class ReferenceSelectField ( SelectField ) : <EOL> def __init__ ( self , model , group_field = None , value_field = '<STR_LIT:id>' , condition = None , <EOL> query = None , label = '<STR_LIT>' , default = None , required = False , validators = None , <EOL> name = '<STR_LIT>' , html_attrs = None , help_string = '<STR_LIT>' , build = None , empty = '<STR_LIT>' , <EOL> get_display = None , post_choices = None , ** kwargs ) : <EOL> super ( ReferenceSelectField , self ) . __init__ ( label = label , default = default , choices = None , required = required , validators = validators , name = name , html_attrs = html_attrs , help_string = help_string , build = build , empty = empty , ** kwargs ) <EOL> self . model = model <EOL> self . group_field = group_field <EOL> self . value_field = value_field <EOL> self . condition = condition <EOL> self . query = query <EOL> self . get_display = get_display or unicode <EOL> self . post_choices = post_choices <EOL> def get_choices ( self ) : <EOL> if self . choices : <EOL> if callable ( self . choices ) : <EOL> _choices = self . choices ( ) <EOL> else : <EOL> _choices = self . choices <EOL> else : <EOL> model = get_model ( self . model ) <EOL> if not self . group_field : <EOL> if hasattr ( model , '<STR_LIT:Meta>' ) : <EOL> self . group_field = getattr ( model . Meta , '<STR_LIT>' , None ) <EOL> else : <EOL> self . group_field = None <EOL> if self . query is not None : <EOL> query = self . query <EOL> else : <EOL> query = model . all ( ) <EOL> if hasattr ( model , '<STR_LIT:Meta>' ) and hasattr ( model . Meta , '<STR_LIT>' ) : <EOL> _order = model . Meta . order_by <EOL> if not isinstance ( _order , ( list , tuple ) ) : <EOL> _order = [ model . Meta . order_by ] <EOL> for x in _order : <EOL> if x . startswith ( '<STR_LIT:->' ) : <EOL> f = model . c [ x [ <NUM_LIT:1> : ] ] . desc ( ) <EOL> else : <EOL> if x . startswith ( '<STR_LIT:+>' ) : <EOL> x = x [ <NUM_LIT:1> : ] <EOL> f = model . c [ x ] . asc ( ) <EOL> query = query . order_by ( f ) <EOL> if self . condition is not None : <EOL> query = query . filter ( self . condition ) <EOL> if isinstance ( query , ( list , tuple ) ) : <EOL> _count = len ( query ) <EOL> else : <EOL> _count = query . count ( ) <EOL> if _count > max_reference_field_count : <EOL> log . error ( '<STR_LIT>' . format ( <EOL> str ( query ) , _count , max_reference_field_count ) ) <EOL> if self . group_field : <EOL> query = query . order_by ( model . c [ self . group_field ] . asc ( ) ) <EOL> if self . group_field : <EOL> _choices = [ ( x . get_display_value ( self . group_field ) , getattr ( x , self . value_field ) , self . get_display ( x ) ) for x in query ] <EOL> else : <EOL> _choices = [ ( getattr ( x , self . value_field ) , self . get_display ( x ) ) for x in query ] <EOL> if self . post_choices : <EOL> return self . post_choices ( _choices ) <EOL> else : <EOL> return _choices <EOL> def to_python ( self , data ) : <EOL> attr = getattr ( get_model ( self . model ) , self . value_field ) <EOL> return attr . validate ( data ) <EOL> class ManyToManySelectField ( ReferenceSelectField ) : <EOL> def __init__ ( self , model , group_field = None , value_field = '<STR_LIT:id>' , <EOL> condition = None , query = None , label = '<STR_LIT>' , default = [ ] , <EOL> required = False , validators = None , name = '<STR_LIT>' , html_attrs = None , <EOL> help_string = '<STR_LIT>' , build = None , get_display = None , ** kwargs ) : <EOL> super ( ManyToManySelectField , self ) . __init__ ( model = model , group_field = group_field , <EOL> value_field = value_field , condition = condition , query = query , label = label , <EOL> default = default , required = required , validators = validators , name = name , <EOL> html_attrs = html_attrs , help_string = help_string , build = build , <EOL> empty = None , multiple = True , get_display = get_display , ** kwargs ) <EOL> class RemoteField ( BaseField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , label = '<STR_LIT>' , default = None , required = False , validators = None , <EOL> name = '<STR_LIT>' , html_attrs = None , help_string = '<STR_LIT>' , build = None , alt = '<STR_LIT>' , url = '<STR_LIT>' , <EOL> datatype = int , ** kwargs ) : <EOL> _attrs = { '<STR_LIT:url>' : url , '<STR_LIT>' : alt , '<STR_LIT>' : '<STR_LIT>' } <EOL> _attrs . update ( html_attrs or { } ) <EOL> BaseField . __init__ ( self , label = label , default = default , required = required , <EOL> validators = validators , name = name , html_attrs = _attrs , help_string = help_string , <EOL> build = build , datatype = datatype , ** kwargs ) <EOL> class GenericReference ( orm . Property ) : <EOL> property_type = '<STR_LIT>' <EOL> def __init__ ( self , verbose_name = None , table_fieldname = '<STR_LIT>' , <EOL> object_fieldname = '<STR_LIT>' , ** attrs ) : <EOL> """<STR_LIT>""" <EOL> super ( GenericReference , self ) . __init__ ( <EOL> verbose_name = verbose_name , ** attrs ) <EOL> self . table_fieldname = table_fieldname <EOL> self . object_fieldname = object_fieldname <EOL> self . table = get_model ( '<STR_LIT>' ) <EOL> def create ( self , cls ) : <EOL> pass <EOL> def pre_create ( self , cls ) : <EOL> if self . table_fieldname not in cls . properties : <EOL> prop = orm . Field ( orm . PKTYPE ( ) ) <EOL> cls . add_property ( self . table_fieldname , prop ) <EOL> if self . object_fieldname not in cls . properties : <EOL> prop = orm . Field ( orm . PKTYPE ( ) ) <EOL> cls . add_property ( self . object_fieldname , prop ) <EOL> def __property_config__ ( self , model_class , property_name ) : <EOL> """<STR_LIT>""" <EOL> super ( GenericReference , self ) . __property_config__ ( model_class , property_name ) <EOL> def filter ( self , obj ) : <EOL> if isinstance ( obj , ( tuple , list ) ) : <EOL> if len ( obj ) != <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> table_id = self . table . get_table ( obj [ <NUM_LIT:0> ] ) . id <EOL> obj_id = obj [ <NUM_LIT:1> ] <EOL> else : <EOL> if not isinstance ( obj , orm . Model ) : <EOL> raise ValueError ( "<STR_LIT>" % obj ) <EOL> table_id = self . table . get_table ( obj . __class__ . tablename ) . id <EOL> obj_id = obj . id <EOL> return self . model_class . filter ( self . model_class . c [ self . table_fieldname ] == table_id ) . filter ( self . model_class . c [ self . object_fieldname ] == obj_id ) <EOL> def __get__ ( self , model_instance , model_class ) : <EOL> """<STR_LIT>""" <EOL> if model_instance : <EOL> table_id , object_id = self . get_value_for_datastore ( model_instance ) <EOL> if not table_id and not object_id : <EOL> return None <EOL> model = self . table . get_model ( table_id ) <EOL> return model . get ( object_id ) <EOL> else : <EOL> return self <EOL> def __set__ ( self , model_instance , value ) : <EOL> if model_instance is None : <EOL> return <EOL> if value : <EOL> if isinstance ( value , ( tuple , list ) ) : <EOL> if not len ( value ) == <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> table_id , object_id = value <EOL> if isinstance ( table_id , ( str , unicode ) ) : <EOL> table_id = self . table . get_table ( table_id ) . id <EOL> if inspect . isclass ( table_id ) and issubclass ( table_id , orm . Model ) : <EOL> table_id = table_id . tablename <EOL> elif isinstance ( value , orm . Model ) : <EOL> table_id = self . table . get_table ( value . tablename ) . id <EOL> object_id = value . id <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> model_instance . properties [ self . table_fieldname ] . __set__ ( model_instance , table_id ) <EOL> model_instance . properties [ self . object_fieldname ] . __set__ ( model_instance , object_id ) <EOL> setattr ( model_instance , self . _attr_name ( ) , value ) <EOL> def get_value_for_datastore ( self , model_instance ) : <EOL> """<STR_LIT>""" <EOL> table_id = getattr ( model_instance , self . table_fieldname , None ) <EOL> object_id = getattr ( model_instance , self . object_fieldname , None ) <EOL> return table_id , object_id <EOL> def get_display_value ( self , value ) : <EOL> return unicode ( value ) <EOL> class GenericRelation ( orm . Property ) : <EOL> property_type = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , reference_fieldname = '<STR_LIT>' , ** attrs ) : <EOL> """<STR_LIT>""" <EOL> super ( GenericRelation , self ) . __init__ ( ** attrs ) <EOL> self . _model = model <EOL> self . reference_fieldname = reference_fieldname <EOL> self . table = get_model ( '<STR_LIT>' ) <EOL> def create ( self , cls ) : <EOL> pass <EOL> def __get__ ( self , model_instance , model_class ) : <EOL> """<STR_LIT>""" <EOL> if model_instance is not None : <EOL> table_id = self . table . get_table ( self . model_class . tablename ) . id <EOL> model = get_model ( self . _model ) <EOL> return model . filter ( model . c [ self . reference_fieldname ] == table_id ) <EOL> else : <EOL> return self <EOL> def __set__ ( self , model_instance , value ) : <EOL> """<STR_LIT>""" <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def get_fields ( model , fields , meta = None ) : <EOL> """<STR_LIT>""" <EOL> model = get_model ( model ) <EOL> if fields is not None : <EOL> f = fields <EOL> elif meta and hasattr ( model , meta ) : <EOL> m = getattr ( model , meta ) <EOL> if hasattr ( m , '<STR_LIT>' ) : <EOL> f = m . fields <EOL> else : <EOL> f = model . _fields_list <EOL> else : <EOL> f = model . _fields_list <EOL> fields_list = [ ] <EOL> for x in f : <EOL> field = { } <EOL> if isinstance ( x , str ) : <EOL> field [ '<STR_LIT:name>' ] = x <EOL> elif isinstance ( x , tuple ) : <EOL> field [ '<STR_LIT:name>' ] = x [ <NUM_LIT:0> ] <EOL> field [ '<STR_LIT>' ] = x [ <NUM_LIT:1> ] <EOL> elif isinstance ( x , dict ) : <EOL> field = x . copy ( ) <EOL> else : <EOL> raise UliwebError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in field : <EOL> if hasattr ( model , field [ '<STR_LIT:name>' ] ) : <EOL> field [ '<STR_LIT>' ] = getattr ( model , field [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> field [ '<STR_LIT>' ] = None <EOL> fields_list . append ( ( field [ '<STR_LIT:name>' ] , field ) ) <EOL> return fields_list <EOL> def get_layout ( model , meta ) : <EOL> f = None <EOL> if hasattr ( model , meta ) : <EOL> m = getattr ( model , meta ) <EOL> if hasattr ( m , '<STR_LIT>' ) : <EOL> f = m . layout <EOL> return f <EOL> def get_url ( ok_url , * args , ** kwargs ) : <EOL> if callable ( ok_url ) : <EOL> return ok_url ( * args , ** kwargs ) <EOL> else : <EOL> return ok_url . format ( * args , ** kwargs ) <EOL> def get_obj_url ( obj ) : <EOL> from uliweb import settings <EOL> from uliweb . core . html import Tag <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> display = obj . get_url ( ) <EOL> else : <EOL> url = settings . get_var ( '<STR_LIT>' + obj . tablename ) <EOL> if url : <EOL> display = str ( Tag ( '<STR_LIT:a>' , unicode ( obj ) , href = url . format ( obj . to_dict ( ) ) ) ) <EOL> else : <EOL> display = unicode ( obj ) <EOL> return display <EOL> def to_json_result ( success , msg = '<STR_LIT>' , d = None , json_func = None , ** kwargs ) : <EOL> json_func = json_func or json <EOL> t = { '<STR_LIT:success>' : success , '<STR_LIT:message>' : safe_str ( msg ) , '<STR_LIT:data>' : d } <EOL> t . update ( kwargs ) <EOL> return json_func ( t ) <EOL> def make_form_field ( field , model = None , field_cls = None , <EOL> use_default_value = True , <EOL> builds_args_map = None ) : <EOL> """<STR_LIT>""" <EOL> import uliweb . form as form <EOL> from uliweb . form . validators import TEST_MAXLENGTH <EOL> builds_args_map = builds_args_map or { } <EOL> if model : <EOL> model = get_model ( model ) <EOL> if isinstance ( field , BaseField ) : <EOL> return field <EOL> if isinstance ( field , dict ) and '<STR_LIT>' in field and isinstance ( field [ '<STR_LIT>' ] , BaseField ) : <EOL> return field [ '<STR_LIT>' ] <EOL> field_type = None <EOL> prop = None <EOL> if isinstance ( field , ( str , unicode ) ) : <EOL> field = { '<STR_LIT:name>' : field } <EOL> if isinstance ( field , dict ) : <EOL> prop = field . get ( '<STR_LIT>' ) <EOL> if not prop and model : <EOL> field_name = field . get ( '<STR_LIT>' ) or field [ '<STR_LIT:name>' ] <EOL> prop = get_model_property ( model , field_name ) <EOL> if not prop and not field_cls : <EOL> raise UliwebError ( "<STR_LIT>" % <EOL> ( field_name , model . __name__ ) ) <EOL> else : <EOL> raise UliwebError ( "<STR_LIT>" % type ( field ) ) <EOL> default_kwargs = { } <EOL> if prop : <EOL> default_kwargs = { <EOL> '<STR_LIT:label>' : prop . label or prop . property_name , <EOL> '<STR_LIT>' : prop . hint , <EOL> '<STR_LIT>' : prop . placeholder , <EOL> '<STR_LIT>' : prop . extra . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : prop . required , <EOL> } <EOL> kwargs = { <EOL> '<STR_LIT:label>' : field . get ( '<STR_LIT:label>' ) or field . get ( '<STR_LIT>' ) or default_kwargs . get ( '<STR_LIT:label>' ) , <EOL> '<STR_LIT>' : field . get ( '<STR_LIT>' ) or field . get ( '<STR_LIT>' ) or default_kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : field . get ( '<STR_LIT>' ) or default_kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : field . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , { } ) or default_kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : field . get ( '<STR_LIT>' , False ) if '<STR_LIT>' in field else default_kwargs . get ( '<STR_LIT>' , False ) <EOL> } <EOL> if use_default_value : <EOL> v = prop . default_value ( ) <EOL> kwargs [ '<STR_LIT:default>' ] = v <EOL> if field . get ( '<STR_LIT>' ) : <EOL> field_type = form . StringField <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> if prop . choices is not None : <EOL> kwargs [ '<STR_LIT>' ] = prop . get_choices ( ) <EOL> if field . get ( '<STR_LIT>' ) : <EOL> field_type = form . HiddenField <EOL> if field_cls : <EOL> field_type = field_cls <EOL> elif not field_type : <EOL> cls = prop . __class__ <EOL> type_name = prop . type_name <EOL> if type_name == '<STR_LIT>' : <EOL> pass <EOL> elif type_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> field_type = form . TextField <EOL> elif type_name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if prop . choices is not None : <EOL> field_type = form . SelectField <EOL> kwargs [ '<STR_LIT>' ] = prop . get_choices ( ) <EOL> else : <EOL> if cls . __name__ == '<STR_LIT>' : <EOL> field_type = form . FileField <EOL> kwargs [ '<STR_LIT>' ] = prop . upload_to <EOL> kwargs [ '<STR_LIT>' ] = prop . upload_to_sub <EOL> else : <EOL> field_type = form . UnicodeField <EOL> elif type_name == '<STR_LIT>' : <EOL> field_type = form . BooleanField <EOL> elif type_name == '<STR_LIT>' : <EOL> field_type = form . DateField <EOL> elif type_name == '<STR_LIT>' : <EOL> field_type = form . TimeField <EOL> elif type_name == '<STR_LIT>' : <EOL> field_type = form . DateTimeField <EOL> elif type_name == '<STR_LIT>' : <EOL> field_type = form . StringField <EOL> if prop . choices is not None : <EOL> field_type = form . SelectField <EOL> kwargs [ '<STR_LIT>' ] = prop . get_choices ( ) <EOL> elif type_name == '<STR_LIT>' : <EOL> field_type = form . FloatField <EOL> elif type_name == '<STR_LIT>' : <EOL> if '<STR_LIT>' not in prop . kwargs : <EOL> if prop . choices is not None : <EOL> field_type = form . SelectField <EOL> kwargs [ '<STR_LIT>' ] = prop . get_choices ( ) <EOL> kwargs [ '<STR_LIT>' ] = int <EOL> else : <EOL> field_type = form . IntField <EOL> elif type_name == '<STR_LIT>' : <EOL> kwargs [ '<STR_LIT>' ] = prop . reference_class <EOL> field_type = ManyToManySelectField <EOL> elif type_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = prop . reference_class <EOL> kwargs [ '<STR_LIT>' ] = prop . reference_fieldname <EOL> field_type = ReferenceSelectField <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( field [ '<STR_LIT:name>' ] , prop . __class__ . __name__ ) ) <EOL> if field_type : <EOL> build_args = builds_args_map . get ( field_type , { } ) <EOL> if not build_args : <EOL> build_args = get_fileds_builds ( ) . get ( field_type , { } ) <EOL> kwargs . update ( build_args ) <EOL> if issubclass ( prop . __class__ , ( orm . StringProperty , orm . CharProperty , orm . UnicodeProperty ) ) : <EOL> v = kwargs . setdefault ( '<STR_LIT>' , [ ] ) <EOL> if isinstance ( prop . max_length , int ) : <EOL> v . append ( TEST_MAXLENGTH ( prop . max_length ) ) <EOL> f = field_type ( ** kwargs ) <EOL> return f <EOL> def make_view_field ( field , obj = None , types_convert_map = None , fields_convert_map = None , <EOL> value = __default_value__ , auto_convert = True ) : <EOL> """<STR_LIT>""" <EOL> from uliweb . utils . textconvert import text2html <EOL> from uliweb . core . html import Tag <EOL> old_value = value <EOL> types_convert_map = types_convert_map or { } <EOL> fields_convert_map = fields_convert_map or { } <EOL> default_convert_map = { orm . TextProperty : lambda v , o : text2html ( v ) } <EOL> if isinstance ( field , dict ) : <EOL> if '<STR_LIT>' in field and field . get ( '<STR_LIT>' ) : <EOL> prop = field [ '<STR_LIT>' ] <EOL> else : <EOL> prop = field <EOL> name = field . get ( '<STR_LIT:name>' ) <EOL> else : <EOL> prop = field <EOL> name = prop . property_name <EOL> if not isinstance ( prop , orm . Property ) : <EOL> if old_value is __default_value__ : <EOL> value = prop . get ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> display = prop . get ( '<STR_LIT>' , value ) <EOL> label = prop . get ( '<STR_LIT:label>' , '<STR_LIT>' ) or prop . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> convert = prop . get ( '<STR_LIT>' , None ) <EOL> else : <EOL> if old_value is __default_value__ : <EOL> if isinstance ( obj , Model ) : <EOL> value = prop . get_value_for_datastore ( obj ) <EOL> if value is Lazy : <EOL> getattr ( obj , prop . property_name ) <EOL> value = prop . get_value_for_datastore ( obj ) <EOL> else : <EOL> value = obj [ name ] <EOL> if auto_convert or prop . choices : <EOL> display = prop . get_display_value ( value ) <EOL> else : <EOL> display = value <EOL> if isinstance ( field , dict ) : <EOL> initial = field . get ( '<STR_LIT>' , None ) <EOL> else : <EOL> initial = '<STR_LIT>' <EOL> label = initial or prop . verbose_name or name <EOL> if name in fields_convert_map : <EOL> convert = fields_convert_map . get ( name , None ) <EOL> else : <EOL> if isinstance ( prop , orm . Property ) : <EOL> convert = types_convert_map . get ( prop . __class__ , None ) <EOL> if not convert : <EOL> convert = default_convert_map . get ( prop . __class__ , None ) <EOL> convert_result = None <EOL> if convert : <EOL> convert_result = convert ( value , obj ) <EOL> if convert_result is None : <EOL> if value is not None : <EOL> if isinstance ( prop , orm . ManyToMany ) : <EOL> s = [ ] <EOL> if old_value is not __default_value__ : <EOL> if prop . reference_fieldname == '<STR_LIT:id>' : <EOL> query = [ ] <EOL> for _id in old_value : <EOL> _v = functions . get_cached_object ( prop . reference_class , _id ) <EOL> query . append ( _v ) <EOL> else : <EOL> query = prop . reference_class . filter ( prop . reference_class . c [ prop . reversed_fieldname ] . in_ ( old_value ) ) <EOL> else : <EOL> if prop . reference_fieldname == '<STR_LIT:id>' : <EOL> query = [ ] <EOL> _ids = prop . get_value_for_datastore ( obj , cached = True ) <EOL> for _id in _ids : <EOL> _v = functions . get_cached_object ( prop . reference_class , _id ) <EOL> if not _v : <EOL> log . debug ( "<STR_LIT>" % ( prop . reference_class . __name__ , _id ) ) <EOL> _v = _id <EOL> query . append ( _v ) <EOL> else : <EOL> query = getattr ( obj , prop . property_name ) . all ( ) <EOL> for x in query : <EOL> if isinstance ( x , orm . Model ) : <EOL> s . append ( get_obj_url ( x ) ) <EOL> else : <EOL> s . append ( str ( x ) ) <EOL> display = '<STR_LIT:U+0020>' . join ( s ) <EOL> elif isinstance ( prop , orm . ReferenceProperty ) or isinstance ( prop , orm . OneToOne ) : <EOL> try : <EOL> if old_value is not __default_value__ : <EOL> d = prop . reference_class . c [ prop . reference_fieldname ] <EOL> if prop . reference_fieldname == '<STR_LIT:id>' : <EOL> v = functions . get_cached_object ( prop . reference_class , old_value ) <EOL> else : <EOL> v = prop . reference_class . get ( d == old_value ) <EOL> if not isinstance ( obj , Model ) : <EOL> d = prop . reference_class . c [ prop . reference_fieldname ] <EOL> if prop . reference_fieldname == '<STR_LIT:id>' : <EOL> v = functions . get_cached_object ( prop . reference_class , value ) <EOL> else : <EOL> v = prop . reference_class . get ( d == value ) <EOL> else : <EOL> if prop . reference_fieldname == '<STR_LIT:id>' : <EOL> v = functions . get_cached_object ( prop . reference_class , obj . get_datastore_value ( prop . property_name ) ) <EOL> else : <EOL> v = getattr ( obj , prop . property_name ) <EOL> except orm . Error : <EOL> display = prop . get_datastore_value ( obj ) or '<STR_LIT>' <EOL> v = None <EOL> if isinstance ( v , Model ) : <EOL> display = get_obj_url ( v ) <EOL> else : <EOL> display = str ( v if v is not None else '<STR_LIT>' ) <EOL> elif isinstance ( prop , orm . FileProperty ) : <EOL> url = functions . get_href ( value ) <EOL> if url : <EOL> display = str ( Tag ( '<STR_LIT:a>' , value , href = url ) ) <EOL> else : <EOL> display = '<STR_LIT>' <EOL> if prop . __class__ is orm . TextProperty : <EOL> display = text2html ( value ) <EOL> else : <EOL> display = convert_result <EOL> if isinstance ( display , unicode ) : <EOL> display = display . encode ( '<STR_LIT:utf-8>' ) <EOL> if display is None : <EOL> display = '<STR_LIT>' <EOL> return Storage ( { '<STR_LIT:label>' : label , '<STR_LIT:value>' : value , '<STR_LIT>' : display , '<STR_LIT:name>' : name } ) <EOL> def get_view_field ( model , field_name , obj = None , types_convert_map = None , fields_convert_map = None , value = __default_value__ ) : <EOL> m = get_model ( model ) <EOL> field = get_model_property ( m , field_name ) <EOL> r = make_view_field ( field , obj = obj , types_convert_map = types_convert_map , fields_convert_map = fields_convert_map , value = value ) <EOL> return r <EOL> def get_field_display ( model , field_name , obj = None , types_convert_map = None , fields_convert_map = None , value = __default_value__ ) : <EOL> m = get_model ( model ) <EOL> field = get_model_property ( m , field_name ) <EOL> return make_view_field ( field , obj = obj , types_convert_map = types_convert_map , fields_convert_map = fields_convert_map , value = value ) [ '<STR_LIT>' ] <EOL> def get_model_display ( model , obj , fields = None , types_convert_map = None , fields_convert_map = None , data = None ) : <EOL> data = data or { } <EOL> r = Storage ( { } ) <EOL> for name , field in get_fields ( model , fields ) : <EOL> value = data . get ( name , __default_value__ ) <EOL> r [ name ] = make_view_field ( field , obj = obj , types_convert_map = types_convert_map , fields_convert_map = fields_convert_map , value = value ) <EOL> return r <EOL> class AddView ( object ) : <EOL> success_msg = _ ( '<STR_LIT>' ) <EOL> fail_msg = _ ( '<STR_LIT>' ) <EOL> builds_args_map = { } <EOL> protect_field_name = '<STR_LIT>' <EOL> def __init__ ( self , model , ok_url = None , ok_template = None , form = None , success_msg = None , <EOL> fail_msg = None , use_flash = True , <EOL> data = None , default_data = None , fields = None , form_cls = None , form_args = None , <EOL> static_fields = None , hidden_fields = None , save = None , pre_save = None , post_save = None , <EOL> post_created_form = None , layout_class = None , layout = None , rules = None , <EOL> file_replace = True , template_data = None , <EOL> success_data = None , fail_data = None , meta = '<STR_LIT>' , get_form_field = None , post_fail = None , <EOL> types_convert_map = None , fields_convert_map = None , json_func = None , <EOL> file_convert = True , upload_to = None , upload_to_sub = None , <EOL> fileserving_config = '<STR_LIT>' , protect = False , protect_field_name = None , <EOL> version = False , version_fieldname = '<STR_LIT:version>' , version_exception = True ) : <EOL> self . model = get_model ( model ) <EOL> self . meta = meta <EOL> self . ok_url = ok_url <EOL> self . ok_template = ok_template <EOL> if success_msg : <EOL> self . success_msg = success_msg <EOL> if fail_msg : <EOL> self . fail_msg = fail_msg <EOL> self . use_flash = use_flash <EOL> self . data = data or { } <EOL> self . template_data = template_data or { } <EOL> self . default_data = default_data or { } <EOL> self . get_form_field = get_form_field <EOL> self . layout_class = layout_class <EOL> self . layout = layout <EOL> self . rules = rules <EOL> self . fields = fields <EOL> self . form_cls = form_cls <EOL> self . form_args = form_args or { } <EOL> self . static_fields = static_fields or [ ] <EOL> self . hidden_fields = hidden_fields or [ ] <EOL> self . _save = save or self . save <EOL> self . pre_save = pre_save <EOL> self . post_save = post_save <EOL> self . post_created_form = post_created_form <EOL> self . post_fail = post_fail <EOL> self . file_replace = file_replace <EOL> self . success_data = success_data <EOL> self . fail_data = fail_data <EOL> self . types_convert_map = types_convert_map or { } <EOL> self . fields_convert_map = fields_convert_map or { } <EOL> self . json_func = json_func or json <EOL> self . file_convert = file_convert <EOL> self . upload_to = upload_to <EOL> self . upload_to_sub = upload_to_sub <EOL> self . fileserving = get_fileserving ( fileserving_config ) <EOL> self . protect = protect <EOL> self . protect_field_name = protect_field_name or self . protect_field_name <EOL> self . version = version <EOL> self . version_fieldname = version_fieldname <EOL> self . version_exception = version_exception <EOL> self . form = self . make_form ( form ) <EOL> def get_fields ( self ) : <EOL> f = [ ] <EOL> for field_name , prop in get_fields ( self . model , self . fields , self . meta ) : <EOL> d = prop . copy ( ) <EOL> d [ '<STR_LIT>' ] = field_name in self . static_fields <EOL> d [ '<STR_LIT>' ] = field_name in self . hidden_fields <EOL> f . append ( d ) <EOL> return f <EOL> def get_layout ( self ) : <EOL> if self . layout : <EOL> return self . layout <EOL> if hasattr ( self . model , self . meta ) : <EOL> m = getattr ( self . model , self . meta ) <EOL> if hasattr ( m , '<STR_LIT>' ) : <EOL> return getattr ( m , '<STR_LIT>' ) <EOL> def prepare_static_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> d = data . copy ( ) <EOL> for f in self . get_fields ( ) : <EOL> if f [ '<STR_LIT>' ] and f [ '<STR_LIT:name>' ] in d : <EOL> d [ f [ '<STR_LIT:name>' ] ] = make_view_field ( f , None , self . types_convert_map , self . fields_convert_map , d [ f [ '<STR_LIT:name>' ] ] ) [ '<STR_LIT>' ] <EOL> return d <EOL> def make_form ( self , form ) : <EOL> from uliweb . form import Form , Button , make_form <EOL> if form : <EOL> return form <EOL> DummyForm = make_form ( layout_class = self . layout_class , layout = self . layout , <EOL> base_class = self . form_cls , rules = self . rules ) <EOL> layout = self . get_layout ( ) <EOL> DummyForm . layout = layout <EOL> for f in self . get_fields ( ) : <EOL> flag = False <EOL> if self . get_form_field : <EOL> field = self . get_form_field ( f [ '<STR_LIT:name>' ] ) <EOL> if field : <EOL> flag = True <EOL> if not flag : <EOL> field = make_form_field ( f , self . model , builds_args_map = self . builds_args_map ) <EOL> if field : <EOL> DummyForm . add_field ( f [ '<STR_LIT:name>' ] , field , True ) <EOL> if self . post_created_form : <EOL> self . post_created_form ( DummyForm , self . model ) <EOL> return DummyForm ( data = self . data , ** self . form_args ) <EOL> def process_files ( self , data , edit = True , obj = None ) : <EOL> from uliweb . form import FileField <EOL> file_fields = { } <EOL> flag = False <EOL> fields_list = self . get_fields ( ) <EOL> for name , f in self . form . fields_list : <EOL> if isinstance ( f , FileField ) : <EOL> if name in data and data [ name ] : <EOL> if edit : <EOL> if not obj : <EOL> raise UliwebError ( "<STR_LIT>" ) <EOL> filename = getattr ( obj , name ) <EOL> if filename : <EOL> self . fileserving . delete_filename ( filename ) <EOL> data [ name ] = self . _process_file ( obj , data [ name ] , f ) <EOL> flag = True <EOL> else : <EOL> if edit : <EOL> data . pop ( name , None ) <EOL> return flag <EOL> def _get_upload_path ( self , f , name , obj ) : <EOL> func = getattr ( f , name ) <EOL> path = '<STR_LIT>' <EOL> if func : <EOL> if callable ( func ) : <EOL> path = func ( obj ) <EOL> else : <EOL> path = func <EOL> return path <EOL> def _process_file ( self , obj , fobj , field ) : <EOL> """<STR_LIT>""" <EOL> from uliweb import settings <EOL> paths = [ ] <EOL> upload_to = self . upload_to or self . _get_upload_path ( field , '<STR_LIT>' , obj ) <EOL> if upload_to : <EOL> self . fileserving . to_path = upload_to <EOL> upload_to_sub = self . upload_to_sub or self . _get_upload_path ( field , '<STR_LIT>' , obj ) <EOL> if upload_to_sub : <EOL> paths . append ( upload_to_sub ) <EOL> paths . append ( fobj [ '<STR_LIT:filename>' ] ) <EOL> return self . fileserving . save_file ( os . path . join ( * paths ) , <EOL> fobj [ '<STR_LIT:file>' ] , replace = self . file_replace , <EOL> convert = self . file_convert ) <EOL> def on_success_data ( self , obj , data ) : <EOL> if self . success_data is True : <EOL> return obj . to_dict ( ) <EOL> elif callable ( self . success_data ) : <EOL> return self . success_data ( obj , data ) <EOL> else : <EOL> return None <EOL> def on_fail_data ( self , obj , errors , data ) : <EOL> if callable ( self . fail_data ) : <EOL> return self . fail_data ( obj , errors , data ) <EOL> else : <EOL> return errors <EOL> def get_url ( self , obj ) : <EOL> if callable ( self . ok_url ) : <EOL> args = inspect . getargspec ( self . ok_url ) . args <EOL> kwargs = { } <EOL> _dict = obj . to_dict ( ) <EOL> for k in args : <EOL> if k in _dict : <EOL> kwargs [ k ] = _dict [ k ] <EOL> elif k == '<STR_LIT>' : <EOL> kwargs [ '<STR_LIT>' ] = obj <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( k ) ) <EOL> else : <EOL> kwargs = obj . to_dict ( ) <EOL> url = get_url ( self . ok_url , ** kwargs ) <EOL> return url <EOL> def on_success ( self , d , json_result = False ) : <EOL> from uliweb import response <EOL> if self . pre_save : <EOL> self . pre_save ( d ) <EOL> files = self . process_files ( d , edit = False ) <EOL> obj = self . _save ( d ) <EOL> if self . post_save : <EOL> self . post_save ( obj , d ) <EOL> if json_result : <EOL> return to_json_result ( True , self . success_msg , self . on_success_data ( obj , d ) , json_func = self . json_func ) <EOL> else : <EOL> if self . use_flash : <EOL> if '<STR_LIT>' in functions : <EOL> functions . flash ( self . success_msg ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" ) <EOL> if self . ok_url : <EOL> return redirect ( self . get_url ( obj ) ) <EOL> else : <EOL> response . template = self . ok_template <EOL> return d <EOL> def on_fail ( self , d , json_result = False ) : <EOL> log . debug ( self . form . errors ) <EOL> if json_result : <EOL> return to_json_result ( False , self . form . errors . get ( '<STR_LIT:_>' ) or self . fail_msg , self . on_fail_data ( None , self . form . errors , d ) , json_func = self . json_func ) <EOL> else : <EOL> if self . use_flash : <EOL> functions . flash ( self . form . errors . get ( '<STR_LIT:_>' ) or self . fail_msg , '<STR_LIT:error>' ) <EOL> return d <EOL> def display ( self , json_result = False ) : <EOL> d = self . template_data . copy ( ) <EOL> d . update ( { '<STR_LIT>' : self . form } ) <EOL> return d <EOL> def _get_data ( self ) : <EOL> from uliweb import request <EOL> from json import loads <EOL> if '<STR_LIT:application/json>' in request . content_type : <EOL> data = ( loads ( request . data ) , ) <EOL> else : <EOL> data = request . values , request . files <EOL> return data <EOL> def execute ( self , json_result = False ) : <EOL> import copy <EOL> flag = self . _post_protect ( ) <EOL> if flag : <EOL> flag = self . form . validate ( * self . _get_data ( ) ) <EOL> if flag : <EOL> d = copy . deepcopy ( self . default_data ) <EOL> d . update ( self . form . data ) <EOL> return self . on_success ( d , json_result ) <EOL> else : <EOL> d = copy . deepcopy ( self . template_data ) <EOL> data = self . prepare_static_data ( self . form . data ) <EOL> self . _pre_protect ( data ) <EOL> self . form . bind ( data ) <EOL> d . update ( { '<STR_LIT>' : self . form } ) <EOL> if self . post_fail : <EOL> self . post_fail ( d ) <EOL> return self . on_fail ( d , json_result ) <EOL> def _get_protect_key ( self , token ) : <EOL> return '<STR_LIT>' + token <EOL> def _pre_protect ( self , data ) : <EOL> from uliweb . utils . common import get_uuid <EOL> from uliweb . form import HiddenField <EOL> from uliweb import request <EOL> if self . protect : <EOL> if self . protect_field_name not in self . form . fields : <EOL> self . form . add_field ( self . protect_field_name , HiddenField ( ) , True ) <EOL> cache = functions . get_cache ( ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> token = get_uuid ( ) <EOL> data [ self . protect_field_name ] = token <EOL> else : <EOL> token = request . values . get ( self . protect_field_name , '<STR_LIT>' ) <EOL> if token : <EOL> cache . set ( self . _get_protect_key ( token ) , <NUM_LIT:1> , expire = <NUM_LIT> ) <EOL> def _post_protect ( self ) : <EOL> from uliweb import request <EOL> flag = True <EOL> if self . protect : <EOL> v = request . values . get ( self . protect_field_name ) <EOL> if not v : <EOL> flag = False <EOL> if flag : <EOL> cache = functions . get_cache ( ) <EOL> t = cache . dec ( self . _get_protect_key ( v ) ) <EOL> if t < <NUM_LIT:0> : <EOL> flag = False <EOL> if not flag : <EOL> self . form . errors = { '<STR_LIT:_>' : _ ( "<STR_LIT>" ) } <EOL> return flag <EOL> def run ( self , json_result = False ) : <EOL> from uliweb import request <EOL> if request . method == '<STR_LIT:POST>' : <EOL> return self . execute ( json_result ) <EOL> else : <EOL> data = self . prepare_static_data ( self . form . data ) <EOL> self . _pre_protect ( data ) <EOL> self . form . bind ( data ) <EOL> return self . display ( json_result ) <EOL> def save ( self , data ) : <EOL> obj = self . model ( ** data ) <EOL> if self . version : <EOL> obj . save ( version = self . version , version_fieldname = self . version_fieldname , <EOL> version_exception = self . version_exception ) <EOL> else : <EOL> obj . save ( ) <EOL> return obj <EOL> class EditView ( AddView ) : <EOL> success_msg = _ ( '<STR_LIT>' ) <EOL> fail_msg = _ ( '<STR_LIT>' ) <EOL> builds_args_map = { } <EOL> def __init__ ( self , model , ok_url = None , condition = None , obj = None , meta = '<STR_LIT>' , ** kwargs ) : <EOL> self . model = get_model ( model ) <EOL> self . condition = condition <EOL> self . obj = obj or self . query ( ) <EOL> AddView . __init__ ( self , model , ok_url , meta = meta , ** kwargs ) <EOL> self . form . object = self . obj <EOL> def display ( self , json_result = False ) : <EOL> d = self . template_data . copy ( ) <EOL> d . update ( { '<STR_LIT>' : self . form , '<STR_LIT:object>' : self . obj } ) <EOL> return d <EOL> def execute ( self , json_result = False ) : <EOL> flag = self . form . validate ( * self . _get_data ( ) ) <EOL> if flag : <EOL> d = self . default_data . copy ( ) <EOL> d . update ( self . form . data ) <EOL> return self . on_success ( d , json_result ) <EOL> else : <EOL> d = self . template_data . copy ( ) <EOL> new_d = self . prepare_static_data ( self . form . data ) <EOL> self . form . bind ( new_d ) <EOL> d . update ( { '<STR_LIT>' : self . form , '<STR_LIT:object>' : self . obj } ) <EOL> if self . post_fail : <EOL> self . post_fail ( d , self . obj ) <EOL> return self . on_fail ( d , json_result ) <EOL> def on_success ( self , d , json_result ) : <EOL> from uliweb import response <EOL> if self . pre_save : <EOL> self . pre_save ( self . obj , d ) <EOL> r = self . process_files ( d , edit = True , obj = self . obj ) <EOL> r = self . _save ( self . obj , d ) or r <EOL> if self . post_save : <EOL> r = self . post_save ( self . obj , d ) or r <EOL> if r : <EOL> msg = self . success_msg <EOL> else : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> if json_result : <EOL> return to_json_result ( True , msg , self . on_success_data ( self . obj , d ) , modified = r , json_func = self . json_func ) <EOL> else : <EOL> if self . use_flash : <EOL> functions . flash ( msg ) <EOL> if self . ok_url : <EOL> return redirect ( self . get_url ( self . obj ) ) <EOL> else : <EOL> response . template = self . ok_template <EOL> return d <EOL> def on_fail ( self , d , json_result = False ) : <EOL> log . debug ( self . form . errors ) <EOL> if json_result : <EOL> return to_json_result ( False , self . fail_msg , self . on_fail_data ( self . obj , self . form . errors , d ) , json_func = self . json_func ) <EOL> else : <EOL> if self . use_flash : <EOL> functions . flash ( self . fail_msg , '<STR_LIT:error>' ) <EOL> return d <EOL> def prepare_static_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> d = self . obj . to_dict ( ) <EOL> d . update ( data . copy ( ) ) <EOL> for f in self . get_fields ( ) : <EOL> if f [ '<STR_LIT>' ] and f [ '<STR_LIT:name>' ] in d : <EOL> v = make_view_field ( f , self . obj , self . types_convert_map , self . fields_convert_map , d [ f [ '<STR_LIT:name>' ] ] ) <EOL> d [ f [ '<STR_LIT:name>' ] ] = v [ '<STR_LIT>' ] <EOL> return d <EOL> def run ( self , json_result = False ) : <EOL> from uliweb import request <EOL> if request . method == '<STR_LIT:POST>' : <EOL> return self . execute ( json_result ) <EOL> else : <EOL> d = self . prepare_static_data ( self . form . data ) <EOL> self . form . bind ( d ) <EOL> return self . display ( json_result ) <EOL> def save ( self , obj , data ) : <EOL> obj . update ( ** data ) <EOL> if self . version : <EOL> r = obj . save ( version = self . version , version_fieldname = self . version_fieldname , <EOL> version_exception = self . version_exception ) <EOL> else : <EOL> r = obj . save ( ) <EOL> return r <EOL> def query ( self ) : <EOL> return self . model . get ( self . condition ) <EOL> def make_form ( self , form ) : <EOL> from uliweb . form import Form , Button , make_form <EOL> if form : <EOL> return form <EOL> DummyForm = make_form ( layout_class = self . layout_class , layout = self . layout , <EOL> base_class = self . form_cls , rules = self . rules ) <EOL> fields_list = self . get_fields ( ) <EOL> fields_name = [ x [ '<STR_LIT:name>' ] for x in fields_list ] <EOL> data = self . obj . to_dict ( fields_name , convert = False ) . copy ( ) <EOL> data . update ( self . data ) <EOL> layout = self . get_layout ( ) <EOL> DummyForm . layout = layout <EOL> for f in fields_list : <EOL> if f [ '<STR_LIT:name>' ] == '<STR_LIT:id>' : <EOL> f [ '<STR_LIT>' ] = True <EOL> elif isinstance ( f [ '<STR_LIT>' ] , orm . IntegerProperty ) and '<STR_LIT>' in f [ '<STR_LIT>' ] . kwargs : <EOL> f [ '<STR_LIT>' ] = True <EOL> flag = False <EOL> if self . get_form_field : <EOL> field = self . get_form_field ( f [ '<STR_LIT:name>' ] , self . obj ) <EOL> if field : <EOL> flag = True <EOL> if not flag : <EOL> field = make_form_field ( f , self . model , builds_args_map = self . builds_args_map ) <EOL> if field : <EOL> DummyForm . add_field ( f [ '<STR_LIT:name>' ] , field , True ) <EOL> if isinstance ( f [ '<STR_LIT>' ] , orm . ManyToMany ) : <EOL> value = getattr ( self . obj , f [ '<STR_LIT:name>' ] ) . ids ( ) <EOL> data [ f [ '<STR_LIT:name>' ] ] = value <EOL> if self . post_created_form : <EOL> self . post_created_form ( DummyForm , self . model , self . obj ) <EOL> return DummyForm ( data = data , ** self . form_args ) <EOL> from uliweb . core import uaml <EOL> from uliweb . core . html import begin_tag , end_tag , u_str <EOL> class DetailWriter ( uaml . Writer ) : <EOL> def __init__ ( self , get_field ) : <EOL> self . get_field = get_field <EOL> def do_static ( self , indent , value , ** kwargs ) : <EOL> name = kwargs . get ( '<STR_LIT:name>' , None ) <EOL> if name : <EOL> f = self . get_field ( name ) <EOL> f [ '<STR_LIT>' ] = f [ '<STR_LIT>' ] or '<STR_LIT>' <EOL> return indent * '<STR_LIT:U+0020>' + '<STR_LIT>' % f <EOL> else : <EOL> return '<STR_LIT>' <EOL> def do_td_field ( self , indent , value , ** kwargs ) : <EOL> name = kwargs . pop ( '<STR_LIT:name>' , None ) <EOL> if name : <EOL> f = self . get_field ( name ) <EOL> f [ '<STR_LIT>' ] = f [ '<STR_LIT>' ] or '<STR_LIT>' <EOL> if '<STR_LIT:width>' not in kwargs : <EOL> kwargs [ '<STR_LIT:width>' ] = <NUM_LIT:200> <EOL> td = begin_tag ( '<STR_LIT>' , ** kwargs ) + u_str ( f [ '<STR_LIT>' ] ) + end_tag ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' % f + td <EOL> else : <EOL> return '<STR_LIT>' <EOL> class DetailLayout ( object ) : <EOL> def __init__ ( self , layout_file , get_field , model = None , writer = None , ** kwargs ) : <EOL> self . layout_file = layout_file <EOL> self . writer = writer or DetailWriter ( get_field ) <EOL> self . model = model <EOL> self . kwargs = kwargs <EOL> def get_text ( self ) : <EOL> from uliweb import application <EOL> f = file ( application . get_file ( self . layout_file , dir = '<STR_LIT>' ) , '<STR_LIT:rb>' ) <EOL> text = f . read ( ) <EOL> f . close ( ) <EOL> return text <EOL> def render ( self ) : <EOL> return uaml . Parser ( self . get_text ( ) , self . writer ) <EOL> def __str__ ( self ) : <EOL> return str ( self . render ( ) ) <EOL> class DetailTableLayout ( object ) : <EOL> def __init__ ( self , layout , get_field , model = None , table_class = '<STR_LIT>' ) : <EOL> self . layout = layout <EOL> self . get_field = get_field <EOL> self . model = model <EOL> self . table_class = table_class <EOL> def line ( self , fields , n ) : <EOL> from uliweb . core . html import Tag <EOL> _x = <NUM_LIT:0> <EOL> for _f in fields : <EOL> if isinstance ( _f , ( str , unicode ) ) : <EOL> _x += <NUM_LIT:1> <EOL> elif isinstance ( _f , dict ) : <EOL> _x += _f . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> else : <EOL> raise Exception , '<STR_LIT>' <EOL> tr = Tag ( '<STR_LIT>' ) <EOL> with tr : <EOL> for x in fields : <EOL> _span = n / _x <EOL> if isinstance ( x , ( str , unicode ) ) : <EOL> f = self . get_field ( x ) <EOL> elif isinstance ( x , dict ) : <EOL> f = self . get_field ( x [ '<STR_LIT:name>' ] ) <EOL> _span = _span * x . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> with tr . td ( colspan = _span , width = '<STR_LIT>' % ( <NUM_LIT:100> * _span / n , ) ) : <EOL> with tr . div : <EOL> with tr . span ( _class = '<STR_LIT>' ) : <EOL> tr << '<STR_LIT>' + f [ '<STR_LIT:label>' ] + '<STR_LIT>' <EOL> if isinstance ( x , dict ) and x . get ( '<STR_LIT>' ) : <EOL> tr << '<STR_LIT>' <EOL> with tr . span ( _class = '<STR_LIT>' ) : <EOL> tr << f [ '<STR_LIT>' ] <EOL> return tr <EOL> def render ( self ) : <EOL> from uliweb . core . html import Builder <EOL> from uliweb . form . layout import min_times <EOL> m = [ ] <EOL> for line in self . layout : <EOL> if isinstance ( line , ( tuple , list ) ) : <EOL> _x = <NUM_LIT:0> <EOL> for f in line : <EOL> if isinstance ( f , ( str , unicode ) ) : <EOL> _x += <NUM_LIT:1> <EOL> elif isinstance ( f , dict ) : <EOL> _x += f . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> else : <EOL> raise Exception , '<STR_LIT>' <EOL> m . append ( _x ) <EOL> else : <EOL> m . append ( <NUM_LIT:1> ) <EOL> n = min_times ( m ) <EOL> buf = Builder ( '<STR_LIT>' , '<STR_LIT:body>' , '<STR_LIT:end>' ) <EOL> table = None <EOL> fieldset = None <EOL> first = True <EOL> for fields in self . layout : <EOL> if not isinstance ( fields , ( tuple , list ) ) : <EOL> if isinstance ( fields , ( str , unicode ) ) and fields . startswith ( '<STR_LIT>' ) and fields . endswith ( '<STR_LIT>' ) : <EOL> if table : <EOL> buf . body << '<STR_LIT>' <EOL> if fieldset : <EOL> buf . body << '<STR_LIT>' <EOL> title = fields [ <NUM_LIT:2> : - <NUM_LIT:2> ] . strip ( ) <EOL> if title : <EOL> fieldset = True <EOL> buf . body << '<STR_LIT>' <EOL> buf . body << self . title ( title ) <EOL> buf . body << '<STR_LIT>' % self . table_class <EOL> table = True <EOL> first = False <EOL> continue <EOL> else : <EOL> fields = [ fields ] <EOL> if first : <EOL> first = False <EOL> buf . begin << '<STR_LIT>' % self . table_class <EOL> buf . body << '<STR_LIT>' <EOL> table = True <EOL> buf . body << self . line ( fields , n ) <EOL> if table : <EOL> buf . end << '<STR_LIT>' <EOL> buf . body << '<STR_LIT>' <EOL> if fieldset : <EOL> buf . body << '<STR_LIT>' <EOL> return buf <EOL> def __str__ ( self ) : <EOL> return str ( self . render ( ) ) <EOL> def title ( self , title = '<STR_LIT>' ) : <EOL> if title : <EOL> return '<STR_LIT>' % title <EOL> return '<STR_LIT>' <EOL> class DetailView ( object ) : <EOL> def __init__ ( self , model , condition = None , obj = None , fields = None , <EOL> types_convert_map = None , fields_convert_map = None , table_class_attr = '<STR_LIT>' , <EOL> layout_class = None , layout = None , layout_kwargs = None , template_data = None , meta = '<STR_LIT>' ) : <EOL> self . model = get_model ( model ) <EOL> self . meta = meta <EOL> self . condition = condition <EOL> if not obj : <EOL> self . obj = self . query ( ) <EOL> else : <EOL> self . obj = obj <EOL> self . fields = fields <EOL> self . types_convert_map = types_convert_map or { } <EOL> self . fields_convert_map = fields_convert_map or { } <EOL> self . table_class_attr = table_class_attr <EOL> self . layout = layout or get_layout ( model , meta ) <EOL> self . layout_class = layout_class <EOL> if isinstance ( self . layout , ( str , unicode ) ) : <EOL> self . layout_class = layout_class or DetailLayout <EOL> elif isinstance ( self . layout , ( tuple , list ) ) : <EOL> self . layout_class = layout_class or DetailTableLayout <EOL> self . template_data = template_data or { } <EOL> self . result_fields = Storage ( { } ) <EOL> self . r = self . result_fields <EOL> self . f = Storage ( { } ) <EOL> self . layout_kwargs = layout_kwargs or { } <EOL> def run ( self ) : <EOL> text = self . render ( ) <EOL> result = self . template_data . copy ( ) <EOL> result . update ( { '<STR_LIT:object>' : self . obj , '<STR_LIT>' : text , '<STR_LIT>' : self } ) <EOL> return result <EOL> def query ( self ) : <EOL> return self . model . get ( self . condition ) <EOL> def render ( self ) : <EOL> if self . layout : <EOL> fields = dict ( get_fields ( self . model , self . fields , self . meta ) ) <EOL> def get_field ( name ) : <EOL> prop = fields [ name ] <EOL> return make_view_field ( prop , self . obj , self . types_convert_map , self . fields_convert_map ) <EOL> return self . layout_class ( self . layout , get_field , self . model , ** self . layout_kwargs ) . render ( ) <EOL> else : <EOL> return self . _render ( ) <EOL> def _render ( self ) : <EOL> b = Builder ( '<STR_LIT>' , '<STR_LIT:body>' , '<STR_LIT:end>' ) <EOL> b . begin << '<STR_LIT>' % self . table_class_attr <EOL> text = [ ] <EOL> for field_name , prop in get_fields ( self . model , self . fields , self . meta ) : <EOL> field = make_view_field ( prop , self . obj , self . types_convert_map , self . fields_convert_map ) <EOL> if field : <EOL> text . append ( '<STR_LIT>' % ( field [ "<STR_LIT:label>" ] , field [ "<STR_LIT>" ] ) ) <EOL> self . result_fields [ field_name ] = field <EOL> self . f [ field_name ] = field [ '<STR_LIT>' ] <EOL> b . body << '<STR_LIT:\n>' . join ( text ) <EOL> b . end << '<STR_LIT>' <EOL> return b <EOL> def body ( self ) : <EOL> text = [ ] <EOL> for field_name , prop in get_fields ( self . model , self . fields , self . meta ) : <EOL> field = make_view_field ( prop , self . obj , self . types_convert_map , self . fields_convert_map ) <EOL> if field : <EOL> text . append ( '<STR_LIT>' % ( field [ "<STR_LIT:label>" ] , field [ "<STR_LIT>" ] ) ) <EOL> self . result_fields [ field_name ] = field <EOL> self . f [ field_name ] = field [ '<STR_LIT>' ] <EOL> return '<STR_LIT:\n>' . join ( text ) <EOL> class DeleteView ( object ) : <EOL> success_msg = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self , model , ok_url = '<STR_LIT>' , fail_url = '<STR_LIT>' , condition = None , obj = None , <EOL> pre_delete = None , post_delete = None , validator = None , json_func = None , <EOL> use_flash = True , use_delete_fieldname = None , success_data = None , <EOL> fail_data = None ) : <EOL> self . model = get_model ( model ) <EOL> self . condition = condition <EOL> self . obj = obj <EOL> self . validator = validator <EOL> self . json_func = json_func or json <EOL> if not obj : <EOL> self . obj = self . model . get ( self . condition ) <EOL> else : <EOL> self . obj = obj <EOL> self . ok_url = ok_url <EOL> self . fail_url = fail_url <EOL> self . pre_delete = pre_delete <EOL> self . post_delete = post_delete <EOL> self . use_flash = use_flash <EOL> self . use_delete_fieldname = use_delete_fieldname <EOL> self . success_data = success_data <EOL> self . fail_data = fail_data <EOL> def run ( self , json_result = False ) : <EOL> if self . validator : <EOL> msg = self . validator ( self . obj ) <EOL> if msg : <EOL> if json_result : <EOL> return to_json_result ( False , msg , self . on_fail_data ( self . obj , { } , { } ) , json_func = self . json_func ) <EOL> else : <EOL> if self . use_flash : <EOL> functions . flash ( msg , '<STR_LIT:error>' ) <EOL> return redirect ( self . fail_url ) <EOL> if self . pre_delete : <EOL> self . pre_delete ( self . obj ) <EOL> self . delete ( self . obj ) <EOL> if self . post_delete : <EOL> self . post_delete ( ) <EOL> if json_result : <EOL> return to_json_result ( True , self . success_msg , self . on_success_data ( None , { } ) , json_func = self . json_func ) <EOL> else : <EOL> if self . use_flash : <EOL> functions . flash ( self . success_msg ) <EOL> return redirect ( get_url ( self . ok_url ) ) <EOL> def on_success_data ( self , obj , data ) : <EOL> if callable ( self . success_data ) : <EOL> return self . success_data ( obj , data ) <EOL> else : <EOL> return None <EOL> def on_fail_data ( self , obj , errors , data ) : <EOL> if callable ( self . fail_data ) : <EOL> return self . fail_data ( obj , errors , data ) <EOL> else : <EOL> return errors <EOL> def delete ( self , obj ) : <EOL> if obj : <EOL> self . delete_manytomany ( obj ) <EOL> if self . use_delete_fieldname : <EOL> setattr ( obj , self . use_delete_fieldname , True ) <EOL> obj . save ( ) <EOL> else : <EOL> obj . delete ( ) <EOL> def delete_manytomany ( self , obj ) : <EOL> for k , v in obj . _manytomany . iteritems ( ) : <EOL> getattr ( obj , k ) . clear ( ) <EOL> class GenericFileServing ( FileServing ) : <EOL> default_config = '<STR_LIT>' <EOL> options = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , FilenameConverter ) , <EOL> } <EOL> def download_file ( filename ) : <EOL> from uliweb import request <EOL> import urllib2 <EOL> alt_filename = request . GET . get ( '<STR_LIT>' ) <EOL> if not alt_filename : <EOL> alt_filename = filename <EOL> else : <EOL> alt_filename = urllib2 . unquote ( alt_filename ) <EOL> downloader = GenericFileServing ( ) <EOL> real_filename = downloader . get_filename ( filename , convert = False ) <EOL> x_sendfile = downloader . x_sendfile <EOL> if x_sendfile : <EOL> x_filename = os . path . join ( downloader . x_file_prefix , filename ) <EOL> else : <EOL> x_filename = '<STR_LIT>' <EOL> return downloader . download ( alt_filename , action = '<STR_LIT>' , real_filename = real_filename , <EOL> x_sendfile = x_sendfile , x_filename = x_filename ) <EOL> class SimpleListView ( object ) : <EOL> def __init__ ( self , fields = None , query = None , <EOL> pageno = <NUM_LIT:0> , rows_per_page = <NUM_LIT:10> , id = '<STR_LIT>' , fields_convert_map = None , <EOL> table_class_attr = '<STR_LIT>' , table_width = False , pagination = True , total_fields = None , <EOL> template_data = None , default_column_width = <NUM_LIT:100> , total = None , manual = False , <EOL> render = None , record_render = None , post_record_render = None ) : <EOL> """<STR_LIT>""" <EOL> self . fields = fields <EOL> self . _query = query <EOL> self . pageno = pageno <EOL> self . rows_per_page = rows_per_page <EOL> self . rows_num = <NUM_LIT:0> <EOL> self . id = id <EOL> self . table_class_attr = table_class_attr <EOL> self . fields_convert_map = fields_convert_map or { } <EOL> self . total = total or <NUM_LIT:0> <EOL> self . table_width = table_width <EOL> self . pagination = pagination <EOL> self . create_total_infos ( total_fields ) <EOL> self . template_data = template_data or { } <EOL> self . default_column_width = default_column_width <EOL> self . manual = manual <EOL> self . downloader = GenericFileServing ( ) <EOL> self . render_func = render <EOL> self . record_render = record_render <EOL> self . post_record_render = post_record_render <EOL> self . init ( ) <EOL> def init ( self ) : <EOL> from uliweb import request <EOL> if '<STR_LIT>' in request . values : <EOL> self . pageno = int ( request . values . get ( '<STR_LIT>' ) ) - <NUM_LIT:1> <EOL> if '<STR_LIT>' in request . values : <EOL> self . rows_per_page = int ( request . values . get ( '<STR_LIT>' ) ) <EOL> elif '<STR_LIT>' in request . values : <EOL> self . rows_per_page = int ( request . values . get ( '<STR_LIT>' ) ) <EOL> self . table_info = self . get_table_info ( ) <EOL> def create_total_infos ( self , total_fields ) : <EOL> if total_fields : <EOL> self . total_fields = { } <EOL> for x in total_fields [ '<STR_LIT>' ] : <EOL> if isinstance ( x , ( str , unicode ) ) : <EOL> self . total_fields [ x ] = { } <EOL> elif isinstance ( x , dict ) : <EOL> self . total_fields [ x [ '<STR_LIT:name>' ] ] = x <EOL> else : <EOL> raise Exception , "<STR_LIT>" % ( type ( x ) , x ) <EOL> total_fields [ '<STR_LIT>' ] <EOL> self . total_field_name = total_fields . get ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> self . total_fields = { } <EOL> self . total_field_name = None <EOL> self . total_sums = { } <EOL> def _cal_sum ( self , record ) : <EOL> if self . total_fields : <EOL> for f in self . total_fields : <EOL> if isinstance ( record , ( tuple , list ) ) : <EOL> i = self . table_info [ '<STR_LIT>' ] . index ( f ) <EOL> v = record [ i ] <EOL> elif isinstance ( record , dict ) : <EOL> v = record . get ( f ) <EOL> else : <EOL> v = getattr ( record , f ) <EOL> x = self . total_fields [ f ] <EOL> cal = x . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if cal : <EOL> self . total_sums [ f ] = self . total_sums . setdefault ( f , <NUM_LIT:0> ) + v <EOL> def _get_sum ( self ) : <EOL> s = [ ] <EOL> if self . total_fields : <EOL> for i , f in enumerate ( self . table_info [ '<STR_LIT>' ] ) : <EOL> if i == <NUM_LIT:0> : <EOL> v = self . total_field_name <EOL> else : <EOL> if f in self . total_fields : <EOL> v = self . total_sums . get ( f , <NUM_LIT:0> ) <EOL> x = self . total_fields [ f ] <EOL> cal = x . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if cal == '<STR_LIT>' : <EOL> pass <EOL> elif cal == '<STR_LIT>' : <EOL> v = v * <NUM_LIT:1.0> / self . rows_num <EOL> render = x . get ( '<STR_LIT>' , None ) <EOL> if render : <EOL> v = render ( v , self . total_sums ) <EOL> else : <EOL> v = str ( v ) <EOL> else : <EOL> v = '<STR_LIT>' <EOL> s . append ( v ) <EOL> return s <EOL> def _render_sum ( self , json = False ) : <EOL> s = [ ] <EOL> if self . total_fields : <EOL> if json : <EOL> for v in self . _get_sum ( ) : <EOL> v = str ( v ) <EOL> s . append ( v ) <EOL> return dict ( zip ( self . table_info [ '<STR_LIT>' ] , s ) ) <EOL> else : <EOL> s . append ( '<STR_LIT>' ) <EOL> for v in self . _get_sum ( ) : <EOL> v = str ( v ) or '<STR_LIT>' <EOL> s . append ( '<STR_LIT>' % v ) <EOL> s . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> return '<STR_LIT>' <EOL> def query_all ( self ) : <EOL> return self . query_range ( <NUM_LIT:0> , pagination = False ) <EOL> def query ( self ) : <EOL> return self . query_range ( self . pageno , self . pagination ) <EOL> def query_range ( self , pageno = <NUM_LIT:0> , pagination = True ) : <EOL> if callable ( self . _query ) : <EOL> query_result = self . _query ( ) <EOL> else : <EOL> query_result = self . _query <EOL> def repeat ( data , begin , n ) : <EOL> result = [ ] <EOL> no_data_flag = False <EOL> i = <NUM_LIT:0> <EOL> while ( begin > <NUM_LIT:0> and i < begin ) or ( begin == - <NUM_LIT:1> ) : <EOL> try : <EOL> result . append ( data . next ( ) ) <EOL> i += <NUM_LIT:1> <EOL> n += <NUM_LIT:1> <EOL> except StopIteration : <EOL> no_data_flag = True <EOL> break <EOL> return no_data_flag , n , result <EOL> if self . manual : <EOL> if isinstance ( query_result , ( list , tuple ) ) : <EOL> if not self . total : <EOL> self . total = len ( query_result ) <EOL> return query_result <EOL> else : <EOL> if not self . total : <EOL> flag , self . total , result = repeat ( query_result , - <NUM_LIT:1> , self . total ) <EOL> else : <EOL> result = query_result <EOL> return result <EOL> else : <EOL> self . total = <NUM_LIT:0> <EOL> if pagination : <EOL> if isinstance ( query_result , ( list , tuple ) ) : <EOL> self . total = len ( query_result ) <EOL> result = query_result [ pageno * self . rows_per_page : ( pageno + <NUM_LIT:1> ) * self . rows_per_page ] <EOL> return result <EOL> else : <EOL> flag , self . total , result = repeat ( query_result , pageno * self . rows_per_page , self . total ) <EOL> if flag : <EOL> return [ ] <EOL> flag , self . total , result = repeat ( query_result , self . rows_per_page , self . total ) <EOL> if flag : <EOL> return result <EOL> flag , self . total , r = repeat ( query_result , - <NUM_LIT:1> , self . total ) <EOL> return result <EOL> else : <EOL> if isinstance ( query_result , ( list , tuple ) ) : <EOL> self . total = len ( query_result ) <EOL> return query_result <EOL> else : <EOL> flag , self . total , result = repeat ( query_result , - <NUM_LIT:1> , self . total ) <EOL> return result <EOL> def count ( self , query ) : <EOL> """<STR_LIT>""" <EOL> if self . manual : <EOL> return self . total <EOL> if isinstance ( query , Select ) : <EOL> q = query . with_only_columns ( [ func . count ( ) ] ) . order_by ( None ) . limit ( None ) . offset ( None ) <EOL> return do_ ( q ) . scalar ( ) <EOL> return query . count ( ) <EOL> def download ( self , filename , timeout = <NUM_LIT> , action = None , query = None , fields_convert_map = None , type = None , domain = None ) : <EOL> """<STR_LIT>""" <EOL> from uliweb import settings <EOL> fields_convert_map = fields_convert_map or self . fields_convert_map <EOL> t_filename = self . get_real_file ( filename ) <EOL> if os . path . exists ( t_filename ) : <EOL> if timeout and os . path . getmtime ( t_filename ) + timeout > time . time ( ) : <EOL> return self . downloader . download ( filename , action ) <EOL> if not query : <EOL> query = self . query_all ( ) <EOL> if not type : <EOL> type = os . path . splitext ( filename ) [ <NUM_LIT:1> ] <EOL> if type : <EOL> type = type [ <NUM_LIT:1> : ] <EOL> else : <EOL> type = '<STR_LIT>' <EOL> if type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not domain : <EOL> domain = settings . get_var ( '<STR_LIT>' ) <EOL> return self . download_xlt ( filename , query , action , fields_convert_map , domain , not_tempfile = bool ( timeout ) ) <EOL> else : <EOL> return self . download_csv ( filename , query , action , fields_convert_map , not_tempfile = bool ( timeout ) ) <EOL> def get_column ( self , name , model = None ) : <EOL> """<STR_LIT>""" <EOL> col = None <EOL> if '<STR_LIT:.>' in name : <EOL> m , fname = name . split ( '<STR_LIT:.>' ) <EOL> model = get_model ( m ) <EOL> if model and fname in model . c : <EOL> col = get_model ( m ) . c [ fname ] <EOL> else : <EOL> if model and name in model . c : <EOL> col = model . c [ name ] <EOL> return model , col <EOL> def get_field ( self , name , model = None ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:.>' in name : <EOL> m , name = name . split ( '<STR_LIT:.>' ) <EOL> model = get_model ( m ) <EOL> if model : <EOL> return getattr ( model , name , None ) <EOL> def get_table_meta_field ( self , name , model = None ) : <EOL> field = self . get_field ( name , model ) <EOL> if field : <EOL> d = { '<STR_LIT:name>' : name , '<STR_LIT>' : field . verbose_name or field . name } <EOL> return d <EOL> def _get_record ( self , record ) : <EOL> r = record <EOL> if isinstance ( record , RowProxy ) : <EOL> r = { } <EOL> labels = self . table_info [ '<STR_LIT>' ] <EOL> keys = self . table_info [ '<STR_LIT>' ] <EOL> _keys = [ ] <EOL> for k , v in record . items ( ) : <EOL> key = labels . get ( k ) <EOL> if key : <EOL> r [ key ] = v <EOL> _keys . append ( key ) <EOL> for k in list ( set ( keys ) - set ( _keys ) ) : <EOL> r [ k ] = None <EOL> elif not isinstance ( record , ( orm . Model , dict ) ) : <EOL> if not isinstance ( record , ( tuple , list ) ) : <EOL> record = list ( record ) <EOL> r = dict ( zip ( self . table_info [ '<STR_LIT>' ] , record ) ) <EOL> return r <EOL> def get_data ( self , query , fields_convert_map , encoding = '<STR_LIT:utf-8>' , auto_convert = True , include_hidden = False ) : <EOL> """<STR_LIT>""" <EOL> fields_convert_map = fields_convert_map or { } <EOL> d = self . fields_convert_map . copy ( ) <EOL> d . update ( fields_convert_map ) <EOL> if isinstance ( query , Select ) : <EOL> query = do_ ( query ) <EOL> def get_value ( name , value , record ) : <EOL> convert = d . get ( name ) <EOL> if convert : <EOL> value = convert ( value , record ) <EOL> return safe_unicode ( value , encoding ) <EOL> for record in query : <EOL> self . _cal_sum ( record ) <EOL> row = [ ] <EOL> record = self . _get_record ( record ) <EOL> if isinstance ( record , orm . Model ) : <EOL> model = record . __class__ <EOL> else : <EOL> model = None <EOL> for i , x in enumerate ( self . table_info [ '<STR_LIT>' ] ) : <EOL> field = self . get_field ( x [ '<STR_LIT:name>' ] , model ) <EOL> if not field : <EOL> field = { '<STR_LIT:name>' : x [ '<STR_LIT:name>' ] } <EOL> else : <EOL> field = { '<STR_LIT:name>' : x [ '<STR_LIT:name>' ] , '<STR_LIT>' : field } <EOL> if not include_hidden and x . get ( '<STR_LIT>' ) : <EOL> continue <EOL> if isinstance ( record , orm . Model ) : <EOL> v = make_view_field ( field , record , fields_convert_map = d , <EOL> auto_convert = auto_convert ) <EOL> else : <EOL> v = make_view_field ( field , record , fields_convert_map = d , <EOL> auto_convert = auto_convert , value = record [ x [ '<STR_LIT:name>' ] ] ) <EOL> value = v [ '<STR_LIT>' ] <EOL> row . append ( value ) <EOL> yield row <EOL> total = self . _get_sum ( ) <EOL> if total : <EOL> row = [ ] <EOL> for x in total : <EOL> v = x <EOL> if isinstance ( x , str ) : <EOL> v = safe_unicode ( x , encoding ) <EOL> row . append ( v ) <EOL> yield row <EOL> def get_real_file ( self , filename ) : <EOL> t_filename = self . downloader . get_filename ( filename ) <EOL> return t_filename <EOL> def get_download_file ( self , filename , not_tempfile ) : <EOL> import tempfile <EOL> t_filename = self . get_real_file ( filename ) <EOL> dirname = os . path . dirname ( t_filename ) <EOL> if not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> bfile = os . path . basename ( t_filename ) <EOL> if not not_tempfile : <EOL> tfile = tempfile . NamedTemporaryFile ( suffix = "<STR_LIT>" , prefix = bfile + '<STR_LIT:_>' , dir = dirname , delete = False ) <EOL> else : <EOL> tfile = open ( t_filename , '<STR_LIT:wb>' ) <EOL> ufile = os . path . join ( os . path . dirname ( filename ) , os . path . basename ( tfile . name ) ) <EOL> return tfile , bfile , ufile <EOL> def download_xlt ( self , filename , data , action , fields_convert_map = None , domain = None , not_tempfile = False ) : <EOL> from uliweb . utils . xlt import ExcelWriter <EOL> from uliweb import request , settings <EOL> fields_convert_map = fields_convert_map or { } <EOL> tfile , bfile , ufile = self . get_download_file ( filename , not_tempfile ) <EOL> if not domain : <EOL> domain = settings . get_var ( '<STR_LIT>' , request . host_url ) <EOL> default_encoding = settings . get_var ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> header = [ x for x in self . table_info [ '<STR_LIT>' ] if not x . get ( '<STR_LIT>' ) ] <EOL> w = ExcelWriter ( header = header , data = self . get_data ( data , <EOL> fields_convert_map , default_encoding , auto_convert = False ) , <EOL> encoding = default_encoding , domain = domain ) <EOL> w . save ( tfile . name ) <EOL> return self . downloader . download ( bfile , action = action , x_filename = ufile , <EOL> real_filename = tfile . name ) <EOL> def download_csv ( self , filename , data , action , fields_convert_map = None , not_tempfile = False ) : <EOL> from uliweb import settings <EOL> from uliweb . utils . common import simple_value , safe_unicode <EOL> import csv <EOL> fields_convert_map = fields_convert_map or { } <EOL> tfile , bfile , ufile = self . get_download_file ( filename , not_tempfile ) <EOL> encoding = settings . get_var ( '<STR_LIT>' , sys . getfilesystemencoding ( ) or '<STR_LIT:utf-8>' ) <EOL> default_encoding = settings . get_var ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> with tfile as f : <EOL> w = csv . writer ( f ) <EOL> row = [ safe_unicode ( x , default_encoding ) for x in self . table_info [ '<STR_LIT>' ] ] <EOL> w . writerow ( simple_value ( row , encoding ) ) <EOL> for row in self . get_data ( data , fields_convert_map , default_encoding ) : <EOL> w . writerow ( simple_value ( row , encoding ) ) <EOL> return self . downloader . download ( bfile , action = action , x_filename = ufile , <EOL> real_filename = tfile . name ) <EOL> def run ( self , head = True , body = True , json_result = False ) : <EOL> result = self . template_data . copy ( ) <EOL> result . update ( self . render ( json_result = json_result ) ) <EOL> return result <EOL> def json ( self ) : <EOL> return self . run ( json_result = True ) <EOL> def render ( self , json_result = False ) : <EOL> result = { <EOL> '<STR_LIT>' : self . id , <EOL> '<STR_LIT>' : self . pageno + <NUM_LIT:1> , <EOL> '<STR_LIT>' : self . pageno + <NUM_LIT:1> , <EOL> '<STR_LIT>' : self . rows_per_page , <EOL> '<STR_LIT>' : self . rows_per_page , <EOL> } <EOL> if not json_result : <EOL> s = Builder ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:body>' , '<STR_LIT:end>' ) <EOL> s . begin << '<STR_LIT>' % ( self . table_class_attr , self . id ) <EOL> with s . colgroup . colgroup : <EOL> s . colgroup << self . create_table_colgroup ( ) <EOL> with s . head . thead : <EOL> s . head << self . create_table_head ( ) <EOL> s . body << '<STR_LIT>' <EOL> for r in self . objects ( ) : <EOL> render_func = self . render_func or self . default_body_render <EOL> data = [ ] <EOL> for f in self . table_info [ '<STR_LIT>' ] : <EOL> data . append ( ( f [ '<STR_LIT:name>' ] , r [ f [ '<STR_LIT:name>' ] ] ) ) <EOL> s . body << render_func ( data , r . get ( '<STR_LIT>' , { } ) ) <EOL> s . body << '<STR_LIT>' <EOL> s . end << '<STR_LIT>' <EOL> result [ '<STR_LIT>' ] = s <EOL> else : <EOL> s = [ ] <EOL> for r in self . objects ( json_result ) : <EOL> render_func = self . render_func or self . json_body_render <EOL> data = [ ] <EOL> for f in self . table_info [ '<STR_LIT>' ] : <EOL> data . append ( ( f [ '<STR_LIT:name>' ] , r [ f [ '<STR_LIT:name>' ] ] ) ) <EOL> s . append ( render_func ( data , r . get ( '<STR_LIT>' , { } ) ) ) <EOL> result [ '<STR_LIT>' ] = s <EOL> result [ '<STR_LIT>' ] = self . total <EOL> return result <EOL> def objects ( self , json_result = False ) : <EOL> """<STR_LIT>""" <EOL> self . rows_num = <NUM_LIT:0> <EOL> query = self . query ( ) <EOL> if not isinstance ( query , ( orm . Result , list , dict ) ) : <EOL> query = do_ ( query ) <EOL> for record in query : <EOL> self . rows_num += <NUM_LIT:1> <EOL> r = self . object ( record , json_result ) <EOL> self . _cal_sum ( record ) <EOL> yield r <EOL> total = self . _render_sum ( True ) <EOL> if total : <EOL> yield total <EOL> def object ( self , record , json_result = False ) : <EOL> r = SortedDict ( ) <EOL> record = self . _get_record ( record ) <EOL> if self . record_render : <EOL> r = self . record_render ( record ) <EOL> else : <EOL> for i , x in enumerate ( self . table_info [ '<STR_LIT>' ] ) : <EOL> v = self . make_view_field ( x , record , self . fields_convert_map ) <EOL> r [ x [ '<STR_LIT:name>' ] ] = v [ '<STR_LIT>' ] <EOL> r [ '<STR_LIT>' ] = record <EOL> if self . post_record_render : <EOL> self . post_record_render ( r ) <EOL> return r <EOL> def json_body_render ( self , data , record ) : <EOL> d = dict ( data ) <EOL> if '<STR_LIT:id>' not in d and hasattr ( record , '<STR_LIT:id>' ) : <EOL> d [ '<STR_LIT:id>' ] = getattr ( record , '<STR_LIT:id>' ) <EOL> return d <EOL> def default_body_render ( self , data , record ) : <EOL> s = [ '<STR_LIT>' ] <EOL> for k , v in data : <EOL> s . append ( '<STR_LIT>' % v ) <EOL> s . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> def make_view_field ( self , field , record , fields_convert_map ) : <EOL> fields_convert_map = fields_convert_map or { } <EOL> convert = None <EOL> name = field [ '<STR_LIT:name>' ] <EOL> label = field . get ( '<STR_LIT>' , None ) or field [ '<STR_LIT:name>' ] <EOL> if name in fields_convert_map : <EOL> convert = fields_convert_map . get ( name , None ) <EOL> value = record [ name ] <EOL> if convert : <EOL> display = convert ( value , record ) <EOL> else : <EOL> display = value <EOL> if isinstance ( display , unicode ) : <EOL> display = display . encode ( '<STR_LIT:utf-8>' ) <EOL> if display is None : <EOL> display = '<STR_LIT>' <EOL> return { '<STR_LIT:label>' : label , '<STR_LIT:value>' : value , '<STR_LIT>' : display } <EOL> def create_table_colgroup ( self ) : <EOL> s = [ ] <EOL> for f in self . table_info [ '<STR_LIT>' ] : <EOL> s . append ( '<STR_LIT>' % f . get ( '<STR_LIT>' , '<STR_LIT:*>' ) ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> def create_table_head ( self ) : <EOL> from uliweb . core . html import Tag <EOL> from uliweb . utils . common import simple_value <EOL> s = [ ] <EOL> fields = [ ] <EOL> max_rowspan = <NUM_LIT:0> <EOL> for i , f in enumerate ( self . table_info [ '<STR_LIT>' ] ) : <EOL> _f = list ( f . split ( '<STR_LIT:/>' ) ) <EOL> max_rowspan = max ( max_rowspan , len ( _f ) ) <EOL> fields . append ( ( _f , i ) ) <EOL> def get_field ( fields , i , m_rowspan ) : <EOL> f_list , col = fields [ i ] <EOL> field = { '<STR_LIT:name>' : f_list [ <NUM_LIT:0> ] , '<STR_LIT>' : col , '<STR_LIT:width>' : self . table_info [ '<STR_LIT>' ] [ col ] . get ( '<STR_LIT:width>' , <NUM_LIT:0> ) , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:title>' : self . table_info [ '<STR_LIT>' ] [ col ] . get ( '<STR_LIT>' , '<STR_LIT>' ) } <EOL> if len ( f_list ) == <NUM_LIT:1> : <EOL> field [ '<STR_LIT>' ] = m_rowspan <EOL> return field <EOL> def remove_field ( fields , i ) : <EOL> del fields [ i ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def clear_fields ( fields ) : <EOL> for i in range ( len ( fields ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if len ( fields [ i ] [ <NUM_LIT:0> ] ) == <NUM_LIT:0> : <EOL> del fields [ i ] <EOL> n = len ( fields ) <EOL> y = <NUM_LIT:0> <EOL> while n > <NUM_LIT:0> : <EOL> i = <NUM_LIT:0> <EOL> s . append ( '<STR_LIT>' ) <EOL> while i < n : <EOL> field = get_field ( fields , i , max_rowspan - y ) <EOL> remove_field ( fields , i ) <EOL> j = i + <NUM_LIT:1> <EOL> while j < n : <EOL> field_n = get_field ( fields , j , max_rowspan - y ) <EOL> if simple_value ( field [ '<STR_LIT:name>' ] ) == simple_value ( field_n [ '<STR_LIT:name>' ] ) and field [ '<STR_LIT>' ] == field_n [ '<STR_LIT>' ] : <EOL> remove_field ( fields , j ) <EOL> field [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> field [ '<STR_LIT:width>' ] += field_n [ '<STR_LIT:width>' ] <EOL> j += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> kwargs = { } <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:left>' <EOL> if field [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> kwargs [ '<STR_LIT>' ] = field [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if field [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> kwargs [ '<STR_LIT>' ] = field [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT:width>' ] = field [ '<STR_LIT:width>' ] <EOL> if not kwargs [ '<STR_LIT:width>' ] : <EOL> kwargs [ '<STR_LIT:width>' ] = self . default_column_width <EOL> _f = self . table_info [ '<STR_LIT>' ] [ field [ '<STR_LIT>' ] ] <EOL> kwargs [ '<STR_LIT>' ] = _f [ '<STR_LIT:name>' ] <EOL> if kwargs . get ( '<STR_LIT>' , <NUM_LIT:1> ) + y != max_rowspan : <EOL> kwargs . pop ( '<STR_LIT:width>' , None ) <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> s . append ( str ( Tag ( '<STR_LIT>' , field [ '<STR_LIT:name>' ] , ** kwargs ) ) ) <EOL> i = j <EOL> clear_fields ( fields ) <EOL> s . append ( '<STR_LIT>' ) <EOL> n = len ( fields ) <EOL> y += <NUM_LIT:1> <EOL> return s <EOL> def get_columns ( self , frozen = None ) : <EOL> from uliweb . utils . common import simple_value <EOL> columns = [ ] <EOL> if frozen : <EOL> columns = [ [ ] ] <EOL> fields = [ ] <EOL> max_rowspan = <NUM_LIT:0> <EOL> for i , f in enumerate ( self . table_info [ '<STR_LIT>' ] ) : <EOL> _f = list ( f . split ( '<STR_LIT:/>' ) ) <EOL> max_rowspan = max ( max_rowspan , len ( _f ) ) <EOL> fields . append ( ( _f , i ) ) <EOL> def get_field ( fields , i , m_rowspan ) : <EOL> f_list , col = fields [ i ] <EOL> field = { '<STR_LIT:name>' : f_list [ <NUM_LIT:0> ] , '<STR_LIT>' : col , '<STR_LIT:width>' : self . table_info [ '<STR_LIT>' ] [ col ] . get ( '<STR_LIT:width>' , <NUM_LIT:0> ) , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:title>' : self . table_info [ '<STR_LIT>' ] [ col ] . get ( '<STR_LIT>' , '<STR_LIT>' ) } <EOL> if len ( f_list ) == <NUM_LIT:1> : <EOL> field [ '<STR_LIT>' ] = m_rowspan <EOL> return field <EOL> def remove_field ( fields , i ) : <EOL> del fields [ i ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def clear_fields ( fields ) : <EOL> for i in range ( len ( fields ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if len ( fields [ i ] [ <NUM_LIT:0> ] ) == <NUM_LIT:0> : <EOL> del fields [ i ] <EOL> n = len ( fields ) <EOL> y = <NUM_LIT:0> <EOL> while n > <NUM_LIT:0> : <EOL> s = [ ] <EOL> i = <NUM_LIT:0> <EOL> while i < n : <EOL> field = get_field ( fields , i , max_rowspan - y ) <EOL> remove_field ( fields , i ) <EOL> j = i + <NUM_LIT:1> <EOL> while j < n : <EOL> field_n = get_field ( fields , j , max_rowspan - y ) <EOL> if simple_value ( field [ '<STR_LIT:name>' ] ) == simple_value ( field_n [ '<STR_LIT:name>' ] ) and field [ '<STR_LIT>' ] == field_n [ '<STR_LIT>' ] : <EOL> remove_field ( fields , j ) <EOL> field [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> field [ '<STR_LIT:width>' ] += field_n [ '<STR_LIT:width>' ] <EOL> j += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> _f = self . table_info [ '<STR_LIT>' ] [ field [ '<STR_LIT>' ] ] . copy ( ) <EOL> kwargs = { } <EOL> kwargs [ '<STR_LIT>' ] = _f . pop ( '<STR_LIT:name>' ) <EOL> _f . pop ( '<STR_LIT>' , None ) <EOL> _f . pop ( '<STR_LIT>' , None ) <EOL> kwargs [ '<STR_LIT:title>' ] = simple_value ( field [ '<STR_LIT:name>' ] ) <EOL> span = False <EOL> if field [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> kwargs [ '<STR_LIT>' ] = field [ '<STR_LIT>' ] <EOL> span = True <EOL> if field [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> kwargs [ '<STR_LIT>' ] = field [ '<STR_LIT>' ] <EOL> span = True <EOL> if kwargs . get ( '<STR_LIT>' , <NUM_LIT:1> ) + y != max_rowspan : <EOL> _f . pop ( '<STR_LIT:width>' , None ) <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> _f . pop ( '<STR_LIT:title>' , None ) <EOL> else : <EOL> kwargs [ '<STR_LIT:width>' ] = _f . pop ( '<STR_LIT:width>' , self . default_column_width ) <EOL> kwargs . update ( _f ) <EOL> frozen_flag = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if frozen is None or frozen_flag is frozen : <EOL> s . append ( kwargs ) <EOL> i = j <EOL> clear_fields ( fields ) <EOL> n = len ( fields ) <EOL> y += <NUM_LIT:1> <EOL> if frozen : <EOL> columns [ <NUM_LIT:0> ] . extend ( s ) <EOL> else : <EOL> columns . append ( s ) <EOL> return columns <EOL> def get_table_info ( self ) : <EOL> t = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : { } } <EOL> t [ '<STR_LIT>' ] = self . fields <EOL> for x in self . fields : <EOL> t [ '<STR_LIT>' ] . append ( x [ '<STR_LIT>' ] ) <EOL> t [ '<STR_LIT>' ] . append ( x [ '<STR_LIT:name>' ] ) <EOL> t [ '<STR_LIT>' ] [ x [ '<STR_LIT:name>' ] . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) ] = x [ '<STR_LIT:name>' ] <EOL> w = x . get ( '<STR_LIT:width>' ) <EOL> if w : <EOL> if isinstance ( w , int ) : <EOL> width = '<STR_LIT>' % w <EOL> else : <EOL> width = w <EOL> else : <EOL> width = '<STR_LIT:*>' <EOL> x [ '<STR_LIT>' ] = width <EOL> return t <EOL> class ListView ( SimpleListView ) : <EOL> def __init__ ( self , model , condition = None , query = None , pageno = <NUM_LIT:0> , order_by = None , <EOL> group_by = None , having = None , <EOL> fields = None , rows_per_page = <NUM_LIT:10> , types_convert_map = None , pagination = True , <EOL> fields_convert_map = None , id = '<STR_LIT>' , table_class_attr = '<STR_LIT>' , table_width = True , <EOL> total_fields = None , template_data = None , default_column_width = <NUM_LIT:100> , <EOL> meta = '<STR_LIT>' , render = None , total = <NUM_LIT:0> , manual = False , <EOL> record_render = None , post_record_render = None ) : <EOL> """<STR_LIT>""" <EOL> self . model = model and get_model ( model ) <EOL> self . meta = meta <EOL> self . condition = condition <EOL> self . pageno = pageno <EOL> self . order_by = order_by <EOL> self . group_by = group_by <EOL> self . having = having <EOL> self . fields = fields <EOL> self . rows_per_page = rows_per_page <EOL> self . types_convert_map = types_convert_map or { } <EOL> self . fields_convert_map = fields_convert_map or { } <EOL> self . id = id <EOL> self . rows_num = <NUM_LIT:0> <EOL> self . _query = query <EOL> self . table_width = table_width <EOL> self . table_class_attr = table_class_attr <EOL> self . total = total or <NUM_LIT:0> <EOL> self . pagination = pagination <EOL> self . create_total_infos ( total_fields ) <EOL> self . template_data = template_data or { } <EOL> self . default_column_width = default_column_width <EOL> self . downloader = GenericFileServing ( ) <EOL> self . render_func = render <EOL> self . manual = manual <EOL> self . record_render = record_render <EOL> self . post_record_render = post_record_render <EOL> self . init ( ) <EOL> def init ( self ) : <EOL> super ( ListView , self ) . init ( ) <EOL> if not self . id : <EOL> self . id = self . model . tablename <EOL> self . table_info = self . get_table_info ( ) <EOL> def query ( self ) : <EOL> if self . _query is None or isinstance ( self . _query , ( orm . Result , Select ) ) : <EOL> offset = self . pageno * self . rows_per_page <EOL> limit = self . rows_per_page <EOL> query = self . query_model ( self . model , self . condition , group_by = self . group_by , <EOL> having = self . having , <EOL> offset = offset , limit = limit , order_by = self . order_by ) <EOL> if not self . manual : <EOL> self . total = self . count ( query ) <EOL> else : <EOL> query = self . query_range ( self . pageno , self . pagination ) <EOL> return query <EOL> def object ( self , record , json_result = False ) : <EOL> if self . record_render : <EOL> r = self . record_render ( record ) <EOL> else : <EOL> r = SortedDict ( ) <EOL> for i , x in enumerate ( self . table_info [ '<STR_LIT>' ] ) : <EOL> if hasattr ( self . model , x [ '<STR_LIT:name>' ] ) : <EOL> field = getattr ( self . model , x [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> field = x <EOL> v = make_view_field ( field , record , self . types_convert_map , self . fields_convert_map , auto_convert = not json_result ) <EOL> r [ x [ '<STR_LIT:name>' ] ] = v [ '<STR_LIT>' ] <EOL> r [ '<STR_LIT>' ] = record <EOL> if self . post_record_render : <EOL> self . post_record_render ( r ) <EOL> return r <EOL> def query_all ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . query_model ( self . model , self . condition , order_by = self . order_by , <EOL> group_by = self . group_by , having = self . having ) <EOL> def query_model ( self , model , condition = None , offset = None , limit = None , <EOL> order_by = None , group_by = None , having = None , fields = None ) : <EOL> """<STR_LIT>""" <EOL> from uliweb import settings , request <EOL> from uliweb . utils . common import log <EOL> from uliweb . orm import rawsql <EOL> if self . _query is not None : <EOL> query = self . _query <EOL> if condition is not None and isinstance ( query , Result ) : <EOL> query = query . filter ( condition ) <EOL> else : <EOL> query = self . model . filter ( condition ) <EOL> if request . GET . get ( '<STR_LIT>' ) : <EOL> log . info ( rawsql ( query . get_query ( ) ) ) <EOL> if self . pagination : <EOL> if offset is not None : <EOL> query = query . offset ( int ( offset ) ) <EOL> if limit is not None : <EOL> query = query . limit ( int ( limit ) ) <EOL> if order_by is not None : <EOL> if isinstance ( order_by , ( tuple , list ) ) : <EOL> query = query . order_by ( * order_by ) <EOL> else : <EOL> query = query . order_by ( order_by ) <EOL> if group_by is not None : <EOL> if isinstance ( group_by , ( tuple , list ) ) : <EOL> query = query . group_by ( * group_by ) <EOL> else : <EOL> query = query . group_by ( group_by ) <EOL> if having is not None : <EOL> query = query . having ( having ) <EOL> return query <EOL> def get_table_info ( self ) : <EOL> t = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : { } } <EOL> if self . fields is not None : <EOL> fields = self . fields <EOL> elif hasattr ( self . model , self . meta ) : <EOL> fields = getattr ( self . model , self . meta ) . fields <EOL> else : <EOL> fields = [ x for x , y in self . model . _fields_list ] <EOL> fields_list = [ ] <EOL> for x in fields : <EOL> if isinstance ( x , ( str , unicode ) ) : <EOL> name = x <EOL> d = { '<STR_LIT:name>' : x } <EOL> f = self . get_table_meta_field ( name , self . model ) <EOL> if f : <EOL> d = f <EOL> else : <EOL> d [ '<STR_LIT>' ] = x <EOL> elif isinstance ( x , dict ) : <EOL> name = x [ '<STR_LIT:name>' ] <EOL> d = x <EOL> if '<STR_LIT>' not in d : <EOL> f = self . get_table_meta_field ( name , x . get ( '<STR_LIT>' , self . model ) ) <EOL> if f : <EOL> d [ '<STR_LIT>' ] = f [ '<STR_LIT>' ] <EOL> else : <EOL> d [ '<STR_LIT>' ] = name <EOL> w = d . get ( '<STR_LIT:width>' ) <EOL> if w : <EOL> if isinstance ( w , int ) : <EOL> width = '<STR_LIT>' % w <EOL> else : <EOL> width = w <EOL> else : <EOL> width = '<STR_LIT:*>' <EOL> d [ '<STR_LIT>' ] = width <EOL> t [ '<STR_LIT>' ] . append ( d ) <EOL> t [ '<STR_LIT>' ] . append ( d [ '<STR_LIT>' ] ) <EOL> t [ '<STR_LIT>' ] . append ( name ) <EOL> t [ '<STR_LIT>' ] [ name . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) ] = name <EOL> return t <EOL> class SelectListView ( ListView ) : <EOL> def __init__ ( self , model = None , fields = None , condition = None , query = None , pageno = <NUM_LIT:0> , order_by = None , <EOL> rows_per_page = <NUM_LIT:10> , types_convert_map = None , pagination = True , <EOL> fields_convert_map = None , id = '<STR_LIT>' , table_class_attr = '<STR_LIT>' , table_width = True , <EOL> total_fields = None , template_data = None , default_column_width = <NUM_LIT:100> , meta = '<STR_LIT>' , <EOL> render = None , total = <NUM_LIT:0> , manual = False , record_render = None , post_record_render = None ) : <EOL> """<STR_LIT>""" <EOL> super ( SelectListView , self ) . __init__ ( model = model , condition = condition , <EOL> query = query , pageno = pageno , order_by = order_by , fields = fields , <EOL> rows_per_page = rows_per_page , types_convert_map = types_convert_map , <EOL> pagination = pagination , fields_convert_map = fields_convert_map , <EOL> id = id , table_class_attr = table_class_attr , table_width = table_width , <EOL> total_fields = total_fields , template_data = template_data , <EOL> default_column_width = default_column_width , meta = meta , <EOL> render = render , total = total , manual = manual , record_render = record_render , <EOL> post_record_render = post_record_render ) <EOL> self . _field_labels = [ ] <EOL> for f in self . table_info [ '<STR_LIT>' ] : <EOL> if '<STR_LIT:.>' in f : <EOL> self . _field_labels . append ( ( f , f . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) ) ) <EOL> else : <EOL> self . _field_labels . append ( ( f , f ) ) <EOL> def get_select ( self ) : <EOL> columns = [ ] <EOL> for f in self . table_info [ '<STR_LIT>' ] : <EOL> model , col = self . get_column ( f ) <EOL> if col is not None : <EOL> columns . append ( col ) <EOL> return select ( columns , use_labels = True ) <EOL> def query ( self ) : <EOL> offset = self . pageno * self . rows_per_page <EOL> limit = self . rows_per_page <EOL> query = self . query_model ( self . model , self . condition , offset = offset , limit = limit , order_by = self . order_by ) <EOL> if not self . manual : <EOL> self . total = self . count ( query ) <EOL> return query <EOL> def query_model ( self , model , condition = None , offset = None , limit = None , <EOL> group_by = None , having = None , order_by = None , fields = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _query is not None : <EOL> query = self . _query <EOL> else : <EOL> query = self . get_select ( ) <EOL> if condition is not None : <EOL> if isinstance ( query , Result ) : <EOL> query = query . filter ( condition ) <EOL> else : <EOL> query = query . where ( condition ) <EOL> if self . pagination : <EOL> if offset is not None : <EOL> query = query . offset ( int ( offset ) ) <EOL> if limit is not None : <EOL> query = query . limit ( int ( limit ) ) <EOL> if order_by is not None : <EOL> if isinstance ( order_by , ( tuple , list ) ) : <EOL> for order in order_by : <EOL> query = query . order_by ( order ) <EOL> else : <EOL> query = query . order_by ( order_by ) <EOL> if group_by is not None : <EOL> if isinstance ( group_by , ( tuple , list ) ) : <EOL> query = query . group_by ( * group_by ) <EOL> else : <EOL> query = query . group_by ( group_by ) <EOL> if having is not None : <EOL> query = query . having ( having ) <EOL> return query <EOL> def object ( self , record , json_result = False ) : <EOL> r = SortedDict ( ) <EOL> _record = { } <EOL> d = dict ( record ) <EOL> for name , label in self . _field_labels : <EOL> _record [ name ] = d . get ( label , None ) <EOL> if self . record_render : <EOL> r = self . record_render ( _record ) <EOL> else : <EOL> for i , x in enumerate ( self . table_info [ '<STR_LIT>' ] ) : <EOL> field = self . get_field ( x [ '<STR_LIT:name>' ] , self . model ) <EOL> if not field : <EOL> field = { '<STR_LIT:name>' : x [ '<STR_LIT:name>' ] } <EOL> else : <EOL> field = { '<STR_LIT:name>' : x [ '<STR_LIT:name>' ] , '<STR_LIT>' : field } <EOL> v = make_view_field ( field , _record , self . types_convert_map , <EOL> self . fields_convert_map , auto_convert = not json_result , <EOL> value = _record [ x [ '<STR_LIT:name>' ] ] ) <EOL> r [ x [ '<STR_LIT:name>' ] ] = v [ '<STR_LIT>' ] <EOL> r [ '<STR_LIT>' ] = _record <EOL> if self . post_record_render : <EOL> self . post_record_render ( r ) <EOL> return r <EOL> class QueryView ( object ) : <EOL> success_msg = _ ( '<STR_LIT>' ) <EOL> fail_msg = _ ( '<STR_LIT>' ) <EOL> builds_args_map = { } <EOL> meta = '<STR_LIT>' <EOL> def __init__ ( self , model , ok_url = None , form = None , success_msg = None , fail_msg = None , <EOL> data = None , fields = None , form_cls = None , form_args = None , <EOL> static_fields = None , hidden_fields = None , post_created_form = None , <EOL> layout = None , get_form_field = None , links = None ) : <EOL> self . model = functions . get_model ( model ) <EOL> self . ok_url = ok_url <EOL> self . form = form <EOL> if success_msg : <EOL> self . success_msg = success_msg <EOL> if fail_msg : <EOL> self . fail_msg = fail_msg <EOL> self . data = data or { } <EOL> self . result = { } <EOL> self . get_form_field = get_form_field <EOL> self . fields = fields or [ ] <EOL> self . form_cls = form_cls <EOL> self . form_args = form_args or { } <EOL> self . static_fields = static_fields or [ ] <EOL> self . hidden_fields = hidden_fields or [ ] <EOL> self . post_created_form = post_created_form <EOL> self . links = links or [ ] <EOL> self . layout = layout <EOL> def get_fields ( self ) : <EOL> fields = [ ] <EOL> for f in self . fields : <EOL> d = self . _make_form_field ( self . model , f ) <EOL> field_name = d [ '<STR_LIT:name>' ] <EOL> d [ '<STR_LIT>' ] = bool ( field_name in self . static_fields ) <EOL> d [ '<STR_LIT>' ] = bool ( field_name in self . hidden_fields ) <EOL> d [ '<STR_LIT>' ] = False <EOL> fields . append ( d ) <EOL> return fields <EOL> def get_layout ( self ) : <EOL> if self . layout : <EOL> return self . layout <EOL> if hasattr ( self . model , self . meta ) : <EOL> m = getattr ( self . model , self . meta ) <EOL> if hasattr ( m , '<STR_LIT>' ) : <EOL> return getattr ( m , '<STR_LIT>' ) <EOL> layout = [ ] <EOL> for x in self . fields : <EOL> if isinstance ( x , str ) : <EOL> layout . append ( x ) <EOL> elif isinstance ( x , ( tuple , list ) ) : <EOL> layout . append ( x [ <NUM_LIT:0> ] ) <EOL> else : <EOL> layout . append ( x [ '<STR_LIT:name>' ] ) <EOL> self . layout = [ tuple ( layout ) ] <EOL> return self . layout <EOL> def make_form ( self ) : <EOL> import uliweb . form as form <EOL> from uliweb . form . layout import QueryLayout <EOL> if self . form : <EOL> return self . form <EOL> if isinstance ( self . model , str ) : <EOL> self . model = get_model ( self . model ) <EOL> if self . form_cls : <EOL> class DummyForm ( self . form_cls ) : pass <EOL> if not hasattr ( DummyForm , '<STR_LIT>' ) : <EOL> DummyForm . form_buttons = form . Button ( value = _ ( '<STR_LIT>' ) , _class = "<STR_LIT>" , type = '<STR_LIT>' ) <EOL> if not hasattr ( DummyForm , '<STR_LIT>' ) : <EOL> DummyForm . layout_class = QueryLayout <EOL> if not hasattr ( DummyForm , '<STR_LIT>' ) : <EOL> DummyForm . form_method = '<STR_LIT:GET>' <EOL> else : <EOL> class DummyForm ( form . Form ) : <EOL> layout_class = QueryLayout <EOL> form_method = '<STR_LIT:GET>' <EOL> form_buttons = form . Button ( value = _ ( '<STR_LIT>' ) , _class = "<STR_LIT>" , type = '<STR_LIT>' ) <EOL> layout = self . get_layout ( ) <EOL> DummyForm . layout = layout <EOL> for f in self . get_fields ( ) : <EOL> flag = False <EOL> if self . get_form_field : <EOL> field = self . get_form_field ( f [ '<STR_LIT:name>' ] ) <EOL> if field : <EOL> flag = True <EOL> if not flag : <EOL> field = make_form_field ( f , self . model , builds_args_map = self . builds_args_map ) <EOL> if field : <EOL> DummyForm . add_field ( f [ '<STR_LIT:name>' ] , field , True ) <EOL> if self . post_created_form : <EOL> self . post_created_form ( DummyForm , self . model ) <EOL> form = DummyForm ( data = self . data , ** self . form_args ) <EOL> form . links = self . links <EOL> return form <EOL> def run ( self ) : <EOL> from uliweb import request <EOL> if not self . form : <EOL> self . form = self . make_form ( ) <EOL> flag = self . form . validate ( request . values ) <EOL> if flag : <EOL> if self . data : <EOL> for k , v in self . data . items ( ) : <EOL> if not self . form . data . get ( k ) : <EOL> self . form . data [ k ] = v <EOL> self . result = self . form . data . copy ( ) <EOL> else : <EOL> self . result = self . form . data <EOL> return self . result <EOL> def _make_form_field ( self , model , field ) : <EOL> from uliweb . utils . generic import make_form_field <EOL> from uliweb . form import get_field_cls <EOL> if isinstance ( field , str ) : <EOL> d = { '<STR_LIT:name>' : field , '<STR_LIT>' : make_form_field ( field , model , use_default_value = False ) } <EOL> elif isinstance ( field , dict ) : <EOL> d = field <EOL> if '<STR_LIT>' not in field : <EOL> _type = field . get ( '<STR_LIT:type>' ) <EOL> if _type : <EOL> field_cls = get_field_cls ( _type ) <EOL> else : <EOL> field_cls = None <EOL> field [ '<STR_LIT>' ] = field . get ( '<STR_LIT>' , { } ) <EOL> d [ '<STR_LIT>' ] = make_form_field ( field , model = model , <EOL> use_default_value = False , <EOL> field_cls = field_cls ) <EOL> elif isinstance ( field , ( tuple , list ) ) : <EOL> d = { '<STR_LIT:name>' : field [ <NUM_LIT:0> ] , '<STR_LIT>' : field [ <NUM_LIT:1> ] } <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % type ( field ) ) <EOL> d [ '<STR_LIT>' ] . required = False <EOL> return d <EOL> def _make_like ( self , column , format , value ) : <EOL> """<STR_LIT>""" <EOL> c = [ ] <EOL> if format . startswith ( '<STR_LIT:%>' ) : <EOL> c . append ( '<STR_LIT:%>' ) <EOL> c . append ( value ) <EOL> if format . endswith ( '<STR_LIT:%>' ) : <EOL> c . append ( '<STR_LIT:%>' ) <EOL> return column . like ( '<STR_LIT>' . join ( c ) ) <EOL> def _make_op ( self , column , op , value ) : <EOL> """<STR_LIT>""" <EOL> if not op : <EOL> return None <EOL> if op == '<STR_LIT>' : <EOL> return column > value <EOL> elif op == '<STR_LIT>' : <EOL> return column < value <EOL> elif op == '<STR_LIT>' : <EOL> return column >= value <EOL> elif op == '<STR_LIT>' : <EOL> return column <= value <EOL> elif op == '<STR_LIT>' : <EOL> return column == value <EOL> elif op == '<STR_LIT>' : <EOL> return column != value <EOL> elif op == '<STR_LIT>' : <EOL> return column . in_ ( value ) <EOL> else : <EOL> raise KeyError ( '<STR_LIT>' % op ) <EOL> def _get_query_condition ( self , model , fields , values ) : <EOL> from sqlalchemy import true , and_ <EOL> condition = true ( ) <EOL> for v in fields : <EOL> if isinstance ( v , ( tuple , list ) ) : <EOL> v = { '<STR_LIT:name>' : v [ <NUM_LIT:0> ] } <EOL> elif not isinstance ( v , dict ) : <EOL> v = { '<STR_LIT:name>' : v } <EOL> name = v [ '<STR_LIT:name>' ] <EOL> if name in values : <EOL> render = v . get ( '<STR_LIT>' ) <EOL> value = values [ name ] <EOL> if not value : <EOL> continue <EOL> _cond = None <EOL> if render : <EOL> _cond = render ( model , name , value , values ) <EOL> else : <EOL> column = model . c [ name ] <EOL> if '<STR_LIT>' in v : <EOL> _cond = self . _make_like ( column , v [ '<STR_LIT>' ] , value ) <EOL> elif '<STR_LIT>' in v : <EOL> _cond = self . _make_op ( column , v [ '<STR_LIT>' ] , value ) <EOL> else : <EOL> if isinstance ( value , ( tuple , list ) ) : <EOL> _cond = column . in_ ( value ) <EOL> else : <EOL> _cond = column == value <EOL> if _cond is not None : <EOL> condition = and_ ( _cond , condition ) <EOL> log . debug ( "<STR_LIT>" , condition ) <EOL> return condition <EOL> def get_condition ( self , values = None ) : <EOL> from sqlalchemy import true , and_ <EOL> values = values or self . result <EOL> condition = true ( ) <EOL> model = self . model <EOL> for v in self . fields : <EOL> if isinstance ( v , ( tuple , list ) ) : <EOL> v = { '<STR_LIT:name>' : v [ <NUM_LIT:0> ] } <EOL> elif not isinstance ( v , dict ) : <EOL> v = { '<STR_LIT:name>' : v } <EOL> name = v [ '<STR_LIT:name>' ] <EOL> if name in values : <EOL> render = v . get ( '<STR_LIT>' ) <EOL> value = values [ name ] <EOL> if not value : <EOL> continue <EOL> _cond = None <EOL> if render : <EOL> _cond = render ( model , name , value , values ) <EOL> else : <EOL> column = model . c [ name ] <EOL> if '<STR_LIT>' in v : <EOL> _cond = self . _make_like ( column , v [ '<STR_LIT>' ] , value ) <EOL> elif '<STR_LIT>' in v : <EOL> _cond = self . _make_op ( column , v [ '<STR_LIT>' ] , value ) <EOL> else : <EOL> if isinstance ( value , ( tuple , list ) ) : <EOL> _cond = column . in_ ( value ) <EOL> else : <EOL> _cond = column == value <EOL> if _cond is not None : <EOL> condition = and_ ( _cond , condition ) <EOL> log . debug ( "<STR_LIT>" , condition ) <EOL> return condition </s>
