def edit(request, subnet_id): try: subnet = maas.get_subnets(subnet_id) if not subnet: return redirect('maas:subnets:index') form = SubnetForm(request.POST or None, initial=subnet) if form.is_valid(): m = MAAS() data = form.clean() if data['vlan']: vl = maas.get_vlans(int(data['vlan'])) data['vid'] = vl['vid'] data['fabric'] = vl['fabric_id'] resp = m.put(f'subnets/{subnet_id}/', data=data) if resp.status_code in m.ok: sweetify.sweetalert(request, 'Success', text=_('Subnet Update Successfully'), icon='success', timer=2000) return redirect('maas:subnets:index') sweetify.sweetalert(request, 'Warning', text=_(resp.text), icon='warning', timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', timer=5000) form = None context = { 'title': _('Form Edit Subnet'), 'form': form } return render(request, 'maas/subnets/add.html', context)
def detail(request, subnet_id): try: subnet = maas.get_subnets(subnet_id) rir = maas.get_subnets(subnet_id, op='reserved_ip_ranges') if request.is_ajax(): return JsonResponse({'subnet': subnet}) context = { 'title': f"Subnet - {subnet['name']}", 'subnet': subnet, 'rir': rir, 'menu_active': 'subnets', } except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', timer=5000) return render(request, 'maas/subnets/subnet_detail.html', context)
def detail(request, space_id): try: spaces = maas.get_spaces(space_id) context = { 'title': _('Detail Space'), 'space': spaces, 'subnets': maas.get_subnets() } except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', icon='error', text=str(e), button='Ok', timer=5000) return render(request, 'maas/spaces/detail.html', context)
def index(request): try: if request.is_ajax(): return JsonResponse({'subnets': maas.get_subnets()}) subnets = maas.get_subnets() fabrics = maas.get_fabrics() spaces = maas.get_spaces() group_subnet = maas.get_subnet_byfabric() context = { 'title': 'Subnet By Fabric', 'subnets': subnets, 'fabrics': fabrics, 'spaces': spaces, 'group_subnet': group_subnet, 'menus_active': 'subnets_active' } except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', timer=5000) context = None return render(request, 'maas/subnets/index.html', context)
def delete(request, subnet_id): try: subnets = maas.get_subnets() subnet = [s for s in subnets if s['id'] == subnet_id] if subnet[0] is not None: m = MAAS() resp = m.delete(f'subnets/{subnet_id}/') if resp.status_code in m.ok: sweetify.success(request, _('Subnet Deleted Successfully'), timer=2000) return redirect('maas:subnets:index') sweetify.warning(request, _(resp.text), timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', timer=5000) return redirect('maas:subnets:index')