Skip to content

Commit

Permalink
Refine the help and form for the Package import #84
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed May 15, 2024
1 parent 24899b3 commit 0c18183
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Release notes
- Add a new AboutCode tab in Package details view.
https://github.com/nexB/dejacode/issues/42

- Enhance Package Import to support modifications.
https://github.com/nexB/dejacode/issues/84

### Version 5.0.1

- Improve the stability of the "Check for new Package versions" feature.
Expand Down
10 changes: 10 additions & 0 deletions component_catalog/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from component_catalog.models import Package
from component_catalog.models import Subcomponent
from component_catalog.programming_languages import PROGRAMMING_LANGUAGES
from dje.fields import SmartFileField
from dje.forms import JSONListField
from dje.importers import BaseImporter
from dje.importers import BaseImportModelForm
Expand Down Expand Up @@ -305,8 +306,17 @@ def save(self, commit=True):
return package


class PackageImportableUploadFileForm(forms.Form):
file = SmartFileField(extensions=["csv", "json"])

@property
def header(self):
return "Select a <strong>CSV (.csv) or JSON (.json)</strong> file"


class PackageImporter(BaseImporter):
model_form = PackageImportForm
upload_form_class = PackageImportableUploadFileForm
add_to_product_perm = "product_portfolio.add_productpackage"
relation_model = ProductPackage
update_existing = True
Expand Down
28 changes: 7 additions & 21 deletions dje/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ def clean(self):
raise forms.ValidationError("One of the row is a duplicate.")


class ImportableUploadFileForm(forms.Form):
file = SmartFileField(extensions=["csv"])


class BaseImporter:
"""
Import in 3 steps:
Expand All @@ -309,6 +313,7 @@ class BaseImporter:

model_form = None
formset_class = BaseImportModelFormSet
upload_form_class = ImportableUploadFileForm
add_to_product = False
update_existing = False

Expand Down Expand Up @@ -590,7 +595,7 @@ def save_all(self):
def save_form(self, form):
instance = form.instance

if not instance.pk: # Save only addition for now
if not instance.pk:
saved_instance = form.save()
self.results["added"].append(saved_instance)
History.log_addition(self.user, saved_instance)
Expand Down Expand Up @@ -643,30 +648,11 @@ def get_add_to_product_form(self, request):
return form


class ImportableUploadFileForm(forms.Form):
file = SmartFileField(extensions=["csv"])


class PackageImportableUploadFileForm(forms.Form):
file = SmartFileField(extensions=["csv", "json"])
update_existing_packages = forms.BooleanField(
label="Update existing packages with import data",
required=False,
initial=True,
)

@property
def header(self):
return "Select a <strong>CSV (.csv) or JSON (.json)</strong> file"


@login_required()
def import_view(request, importer_class):
user = request.user
importer = importer_class(user)
upload_form_class = ImportableUploadFileForm
if importer_class.__name__ == "PackageImporter":
upload_form_class = PackageImportableUploadFileForm
upload_form_class = importer.upload_form_class

opts = importer.model_form._meta.model._meta
perm_codename = get_permission_codename("add", opts)
Expand Down
14 changes: 9 additions & 5 deletions dje/templates/admin/object_import.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ <h5>Help for {{ importer.verbose_name|title }} Import:</h5>
<li>The character encoding of the input file needs to be "UTF-8"</li>
<li>The column order does not matter.</li>
<li>The column names are case sensitive.</li>
<li>Required column{{ importer.required_fields|pluralize }}: <strong>{{ importer.required_fields|join:", " }}</strong></li>
<li>Supported values for boolean fields:<br>
<strong>Yes:</strong> True, T, Yes, Y<br>
<strong>No:</strong> False, F, No, N
{% if importer.required_fields %}
<li>Required column{{ importer.required_fields|pluralize }}: <strong>{{ importer.required_fields|join:", " }}</strong></li>
{% endif %}
<li>Supported values for boolean fields:
<strong>Yes:</strong> True, T, Yes, Y; <strong>No:</strong> False, F, No, N
</li>
{% if importer.verbose_name == "package" %}
<li>Note that a <strong>Package is uniquely defined in DejaCode by a combination of filename, download_url, and the six Package URL fields</strong> type, namespace, name, version, qualifiers, and subpath. You are not required to provide values in all of these fields (qualifiers and subpath are less commonly used) but if any of them are different from an existing similar package already in DejaCode, then the importer will perform an addition rather than a modification.</li>
{% endif %}
{% endif %}
</ul>
<div class="text-center">
Expand Down Expand Up @@ -273,7 +277,7 @@ <h3>Step 1: Select your file</h3>
</div>
{% endif %}
<form enctype="multipart/form-data" method="post">{% csrf_token %}
<p><input type="file" id="id_file" name="file"></p>
<p>{{ file_form.file }}</p>
<input class="btn btn-primary" type="submit" value="Upload" />
</form>
</div>
Expand Down

0 comments on commit 0c18183

Please sign in to comment.