def post_data(request, add_type): """ Add Type is passed via the URI Valid Types and actions are: rule - new, update """ # If not authenticated if not request.user.is_authenticated(): error_line = "You need to be logged in to perform that action" return render(request, 'error.html', {'error': error_line}) # Get all the POST Vars action = request.POST['action'] if add_type == 'rule': rule_id = request.POST['rule_id'] if action == 'delete': Rule.objects.filter(id=rule_id).delete() return redirect('/') if action == 'new': # need to get the rule details in to post before i look at this. pass elif action == 'update': rule = Rule.objects.get(pk=rule_id) rule.rule_version += 1 rule_condition = Condition.objects.get(rule=rule) rule.save() else: error_line = "Not a valid Action Type" return render(request, 'error.html', {'error': error_line}) #meta data meta_ids = request.POST.getlist('meta_id') meta_values = request.POST.getlist('metaValues') meta_keys = request.POST.getlist('metaKeys') meta_save = [] for i in range(len(meta_values)): if meta_ids[i] == 'new': meta_data = MetaData() meta_data.rule = rule else: meta_data = MetaData.objects.get(pk=meta_ids[i]) meta_data.meta_key = meta_keys[i] meta_data.meta_value = meta_values[i] meta_data.save() meta_save.append(meta_data.id) # Delete Rows meta_db = rule.metadata_set.all() for obj in meta_db: if obj.id not in meta_save: print "dropping Meta with ID", obj.id MetaData.objects.filter(id=obj.id).delete() # Strings string_ids = request.POST.getlist('string_id') string_names = request.POST.getlist('stringName') string_values = request.POST.getlist('stringValues') string_nocases = request.POST.getlist('caseValues') string_wides = request.POST.getlist('wideValues') string_fulls = request.POST.getlist('fullValues') string_asciis = request.POST.getlist('asciiValues') # Collect the string vars string_save = [] for i in range(len(string_names)): if string_ids[i] == 'new': rule_strings = RuleStrings() rule_strings.rule = rule else: rule_strings = RuleStrings.objects.get(pk=string_ids[i]) rule_strings.string_name = string_names[i] rule_strings.string_value = string_values[i] rule_strings.string_nocase = True if string_nocases[i] == '1' else False rule_strings.string_wide = True if string_wides[i] == '1' else False rule_strings.string_full = True if string_fulls[i] == '1' else False rule_strings.string_ascii = True if string_asciis[i] == '1' else False rule_strings.save() string_save.append(rule_strings.id) # Delete Rows string_db = rule.rulestrings_set.all() for obj in string_db: if obj.id not in string_save: print "dropping String with ID", obj.id RuleStrings.objects.filter(id=obj.id).delete() return redirect('/rule/{0}'.format(rule_id)) # Add Rules if add_type == 'addfile': rule_file = request.FILES rule_source = request.POST['Source'] rule_category = request.POST['Category'] if rule_file and action == 'new': rule_file = rule_file['rule_file'] rule_data = rule_file.read() ruleparser.split_rules({'rule_data':rule_data, 'rule_source':rule_source, 'rule_category':rule_category}) return redirect('/')