Exemple #1
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(
            data=request.data, context={"request": request}
        )
        if serializer.is_valid():
            dump = Dump.objects.create(
                index=str(uuid.uuid1()),
                author=request.user,
                upload=request.FILES["upload"],
                name=serializer.validated_data["name"],
                operating_system=serializer.validated_data["operating_system"],
            )

            os.mkdir("{}/{}".format(settings.MEDIA_ROOT, dump.index))
            Result.objects.bulk_create(
                [
                    Result(
                        plugin=up.plugin,
                        dump=dump,
                        result=5 if not up.automatic else 0,
                    )
                    for up in UserPlugin.objects.filter(
                        plugin__operating_system__in=[dump.operating_system, "Other"],
                        user=request.user,
                        plugin__disabled=False,
                    )
                ]
            )
            transaction.on_commit(lambda: index_f_and_f(dump.pk, request.user.pk))
            return Response(
                status=status.HTTP_200_OK,
                data=ShortDumpSerializer(dump, context={"request": request}).data,
            )
        return Response(status=status.HTTP_400_BAD_REQUEST, data=serializer.errors)
Exemple #2
0
def create(request):
    """
    Manage new index creation
    """
    data = dict()

    if request.method == "POST":
        form = DumpForm(data=request.POST)
        if form.is_valid():
            with transaction.atomic():
                dump = form.save(commit=False)
                dump.author = request.user
                dump.upload = form.cleaned_data["upload"]
                dump.index = str(uuid.uuid1())
                dump.save()
                form.delete_temporary_files()
                os.mkdir("{}/{}".format(settings.MEDIA_ROOT, dump.index))
                data["form_is_valid"] = True

                # for each plugin enabled and for that os I create a result
                # if the user selected that for automation, run it immediately on dask
                Result.objects.bulk_create(
                    [
                        Result(
                            plugin=up.plugin,
                            dump=dump,
                            result=5 if not up.automatic else 0,
                        )
                        for up in UserPlugin.objects.filter(
                            plugin__operating_system__in=[
                                dump.operating_system,
                                "Other",
                            ],
                            user=request.user,
                            plugin__disabled=False,
                        )
                    ]
                )

                transaction.on_commit(lambda: index_f_and_f(dump.pk, request.user.pk))

            # Return the new list of available indexes
            data["form_is_valid"] = True
            data["dumps"] = render_to_string(
                "website/partial_indices.html",
                {
                    "dumps": [
                        x
                        for x in get_objects_for_user(request.user, "website.can_see")
                        .values_list(
                            "index", "color", "name", "operating_system", "author"
                        )
                        .order_by("-created_at")
                    ]
                },
                request=request,
            )
        else:
            data["form_is_valid"] = False
    else:
        form = DumpForm()

    context = {"form": form}
    data["html_form"] = render_to_string(
        "website/partial_create.html",
        context,
        request=request,
    )
    return JsonResponse(data)
Exemple #3
0
    def import_local(self, request):
        local_path = Path(request.data["filepath"])
        media_path = "{}/{}".format(settings.MEDIA_ROOT, "uploads")

        uploaded_name = "{}/{}".format(media_path, local_path.name)

        if not local_path.exists():
            return Response(
                {"Error": "Filepath does not exists!"},
                status=status.HTTP_400_BAD_REQUEST,
            )

        if not Path(settings.MEDIA_ROOT) in Path(local_path).parents:
            return Response(
                {"Error": "Filepath must be under MEDIA PATH!"},
                status=status.HTTP_400_BAD_REQUEST,
            )

        # IF ALREADY UNDER RIGHT FOLDER OK, ELSE MOVE IT
        if local_path.parent.absolute() == media_path:
            uploaded_name = local_path
        else:
            local_path.rename(uploaded_name)

        operating_system = request.data["operating_system"]
        operating_system = operating_system.capitalize()
        if operating_system not in ["Linux", "Windows", "Mac"]:
            return Response(
                {
                    "Error":
                    "Option selected for OS is not valid [Linux, Windows, Mac]."
                },
                status=status.HTTP_400_BAD_REQUEST,
            )

        name = request.data["name"]
        operating_system = operating_system

        with transaction.atomic():
            dump = Dump(
                author=request.user,
                index=str(uuid.uuid1()),
                name=name,
                operating_system=operating_system,
            )
            dump.upload.name = str(uploaded_name)
            dump.save()
            Result.objects.bulk_create([
                Result(
                    plugin=up.plugin,
                    dump=dump,
                    result=5 if not up.automatic else 0,
                ) for up in UserPlugin.objects.filter(
                    plugin__operating_system__in=[
                        operating_system,
                        "Other",
                    ],
                    user=request.user,
                    plugin__disabled=False,
                )
            ])
            transaction.on_commit(
                lambda: index_f_and_f(dump.pk, request.user.pk))

        return Response(
            status=status.HTTP_200_OK,
            data=ShortDumpSerializer(dump, context={
                "request": request
            }).data,
        )
Exemple #4
0
    def handle(self, *args, **options):
        local_path = Path(options["filepath"])
        media_path = Path("{}/{}".format(settings.MEDIA_ROOT, "uploads"))

        uploaded_name = "{}/{}".format(media_path, local_path.name)

        if not local_path.exists():
            self.stdout.write(self.style.ERROR("Path does not exists"))
            return

        if Path(settings.MEDIA_ROOT) not in Path(local_path).parents:
            self.stdout.write(self.style.ERROR("Path not valid"))
            return

        # IF ALREADY UNDER RIGHT FOLDER OK, ELSE MOVE IT
        if local_path.parent.absolute() == media_path:
            self.stdout.write("File in correct path")
            uploaded_name = local_path
        else:
            local_path.rename(uploaded_name)
            self.stdout.write("File moved to upload folder")

        operating_system = options["os"]
        operating_system = operating_system.capitalize()
        if operating_system not in ["Linux", "Windows", "Mac"]:
            self.stdout.write(
                self.style.ERROR(
                    'Os not valid: options available "Linux", "Windows", "Mac"'
                ))
            return

        name = options["name"]
        author = get_user_model().objects.get(username=options["author"])

        with transaction.atomic():
            dump = Dump(
                author=author,
                index=str(uuid.uuid1()),
                name=name,
                operating_system=operating_system,
            )
            dump.upload.name = str(uploaded_name)
            dump.save()
            Result.objects.bulk_create([
                Result(
                    plugin=up.plugin,
                    dump=dump,
                    result=5 if not up.automatic else 0,
                ) for up in UserPlugin.objects.filter(
                    plugin__operating_system__in=[
                        operating_system,
                        "Other",
                    ],
                    user=author,
                    plugin__disabled=False,
                )
            ])
            transaction.on_commit(lambda: index_f_and_f(dump.pk, author.pk))

        self.stdout.write(
            self.style.SUCCESS("Dump {} created, file at {}!".format(
                dump.name, dump.upload.path)))