|
20 | 20 | from django.contrib.auth.decorators import login_required |
21 | 21 | from django.contrib.auth.mixins import LoginRequiredMixin |
22 | 22 | from django.core import signing |
| 23 | +from django.core.validators import EMPTY_VALUES |
23 | 24 | from django.db.models import Prefetch |
24 | 25 | from django.http import FileResponse |
25 | 26 | from django.http import Http404 |
|
45 | 46 | from natsort import natsorted |
46 | 47 | from notifications.signals import notify |
47 | 48 | from packageurl import PackageURL |
| 49 | +from packageurl.contrib import purl2url |
48 | 50 | from packageurl.contrib import url2purl |
49 | 51 |
|
50 | 52 | from component_catalog.filters import ComponentFilterSet |
@@ -1896,26 +1898,52 @@ def get_initial(self): |
1896 | 1898 | """Pre-fill the form with initial data from a PurlDB entry or a `package_url`.""" |
1897 | 1899 | initial = super().get_initial() |
1898 | 1900 |
|
1899 | | - purldb_uuid = self.request.GET.get("purldb_uuid", None) |
1900 | | - if purldb_uuid: |
1901 | | - purldb_entry = PurlDB(self.request.user).get_package(purldb_uuid) |
| 1901 | + if purldb_entry := self.get_entry_from_purldb(): |
1902 | 1902 | purldb_entry["license_expression"] = purldb_entry.get("declared_license_expression") |
1903 | | - model_fields = [field.name for field in Package._meta.get_fields()] |
| 1903 | + model_fields = [ |
| 1904 | + field.name |
| 1905 | + for field in Package._meta.get_fields() |
| 1906 | + # Generic keywords are not supported because of validation |
| 1907 | + if field.name != "keywords" |
| 1908 | + ] |
1904 | 1909 | initial_from_purldb_entry = { |
1905 | 1910 | field_name: value |
1906 | 1911 | for field_name, value in purldb_entry.items() |
1907 | | - if value and field_name in model_fields |
| 1912 | + if value not in EMPTY_VALUES and field_name in model_fields |
1908 | 1913 | } |
1909 | 1914 | initial.update(initial_from_purldb_entry) |
| 1915 | + messages.info(self.request, "Initial data fetched from PurlDB.") |
1910 | 1916 |
|
1911 | | - package_url = self.request.GET.get("package_url", None) |
1912 | | - if package_url: |
| 1917 | + elif package_url := self.request.GET.get("package_url", None): |
1913 | 1918 | purl = PackageURL.from_string(package_url) |
1914 | 1919 | package_url_dict = purl.to_dict(encode=True, empty="") |
1915 | 1920 | initial.update(package_url_dict) |
| 1921 | + if download_url := purl2url.get_download_url(package_url): |
| 1922 | + initial.update({"download_url": download_url}) |
1916 | 1923 |
|
1917 | 1924 | return initial |
1918 | 1925 |
|
| 1926 | + def get_entry_from_purldb(self): |
| 1927 | + user = self.request.user |
| 1928 | + purldb = PurlDB(user) |
| 1929 | + is_purldb_enabled = all( |
| 1930 | + [ |
| 1931 | + purldb.is_configured(), |
| 1932 | + user.dataspace.enable_purldb_access, |
| 1933 | + ] |
| 1934 | + ) |
| 1935 | + |
| 1936 | + if not is_purldb_enabled: |
| 1937 | + return |
| 1938 | + |
| 1939 | + purldb_uuid = self.request.GET.get("purldb_uuid", None) |
| 1940 | + package_url = self.request.GET.get("package_url", None) |
| 1941 | + |
| 1942 | + if purldb_uuid: |
| 1943 | + return purldb.get_package(purldb_uuid) |
| 1944 | + elif package_url: |
| 1945 | + return purldb.get_package_by_purl(package_url) |
| 1946 | + |
1919 | 1947 | def get_success_message(self, cleaned_data): |
1920 | 1948 | success_message = super().get_success_message(cleaned_data) |
1921 | 1949 |
|
|
0 commit comments