def export_dhcp(request): ''' This function is responsible for acquiring, formatting and exporting the appropriete and permitted dns records. ''' if request.user.is_staff: if request.method == 'POST': subnet = request.POST.get('subnets') if subnet == 'all': data = get_and_format_subnet_data(request.user, subnet) response = HttpResponse(data, mimetype='application/text') response['Content-Disposition'] = 'attachment; filename=dhcp_%s.txt' %subnet else: try: subnet = IPNetwork(subnet) data = get_and_format_subnet_data(request.user, subnet) response = HttpResponse(data, mimetype='application/text') response['Content-Disposition'] = 'attachment; filename=dhcp_%s.txt' %subnet except: subnets = get_address_blocks_managed_by(request.user) response = render_to_response('qmul_export.html',{'subnets':subnets,'type': 'DHCP'},context_instance=RequestContext(request)) else: subnets = get_address_blocks_managed_by(request.user) response = render_to_response('qmul_export.html',{'subnets':subnets,'type': 'DHCP'},context_instance=RequestContext(request)) else: response = render_to_response('qmul_import.html', {'PermissionError': True}, context_instance=RequestContext(request)) return response
def get_and_format_subnet_data( user_obj, subnet): ''' This function is responsible for getting the permitted dhcp records and formating it in the export format. See DHCP.models.ExportRepresentation() for information about how the records are formatted ''' formatted_data = '' if subnet == 'all': subnets = get_address_blocks_managed_by(user_obj) formatted_data = '# Subnets belonging to: %s #\n' %subnets records_machines = DHCP_machine.objects.get_permitted_records(user_obj, 'ip', 'asc', '') records_pools = DHCP_ip_pool.objects.get_permitted_records(user_obj, 'ip', 'asc', '') else: formatted_data = '#################### Subnet:%s ####################\n' % (str(subnet)) [records_machines, error] = DHCP_machine.objects.get_records_in_subnet(subnet) [records_pools, error] = DHCP_ip_pool.objects.get_records_in_subnet(subnet) formatted_data = formatted_data + '#################### DHCP Machines ####################\n' if records_machines: for each_record in records_machines: formatted_data = formatted_data + each_record.ExportRepresentation() else: formatted_data = formatted_data + '#None\n' formatted_data = formatted_data + '#################### DHCP Pools ####################\n' if records_pools: for each_record in records_pools: formatted_data = formatted_data + each_record.ExportRepresentation() else: formatted_data = formatted_data + '#None\n' return formatted_data
def is_permitted_custom(user, value_list): errormessage = list() record_list = list() ip_blocks = get_address_blocks_managed_by(user) dns_expressions = get_dns_patterns_managed_by(user) for value in value_list: has_permission = False if value["dns_type"] == "2NA": for item in dns_expressions: # for each dns expression in all dns expressions in the list... if re.match(re.compile(item.expression), value["name"]): # ... and check if matches with input dns_name has_permission = True record_list.append(value) break if not has_permission: var = ( "line %s, You are not allowed to add this DNS Name: %s, it is not part of your network resource group.\n" % (value["line_no"], value["name"]) ) errormessage.append(var) elif value["dns_type"] == "3AN": for block in ip_blocks: # for each ip address block in all ip address blocks in the list... if value["ip_address"] in block.ip_network: # ...check if ip_address is within range has_permission = True record_list.append(value) break if not has_permission: var = ( "line %s, You are not allowed to add this IP Address: %s, it is not part of your network resource group.\n" % (value["line_no"], value["ip_address"]) ) errormessage.append(var) elif value["dns_type"] == "1BD": for block in ip_blocks: # for each ip address block in all ip address blocks in the list... if value["ip_address"] in block.ip_network: # ...check if ip_address is within range has_permission = True break if has_permission: has_permission = False for item in dns_expressions: # for each dns expression in all dns expressions in the list... if re.match( re.compile(item.expression), value["name"] ): # ... and check if matches with input dns_name has_permission = True record_list.append(value) break if not has_permission: var = ( "line %s, You are not allowed to add this DNS Name: %s, it is not part of your network resource group.\n" % (value["line_no"], value["name"]) ) errormessage.append(var) else: var = ( "line %s, You are not allowed to add this IP Address: %s, it is not part of your network resource group.\n" % (value["line_no"], value["ip_address"]) ) errormessage.append(var) return has_permission, errormessage
def import_dhcp(request): ''' Import dhcp functional page and form. Passes file (specified by the user) to handle_uploaded_file function and deals approprietly with the response. ''' if request.user.is_staff: if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) subnet = request.POST.get('subnets') if form.is_valid(): if subnet == 'all': subnet = get_address_blocks_managed_by(request.user) log, error = handle_uploaded_file(request, request.FILES['file'], subnet) pageContext = {'type': 'DHCP', 'error': error['is_error'], 'errormessage':error['Msg'] , 'errortype':error['Type'], 'log': log} response = render_to_response('qmul_import.html',{'form': form, 'subnets':subnet, 'type': 'DHCP'}) else: #try: subnet = [IPNetwork(subnet)] log, error = handle_uploaded_file(request, request.FILES['file'], subnet) pageContext = {'type': 'DHCP', 'error': error['is_error'], 'errormessage':error['Msg'] , 'errortype':error['Type'], 'log': log} response = render_to_response('qmul_import_result.html', pageContext) #except Exception, e: # subnets = get_address_blocks_managed_by(request.user) # print e # response = render_to_response('qmul_import.html',{'form': form, 'subnets':subnets, 'type': 'DHCP'}) else: subnets = get_address_blocks_managed_by(request.user) response = render_to_response('qmul_import.html',{'form': form, 'subnets':subnets, 'type': 'DHCP'}) else: form = UploadFileForm() subnets = get_address_blocks_managed_by(request.user) response = render_to_response('qmul_import.html',{'form': form, 'subnets':subnets, 'type': 'DHCP'}) else: response = render_to_response('qmul_import.html', {'PermissionError': True}) return response
def get_and_format_subnet_data( user_obj, subnet): ''' This function is responsible for getting the permitted dns records and formating it in the export format. See DNS.models.ExportRepresentation() for information about how the records are formatted ''' formatted_data = '' if subnet == 'all': subnets = get_address_blocks_managed_by(user_obj) formatted_data = '# Subnets belonging to: %s #\n' %subnets records = DNS_name.objects.get_permitted_records(user_obj, True, 'ip', 'asc', '') else: formatted_data = '#################### Subnet:%s ####################\n' % (str(subnet)) records = DNS_name.objects.get_records_in_subnet(user_obj, subnet) if records: for each_record in records: formatted_data = formatted_data + each_record.ExportRepresentation() else: formatted_data = formatted_data + '#None' return formatted_data