Ejemplo n.º 1
0
    def post(self, request, **kwargs):
        logger = logging.getLogger("peering.manager.views.ObjectDeleteView")
        o = self.get_object(kwargs)
        form = ConfirmationForm(request.POST)

        if form.is_valid():
            logger.debug("form validation was successful")
            o.delete()

            msg = f"Deleted {self.queryset.model._meta.verbose_name} {o}"
            logger.info(msg)
            messages.success(request, msg)

            return_url = form.cleaned_data.get("return_url")
            if return_url is not None and is_safe_url(
                    url=return_url, allowed_hosts=request.get_host()):
                return redirect(return_url)
            else:
                return redirect(self.get_return_url(request, instance=o))
        else:
            logger.debug("form validation failed")

        return render(
            request,
            self.template_name,
            {
                "object": o,
                "object_type": self.queryset.model._meta.verbose_name,
                "form": form,
                "return_url": self.get_return_url(request, instance=o),
            },
        )
Ejemplo n.º 2
0
    def get(self, request, slug, network_id, network_ixlan_id):
        # Get required objects or fail if some are missing
        internet_exchange = get_object_or_404(InternetExchange, slug=slug)
        network = get_object_or_404(Network, id=network_id)
        network_ixlan = get_object_or_404(NetworkIXLAN, id=network_ixlan_id)

        # Check if the AS we are going to peer with is already known
        known_autonomous_system = True
        try:
            AutonomousSystem.objects.get(asn=network.asn)
        except AutonomousSystem.DoesNotExist:
            known_autonomous_system = False

        # Init a form that the user must submit to confirm the peering
        form = ConfirmationForm(initial=request.GET)

        context = {
            'internet_exchange': internet_exchange,
            'known_autonomous_system': known_autonomous_system,
            'network': network,
            'network_ixlan': network_ixlan,
            'form': form,
            'return_url': internet_exchange.get_peer_list_url(),
        }
        return render(request, 'peering/ix/add_peer.html', context)
Ejemplo n.º 3
0
    def post(self, request, pk):
        token = get_object_or_404(Token.objects.filter(user=request.user), pk=pk)
        form = ConfirmationForm(request.POST)
        if form.is_valid():
            token.delete()
            messages.success(request, "Token deleted")
            return redirect("users:token_list")

        return render(
            request,
            "generic/object_delete.html",
            {
                "object": token,
                "object_type": token._meta.verbose_name,
                "form": form,
                "return_url": reverse("users:token_list"),
            },
        )
Ejemplo n.º 4
0
    def post(self, request):
        local_as, missing_ixps = self.get_missing_ixps(request)
        form = ConfirmationForm(request.POST)

        if form.is_valid():
            ixp_number, connection_number = self.import_ixps(local_as, missing_ixps)

            if ixp_number == 0 and connection_number == 0:
                messages.warning(request, "No IXPs imported.")
            else:
                message = ["Imported"]
                if ixp_number > 0:
                    message.append(f"{ixp_number} IXP{pluralize(ixp_number)}")
                if connection_number > 0:
                    message.append(
                        f"{connection_number} connection{pluralize(connection_number)}"
                    )
                messages.success(request, f"{' '.join(message)}.")

        return redirect(self.get_return_url(request))
Ejemplo n.º 5
0
    def get(self, request, **kwargs):
        o = self.get_object(kwargs)
        form = ConfirmationForm(initial=request.GET)

        return render(
            request,
            self.template_name,
            {
                "object": o,
                "object_type": self.queryset.model._meta.verbose_name,
                "form": form,
                "return_url": self.get_return_url(request, instance=o),
            },
        )
Ejemplo n.º 6
0
    def get(self, request, pk):
        token = get_object_or_404(Token.objects.filter(user=request.user), pk=pk)
        initial_data = {"return_url": reverse("users:token_list")}
        form = ConfirmationForm(initial=initial_data)

        return render(
            request,
            "generic/object_delete.html",
            {
                "object": token,
                "object_type": token._meta.verbose_name,
                "form": form,
                "return_url": reverse("users:token_list"),
            },
        )
Ejemplo n.º 7
0
    def get(self, request):
        _, missing_ixps = self.get_missing_ixps(request)

        if not missing_ixps:
            messages.warning(request, "No IXPs nor connections to import.")
            return redirect(self.get_return_url(request))

        return render(
            request,
            "peering/internetexchange/import.html",
            {
                "form": ConfirmationForm(initial=request.GET),
                "missing_ixps": missing_ixps,
                "return_url": self.get_return_url(request),
            },
        )
Ejemplo n.º 8
0
    def post(self, request, slug, network_id, network_ixlan_id):
        # Get required objects or fail if some are missing
        internet_exchange = get_object_or_404(InternetExchange, slug=slug)
        network = get_object_or_404(Network, id=network_id)
        network_ixlan = get_object_or_404(NetworkIXLAN, id=network_ixlan_id)

        # Init the form that the user must have submitted
        form = ConfirmationForm(request.POST)
        if form.is_valid():
            peer_added = False

            with transaction.atomic():
                # Create the new AS if needed
                try:
                    autonomous_system = AutonomousSystem.objects.get(
                        asn=network.asn)
                except AutonomousSystem.DoesNotExist:
                    values = {
                        'asn': network.asn,
                        'name': network.name,
                        'irr_as_set': network.irr_as_set,
                        'ipv6_max_prefixes': network.info_prefixes6,
                        'ipv4_max_prefixes': network.info_prefixes4,
                    }
                    autonomous_system = AutonomousSystem(**values)
                    autonomous_system.save()
                    peer_added = True
                    # Log the action
                    UserAction.objects.log_create(
                        request.user, autonomous_system,
                        'Created {} {}'.format(
                            AutonomousSystem._meta.verbose_name,
                            escape(autonomous_system)))

                # Record the IPv6 session if we can
                if network_ixlan.ipaddr6:
                    try:
                        PeeringSession.objects.get(
                            autonomous_system=autonomous_system,
                            internet_exchange=internet_exchange,
                            ip_address=network_ixlan.ipaddr6)
                    except PeeringSession.DoesNotExist:
                        values = {
                            'autonomous_system': autonomous_system,
                            'internet_exchange': internet_exchange,
                            'ip_address': network_ixlan.ipaddr6,
                        }
                        session = PeeringSession(**values)
                        session.save()
                        peer_added = True
                        # Log the action
                        UserAction.objects.log_create(
                            request.user, session, 'Created {} {}'.format(
                                PeeringSession._meta.verbose_name,
                                escape(session)))

                # Record the IPv4 session if we can
                if network_ixlan.ipaddr4:
                    try:
                        PeeringSession.objects.get(
                            autonomous_system=autonomous_system,
                            internet_exchange=internet_exchange,
                            ip_address=network_ixlan.ipaddr4)
                    except PeeringSession.DoesNotExist:
                        values = {
                            'autonomous_system': autonomous_system,
                            'internet_exchange': internet_exchange,
                            'ip_address': network_ixlan.ipaddr4,
                        }
                        session = PeeringSession(**values)
                        session.save()
                        peer_added = True
                        # Log the action
                        UserAction.objects.log_create(
                            request.user, session, 'Created {} {}'.format(
                                PeeringSession._meta.verbose_name,
                                escape(session)))

                # Notify the user
                if peer_added:
                    messages.success(
                        request, '{} peer successfully added on {}.'.format(
                            autonomous_system, internet_exchange))

            return redirect(internet_exchange.get_absolute_url())

        context = {
            'internet_exchange': internet_exchange,
            'known_autonomous_system': False,
            'network': network,
            'network_ixlan': network_ixlan,
            'form': form,
            'return_url': internet_exchange.get_peer_list_url(),
        }
        return render(request, 'peering/ix/add_peer.html', context)