Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KOBOCAT_ROOT_URI_PREFIX in instance.js for correct API URL - fixes #405 #406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions onadata/apps/main/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def site_name(request):
site_name = site.name
return {'SITE_NAME': site_name}


def base_url(request):
"""
Return a BASE_URL template context for the current request.
Expand All @@ -31,5 +32,11 @@ def base_url(request):
scheme = 'https://'
else:
scheme = 'http://'

return {'BASE_URL': scheme + request.get_host(),}

return {'BASE_URL': scheme + request.get_host(), }


def kobocat_root_uri_prefix(request):
# settings.KOBOCAT_ROOT_URI_PREFIX will have trailing and leading slashes, so we fall back to a
# single (leading) slash when it isn't set
return {'KOBOCAT_ROOT_URI_PREFIX': getattr(settings, 'KOBOCAT_ROOT_URI_PREFIX', '/')}
6 changes: 4 additions & 2 deletions onadata/apps/viewer/static/js/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function addOrEditNote(){
if (note == ""){
return false;
}
var notes_url = '/api/v1/notes',
// KOBOCAT_ROOT_URI_PREFIX variable will be set in the template using a context processor, in case kobocat
// is running under a URL path prefix (e.g. /kobocat/). In case the variable isn't set it will use /
var notes_url = ('KOBOCAT_ROOT_URI_PREFIX' in window ? KOBOCAT_ROOT_URI_PREFIX : '/') + 'api/v1/notes',
post_data = {'note': note, 'instance': instance_id};
if($("#notesform #note-id").val() != undefined){
// Edit Note
Expand Down Expand Up @@ -442,7 +444,7 @@ function deleteNote(obj){
var note_id = $(obj).data('note-id');
if(confirm("Are you sure you want to delete \"" + note + "\"?") == true){
$.ajax({
url: "/api/v1/notes/" + note_id,
url: ('KOBOCAT_ROOT_URI_PREFIX' in window ? KOBOCAT_ROOT_URI_PREFIX : '/') + 'api/v1/notes/' + note_id,
type: "DELETE",
statusCode: {
404: function(){
Expand Down
3 changes: 3 additions & 0 deletions onadata/apps/viewer/templates/instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,7 @@ <h3>{% trans "Delete Confirmation" %}</h3>
});
});
</script>
<script type="text/javascript">
var KOBOCAT_ROOT_URI_PREFIX = "{{ KOBOCAT_ROOT_URI_PREFIX }}" || "/";
</script>
{% endblock %}
3 changes: 2 additions & 1 deletion onadata/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@
'readonly.context_processors.readonly',
'onadata.apps.main.context_processors.google_analytics',
'onadata.apps.main.context_processors.site_name',
'onadata.apps.main.context_processors.base_url'
'onadata.apps.main.context_processors.base_url',
'onadata.apps.main.context_processors.kobocat_root_uri_prefix'
)

MIDDLEWARE_CLASSES = (
Expand Down