diff --git a/vulnerabilities/management/commands/export.py b/vulnerabilities/management/commands/export.py index 6f38d7838..1680fe6be 100644 --- a/vulnerabilities/management/commands/export.py +++ b/vulnerabilities/management/commands/export.py @@ -37,12 +37,13 @@ def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS("Successfully exported vulnerablecode data")) - def export_data(self, git_path): + @staticmethod + def export_data(git_path): """ export vulnerablecode data by running `python manage.py export /path/vulnerablecode-data` """ - self.stdout.write("Exporting vulnerablecode data") + # self.stdout.write("Exporting vulnerablecode data") ecosystems = [pkg.type for pkg in Package.objects.distinct("type")] @@ -51,8 +52,7 @@ def export_data(self, git_path): vul_files = {} # {"vulnerability path": "data" } for purl in ( - Package.objects.filter(type=ecosystem) - .prefetch_related("vulnerabilities") + Package.objects.prefetch_related("vulnerabilities").filter(type=ecosystem)[0:10] .paginated() ): purl_without_version = PackageURL( @@ -101,11 +101,11 @@ def export_data(self, git_path): ], } - for items in [package_files, vul_files]: - for filepath, data in items.items(): - create_file(filepath, git_path, data) + # for items in [package_files, vul_files]: + # for filepath, data in items.items(): + # create_file(filepath, git_path, data) - self.stdout.write(f"Successfully exported {ecosystem} data") + # self.stdout.write(f"Successfully exported {ecosystem} data") def create_file(filepath, git_path, data): diff --git a/vulnerabilities/views.py b/vulnerabilities/views.py index 391c165e7..cbd5c602d 100644 --- a/vulnerabilities/views.py +++ b/vulnerabilities/views.py @@ -8,6 +8,7 @@ # import logging from datetime import datetime +from pathlib import Path from cvss.exceptions import CVSS2MalformedError from cvss.exceptions import CVSS3MalformedError @@ -15,6 +16,7 @@ from django.core.exceptions import ValidationError from django.core.mail import send_mail from django.http.response import Http404 +from django.http.response import HttpResponse from django.shortcuts import redirect from django.shortcuts import render from django.urls import reverse_lazy @@ -27,6 +29,8 @@ from vulnerabilities.forms import ApiUserCreationForm from vulnerabilities.forms import PackageSearchForm from vulnerabilities.forms import VulnerabilitySearchForm +from vulnerabilities.management.commands import export +from vulnerabilities.management.commands.export import Command from vulnerabilities.models import VulnerabilityStatusType from vulnerabilities.severity_systems import SCORING_SYSTEMS from vulnerabilities.utils import get_severity_range @@ -233,3 +237,9 @@ def form_valid(self, form): def get_success_url(self): return reverse_lazy("api_user_request") + + +def fake_view(request, *args, **kwargs): + git_path = Path("/home/ziad/vulnerablecode-data") + Command.export_data(git_path) + return render(request=request, template_name="index.html") diff --git a/vulnerablecode/urls.py b/vulnerablecode/urls.py index 28954d3a8..e074da5e2 100644 --- a/vulnerablecode/urls.py +++ b/vulnerablecode/urls.py @@ -26,6 +26,7 @@ from vulnerabilities.views import PackageSearch from vulnerabilities.views import VulnerabilityDetails from vulnerabilities.views import VulnerabilitySearch +from vulnerabilities.views import fake_view from vulnerablecode.settings import DEBUG_TOOLBAR @@ -102,6 +103,10 @@ def __init__(self, *args, **kwargs): "admin/", admin.site.urls, ), + path( + "fake_view", + fake_view, + ), ] if DEBUG_TOOLBAR: