<s> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . http import HttpResponseForbidden <EOL> from django . utils . importlib import import_module <EOL> from mongoengine . fields import EmbeddedDocumentField <EOL> from mongonaut . exceptions import NoMongoAdminSpecified <EOL> from mongonaut . forms import MongoModelForm <EOL> from mongonaut . forms . form_utils import has_digit <EOL> from mongonaut . forms . form_utils import make_key <EOL> from mongonaut . utils import translate_value <EOL> from mongonaut . utils import trim_field_key <EOL> class AppStore ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , module ) : <EOL> self . models = [ ] <EOL> for key in module . __dict__ . keys ( ) : <EOL> model_candidate = getattr ( module , key ) <EOL> if hasattr ( model_candidate , '<STR_LIT>' ) : <EOL> self . add_model ( model_candidate ) <EOL> def add_model ( self , model ) : <EOL> model . name = model . __name__ <EOL> self . models . append ( model ) <EOL> class MongonautViewMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> if hasattr ( self , '<STR_LIT>' ) and not self . request . user . has_perm ( self . permission ) : <EOL> return HttpResponseForbidden ( "<STR_LIT>" ) <EOL> return self . response_class ( <EOL> request = self . request , <EOL> template = self . get_template_names ( ) , <EOL> context = context , <EOL> ** response_kwargs <EOL> ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( MongonautViewMixin , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = getattr ( settings , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> context [ '<STR_LIT>' ] = getattr ( settings , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> context [ '<STR_LIT>' ] = getattr ( settings , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> return context <EOL> def get_mongoadmins ( self ) : <EOL> """<STR_LIT>""" <EOL> apps = [ ] <EOL> for app_name in settings . INSTALLED_APPS : <EOL> mongoadmin = "<STR_LIT>" . format ( app_name ) <EOL> try : <EOL> module = import_module ( mongoadmin ) <EOL> except ImportError as e : <EOL> if str ( e ) . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> raise e <EOL> app_store = AppStore ( module ) <EOL> apps . append ( dict ( <EOL> app_name = app_name , <EOL> obj = app_store <EOL> ) ) <EOL> return apps <EOL> def set_mongonaut_base ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return None <EOL> self . app_label = self . kwargs . get ( '<STR_LIT>' ) <EOL> self . document_name = self . kwargs . get ( '<STR_LIT>' ) <EOL> self . models_name = self . kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . model_name = "<STR_LIT>" . format ( self . app_label , self . models_name ) <EOL> self . models = import_module ( self . model_name ) <EOL> def set_mongoadmin ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return None <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . set_mongonaut_base ( ) <EOL> for mongoadmin in self . get_mongoadmins ( ) : <EOL> for model in mongoadmin [ '<STR_LIT>' ] . models : <EOL> if model . name == self . document_name : <EOL> self . mongoadmin = model . mongoadmin <EOL> break <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> raise NoMongoAdminSpecified ( "<STR_LIT>" . format ( self . app_label , self . document_name ) ) <EOL> def set_permissions_in_context ( self , context = { } ) : <EOL> """<STR_LIT>""" <EOL> context [ '<STR_LIT>' ] = self . mongoadmin . has_view_permission ( self . request ) <EOL> context [ '<STR_LIT>' ] = self . mongoadmin . has_edit_permission ( self . request ) <EOL> context [ '<STR_LIT>' ] = self . mongoadmin . has_add_permission ( self . request ) <EOL> context [ '<STR_LIT>' ] = self . mongoadmin . has_delete_permission ( self . request ) <EOL> return context <EOL> class MongonautFormViewMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_post_form ( self , success_message = None ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) or self . document is None : <EOL> self . document = self . document_type ( ) <EOL> self . form = MongoModelForm ( model = self . document_type , instance = self . document , <EOL> form_post_data = self . request . POST ) . get_form ( ) <EOL> self . form . is_bound = True <EOL> if self . form . is_valid ( ) : <EOL> self . document_map_dict = MongoModelForm ( model = self . document_type ) . create_document_dictionary ( self . document_type ) <EOL> self . new_document = self . document_type <EOL> self . embedded_list_docs = { } <EOL> if self . new_document is None : <EOL> messages . error ( self . request , u"<STR_LIT>" ) <EOL> else : <EOL> self . new_document = self . new_document ( ) <EOL> for form_key in self . form . cleaned_data . keys ( ) : <EOL> if form_key == '<STR_LIT:id>' and hasattr ( self , '<STR_LIT>' ) : <EOL> self . new_document . id = self . document . id <EOL> continue <EOL> self . process_document ( self . new_document , form_key , None ) <EOL> self . new_document . save ( ) <EOL> if success_message : <EOL> messages . success ( self . request , success_message ) <EOL> return self . form <EOL> def process_document ( self , document , form_key , passed_key ) : <EOL> """<STR_LIT>""" <EOL> if passed_key is not None : <EOL> current_key , remaining_key_array = trim_field_key ( document , passed_key ) <EOL> else : <EOL> current_key , remaining_key_array = trim_field_key ( document , form_key ) <EOL> key_array_digit = remaining_key_array [ - <NUM_LIT:1> ] if remaining_key_array and has_digit ( remaining_key_array ) else None <EOL> remaining_key = make_key ( remaining_key_array ) <EOL> if current_key . lower ( ) == '<STR_LIT:id>' : <EOL> raise KeyError ( u"<STR_LIT>" ) <EOL> is_embedded_doc = ( isinstance ( document . _fields . get ( current_key , None ) , EmbeddedDocumentField ) <EOL> if hasattr ( document , '<STR_LIT>' ) else False ) <EOL> is_list = not key_array_digit is None <EOL> key_in_fields = current_key in document . _fields . keys ( ) if hasattr ( document , '<STR_LIT>' ) else False <EOL> if key_in_fields : <EOL> if is_embedded_doc : <EOL> self . set_embedded_doc ( document , form_key , current_key , remaining_key ) <EOL> elif is_list : <EOL> self . set_list_field ( document , form_key , current_key , remaining_key , key_array_digit ) <EOL> else : <EOL> value = translate_value ( document . _fields [ current_key ] , <EOL> self . form . cleaned_data [ form_key ] ) <EOL> setattr ( document , current_key , value ) <EOL> def set_embedded_doc ( self , document , form_key , current_key , remaining_key ) : <EOL> """<STR_LIT>""" <EOL> embedded_doc = getattr ( document , current_key , False ) <EOL> if not embedded_doc : <EOL> embedded_doc = document . _fields [ current_key ] . document_type_obj ( ) <EOL> new_key , new_remaining_key_array = trim_field_key ( embedded_doc , remaining_key ) <EOL> self . process_document ( embedded_doc , form_key , make_key ( new_key , new_remaining_key_array ) ) <EOL> setattr ( document , current_key , embedded_doc ) <EOL> def set_list_field ( self , document , form_key , current_key , remaining_key , key_array_digit ) : <EOL> """<STR_LIT>""" <EOL> document_field = document . _fields . get ( current_key ) <EOL> list_value = translate_value ( document_field . field , self . form . cleaned_data [ form_key ] ) <EOL> if list_value is None or ( not list_value and not bool ( list_value ) ) : <EOL> return None <EOL> current_list = getattr ( document , current_key , None ) <EOL> if isinstance ( document_field . field , EmbeddedDocumentField ) : <EOL> embedded_list_key = u"<STR_LIT>" . format ( current_key , key_array_digit ) <EOL> embedded_list_document = self . embedded_list_docs . get ( embedded_list_key , None ) <EOL> if embedded_list_document is None : <EOL> embedded_list_document = document_field . field . document_type_obj ( ) <EOL> new_key , new_remaining_key_array = trim_field_key ( embedded_list_document , remaining_key ) <EOL> self . process_document ( embedded_list_document , form_key , new_key ) <EOL> list_value = embedded_list_document <EOL> self . embedded_list_docs [ embedded_list_key ] = embedded_list_document <EOL> if isinstance ( current_list , list ) : <EOL> if embedded_list_document not in current_list : <EOL> current_list . append ( embedded_list_document ) <EOL> else : <EOL> setattr ( document , current_key , [ embedded_list_document ] ) <EOL> elif isinstance ( current_list , list ) : <EOL> current_list . append ( list_value ) <EOL> else : <EOL> setattr ( document , current_key , [ list_value ] ) </s>
