diff --git a/api/templatetags/__init__.py b/api/templatetags/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api/templatetags/custom_api_tags.py b/api/templatetags/custom_api_tags.py new file mode 100644 index 000000000..c60279863 --- /dev/null +++ b/api/templatetags/custom_api_tags.py @@ -0,0 +1,33 @@ +import os +from urllib.parse import urlparse +from urllib.parse import urljoin + +from django import template + + +register = template.Library() + + +@register.simple_tag +def external_api(external_url: str) -> str: + """ + Tag converts an external_url into an internal_url + so the response can be mocked with a fixture. + + Args: + external_url: Url to an external API resource. + + Returns: + When TEST_LIVE_SERVER_DOMAIN is available, the url is transformed so + the external domain is swapped with the TEST_LIVE_SERVER_DOMAIN. + """ + test_live_server_domain = os.environ.get('TEST_LIVE_SERVER_DOMAIN') + if not test_live_server_domain: + return external_url + url_components = urlparse(external_url) + + try: + # return the internal_url formed by the domain and url_components.path + return urljoin(test_live_server_domain, url_components.path) + except Exception: + return external_url diff --git a/helpers/urls.py b/helpers/urls.py index be6b12798..b3eba68ef 100644 --- a/helpers/urls.py +++ b/helpers/urls.py @@ -9,5 +9,6 @@ path('download/community/support-letter/', views.download_community_support_letter, name="download-community-support-letter"), path('community-boundary-view/', views.community_boundary_view, name="community-boundary-view"), path('project-boundary-view/', views.project_boundary_view, name="project-boundary-view"), - path('boundary-view/', views.boundary_view, name="boundary-view"), -] \ No newline at end of file + path('boundary-preview/', views.boundary_preview, name="boundary-preview"), + path('nld-data/', views.native_land_data, name="nld-data"), +] diff --git a/helpers/utils.py b/helpers/utils.py index e67577a2f..130c746eb 100644 --- a/helpers/utils.py +++ b/helpers/utils.py @@ -455,7 +455,7 @@ def create_or_update_boundary( raw_boundary_payload = post_data.get('boundary-payload') - if raw_boundary_payload in ['', None]: + if raw_boundary_payload in ['', '{}', None]: return data = json.loads(raw_boundary_payload) @@ -479,3 +479,17 @@ def create_or_update_boundary( # create boundary when it does not exist entity.boundary = Boundary(coordinates=boundary_coordinates) entity.boundary.save() + + +def retrieve_native_land_all_slug_data() -> dict: + """ + Does request to obtain all NLD slug data list + which includes the groups of coordinates for each slug + """ + url = ( + 'https://raw.githubusercontent.com/biocodellc/' + 'localcontexts_json/refs/heads/main/data/' + 'nativeland_slug_coordinates_description_dict.json' + ) + response = requests.get(url) + return response.json() diff --git a/helpers/views.py b/helpers/views.py index cc4b11c10..0f6d09471 100644 --- a/helpers/views.py +++ b/helpers/views.py @@ -1,8 +1,9 @@ import json +from django.core.cache import cache from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import login_required -from django.http import HttpResponse, Http404 +from django.http import HttpResponse, Http404, JsonResponse from django.conf import settings from django.views.decorators.clickjacking import xframe_options_sameorigin @@ -15,10 +16,13 @@ from .models import NoticeDownloadTracker from institutions.models import Institution from researchers.models import Researcher +from .utils import retrieve_native_land_all_slug_data + def restricted_view(request, exception=None): return render(request, '403.html', status=403) + @login_required(login_url='login') def delete_member_invite(request, pk): invite = InviteMember.objects.get(id=pk) @@ -53,6 +57,7 @@ def download_open_collaborate_notice(request, perm, researcher_id=None, institut return download_otc_notice(request) + @login_required(login_url='login') def download_collections_care_notices(request, institution_id, perm): # perm will be a 1 or 0 @@ -63,6 +68,7 @@ def download_collections_care_notices(request, institution_id, perm): NoticeDownloadTracker.objects.create(institution=Institution.objects.get(id=institution_id), user=request.user, collections_care_notices=True) return download_cc_notices(request) + @login_required(login_url='login') def download_community_support_letter(request): try: @@ -80,6 +86,9 @@ def download_community_support_letter(request): @xframe_options_sameorigin def community_boundary_view(request, community_id): + """ + Uses boundary in community for view + """ community = Community.objects.filter(id=community_id).first() if not community: message = 'Community Does Not Exist' @@ -92,32 +101,14 @@ def community_boundary_view(request, community_id): context = { 'boundary': boundary } - return render(request, 'boundary/boundary-view.html', context) - - -@login_required(login_url='login') -def boundary_view(request): - try: - boundary = request.GET.get('boundary') - if boundary: - boundary = json.loads( - boundary.replace('(', '[').replace(')', ']') - ) - else: - boundary = [] - - context = { - 'boundary': boundary - } - return render(request, 'boundary/boundary-view.html', context) - except Exception as e: - message = 'Invalid Boundary Format' - print(f'{message}: {e}') - raise Exception(message) + return render(request, 'boundary/boundary-preview.html', context) @xframe_options_sameorigin def project_boundary_view(request, project_id): + """ + Uses boundary in project for view + """ project = Project.objects.filter(id=project_id).first() if not project: message = 'Project Does Not Exist' @@ -130,4 +121,46 @@ def project_boundary_view(request, project_id): context = { 'boundary': boundary } - return render(request, 'boundary/boundary-view.html', context) + return render(request, 'boundary/boundary-preview.html', context) + + +@login_required(login_url='login') +def boundary_preview(request): + """ + Uses boundary in local storage for preview + """ + context = { + 'preview_boundary': True, + } + return render(request, 'boundary/boundary-preview.html', context) + + +@login_required(login_url='login') +def native_land_data(request): + """ + Returns data associated with particular slug + """ + slug = request.GET.get('slug') + if slug is None: + return Http404('Slug Variable Is Not Defined In Request') + + # get all slug data from cache + all_slug_data = cache.get('all_slug_data') + + # when cache doesn't exist, then retrieve actual data and cache it + if all_slug_data is None: + try: + all_slug_data = retrieve_native_land_all_slug_data() + except ( + requests.exceptions.Timeout, + requests.exceptions.ConnectionError, + requests.exceptions.HTTPError + ): + return Http404('Unable to Retrieve All NLD Slug Data') + cache.set('all_slug_data', all_slug_data) + + slug_data = all_slug_data.get(slug) + if slug_data is None: + return Http404(f'Unable to Retrieve Specific NLD Slug Data for {slug}') + + return JsonResponse(slug_data) diff --git a/localcontexts/settings.py b/localcontexts/settings.py index 87c0b54ec..f8d649cc9 100644 --- a/localcontexts/settings.py +++ b/localcontexts/settings.py @@ -296,6 +296,12 @@ } } +CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', + } +} + SITE_ID = 1 SOCIALACCOUNT_LOGIN_ON_GET=True diff --git a/templates/boundary/boundary-view.html b/templates/boundary/boundary-preview.html similarity index 62% rename from templates/boundary/boundary-view.html rename to templates/boundary/boundary-preview.html index ecbec32d0..b8df7cba2 100644 --- a/templates/boundary/boundary-view.html +++ b/templates/boundary/boundary-preview.html @@ -33,32 +33,53 @@ margin-top: 50px; padding: 36px; } + .invisible { + display: none + }
- {% if boundary|length == 0 %} -
-
- No boundary Present -
+
+
+ No boundary Present
+
+ + + + {% if preview_boundary %} - {% else %} + {% elif boundary|length > 0 %} {% endif %} - \ No newline at end of file diff --git a/templates/communities/add-community-boundary.html b/templates/communities/add-community-boundary.html index 0878d27c9..ab805c6b8 100644 --- a/templates/communities/add-community-boundary.html +++ b/templates/communities/add-community-boundary.html @@ -1,4 +1,4 @@ -{% extends 'register-base.html' %} {% block title %} Add Community {% endblock %} {% load static %} {% block card %} +{% extends 'register-base.html' %} {% block title %} Add Community {% endblock %} {% load static %} {% block card %} {% load custom_api_tags %}