Skip to content

Commit

Permalink
Add minimal license list and text views #32
Browse files Browse the repository at this point in the history
  • Loading branch information
tdruez authored Dec 7, 2020
1 parent d361f21 commit d0ef181
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
- Add a to_json output pipe returning ScanCode compatible content
https://github.com/nexB/scancode.io/issues/45

- Add minimal license list and text views
https://github.com/nexB/scancode.io/issues/32

- Improve Admin UI for efficient review:
display, navigation, filters, and ability to view file content
https://github.com/nexB/scancode.io/issues/36
Expand Down
73 changes: 73 additions & 0 deletions scancodeio/licenses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# SPDX-License-Identifier: Apache-2.0
#
# http://nexb.com and https://github.com/nexB/scancode.io
# The ScanCode.io software is licensed under the Apache License version 2.0.
# Data generated with ScanCode.io is provided as-is without warranties.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
#
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.

from functools import lru_cache

from django.http import HttpResponse
from django.urls import path
from django.urls import reverse

import saneyaml
from licensedcode.models import load_licenses


@lru_cache(maxsize=None)
def get_licenses():
"""
Load the licenses from the ScanCode-toolkit `licensedcode` data and
return a mapping of `key` to `license` objects.
The result is cached in memory so the load_licenses() process is only
executed once on the first `get_licenses()` call.
"""
return load_licenses()


def license_list_view(request):
"""
Display a list of all the licenses linked to their details.
"""
licenses = get_licenses()
license_links = [
f'<a href="{reverse("license_details", args=[key])}">{key}</a>'
for key in licenses.keys()
]
return HttpResponse("<br>".join(license_links))


def license_details_view(request, key):
"""
Display all the information available about the provided license `key`
followed by the full license text.
"""
licenses = get_licenses()
try:
data = saneyaml.dump(licenses[key].to_dict())
text = licenses[key].text
except KeyError:
return HttpResponse(f"License {key} not found.")
return HttpResponse(f"<pre>{data}</pre><hr><pre>{text}</pre>")


urls = [
path("", license_list_view, name="license_list"),
path("<path:key>/", license_details_view, name="license_details"),
]
2 changes: 2 additions & 0 deletions scancodeio/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from rest_framework.routers import DefaultRouter

from scancodeio import licenses
from scanner.api.views import ScanViewSet
from scanpipe.api.views import ProjectViewSet
from scanpipe.api.views import RunViewSet
Expand All @@ -39,5 +40,6 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include(api_router.urls)),
path("license/", include(licenses.urls)),
path("", RedirectView.as_view(url="api/")),
]

0 comments on commit d0ef181

Please sign in to comment.