Пример #1
0
def write_configuration(request, firmware_version):
	if path.exists(c.CONFIG_FILE + '.bak'):
		remove(c.CONFIG_FILE + '.bak')
	if path.exists(c.CONFIG_FILE):
		rename(c.CONFIG_FILE, c.CONFIG_FILE + '.bak')
		m.add_message(_('A copy of your configuration file was saved as: ') + c.CONFIG_FILE + '.bak')
	f = open_file(c.CONFIG_FILE, 'w')
	if f is None:
		m.add_message(_('Unexpected error: could not open configuration file, configuration was not saved.'))
		return
	flag_network_ok = True
	if request.forms.get('connection_type') == 'static':
		if validate_ip_address(concat_ip_cells(request, 'ip')) and validate_ip_address(concat_ip_cells(request, 'subnet')) and validate_ip_address(concat_ip_cells(request, 'gateway')):
			if not address_in_network(concat_ip_cells(request, 'gateway'), get_network_in_cidr(concat_ip_cells(request, 'ip'), concat_ip_cells(request, 'subnet'))):
				m.add_message(_('Invalid combination of ip, subnet and gateway, addresses not saved.'))
				flag_network_ok = False
	num = 1
	for fieldset in data.CONFIG_FIELDSETS:
		if num == 1:
			f.write('[' + fieldset[1] + ']' + c.CR)
		else:
			f.write(c.CR + '[' + fieldset[1] + ']' + c.CR)
		num = 0
		for row in fieldset[2]:
			if row[0] == 'input_row':
				if row[3] == 'text':
					f.write(row[2] + '=' + request.forms.get(row[2]) + c.CR)
				elif row[3] == 'checkbox':
					f.write(write_checkbox(row[2], request.forms.get(row[2]), 'disable') + c.CR)
				elif row[3] == 'radio':
					f.write(write_radio(row[2], request.forms.get(row[2])) + c.CR)
				elif row[3] == 'file':
					f.write(row[2] + '=' + firmware_version + c.CR)
			elif row[0] == 'select_row':
				f.write(write_select(row[2],request.forms.get(row[2])) + c.CR)
			elif row[0] == 'ip_row':
				if row[4] != 'connection_type':
					#for now we write them
					f.write(write_ip(request, row[2], row[1]) + c.CR)
				elif request.forms.get('connection_type') == 'static':
					if flag_network_ok:
						f.write(write_ip(request, row[2], row[1]) + c.CR)
					elif row[2] != 'ip' and row[2] != 'subnet' and row[2] != 'gateway':
						f.write(write_ip(request, row[2], row[1]) + c.CR)
	f.close()
	m.add_message('<font color="#373">' +_('Configuration saved') + ', ' + strftime("%c") + '.</font>')
Пример #2
0
def write_ip(request, field, label):
	if not is_ip_blank(request, field):
		if not validate_ip_address(concat_ip_cells(request, field)):
			m.add_message(label + _(' was not saved because it was invalid.'))
		else:
			return field + '=' + concat_ip_cells(request, field)