def post(self, request, *args, **kwargs): form = self.form_class(request.POST) if form.is_valid(): system = form.save(commit=False) system.system_created_by_user_id = request.user system.system_modified_by_user_id = request.user system.save() form.save_m2m() # extract lines from ip list lines = request.POST.get('iplist').splitlines() # call function to save ips ips_save(request, system, lines) # workflow handling if 'workflow' in request.POST: error_code = Workflow.apply(request.POST.getlist("workflow"), system, request.user) if error_code: messages.warning(request, 'Could not apply workflow') else: messages.success(request, 'Workflow applied') # call logger system.logger(str(request.user), ' SYSTEM_ADD_EXECUTED') messages.success(request, 'System added') return redirect(reverse('system_detail', args=(system.system_id, ))) else: return render(request, self.template_name, {'form': form})
def test_apply_wrong_value_workflow(self): """ plausibility test""" # get object workflows = ('should_be_integer', ) system = System.objects.get(system_name='system_1') user = User.objects.get(username='******') error_code = Workflow.apply(workflows, system, user) # compare self.assertEqual(error_code, 1)
def test_apply_nonexistent_workflow(self): """ plausibility test""" # get object workflows = (99, ) system = System.objects.get(system_name='system_1') user = User.objects.get(username='******') error_code = Workflow.apply(workflows, system, user) # compare self.assertEqual(error_code, 1)
def helper_apply_workflow(self, system, workflow_name, workflow_amount): """ helper function """ # get workflow workflow = Workflow.objects.get(workflow_name=workflow_name) # build workflow list workflows = [workflow.workflow_id for i in range(workflow_amount)] # get user user = User.objects.get(username='******') # apply workflow return Workflow.apply(workflows, system, user)
def apply_workflows(request, system_id=None): try: # get system by id system = System.objects.get(pk=system_id) except System.DoesNotExist: # faulty system id messages.error(request, 'System does not exist') return redirect(reverse('system_list')) # parse wokflow list (multiple workflows possible) workflows = request.POST.getlist("workflow") error_code = Workflow.apply(workflows, system, request.user) if error_code: messages.error(request, 'Could not apply workflow') return redirect(reverse('workflow_list')) messages.success(request, 'Workflow applied') return redirect(reverse('system_detail', args=(system.system_id, )))
def system_creator_async(request_post, request_user): """ function to create many systems at once """ # call logger debug_logger(str(request_user), ' SYSTEM_CREATOR_START') # extract lines from systemlist (list results from request object via large text area) lines = request_post.get('systemlist').splitlines() # count lines (needed for messages) number_of_lines = len(lines) # set systems_created_counter (needed for messages) systems_created_counter = 0 # set systems_skipped_counter (needed for messages) systems_skipped_counter = 0 # set lines_faulty_counter (needed for messages) lines_faulty_counter = 0 # create empty list (needed for messages) skipped_systems = [] # workflows to apply workflows = request_post.getlist("workflow") # set workflows_applied (needed for messages) if workflows: workflow_count = len(workflows) else: workflow_count = 0 workflows_applied = 0 # iterate over lines for line in lines: # skip empty lines if line == '': # autoincrement counter lines_faulty_counter += 1 # call logger warning_logger(str(request_user), ' SYSTEM_CREATOR_ROW_EMPTY') continue # check line for length of string if len(line) > 50: # autoincrement counter lines_faulty_counter += 1 # call logger warning_logger(str(request_user), ' SYSTEM_CREATOR_LONG_STRING') continue # check for existence of system system = System.objects.filter(system_name=line) """ already existing system """ # in case of existing system if system.count() > 0: # autoincrement counter systems_skipped_counter += 1 # add system name to list of skipped systems skipped_systems.append(line) # call logger warning_logger( str(request_user), f' SYSTEM_CREATOR_SYSTEM_EXISTS system_name:{line}') # leave this loop because system with this systemname already exists continue """ new system """ # create form with request data form = SystemCreatorForm(request_post) # create system if form.is_valid(): """ object creation """ # don't save form yet system = form.save(commit=False) # set system_name system.system_name = line # set auto values system.system_created_by_user_id = request_user system.system_modified_by_user_id = request_user # save object system.save() # save manytomany form.save_m2m() """ object counter / log """ # autoincrement counter systems_created_counter += 1 # call logger system.logger(str(request_user), ' SYSTEM_CREATOR_EXECUTED') # apply workflows if workflows: error_code = Workflow.apply(workflows, system, request_user) if error_code: system.logger(str(request_user), ' COULD_NOT_APPLY_WORKFLOW') else: workflows_applied += workflow_count """ finish system importer """ # call final messages final_messages(systems_created_counter, systems_skipped_counter, lines_faulty_counter, skipped_systems, number_of_lines, request_user, workflow_count, workflows_applied) # call logger info_logger( str(request_user), f' SYSTEM_CREATOR_STATUS' f' created:{systems_created_counter}' f'|skipped:{systems_skipped_counter}' f'|faulty_lines:{lines_faulty_counter}' f'|workflows_applied:{workflows_applied}') # call logger debug_logger(str(request_user), ' SYSTEM_CREATOR_END')
def system_modificator_async(request_post, request_user): """function to modify many systems at once""" # call logger debug_logger(str(request_user), ' SYSTEM_MODIFICATOR_START') # extract lines from systemlist (list results either from request object via multiline selector or via large text area) lines = request_post.getlist('systemlist') system_char_field_used = False # if large text area was used, the list contains only one entry with (one or more) line breaks if len(lines) == 1 and ("\r\n" in lines[0] or not lines[0].isdigit()): system_char_field_used = True lines = lines[0].splitlines() """ prepare and start loop """ # count lines (needed for messages) number_of_lines = len(lines) # set systems_modified_counter (needed for messages) systems_modified_counter = 0 # set systems_skipped_counter (needed for messages) systems_skipped_counter = 0 # set lines_faulty_counter (needed for messages) lines_faulty_counter = 0 # create empty list (needed for messages) skipped_systems = [] # workflows to apply workflows = request_post.getlist("workflow") # set workflows_applied (needed for messages) if workflows: workflow_count = len(workflows) else: workflow_count = 0 workflows_applied = 0 # iterate over lines for line in lines: # skip empty lines if line == '': # autoincrement counter lines_faulty_counter += 1 # call logger warning_logger(str(request_user), ' SYSTEM_MODIFICATOR_ROW_EMPTY') continue # check line for string if not isinstance(line, str): # coverage: ignore branch # autoincrement counter lines_faulty_counter += 1 # call logger warning_logger(str(request_user), ' SYSTEM_MODIFICATOR_NO_STRING') continue # check line for length of string if len(line) > 50: # autoincrement counter lines_faulty_counter += 1 # call logger warning_logger(str(request_user), ' SYSTEM_MODIFICATOR_LONG_STRING') continue # check for existence of system if system_char_field_used: system = System.objects.filter(system_name=line) else: system = System.objects.filter(system_id=line) """ handling non-existing (== 0) or non-unique (> 1) systems """ # system does not exist if system.count() == 0: # autoincrement counter systems_skipped_counter += 1 # add system name to list of skipped systems skipped_systems.append(line) # call logger warning_logger( str(request_user), f' SYSTEM_MODIFICATOR_SYSTEM_DOES_NOT_EXISTS system_id/system_name:{line}', ) # leave this loop because system with this systemname does not exist continue # more than one system exists elif system.count() > 1: # autoincrement counter systems_skipped_counter += 1 # add system name to list of skipped systems skipped_systems.append(line) # call logger warning_logger( str(request_user), f' SYSTEM_MODIFICATOR_SYSTEM_NOT_DISTINCT system_id/system_name:{line}', ) # leave this loop because system with this systemname is not distinct continue """ unique system (== 1) """ # get existing system if system_char_field_used: system = System.objects.get(system_name=line) else: system = System.objects.get(system_id=line) # create form with request data form = SystemModificatorForm( request_post, instance=system, use_system_charfield=system_char_field_used) # extract tags (list results from request object via multiple choice field) tags = request_post.getlist('tag') # extract companies (list results from request object via multiple choice field) companies = request_post.getlist('company') # TODO: [code] add condition for invalid form, not implemented yet because of multiple constraints # modify system if form.is_valid(): """object modification""" # don't save form yet system = form.save(commit=False) # set auto values system.system_modified_by_user_id = request_user """ status fields """ # replace, if 'change status' was selected (checking for status value independent of form field validation) if (form['analysisstatus_choice'].value() == 'change_status' and form['analysisstatus'].value()): # replace status analysisstatus_id = form['analysisstatus'].value() analysisstatus = Analysisstatus.objects.get( analysisstatus_id=analysisstatus_id) system.analysisstatus = analysisstatus # replace, if 'change status' was selected (checking for status value independent of form field validation) if (form['systemstatus_choice'].value() == 'change_status' and form['systemstatus'].value()): # replace status systemstatus_id = form['systemstatus'].value() systemstatus = Systemstatus.objects.get( systemstatus_id=systemstatus_id) system.systemstatus = systemstatus """ fk non-model fields """ # replace / delete, if 'switch_new / Switch to selected item or none' was selected if form['contact_delete'].value() == 'switch_new': # replace, if value was submitted via form if form['contact'].value(): contact_id = form['contact'].value() contact = Contact.objects.get(contact_id=contact_id) system.contact = contact # delete, if form field was empty else: system.contact = None # replace / delete, if 'switch_new / Switch to selected item or none' was selected if form['location_delete'].value() == 'switch_new': # replace, if value was submitted via form if form['location'].value(): location_id = form['location'].value() location = Location.objects.get(location_id=location_id) system.location = location # delete, if form field was empty else: system.location = None # replace / delete, if 'switch_new / Switch to selected item or none' was selected if form['serviceprovider_delete'].value() == 'switch_new': # replace, if value was submitted via form if form['serviceprovider'].value(): serviceprovider_id = form['serviceprovider'].value() serviceprovider = Serviceprovider.objects.get( serviceprovider_id=serviceprovider_id) system.serviceprovider = serviceprovider # delete, if form field was empty else: system.serviceprovider = None # save object system.save() """ object counter / log """ # autoincrement counter systems_modified_counter += 1 # call logger system.logger(str(request_user), ' SYSTEM_MODIFICATOR_EXECUTED') """ many 2 many """ # remove existing relations if 'remove_and_add / Delete existing and add new items' was selected if form['tag_delete'].value() == 'remove_and_add': # remove all m2m system.tag.clear() # add new relations if not 'keep_not_add / Do not change and keep existing' was selected if form['tag_delete'].value() != 'keep_not_add': # add new tags (using save_m2m would replace existing tags it there were any) for tag_id in tags: # get object tag = Tag.objects.get(tag_id=tag_id) # add tag to system system.tag.add(tag) # remove existing relations if 'remove_and_add / Delete existing and add new items' was selected if form['company_delete'].value() == 'remove_and_add': # remove all m2m system.company.clear() # add new relations if not 'keep_not_add / Do not change and keep existing' was selected if form['company_delete'].value() != 'keep_not_add': # add new companies (using save_m2m would replace existing companies it there were any) for company_id in companies: # get object company = Company.objects.get(company_id=company_id) # add company to system system.company.add(company) # apply workflows if workflows: error_code = Workflow.apply(workflows, system, request_user) if error_code: system.logger(str(request_user), ' COULD_NOT_APPLY_WORKFLOW') else: workflows_applied += workflow_count """ finish system modificator """ # call final messages final_messages( systems_modified_counter, systems_skipped_counter, lines_faulty_counter, skipped_systems, number_of_lines, request_user, workflow_count, workflows_applied, ) # call logger info_logger( str(request_user), f' SYSTEM_MODIFICATOR_STATUS' f' modified:{systems_modified_counter}' f'|skipped:{systems_skipped_counter}' f'|faulty_lines:{lines_faulty_counter}', ) # call logger debug_logger(str(request_user), ' SYSTEM_MODIFICATOR_END')