Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…xtshub into 10-4-develop-to-subscriptions-update
  • Loading branch information
arojas1 committed Oct 4, 2024
2 parents e4b10a2 + 664fa42 commit 4130c16
Show file tree
Hide file tree
Showing 21 changed files with 728 additions and 49 deletions.
4 changes: 2 additions & 2 deletions api/base/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class ProjectSerializer(serializers.ModelSerializer):

class Meta:
model = Project
fields = ('unique_id', 'providers_id', 'source_project_uuid', 'project_page', 'title', 'project_privacy', 'date_added', 'date_modified', 'created_by', 'notice', 'sub_projects', 'related_projects', 'project_boundary_geojson')
fields = ('unique_id', 'providers_id', 'publication_doi', 'project_data_guid', 'source_project_uuid', 'project_page', 'title', 'project_privacy', 'date_added', 'date_modified', 'created_by', 'notice', 'sub_projects', 'related_projects', 'project_boundary_geojson')

def get_related_projects(self, obj):
return [project.unique_id for project in obj.related_projects.all()]
Expand All @@ -121,7 +121,7 @@ class ProjectNoNoticeSerializer(serializers.ModelSerializer):

class Meta:
model = Project
fields = ('unique_id', 'providers_id', 'source_project_uuid', 'project_page', 'title', 'project_privacy', 'date_added', 'date_modified', 'created_by', 'bc_labels', 'tk_labels', 'sub_projects', 'related_projects', 'project_boundary_geojson')
fields = ('unique_id', 'providers_id', 'publication_doi', 'project_data_guid', 'source_project_uuid', 'project_page', 'title', 'project_privacy', 'date_added', 'date_modified', 'created_by', 'bc_labels', 'tk_labels', 'sub_projects', 'related_projects', 'project_boundary_geojson')

def get_related_projects(self, obj):
return [project.unique_id for project in obj.related_projects.all()]
Expand Down
2 changes: 1 addition & 1 deletion api/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ProjectList(generics.ListAPIView):
serializer_class = ProjectOverviewSerializer

filter_backends = [filters.SearchFilter]
search_fields = ['^providers_id', '=unique_id', 'title']
search_fields = ['^providers_id', '=unique_id', 'title', 'publication_doi', 'project_data_guid']

# '^' starts-with search
# '=' exact matches
Expand Down
Empty file added api/templatetags/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions api/templatetags/custom_api_tags.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion helpers/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
path('download/community/support-letter/', views.download_community_support_letter, name="download-community-support-letter"),
path('community-boundary-view/<int:community_id>', views.community_boundary_view, name="community-boundary-view"),
path('project-boundary-view/<int:project_id>', views.project_boundary_view, name="project-boundary-view"),
path('boundary-view/', views.boundary_view, name="boundary-view"),
path('boundary-preview/', views.boundary_preview, name="boundary-preview"),
]
2 changes: 1 addition & 1 deletion helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def create_or_update_boundary(post_data: dict, entity: Union['Community', 'Proje
entity.share_boundary_publicly = post_data.get('share-boundary-publicly') == 'on'
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)
Expand Down
45 changes: 22 additions & 23 deletions helpers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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)
Expand Down Expand Up @@ -78,6 +79,7 @@ def download_open_collaborate_notice(request, perm, researcher_id=None, institut
raise UnsubscribedAccountException(message)



@login_required(login_url='login')
def download_collections_care_notices(request, institution_id, perm):
# perm will be a 1 or 0
Expand All @@ -88,6 +90,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:
Expand All @@ -105,6 +108,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'
Expand All @@ -117,32 +123,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'
Expand All @@ -155,4 +143,15 @@ 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)
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,53 @@
margin-top: 50px;
padding: 36px;
}
.invisible {
display: none
}
</style>
</head>
<body>
<div id="map"></div>

{% if boundary|length == 0 %}
<div id="no-boundary-container">
<div id="no-boundary">
No boundary Present
</div>
<div id="no-boundary-container">
<div id="no-boundary">
No boundary Present
</div>
</div>

<script>
// when boundary is not present, set default
let boundary = [
[[8.7832, 34.5085]]
]
let setInitialZoom = true

function boundaryExists() {
// when boundary exists, this is run to modify
// the settings
setInitialZoom = false
document.querySelector('#no-boundary').classList.add('invisible')
}
</script>

{% if preview_boundary %}
<script>
// when boundary are not present, set default
const boundary = [
[[8.7832, 34.5085]]
]
const setInitialZoom = true
// get boundary data from Local Storage
if (localStorage.hasOwnProperty('boundary_preview')) {
boundary = JSON.parse(
localStorage.getItem('boundary_preview')
)
boundaryExists()
}
</script>
{% else %}
{% elif boundary|length > 0 %}
<script>
const boundary = {{ boundary }}
const setInitialZoom = false
// Get boundary data from backend
boundary = {{ boundary }}
boundaryExists()
</script>
{% endif %}


<script>
// define map and its initial boundary
const map = L.map('map')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,11 @@
}

function setViewLocationsHref(selectionUnit, boundary) {
// set boundary-preview value in localStorage
localStorage.setItem('boundary_preview', JSON.stringify(boundary))

// set href for view-boundary-location
const href = `{% url 'boundary-view' %}?boundary=${JSON.stringify(boundary)}`
const href = "{% url 'boundary-preview' %}"
selectionUnit.querySelector('.view-boundary-link').setAttribute('href', href)
}

Expand Down Expand Up @@ -553,6 +556,8 @@
clearShapeFileInput(selectionUnit)
selectInitialShareBoundaryPublicly()
disableContinueButton()
BoundaryPayLoad = {}
setBoundaryPayloadForInputElement()
}
</script>
<script src="{% static 'javascript/shpjs.js' %}"></script>
8 changes: 4 additions & 4 deletions templates/communities/add-community-boundary.html
Original file line number Diff line number Diff line change
@@ -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 %}
<style>
#define-community-top-text{
width: 94%;
Expand Down Expand Up @@ -184,7 +184,7 @@ <h2 class="mt-0">Search the Native Land Digital database</h2>
</div>

<script>
fetch('https://native-land.ca/wp-json/nativeland/v1/map-list').then(resp => resp.json()).then(response => {
fetch('{% external_api "https://native-land.ca/wp-json/nativeland/v1/map-list" %}').then(resp => resp.json()).then(response => {

document.querySelector('#region-container input.search').addEventListener("keyup", (event) => {
let html = ''
Expand Down Expand Up @@ -248,8 +248,8 @@ <h2 class="mt-0">Search the Native Land Digital database</h2>
// do post call to get boundary
const href = document.querySelector('#community-boundary-continue-btn').getAttribute('href')
const slug = document.querySelector('#selected-title').getAttribute('data-slug')

const url = `https://native-land.ca/wp-json/nativeland/v1/api/index.php?maps=territories&name=${slug}`
const url = `{% external_api "https://native-land.ca/wp-json/nativeland/v1/api/index.php?maps=territories&name=${slug}" %}`
const boundaryResponse = await fetch(url)
const boundaryData = await boundaryResponse.json()
document.querySelector("#community-boundary-continue-btn .fa-spinner").classList.remove("spinner-container");
Expand Down
1 change: 1 addition & 0 deletions templates/partials/_project-actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ <h2 class="primary-black-text mt-0">Are you sure?</h2>
{% if 'native-land.ca' in project.source_of_boundary and project.name_of_boundary %}
<span>
<a
data-test-id="native-land-link"
href="{{project.source_of_boundary}}"
class="default-a" target="_blank" rel="noopener"
style="display: block; margin-bottom: 5px;">
Expand Down
5 changes: 4 additions & 1 deletion templates/widget_forms/boundary_widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@
return
}

// set boundary-preview value in localStorage
localStorage.setItem('boundary_preview', JSON.stringify(boundary))

// open boundary preview
const url = `{% url 'boundary-view' %}?boundary=[${boundary}]`
const url = "{% url 'boundary-preview' %}"
window.open(url, '_blank');
}
async function uploadShapeFileData(self) {
Expand Down
14 changes: 14 additions & 0 deletions tests/external_api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from localcontexts.urls import urlpatterns
from django.urls import path

from . views import (
native_land_map_list,
native_land_boundary_response
)


class UrlsWithMockedExternalApi:
urlpatterns = urlpatterns + [
path('wp-json/nativeland/v1/map-list/', native_land_map_list),
path('wp-json/nativeland/v1/api/index.php', native_land_boundary_response)
]
23 changes: 23 additions & 0 deletions tests/external_api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json

from django.http import JsonResponse


def native_land_map_list(request):
"""
This view is called when populating the dropdown
of Native Land territories
"""
json_data = open('tests/fixtures/native_land_map_list.json')
data = json.load(json_data)
return JsonResponse(data, safe=False)


def native_land_boundary_response(request):
"""
This view is called when getting specific boundary data
for a single Native Land territory
"""
json_data = open('tests/fixtures/native_land_boundary_response.json')
data = json.load(json_data)
return JsonResponse(data, safe=False)
Loading

0 comments on commit 4130c16

Please sign in to comment.