Example #1
0
    def execute(my):
        my.search_type = my.kwargs.get("search_type")
        my.element_name = my.kwargs.get("element_name")
        assert my.search_type
        assert my.element_name

        interval = my.kwargs.get('interval')
        if not interval:
            interval = 120

        data_type = my.kwargs.get('data_type')
        if not data_type:
            data_type = 'float'

        class_name = 'tactic.ui.app.aggregate_wdg.AggregateCmd'
        priority = None

        user = Environment.get_user_name()

        # these interval jobs need to have a specific code
        code = "aggregate|%s|%s" % (my.search_type, my.element_name)

        # check to see if the job exists
        job = Search.get_by_code("sthpw/queue", code)
        if not job:
            job = SearchType.create("sthpw/queue")
            job.set_value("code", code)

            job.set_value("project_code", Project.get_project_code() )
            job.set_value("class_name", class_name)
            job.set_value("command", class_name)
            job.set_value("login", user) 
            job.set_value("queue", 'interval')

            # this is meaningless
            job.set_value("priority", 9999)

            # not sure what to do here if it already exists
            job.set_value("state", 'pending')



            # add a column to the table
            from pyasm.command import ColumnAddCmd
            from pyasm.search import AlterTable
            column_name = my.element_name
            cmd = ColumnAddCmd(my.search_type, column_name, data_type)
            cmd.execute()

            # modify the table
            #alter = AlterTable(my.search_type)
            #alter.modify(my.search_type, data_type)
            #print alter.get_statements()


        job.set_value("serialized", str(my.kwargs) )
        job.set_value("interval", interval)
        job.commit()
Example #2
0
    def execute(my):

        search_type = my.kwargs.get("search_type")
        column_info = SearchType.get_column_info(search_type)

        values = my.kwargs.get("values")

        # get the definition config for this search_type
        from pyasm.search import WidgetDbConfig
        config = WidgetDbConfig.get_by_search_type(search_type, "definition")
        if not config:
            config = SearchType.create("config/widget_config")
            config.set_value("search_type", search_type)
            config.set_value("view", "definition")
            config.commit()
            config._init()

        for data in values:

            name = data.get("name")
            name = name.strip()
            if name == '':
                continue

            try:
                name.encode('ascii')
            except UnicodeEncodeError:
                raise TacticException('Column name needs to be in English. Non-English characters can be used in Title when performing [Edit Column Definition] afterwards.')


            if column_info.get(name):
                raise CommandException("Column [%s] is already defined" % name)

            format = data.get("format")
            fps = data.get("fps")
            data_type = data.get("data_type")

            from pyasm.command import ColumnAddCmd
            cmd = ColumnAddCmd(search_type, name, data_type)
            cmd.execute()
            #(my, search_type, attr_name, attr_type, nullable=True):


            class_name = 'tactic.ui.table.FormatElementWdg'
            options = {
                'format': format,
                'type': data_type,
                'fps': fps
            }


            # add a new widget to the definition
            config.append_display_element(name, class_name, options=options)

        config.commit_config()
Example #3
0
    def on_insert(my):
        '''Function that should be run on insert/update. It's already automatically called during insert.
        On update, the caller needs to call this explicitly. It checks the search type
        this pipeline is associated with and if there is no pipeline code
        column, then update it.  It updates the process table also.'''
        search_type = my.get_value('search_type')
        my.update_process_table(search_type=search_type)

        # don't do anything for task sType
        if search_type =='sthpw/task':
            return


        if not search_type:
            return

        if ProdSetting.get_value_by_key('autofill_pipeline_code') != 'false':
            try:
                columns = SearchType.get_columns(search_type)
                if not 'pipeline_code' in columns:
                    # add the pipeline code column
                    from pyasm.command import ColumnAddCmd
                    cmd = ColumnAddCmd(search_type, "pipeline_code", "varchar")
                    cmd.execute()
            except SqlException, e:
                print "Error creating column [pipeline_code] for %" %search_type 
                pass

            # go through all of the sobjects and set all the empty ones
            # to the new pipeline
            search = Search(search_type)
            search.add_op("begin")
            search.add_filter("pipeline_code", "NULL", op='is', quoted=False)
            search.add_filter("pipeline_code", "")
            search.add_op("or")
            sobject_ids = search.get_sobject_ids()
            
            if sobject_ids:
                # this is much faster and memory efficient
                db_resource = SearchType.get_db_resource_by_search_type(search_type)
                sql = DbContainer.get(db_resource)
                tbl = search.get_table()
                sobject_ids = [str(x) for x in sobject_ids]
                pipeline_code =  my.get_value("code")
                sql.do_update('''UPDATE "%s" SET "pipeline_code" = '%s' WHERE id in (%s) ''' %(tbl, pipeline_code, ','.join(sobject_ids)))
            """
    def create_required_columns(my, search_type):
        columns = my.get_required_columns()
        data_type = my.get_option("type")

        constraint = my.get_option("constraint")

        for column_name in columns:
            try:
                column_exist_error = None
                cmd = ColumnAddCmd(search_type, column_name, data_type)
                cmd.execute()
            except TacticException, e:
                if 'already existed in this table' in e.__str__():
                    column_exist_error = e
                else:
                    raise 
            finally:
Example #5
0
    def execute(my):
        if not my.initialized:
            my.init()

        assert my.search_type
        assert my.file_path
        assert my.columns

        csv_parser = CsvParser(my.file_path)
        if my.has_title:
            csv_parser.set_has_title_row(True)
        else:
            csv_parser.set_has_title_row(False)

        if my.encoder:
            csv_parser.set_encoder(my.encoder)

        csv_parser.parse()

        # get the data and columns
        #csv_titles = csv_parser.get_titles()
        csv_data = csv_parser.get_data()
        # make sure all of the new columns are created
        csv_titles = []
        for i, column in enumerate(my.columns):
            if not column:
                new_column = my.new_columns[i]
                new_column_type = my.new_column_types[i]
                if new_column and new_column not in ['id', 'code'] and\
                    i in my.enabled_idx:
                    # create the new column
                    from pyasm.command import ColumnAddCmd
                    #col_type = "Name/Code"
                    cmd = ColumnAddCmd(my.search_type, new_column, new_column_type)
                    cmd.execute()

                    # create the sobject for now
                    sobject = SObjectFactory.create("prod/custom_property")
                    sobject.set_value("search_type", my.search_type)
                    sobject.set_value("name", new_column)
                    sobject.set_value("description", new_column)
                    sobject.commit()

                csv_titles.append( my.new_columns[i] )
            else:
                csv_titles.append( column )

        try:
            id_col = csv_titles.index(my.id_col)
            # id is special we want it to be identifiable at all times
            # but not importable
            if my.id_col != 'id' and id_col not in my.enabled_idx:
                id_col = -1
        except ValueError:
            id_col = -1

        new_entries = []
        updated_entries = []
        error_entries = []
        error = False
 
        # create entries or update values
        for row_count, row in enumerate(csv_data):
            sobject = None
            # if id_col doesn't exist
            is_new_entry = False
            
            if id_col == -1:
                sobject = SObjectFactory.create(my.search_type)
                is_new_entry = True
            else:
                id = row[id_col]
                if id:
                    # this essentially updates the current sobject in db
                    if my.id_col=='code':
                        sobject = Search.get_by_code(my.search_type, id.strip())
                    elif my.id_col=='id':
                        sobject = Search.get_by_id(my.search_type, id.strip())
                    else:
                        u_search = Search(my.search_type)
                        u_search.add_filter(my.id_col, id.strip())
                        sobject = u_search.get_sobject()
                    #assert sobject
                # in case a previously exported sobject with this code
                # or id has been deleted or it is a completely foreign code
                # or id, sobject will be None
                else: # skip if empty id or code
                    continue
                  
                if not sobject:
                    sobject = SObjectFactory.create(my.search_type)
                    is_new_entry = True

            new_columns = 0
            note = None
            for cell_count, cell in enumerate(row):
                '''
                column_override = my.columns[cell_count]

                if column_override:
                    title = column_override
                else:
                    title = csv_titles[cell_count]
                    if not title:
                        continue
                '''
                # skip if not enabled
                if cell_count not in my.enabled_idx:
                    continue

                title = csv_titles[cell_count]
                if not title:
                    continue

                # always skip id column
                if title == "id":
                    continue
                cell = cell.strip()
               
                # remove control, other characters in unicode
                #cell = re.sub(r'\p{Cc}','', cell)
                cell = re.sub(r"[\x01-\x08\x0b-\x1f\x7f-\x9f]",'', cell)
                if title == "(note)":
                    note = cell
                else:
                    sobject.set_value(title, cell)
                new_columns += 1

            if not new_columns:
                msg = "No column or only the id column is selected."
                raise CommandException(msg)

            
            try:
                #sobject.commit(triggers=False)
                sobject.commit(triggers=True)

                if note:
                    note_obj = SearchType.create("sthpw/note")
                    note_obj.set_value("note", note)
                    note_obj.set_value("process", "publish")
                    note_obj.set_value("context", "publish")
                    note_obj.set_user()
                    note_obj.set_parent(sobject)
                    note_obj.commit()

            except SqlException, e:
                msg = "Error creating new entry for row [%s]: %s, %s" % (row_count, str(row), e.__str__() )
                # (Maybe not neccessary) must raise SqlException or it would fail silently
                if my.test_run:
                    error = True
                    error_entries.append(sobject.get_code())
                raise SqlException(msg)
            else:
                if is_new_entry:
                    new_entries.append(sobject.get_code())
                else:
                    updated_entries.append(sobject.get_code())
Example #6
0
    def execute(my):

        web = WebContainer.get_web()

        # get command line options
        search_type = my.kwargs.get("search_type")
        assert search_type

        view = my.kwargs.get("view")
        if not view:
            view = get_template_view()



        # check if this is advanced mode
        mode = web.get_form_value("custom_mode")
        if not mode:
            mode = 'simple'

        if mode == 'xml':
            config_string = web.get_form_value("config_xml")

            # handle the "default" view
            view = DEFAULT_VIEW
            config = WidgetDbConfig.get_by_search_type(search_type, view)
            if not config:
                config = WidgetDbConfig.create(search_type, view)

            xml = Xml()
            xml.read_string(config_string)
            element_name = xml.get_value("element/@name")
            element_name = element_name.strip()
            assert element_name

            type = xml.get_value("element/@type")
            if not type:
                class_name = xml.get_value("element/display/@class")

                if not class_name:
                    raise TacticException("Either a type or class name needs to be defined in config xml.")

            config.append_xml_element(element_name,config_string)
            config.commit_config()

            # create the required columns
            widget = config.get_display_widget(element_name)
            columns = widget.get_required_columns()

            if columns:
                print "WARNING: need to create columns: ", columns

            my.info['element_name'] = element_name

            return



        type = web.get_form_value("custom_type")
        description = web.get_form_value("custom_description")
        if not description:
            description = "No descripton"

        title = web.get_form_value("custom_title")



        name = web.get_form_value("custom_name")
        name = name.strip()
        if not name:
            raise TacticException("No name specified")


        add_to_current_view = web.get_form_value("add_to_current_view")
        add_to_edit_view = web.get_form_value("add_to_edit_view")
        is_searchable = web.get_form_value("is_searchable")

        # create the column
        if type not in ['button', 'empty']:
            cmd = ColumnAddCmd(search_type, name, type)
            cmd.execute()

        
        # create the type
        class_name = None
        options = {}
        # this is actually element attrs
        element_options = {}
        edit_class_name = None
        edit_options = {}
        edit_attrs = {}

        element_type = type

        # Date Range is not used any more in the UI"
        if type == "Date Range":
            class_name = "GanttWdg"
            options["start_date_column"] = "%s_start_date" % name
            options["end_deate_column"] = "%s_end_date" % name

        elif type == "date":
            class_name = "DateWdg"
            #edit_class_name = "CalendarWdg"

            element_type = 'timestamp'
            edit_attrs['type'] = 'timestamp'
            edit_class_name = ""
            add_to_edit_view = True

        elif type == "foreign_key":
            class_name = ""
            edit_class_name = "SelectWdg"
            foreign_search_type = web.get_form_value("foreign_key_search_select")
            edit_options["query"] = '%s|code|code' % foreign_search_type

            # turn on add to edit view
            add_to_edit_view = True

        elif type == "button":
            class_name = "tactic.ui.table.ButtonElementWdg"
            script = web.get_form_value("option_script_select")
            if script:
                options['script'] = script
            icon = web.get_form_value("option_icon_select")
            if icon:
                options['icon'] = icon


            edit_class_name = ""

            # This does not have a type
            element_type = None


        elif type == "empty":
            element_type = None
            pass


        elif type == "list":
            class_name = ""
            edit_class_name = "SelectWdg"
            list_values = web.get_form_value("list_values")
            edit_options['values'] = list_values

            add_to_edit_view = True

        element_options['type'] = element_type
        element_options['title'] = title
        

        # handle the "default" view
        view = DEFAULT_VIEW
        config = WidgetDbConfig.get_by_search_type(search_type, view)
        if not config:
            config = WidgetDbConfig.create(search_type, view)
        config.append_display_element(name, class_name, options=options, \
                element_attrs=element_options)
        config.commit_config()


        # get the config file
        if add_to_current_view:
            config = WidgetDbConfig.get_by_search_type(search_type, view)

            if not config:
                # if it doesn't exist, the check to see, if there is a hard
                # coded view out there
                predefined_config = WidgetConfigView.get_by_search_type(search_type, view)
                xml = predefined_config.get_xml()

                # create a new db one
                config = WidgetDbConfig.create(search_type, view)

                if xml:
                    config.set_value("config", xml.to_string())
                    config._init()

            config.append_display_element(name)
            config.commit_config()

        # TODO: Need to make this searchable using simple search ?????
        if is_searchable:
            element_options['searchable'] = 'true'

        # handle the "edit"
        if add_to_edit_view and view != "edit":
            config = WidgetDbConfig.get_by_search_type(search_type, "edit")
            if not config:
                config = WidgetDbConfig.create(search_type, "edit")
            config.append_display_element(name, edit_class_name, edit_options,element_attrs=edit_attrs)
            config.commit_config()


        """
        # this sType has been deprecated
        sobject = SearchType.create("prod/custom_property")
        sobject.set_value("search_type", search_type)
        sobject.set_value("name", name)
        sobject.set_value("description", description)
        sobject.commit()
        """

        # set some information
        my.description = "Added Property [%s] of type [%s] to [%s]" % \
            (name, type, search_type)

        my.info['element_name'] = name
 def create_required_columns(my, search_type):
     columns = my.get_required_columns()
     data_type = "varchar"
     for column_name in columns:
         cmd = ColumnAddCmd(search_type, column_name, data_type)
         cmd.execute()
Example #8
0
 def create_required_columns(self, search_type):
     column_name = self.get_name()
     data_type = "varchar"
     cmd = ColumnAddCmd(search_type, column_name, data_type)
     cmd.execute()
Example #9
0
    def execute(my):
        web = WebContainer.get_web()
        alter_mode = my.kwargs.get("alter_mode")
        title = my.kwargs.get("title")
        config_mode = web.get_form_value("config_mode")
        view = web.get_form_value('view')
        constraint = web.get_form_value("config_constraint")
        data_type = ''

        if config_mode == "advanced":
            config_string = web.get_form_value("config_xml")
            if config_string:
                xml = Xml()
                xml.read_string(config_string)
                node = xml.get_root_node()
                data_type = xml.get_attribute(node, "data_type")
                nullable = xml.get_attribute(node,
                                             "nullable") in ['true', 'True']

        else:
            data_type = web.get_form_value("config_data_type")
            if data_type == 'Other...':
                data_type = web.get_form_value("config_data_type_custom")
            cb = CheckboxWdg("config_nullable")
            nullable = cb.is_checked()

        # if advanced is selected in the Widget Column view, data_type is ''
        # read from UI
        if not data_type and view == 'definition':
            data_type = web.get_form_value("config_data_type")
            if data_type == 'Other...':
                data_type = web.get_form_value("config_data_type_custom")
            cb = CheckboxWdg("config_nullable")
            nullable = cb.is_checked()

        column_name = web.get_form_value("column_name")
        search_type = web.get_form_value("target_search_type")
        if alter_mode == ManageSearchTypeDetailWdg.REMOVE_COLUMN:
            cmd = ColumnDropCmd(search_type, column_name)
            Command.execute_cmd(cmd)
            # delete widget config from definition view
            widget_config = WidgetDbConfig.get_by_search_type(
                search_type, 'definition')
            if widget_config:
                config = WidgetConfig.get(
                    'definition', xml=widget_config.get_xml_value('config'))
                config.remove_xml_element(column_name)
                new_xml = config.get_xml().to_string()
                widget_config.set_value("config", new_xml)
                widget_config.commit()
                # set cache to {}
                from pyasm.common import Container

                Container.put("WidgetConfigView:config_cache", {})
                #Container.put("WidgetConfig:config_cache", {})
        elif alter_mode == ManageSearchTypeDetailWdg.MODIFY_COLUMN:
            cmd = ColumnAlterCmd(search_type, column_name, data_type, nullable)
            Command.execute_cmd(cmd)
            element_options = {}
            element_options['type'] = data_type
            if title:
                element_options['title'] = title

            # handle the "default" view
            # update the widget config data type in the xml
            view = my.DEFAULT_VIEW
            config = WidgetDbConfig.get_by_search_type(search_type, view)
            if config:
                config.append_display_element(column_name, options={}, \
                    element_attrs=element_options)
                config.commit_config()

        elif alter_mode == ManageSearchTypeDetailWdg.ADD_COLUMN:
            cmd = ColumnAddCmd(search_type, column_name, data_type, nullable)
            Command.execute_cmd(cmd)

        if constraint:
            # add constraint
            from pyasm.command import ColumnAddIndexWdg
            cmd = ColumnAddIndexWdg()
            cmd.execute()
        else:
            # remove constraint
            pass
Example #10
0
    def execute(my):

        web = WebContainer.get_web()

        # get command line options
        search_type = my.kwargs.get("search_type")
        assert search_type

        view = my.kwargs.get("view")
        if not view:
            view = get_template_view()

        # check if this is advanced mode
        mode = web.get_form_value("custom_mode")
        if not mode:
            mode = 'simple'

        if mode == 'xml':
            config_string = web.get_form_value("config_xml")

            # handle the "default" view
            view = DEFAULT_VIEW
            config = WidgetDbConfig.get_by_search_type(search_type, view)
            if not config:
                config = WidgetDbConfig.create(search_type, view)

            xml = Xml()
            xml.read_string(config_string)
            element_name = xml.get_value("element/@name")
            element_name = element_name.strip()
            assert element_name

            type = xml.get_value("element/@type")
            if not type:
                class_name = xml.get_value("element/display/@class")

                if not class_name:
                    raise TacticException(
                        "Either a type or class name needs to be defined in config xml."
                    )

            config.append_xml_element(element_name, config_string)
            config.commit_config()

            # create the required columns
            widget = config.get_display_widget(element_name)
            columns = widget.get_required_columns()

            if columns:
                print "WARNING: need to create columns: ", columns

            my.info['element_name'] = element_name

            return

        type = web.get_form_value("custom_type")
        description = web.get_form_value("custom_description")
        if not description:
            description = "No descripton"

        title = web.get_form_value("custom_title")

        name = web.get_form_value("custom_name")
        name = name.strip()
        if not name:
            raise TacticException("No name specified")

        add_to_current_view = web.get_form_value("add_to_current_view")
        add_to_edit_view = web.get_form_value("add_to_edit_view")
        is_searchable = web.get_form_value("is_searchable")

        # create the column
        if type not in ['button', 'empty']:
            cmd = ColumnAddCmd(search_type, name, type)
            cmd.execute()

        # create the type
        class_name = None
        options = {}
        # this is actually element attrs
        element_options = {}
        edit_class_name = None
        edit_options = {}
        edit_attrs = {}

        element_type = type

        # Date Range is not used any more in the UI"
        if type == "Date Range":
            class_name = "GanttWdg"
            options["start_date_column"] = "%s_start_date" % name
            options["end_deate_column"] = "%s_end_date" % name

        elif type == "date":
            class_name = "DateWdg"
            #edit_class_name = "CalendarWdg"

            element_type = 'timestamp'
            edit_attrs['type'] = 'timestamp'
            edit_class_name = ""
            add_to_edit_view = True

        elif type == "foreign_key":
            class_name = ""
            edit_class_name = "SelectWdg"
            foreign_search_type = web.get_form_value(
                "foreign_key_search_select")
            edit_options["query"] = '%s|code|code' % foreign_search_type

            # turn on add to edit view
            add_to_edit_view = True

        elif type == "button":
            class_name = "tactic.ui.table.ButtonElementWdg"
            script = web.get_form_value("option_script_select")
            if script:
                options['script'] = script
            icon = web.get_form_value("option_icon_select")
            if icon:
                options['icon'] = icon

            edit_class_name = ""

            # This does not have a type
            element_type = None

        elif type == "empty":
            element_type = None
            pass

        elif type == "list":
            class_name = ""
            edit_class_name = "SelectWdg"
            list_values = web.get_form_value("list_values")
            edit_options['values'] = list_values

            add_to_edit_view = True

        element_options['type'] = element_type
        element_options['title'] = title

        # handle the "default" view
        view = DEFAULT_VIEW
        config = WidgetDbConfig.get_by_search_type(search_type, view)
        if not config:
            config = WidgetDbConfig.create(search_type, view)
        config.append_display_element(name, class_name, options=options, \
                element_attrs=element_options)
        config.commit_config()

        # get the config file
        if add_to_current_view:
            config = WidgetDbConfig.get_by_search_type(search_type, view)

            if not config:
                # if it doesn't exist, the check to see, if there is a hard
                # coded view out there
                predefined_config = WidgetConfigView.get_by_search_type(
                    search_type, view)
                xml = predefined_config.get_xml()

                # create a new db one
                config = WidgetDbConfig.create(search_type, view)

                if xml:
                    config.set_value("config", xml.to_string())
                    config._init()

            config.append_display_element(name)
            config.commit_config()

        # TODO: Need to make this searchable using simple search ?????
        if is_searchable:
            element_options['searchable'] = 'true'

        # handle the "edit"
        if add_to_edit_view and view != "edit":
            config = WidgetDbConfig.get_by_search_type(search_type, "edit")
            if not config:
                config = WidgetDbConfig.create(search_type, "edit")
            config.append_display_element(name,
                                          edit_class_name,
                                          edit_options,
                                          element_attrs=edit_attrs)
            config.commit_config()
        """
        # this sType has been deprecated
        sobject = SearchType.create("prod/custom_property")
        sobject.set_value("search_type", search_type)
        sobject.set_value("name", name)
        sobject.set_value("description", description)
        sobject.commit()
        """

        # set some information
        my.description = "Added Property [%s] of type [%s] to [%s]" % \
            (name, type, search_type)

        my.info['element_name'] = name
Example #11
0
 def create_required_columns(my, search_type):
     columns = my.get_required_columns()
     data_type = "varchar"
     for column_name in columns:
         cmd = ColumnAddCmd(search_type, column_name, data_type)
         cmd.execute()
Example #12
0
    def execute(self):

        search_type = self.kwargs.get("search_type")
        column_info = SearchType.get_column_info(search_type)

        values = self.kwargs.get("values")

        # get the definition config for this search_type
        from pyasm.search import WidgetDbConfig
        config = WidgetDbConfig.get_by_search_type(search_type, "definition")
        if not config:
            config = SearchType.create("config/widget_config")
            config.set_value("search_type", search_type)
            config.set_value("view", "definition")
            config.commit()
            config._init()

        # add to the edit definition
        edit_config = WidgetDbConfig.get_by_search_type(
            search_type, "edit_definition")
        if not edit_config:
            edit_config = SearchType.create("config/widget_config")
            edit_config.set_value("search_type", search_type)
            edit_config.set_value("view", "edit_definition")
            edit_config.commit()
            edit_config._init()

        for data in values:

            name = data.get("name")
            name = name.strip()
            if name == '':
                continue

            try:
                name.encode('ascii')
            except UnicodeEncodeError:
                raise TacticException(
                    'Column name needs to be in English. Non-English characters can be used in Title when performing [Edit Column Definition] afterwards.'
                )

            if column_info.get(name):
                raise CommandException("Column [%s] is already defined" % name)

            format = data.get("format")
            fps = data.get("fps")
            data_type = data.get("data_type")

            from pyasm.command import ColumnAddCmd
            cmd = ColumnAddCmd(search_type, name, data_type)
            cmd.execute()
            #(self, search_type, attr_name, attr_type, nullable=True):

            class_name = 'tactic.ui.table.FormatElementWdg'
            options = {'format': format, 'type': data_type, 'fps': fps}

            # add a new widget to the definition
            config.append_display_element(name, class_name, options=options)

            edit_class_name = 'TextWdg'
            edit_options = {}

            # add a new widget to the definition
            edit_config.append_display_element(name,
                                               edit_class_name,
                                               options=edit_options)

        config.commit_config()
        edit_config.commit_config()

        # views to add it to
        table_views = []
        edit_views = []
Example #13
0
    def execute(my):
        if not my.initialized:
            my.init()

        assert my.search_type
        assert my.file_path
        assert my.columns

        csv_parser = CsvParser(my.file_path)
        if my.has_title:
            csv_parser.set_has_title_row(True)
        else:
            csv_parser.set_has_title_row(False)

        if my.encoder:
            csv_parser.set_encoder(my.encoder)

        csv_parser.parse()

        # get the data and columns
        #csv_titles = csv_parser.get_titles()
        csv_data = csv_parser.get_data()
        # make sure all of the new columns are created
        csv_titles = []
        for i, column in enumerate(my.columns):
            if not column:
                new_column = my.new_columns[i]
                new_column_type = my.new_column_types[i]
                if new_column and new_column not in ['id', 'code'] and\
                    i in my.enabled_idx:
                    # create the new column
                    from pyasm.command import ColumnAddCmd
                    #col_type = "Name/Code"
                    cmd = ColumnAddCmd(my.search_type, new_column, new_column_type)
                    cmd.execute()

                    # create the sobject for now
                    sobject = SObjectFactory.create("prod/custom_property")
                    sobject.set_value("search_type", my.search_type)
                    sobject.set_value("name", new_column)
                    sobject.set_value("description", new_column)
                    sobject.commit()

                csv_titles.append( my.new_columns[i] )
            else:
                csv_titles.append( column )

        try:
            id_col = csv_titles.index(my.id_col)
            # id is special we want it to be identifiable at all times
            # but not importable
            if my.id_col != 'id' and id_col not in my.enabled_idx:
                id_col = -1
        except ValueError:
            id_col = -1

        new_entries = []
        updated_entries = []
        error_entries = []
        error = False
 
        # create entries or update values
        for row_count, row in enumerate(csv_data):
            sobject = None
            # if id_col doesn't exist
            is_new_entry = False
            
            if id_col == -1:
                sobject = SObjectFactory.create(my.search_type)
                is_new_entry = True
            else:
                id = row[id_col]
                if id:
                    # this essentially updates the current sobject in db
                    if my.id_col=='code':
                        sobject = Search.get_by_code(my.search_type, id.strip())
                    elif my.id_col=='id':
                        sobject = Search.get_by_id(my.search_type, id.strip())
                    else:
                        u_search = Search(my.search_type)
                        u_search.add_filter(my.id_col, id.strip())
                        sobject = u_search.get_sobject()
                    #assert sobject
                # in case a previously exported sobject with this code
                # or id has been deleted or it is a completely foreign code
                # or id, sobject will be None
                else: # skip if empty id or code
                    continue
                  
                if not sobject:
                    sobject = SObjectFactory.create(my.search_type)
                    is_new_entry = True

            new_columns = 0
            note = None
            for cell_count, cell in enumerate(row):
                '''
                column_override = my.columns[cell_count]

                if column_override:
                    title = column_override
                else:
                    title = csv_titles[cell_count]
                    if not title:
                        continue
                '''
                # skip if not enabled
                if cell_count not in my.enabled_idx:
                    continue

                title = csv_titles[cell_count]
                if not title:
                    continue

                # always skip id column
                if title == "id":
                    continue
                cell = cell.strip()
               
                # remove control, other characters in unicode
                #cell = re.sub(r'\p{Cc}','', cell)
                cell = re.sub(r"[\x01-\x08\x0b-\x1f\x7f-\x9f]",'', cell)
                if title == "(note)":
                    note = cell
                else:
                    sobject.set_value(title, cell)
                new_columns += 1

            if not new_columns:
                msg = "No column or only the id column is selected."
                raise CommandException(msg)

            
            try:
                #sobject.commit(triggers=False)
                sobject.commit(triggers=True)

                if note:
                    note_obj = SearchType.create("sthpw/note")
                    note_obj.set_value("note", note)
                    note_obj.set_value("process", "publish")
                    note_obj.set_value("context", "publish")
                    note_obj.set_user()
                    note_obj.set_parent(sobject)
                    note_obj.commit()

            except SqlException, e:
                msg = "Error creating new entry for row [%s]: %s, %s" % (row_count, str(row), e.__str__() )
                if my.test_run:
                    error = True
                    error_entries.append(sobject.get_code())
                raise SqlException(msg)
            else:
                if is_new_entry:
                    new_entries.append(sobject.get_code())
                else:
                    updated_entries.append(sobject.get_code())