Skip to content

Commit d3dbb17

Browse files
committed
Run License Detection On Text Submission with tempfile #450
* license_scanview function uses tempfile to run license detection on the provided input license text Signed-off-by: Akhil Raj <[email protected]>
1 parent 747222d commit d3dbb17

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

scantext/apps.py

-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@
2424

2525

2626
class ScantextConfig(AppConfig):
27-
default_auto_field = "django.db.models.BigAutoField"
2827
name = "scantext"

scantext/views.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
import tempfile
2626

2727
from django.conf import settings
28+
from django.http import HttpResponseRedirect
2829
from django.shortcuts import render
2930
from django.views import generic
3031

31-
from scantext.forms import EditorForm
32+
from scantext.forms import LicenseForm
3233

3334
SCANCODE_BASE_URL = "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses"
3435
SCANCODE_LICENSE_TEXT_URL = SCANCODE_BASE_URL + "/{}.LICENSE"
@@ -39,36 +40,34 @@
3940

4041

4142
def license_scanview(request):
42-
form = EditorForm()
43+
form = LicenseForm()
4344
if request.method == "POST":
44-
form = EditorForm(request.POST)
45+
form = LicenseForm(request.POST)
4546
if form.is_valid():
4647
text = form.cleaned_data["input_text"]
47-
# license_location = tempfile.NamedTemporaryFile(mode="w", prefix="license_scan_", dir=settings.SCANCODEIO_WORKSPACE_LOCATION)
48-
# with license_location as f:
49-
# f.write(text)
50-
# f.flush()
51-
# x=get_licenses(location=f)
52-
# f.close()
53-
# the get_licenses in the above code (line 56) returns this error
54-
# error:
55-
# expected str, bytes or os.PathLike object, not _TemporaryFileWrapper
56-
# the below code just works
57-
58-
expressions = get_licenses(
59-
location="scantext/tests/data/LICENSES",
60-
include_text=True,
61-
license_text_diagnostics=True,
62-
)
48+
with tempfile.NamedTemporaryFile(
49+
mode="w",
50+
prefix="license_scan_",
51+
dir=settings.SCANCODEIO_WORKSPACE_LOCATION,
52+
) as temp_file:
53+
temp_file.write(text)
54+
temp_file.flush()
55+
expressions = get_licenses(
56+
location=temp_file.name,
57+
include_text=True,
58+
license_text_diagnostics=True,
59+
)
60+
temp_file.close()
61+
6362
return render(
6463
request,
6564
"scantext/license_detail.html",
6665
{
6766
"text": text,
68-
"expr": expressions,
67+
"result": expressions,
6968
},
7069
)
71-
return render(request, "scantext/license_scan.html", {"form": form})
70+
return render(request, "scantext/license_scan_form.html", {"form": form})
7271

7372

7473
def get_licenses(

0 commit comments

Comments
 (0)