<s> import logging , sys <EOL> from flask import flash <EOL> from . import filters <EOL> from . . base import BaseInterface <EOL> from ... _compat import as_unicode <EOL> from ... const import LOGMSG_ERR_DBI_ADD_GENERIC , LOGMSG_ERR_DBI_EDIT_GENERIC , LOGMSG_ERR_DBI_DEL_GENERIC , LOGMSG_WAR_DBI_ADD_INTEGRITY , LOGMSG_WAR_DBI_EDIT_INTEGRITY , LOGMSG_WAR_DBI_DEL_INTEGRITY <EOL> from mongoengine . fields import StringField , IntField , BooleanField , FloatField , DateTimeField , ReferenceField , ListField , FileField , ImageField , ObjectIdField <EOL> log = logging . getLogger ( __name__ ) <EOL> def _include_filters ( obj ) : <EOL> for key in filters . __all__ : <EOL> if not hasattr ( obj , key ) : <EOL> setattr ( obj , key , getattr ( filters , key ) ) <EOL> class MongoEngineInterface ( BaseInterface ) : <EOL> filter_converter_class = filters . MongoEngineFilterConverter <EOL> def __init__ ( self , obj , session = None ) : <EOL> self . session = session <EOL> _include_filters ( self ) <EOL> super ( MongoEngineInterface , self ) . __init__ ( obj ) <EOL> @ property <EOL> def model_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj . __name__ <EOL> def query ( self , filters = None , order_column = '<STR_LIT>' , order_direction = '<STR_LIT>' , <EOL> page = None , page_size = None ) : <EOL> objs = self . obj . objects <EOL> if filters : <EOL> objs = filters . apply_all ( objs ) <EOL> count = objs . count ( ) <EOL> if order_column != '<STR_LIT>' : <EOL> if hasattr ( getattr ( self . obj , order_column ) , '<STR_LIT>' ) : <EOL> order_column = getattr ( getattr ( self . obj , order_column ) , '<STR_LIT>' ) <EOL> if order_direction == '<STR_LIT>' : <EOL> objs = objs . order_by ( '<STR_LIT>' . format ( order_column ) ) <EOL> else : <EOL> objs = objs . order_by ( '<STR_LIT>' . format ( order_column ) ) <EOL> if page_size is None : <EOL> if page is not None : <EOL> log . error ( '<STR_LIT>' % page ) <EOL> if count > <NUM_LIT:100> : <EOL> log . warn ( '<STR_LIT>' % ( count , str ( self . obj ) ) ) <EOL> else : <EOL> offset = ( page or <NUM_LIT:0> ) * page_size <EOL> objs = objs [ offset : offset + page_size ] <EOL> return count , objs <EOL> def is_object_id ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , ObjectIdField ) <EOL> except : <EOL> return False <EOL> def is_string ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , StringField ) <EOL> except : <EOL> return False <EOL> def is_integer ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , IntField ) <EOL> except : <EOL> return False <EOL> def is_float ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , FloatField ) <EOL> except : <EOL> return False <EOL> def is_boolean ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , BooleanField ) <EOL> except : <EOL> return False <EOL> def is_datetime ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , DateTimeField ) <EOL> except : <EOL> return False <EOL> def is_gridfs_file ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , FileField ) <EOL> except : <EOL> return False <EOL> def is_gridfs_image ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , ImageField ) <EOL> except : <EOL> return False <EOL> def is_relation ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , ReferenceField ) or isinstance ( self . obj . _fields [ col_name ] , ListField ) <EOL> except : <EOL> return False <EOL> def is_relation_many_to_one ( self , col_name ) : <EOL> try : <EOL> return isinstance ( self . obj . _fields [ col_name ] , ReferenceField ) <EOL> except : <EOL> return False <EOL> def is_relation_many_to_many ( self , col_name ) : <EOL> try : <EOL> field = self . obj . _fields [ col_name ] <EOL> return isinstance ( field , ListField ) and isinstance ( field . field , ReferenceField ) <EOL> except : <EOL> return False <EOL> def is_relation_one_to_one ( self , col_name ) : <EOL> return False <EOL> def is_relation_one_to_many ( self , col_name ) : <EOL> return False <EOL> def is_nullable ( self , col_name ) : <EOL> return not self . obj . _fields [ col_name ] . required <EOL> def is_unique ( self , col_name ) : <EOL> return self . obj . _fields [ col_name ] . unique <EOL> def is_pk ( self , col_name ) : <EOL> return col_name == '<STR_LIT:id>' <EOL> def get_max_length ( self , col_name ) : <EOL> try : <EOL> col = self . obj . _fields [ col_name ] <EOL> if col . max_length : <EOL> return col . max_length <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> except : <EOL> return - <NUM_LIT:1> <EOL> def get_min_length ( self , col_name ) : <EOL> try : <EOL> col = self . obj . _fields [ col_name ] <EOL> if col . min_length : <EOL> return col . min_length <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> except : <EOL> return - <NUM_LIT:1> <EOL> def add ( self , item ) : <EOL> try : <EOL> item . save ( ) <EOL> self . message = ( as_unicode ( self . add_row_message ) , '<STR_LIT:success>' ) <EOL> return True <EOL> except Exception as e : <EOL> self . message = ( as_unicode ( self . general_error_message + '<STR_LIT:U+0020>' + str ( sys . exc_info ( ) [ <NUM_LIT:0> ] ) ) , '<STR_LIT>' ) <EOL> log . exception ( LOGMSG_ERR_DBI_ADD_GENERIC . format ( str ( e ) ) ) <EOL> return False <EOL> def edit ( self , item ) : <EOL> try : <EOL> item . save ( ) <EOL> self . message = ( as_unicode ( self . edit_row_message ) , '<STR_LIT:success>' ) <EOL> return True <EOL> except Exception as e : <EOL> self . message = ( as_unicode ( self . general_error_message + '<STR_LIT:U+0020>' + str ( sys . exc_info ( ) [ <NUM_LIT:0> ] ) ) , '<STR_LIT>' ) <EOL> log . exception ( LOGMSG_ERR_DBI_EDIT_GENERIC . format ( str ( e ) ) ) <EOL> return False <EOL> def delete ( self , item ) : <EOL> try : <EOL> item . delete ( ) <EOL> self . message = ( as_unicode ( self . delete_row_message ) , '<STR_LIT:success>' ) <EOL> return True <EOL> except Exception as e : <EOL> self . message = ( as_unicode ( self . general_error_message + '<STR_LIT:U+0020>' + str ( sys . exc_info ( ) [ <NUM_LIT:0> ] ) ) , '<STR_LIT>' ) <EOL> log . exception ( LOGMSG_ERR_DBI_DEL_GENERIC . format ( str ( e ) ) ) <EOL> return False <EOL> def get_columns_list ( self ) : <EOL> return self . obj . _fields . keys ( ) <EOL> def get_search_columns_list ( self ) : <EOL> ret_lst = list ( ) <EOL> for col_name in self . get_columns_list ( ) : <EOL> for conversion in self . filter_converter_class . conversion_table : <EOL> if getattr ( self , conversion [ <NUM_LIT:0> ] ) ( col_name ) and not self . is_object_id ( col_name ) : <EOL> ret_lst . append ( col_name ) <EOL> return ret_lst <EOL> def get_user_columns_list ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ col_name for col_name in self . get_columns_list ( ) if not self . is_pk ( col_name ) ] <EOL> def get_order_columns_list ( self , list_columns = None ) : <EOL> """<STR_LIT>""" <EOL> ret_lst = list ( ) <EOL> list_columns = list_columns or self . get_columns_list ( ) <EOL> for col_name in list_columns : <EOL> if hasattr ( self . obj , col_name ) : <EOL> if not hasattr ( getattr ( self . obj , col_name ) , '<STR_LIT>' ) : <EOL> ret_lst . append ( col_name ) <EOL> else : <EOL> ret_lst . append ( col_name ) <EOL> return ret_lst <EOL> def get_related_model ( self , col_name ) : <EOL> field = self . obj . _fields [ col_name ] <EOL> if isinstance ( field , ListField ) : <EOL> return field . field . document_type <EOL> else : <EOL> return field . document_type <EOL> def get_related_interface ( self , col_name ) : <EOL> return self . __class__ ( self . get_related_model ( col_name ) ) <EOL> def get_related_obj ( self , col_name , value ) : <EOL> rel_model = self . get_related_model ( col_name ) <EOL> return rel_model . objects ( pk = value ) [ <NUM_LIT:0> ] <EOL> def get_keys ( self , lst ) : <EOL> """<STR_LIT>""" <EOL> pk_name = self . get_pk_name ( ) <EOL> return [ getattr ( item , pk_name ) for item in lst ] <EOL> def get_related_fk ( self , model ) : <EOL> for col_name in self . get_columns_list ( ) : <EOL> if self . is_relation ( col_name ) : <EOL> if model == self . get_related_model ( col_name ) : <EOL> return col_name <EOL> def get_pk_name ( self ) : <EOL> return '<STR_LIT:id>' <EOL> def get ( self , id , filters = None ) : <EOL> if filters : <EOL> objs = self . obj . objects <EOL> objs = filters . apply_all ( objs ) <EOL> return objs ( pk = id ) . first ( ) <EOL> return self . obj . objects ( pk = id ) . first ( ) </s>
