def store_dataframe_in_db(data_frame, pk): """ Given a dataframe and the primary key of a workflow, it dumps its content on a table that is rewritten every time. :param data_frame: Pandas data frame containing the data :param pk: The unique key for the workflow :return: Nothing. Side effect in the database """ return store_table_in_db(data_frame, pk, create_table_name(pk))
def store_dataframe_in_db(data_frame, pk, reset_keys=True): """ Given a dataframe and the primary key of a workflow, it dumps its content on a table that is rewritten every time. :param data_frame: Pandas data frame containing the data :param pk: The unique key for the workflow :param reset_keys: Reset the field is_key computing its value from scratch :return: Nothing. Side effect in the database """ return store_table_in_db(data_frame, pk, create_table_name(pk), reset_keys=reset_keys)
def create(self, validated_data, **kwargs): # Initial values workflow_obj = None try: workflow_obj = Workflow( user=self.context['user'], name=self.context['name'], description_text=validated_data['description_text'], nrows=0, ncols=0, attributes=validated_data['attributes'], query_builder_ops=validated_data.get('query_builder_ops', {})) workflow_obj.save() # Create the columns column_data = ColumnSerializer(data=validated_data.get( 'columns', []), many=True, context={'workflow': workflow_obj}) # And save its content if column_data.is_valid(): column_data.save() else: raise Exception('Unable to save column information') # If there is any column with position = 0, recompute (this is to # guarantee backward compatibility. if workflow_obj.columns.filter(position=0).exists(): for idx, c in enumerate(workflow_obj.columns.all()): c.position = idx + 1 c.save() # Load the data frame data_frame = validated_data.get('data_frame', None) if data_frame is not None: ops.store_dataframe_in_db(data_frame, workflow_obj.id) # Reconcile now the information in workflow and columns with the # one loaded workflow_obj.data_frame_table_name = \ pandas_db.create_table_name(workflow_obj.pk) workflow_obj.ncols = validated_data['ncols'] workflow_obj.nrows = validated_data['nrows'] workflow_obj.save() # Create the actions pointing to the workflow action_data = ActionSerializer(data=validated_data.get( 'actions', []), many=True, context={'workflow': workflow_obj}) if action_data.is_valid(): action_data.save() else: raise Exception('Unable to save column information') # Create the views pointing to the workflow view_data = ViewSerializer(data=validated_data.get('views', []), many=True, context={'workflow': workflow_obj}) if view_data.is_valid(): view_data.save() else: raise Exception('Unable to save column information') except Exception: # Get rid of the objects created if workflow_obj: if workflow_obj.has_data_frame(): pandas_db.delete_table(workflow_obj.id) if workflow_obj.id: workflow_obj.delete() raise return workflow_obj
def create(self, validated_data, **kwargs): workflow_obj = Workflow( user=self.context['user'], name=self.context['name'], description_text=validated_data['description_text'], nrows=0, ncols=0, attributes=validated_data['attributes'], query_builder_ops=validated_data.get('query_builder_ops', {}) ) workflow_obj.save() # Create the columns column_data = ColumnSerializer( data=validated_data.get('columns', []), many=True, context={'workflow': workflow_obj}) # And save its content if column_data.is_valid(): column_data.save() else: workflow_obj.delete() return None # Create the actions pointing to the workflow action_data = ActionSerializer( data=validated_data.get('actions', []), many=True, context={'workflow': workflow_obj} ) if action_data.is_valid(): action_data.save() else: workflow_obj.delete() return None # Create the views pointing to the workflow view_data = ViewSerializer( data=validated_data.get('views', []), many=True, context={'workflow': workflow_obj} ) if view_data.is_valid(): view_data.save() else: workflow_obj.delete() return None # Load the data frame data_frame = validated_data.get('data_frame', None) if data_frame is not None: ops.store_dataframe_in_db(data_frame, workflow_obj.id) # Reconcile now the information in workflow and columns with the # one loaded workflow_obj.data_frame_table_name = \ pandas_db.create_table_name(workflow_obj.pk) workflow_obj.ncols = validated_data['ncols'] workflow_obj.nrows = validated_data['nrows'] workflow_obj.save() return workflow_obj
def get_queryset_by_workflow_id(workflow_id): return get_table_queryset(create_table_name(workflow_id))
def workflow_id_has_table(workflow_id): return is_table_in_db(create_table_name(workflow_id))