Skip to content

Commit

Permalink
KOBOCAT_ROOT_URI_PREFIX in instance.js for correct API URL - fixes ko…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeverling committed Jan 5, 2018
1 parent 1c9be9c commit fe7b8e2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
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

0 comments on commit fe7b8e2

Please sign in to comment.