<s> """<STR_LIT>""" <EOL> from djangoplugins . models import Plugin , PluginPoint <EOL> from django import forms <EOL> from django . utils . importlib import import_module <EOL> from . models import DoccodePluginMapping <EOL> def create_form_fields ( plugins_param = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if plugins_param == '<STR_LIT>' : <EOL> plugins = Plugin . objects . all ( ) <EOL> else : <EOL> plugins = plugins_param <EOL> all_plugins_types = [ ] <EOL> plugin_types_clean = [ ] <EOL> plugins_done = [ ] <EOL> kwargs = { } <EOL> for plugin in plugins : <EOL> plugin_temp = plugin . get_plugin ( ) <EOL> plugin_temp . pk = plugin . pk <EOL> plugins_done . append ( plugin_temp ) <EOL> all_plugins_types . append ( plugin_temp . plugin_type ) <EOL> if not ( plugin_temp . plugin_type in plugin_types_clean ) : <EOL> plugin_types_clean . append ( plugin_temp . plugin_type ) <EOL> for plugin_type in plugin_types_clean : <EOL> plugins_temp_list = [ ] <EOL> for plugin in plugins_done : <EOL> if plugin . plugin_type == plugin_type : <EOL> plugins_temp_list . append ( plugin . pk ) <EOL> kwargs [ plugin_type ] = forms . ModelMultipleChoiceField ( plugins . filter ( pk__in = plugins_temp_list ) ) <EOL> return kwargs <EOL> def get_all_plugintypes ( plugins_param = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if plugins_param == '<STR_LIT>' : <EOL> plugins = Plugin . objects . all ( ) <EOL> else : <EOL> plugins = plugins_param <EOL> plugins_types_clean = [ ] <EOL> for plugin in plugins : <EOL> plugin_temp = plugin . get_plugin ( ) <EOL> if not ( plugin_temp . plugin_type in plugins_types_clean ) : <EOL> plugins_types_clean . append ( plugin_temp . plugin_type ) <EOL> return plugins_types_clean <EOL> def extract_plugin_pks ( input_post_data , plugins_queryset = False ) : <EOL> """<STR_LIT>""" <EOL> if not plugins_queryset : plugins_queryset = Plugin . objects . all ( ) <EOL> clean_plugintypes = get_all_plugintypes ( plugins_queryset ) <EOL> requested_plugin_pks = [ ] <EOL> for form_field_name in input_post_data : <EOL> if form_field_name in clean_plugintypes : <EOL> requested_plugin_pks . extend ( input_post_data . getlist ( form_field_name ) ) <EOL> return requested_plugin_pks <EOL> def sort_plugins_by_pluginpoint ( requested_plugin_pks , plugins_queryset = False ) : <EOL> """<STR_LIT>""" <EOL> plugins_sorted_dict = { } <EOL> if not plugins_queryset : plugins_queryset = Plugin . objects . all ( ) <EOL> for plugin_pk in requested_plugin_pks : <EOL> current_plugin = plugins_queryset . filter ( pk = plugin_pk ) <EOL> plugin_instance = current_plugin [ <NUM_LIT:0> ] . get_plugin ( ) <EOL> if not plugins_sorted_dict . has_key ( plugin_instance . settings_field_name ) : <EOL> plugins_sorted_dict [ plugin_instance . settings_field_name ] = [ ] <EOL> plugins_sorted_dict [ plugin_instance . settings_field_name ] . append ( plugin_pk ) <EOL> return plugins_sorted_dict <EOL> def sort_plugins_by_type ( pks_array , plugins_queryset = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> plugins_sorted_dict = { } <EOL> if not plugins_queryset : plugins_queryset = Plugin . objects . all ( ) <EOL> for plugin_pk in pks_array : <EOL> current_plugin = plugins_queryset . filter ( pk = plugin_pk ) <EOL> plugin_instance = current_plugin [ <NUM_LIT:0> ] . get_plugin ( ) <EOL> if not plugins_sorted_dict . has_key ( plugin_instance . plugin_type ) : <EOL> plugins_sorted_dict [ plugin_instance . plugin_type ] = [ ] <EOL> plugins_sorted_dict [ plugin_instance . plugin_type ] . append ( plugin_pk ) <EOL> return plugins_sorted_dict <EOL> def save_doccode_model ( plugins_sorted_dict , input_post_data , pk = False ) : <EOL> """<STR_LIT>""" <EOL> if pk : <EOL> mapping = DoccodePluginMapping . objects . get ( pk = pk ) <EOL> mapping . before_storage_plugins = [ ] <EOL> mapping . storage_plugins = [ ] <EOL> mapping . before_retrieval_plugins = [ ] <EOL> mapping . before_removal_plugins = [ ] <EOL> mapping . before_update_plugins = [ ] <EOL> mapping . database_storage_plugins = [ ] <EOL> else : <EOL> mapping = DoccodePluginMapping ( ) <EOL> mapping . active = input_post_data . has_key ( '<STR_LIT>' ) <EOL> mapping . doccode = input_post_data [ '<STR_LIT>' ] <EOL> mapping . save ( ) <EOL> for field_name , value in plugins_sorted_dict . iteritems ( ) : <EOL> setattr ( mapping , field_name , value ) <EOL> mapping . save ( ) <EOL> return mapping <EOL> def get_all_pluginpoints_dict ( all_pluginpoints_queryset = False ) : <EOL> """<STR_LIT>""" <EOL> if not all_pluginpoints_queryset : <EOL> all_pluginpoints_queryset = PluginPoint . objects . all ( ) <EOL> pluginpoints_dict = { } <EOL> for point in all_pluginpoints_queryset : <EOL> pluginpoint = get_plugin_from_string ( point . name ) <EOL> pluginpoints_dict [ str ( point . pk ) ] = pluginpoint . settings_field_name <EOL> return pluginpoints_dict <EOL> def get_plugin_from_string ( plugin_name ) : <EOL> """<STR_LIT>""" <EOL> modulename , classname = plugin_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> module = import_module ( modulename ) <EOL> return getattr ( module , classname ) <EOL> def save_PluginSelectorForm ( serialized_POST , plugins_queryset = False , pk = False ) : <EOL> """<STR_LIT>""" <EOL> if not plugins_queryset : plugins_queryset = Plugin . objects . all ( ) <EOL> plugins_used_list = extract_plugin_pks ( serialized_POST , plugins_queryset ) <EOL> plugins_sorted_dict = sort_plugins_by_pluginpoint ( plugins_used_list , plugins_queryset ) <EOL> instance_ = save_doccode_model ( plugins_sorted_dict , serialized_POST , pk ) <EOL> return instance_ <EOL> def get_plugins_for_doccode ( instance , plugins_queryset = False ) : <EOL> """<STR_LIT>""" <EOL> plg_s = [ ] <EOL> plugins_dict = [ ] <EOL> plg_s . append ( instance . get_before_storage_plugins ( ) ) <EOL> plg_s . append ( instance . get_storage_plugins ( ) ) <EOL> plg_s . append ( instance . get_before_retrieval_plugins ( ) ) <EOL> plg_s . append ( instance . get_before_removal_plugins ( ) ) <EOL> plg_s . append ( instance . get_before_update_plugins ( ) ) <EOL> plg_s . append ( instance . get_database_storage_plugins ( ) ) <EOL> for queryset in plg_s : <EOL> for plugin in queryset : <EOL> plugins_dict . append ( plugin . pk ) <EOL> return plugins_dict <EOL> def serialize_model_for_PluginSelectorForm ( instance ) : <EOL> """<STR_LIT>""" <EOL> pks_array = get_plugins_for_doccode ( instance ) <EOL> dict = sort_plugins_by_type ( pks_array , Plugin . objects . all ( ) ) <EOL> dict [ "<STR_LIT>" ] = instance . doccode <EOL> if instance . active : <EOL> dict [ "<STR_LIT>" ] = u'<STR_LIT>' <EOL> return dict </s>
