def detach_dataframe(workflow): """ Given a workflow object, delete its dataframe :param workflow: :return: Nothing, the workflow object is updated """ pandas_db.delete_table(workflow.id) # Delete number of rows and columns workflow.nrows = 0 workflow.ncols = 0 workflow.n_filterd_rows = -1 workflow.save() # Delete the column_names, column_types and column_unique Column.objects.filter(workflow__id=workflow.id).delete() # Delete the info for QueryBuilder workflow.set_query_builder_ops() # Table name workflow.data_frame_table_name = '' # Save the workflow with the new fields. workflow.save()
def delete(request, pk): workflow = get_workflow(request, pk) if not workflow: # Workflow is not accessible. Go back to index page. return JsonResponse({ 'form_is_valid': True, 'html_redirect': reverse('workflow:index') }) # Ajax result data = dict() # If the request is not done by the user, flat the error if workflow.user != request.user: messages.error(request, _('You can only delete workflows that you created.')) if request.is_ajax(): data['form_is_valid'] = True data['html_redirect'] = reverse('workflow:index') return JsonResponse(data) return redirect('workflow:index') if request.method == 'POST': # Log the event Log.objects.register(request.user, Log.WORKFLOW_DELETE, workflow, { 'id': workflow.id, 'name': workflow.name }) # And drop the table if pandas_db.is_wf_table_in_db(workflow): pandas_db.delete_table(pk) # Perform the delete operation workflow.delete() # In this case, the form is valid anyway data['form_is_valid'] = True data['html_redirect'] = reverse('workflow:index') else: data['html_form'] = \ render_to_string('workflow/includes/partial_workflow_delete.html', {'workflow': workflow}, request=request) return JsonResponse(data)
def flush_workflow(workflow): """ Flush all the data from the workflow and propagate changes throughout the relations with columns, conditions, filters, etc. These steps require: 1) Delete the data frame from the database 2) Delete all the columns attached to the workflow 3) Delete all the conditions attached to the actions 4) Reflect the number of selected columns to -1 for all actions :param workflow: Workflow object :return: Reflected in the DB """ # Step 1: Delete the data frame from the database pandas_db.delete_table(workflow.id) # Reset some of the workflow fields workflow.nrows = 0 workflow.ncols = 0 workflow.n_filterd_rows = -1 workflow.set_query_builder_ops() workflow.data_frame_table_name = '' # Step 2: Delete the column_names, column_types and column_unique Column.objects.filter(workflow__id=workflow.id).delete() # Step 3: Delete the conditions attached to all the actions attached to the # workflow. Condition.objects.filter(action__workflow=workflow).delete() # Step 4: Reset selected columns from all actions for action in Action.objects.filter(workflow=workflow): action.n_selected_rows = -1 action.save() # Save the workflow with the new fields. workflow.save()
def flush(self): """ Flush all the data from the workflow and propagate changes throughout the relations with columns, conditions, filters, etc. These steps require: 1) Delete the data frame from the database 2) Delete all the columns attached to the workflow 3) Delete all the conditions attached to the actions 4) Delete all the views attached to the workflow :return: Reflected in the DB """ # Step 1: Delete the data frame from the database pandas_db.delete_table(self.id) # Reset some of the workflow fields self.nrows = 0 self.ncols = 0 self.n_filterd_rows = -1 self.set_query_builder_ops() self.data_frame_table_name = '' # Step 2: Delete the column_names, column_types and column_unique self.columns.all().delete() # Step 3: Delete the conditions attached to all the actions attached # to the workflow. self.actions.all().delete() # Step 4: Delete all the views attached to the workflow self.views.all().delete() # Save the workflow with the new fields. self.save()
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