diff --git a/gsheets_import/admin.py b/gsheets_import/admin.py index 4a465c9..520e72e 100644 --- a/gsheets_import/admin.py +++ b/gsheets_import/admin.py @@ -23,6 +23,15 @@ 'GSHEETS_IMPORT_APP_ID', ] +## list of keys that need to exist in the POST request data +POST_REQUEST_KEYS = [ + 'google_file_id', + 'google_file_name', + 'oauth_token', + 'subsheet_name', +] + + ############################################################### @@ -32,20 +41,6 @@ ############################################################### -## get the names of all subsheets within the given Google Sheet -def get_subsheets_names(service, sheet_id): - ## extract metadata from specified sheet - result_dict = service.spreadsheets().get( - spreadsheetId = sheet_id - ).execute() - ## get the sheet name (file name) - sheet_name = result_dict.get('properties', {}).get('title') - ## get the names of the subsheets (tab names) - subsheets_info = result_dict.get('sheets', None) - if subsheets_info is not None: - return ([info.get('properties', {}).get('title') for info in subsheets_info], sheet_name) - - ## download the given sheet's content as an in-memory CSV file to the server def download_csv(service, sheet_id, subsheet_name, sheet_name): """ @@ -96,6 +91,7 @@ def get_import_context_data(self, **kwargs): if len(failed_settings_vars) > 0: raise AttributeError("Variables in settings.py related to the Google Sheet import app not set correctly (" + ', '.join(failed_settings_vars) + ').') else: + context['dummy_choice_txt'] = forms.DUMMY_CHOICE_TXT context.update(gsheets_import_context) return context @@ -105,20 +101,19 @@ def import_action(self, request, *args, **kwargs): ## process google sheet if request.POST and request.POST.get('is_google') == 'true': ## only proceed if the required data was sent and is not empty - if all([ (key in request.POST) and (request.POST[key] != '') for key in ['google_file_id', 'oauth_token'] ]): - ## extract information about file ('file_id') and user authorization ('oauth_token') + if all([ (key in request.POST) and (request.POST[key] != '') for key in POST_REQUEST_KEYS ]): + ## extract information about file ('file_id', 'file_name', and 'subsheet_name') and user authorization ('oauth_token') file_id = request.POST['google_file_id'] + file_name = request.POST['google_file_name'] oauth_token = request.POST['oauth_token'] + subsheet_name = request.POST['subsheet_name'] ## create sheets service using the provided OAuth 2.0 token creds = Credentials(oauth_token) sheets_service = build('sheets', 'v4', credentials=creds) - ## extract subsheet names from selected Google Sheet - subsheet_names, sheet_name = get_subsheets_names(sheets_service, file_id) - ## download the specified sheet to an in-memory file - sheet_file = download_csv(sheets_service, file_id, subsheet_names[0], sheet_name) + sheet_file = download_csv(sheets_service, file_id, subsheet_name, file_name) ## NB: The request.FILES property cannot be set for an object of WSGIRequest; ## set the _files attribute instead, which might not be the most elegant way... (rather implement custom upload handler?) diff --git a/gsheets_import/forms.py b/gsheets_import/forms.py index d53b0a7..b087dfb 100644 --- a/gsheets_import/forms.py +++ b/gsheets_import/forms.py @@ -37,6 +37,11 @@ +## text to be displayed in the subsheet name selection widget by default +DUMMY_CHOICE_TXT = '--------' + + + ################################################# # # @@ -62,6 +67,11 @@ class CustomImportForm(forms.Form): label = _('File to import'), widget = FileAndGoogleInput ) + subsheet_name = forms.CharField( + label = _('Subsheet name'), + widget = forms.Select(choices = (('dummy-choice', DUMMY_CHOICE_TXT),)), + required = False + ) def __init__(self, import_formats, *args, **kwargs): super().__init__(*args, **kwargs) diff --git a/gsheets_import/static/gsheets_import/js/import_gs.js b/gsheets_import/static/gsheets_import/js/import_gs.js index afe4773..b34f2c9 100644 --- a/gsheets_import/static/gsheets_import/js/import_gs.js +++ b/gsheets_import/static/gsheets_import/js/import_gs.js @@ -39,10 +39,13 @@ const select_format = document.getElementById("id_input_format"); const button_import = document.getElementById("id_import_file"); const span_import = document.getElementById("id_import_file_span"); const input_os_file = document.getElementById("id_import_file_os_file"); -const input_google_file = document.getElementById("id_import_file_google_id"); +const input_google_file_id = document.getElementById("id_import_file_google_id"); +const input_google_file_name = document.getElementById("id_import_file_google_name"); const input_google_oauth = document.getElementById("id_import_file_google_oauth_token"); const input_is_google = document.getElementById("id_import_file_is_google"); +// relevant HTML elements for subsheet selection +const select_subsheet = document.getElementById("id_subsheet_name"); @@ -94,7 +97,7 @@ var pickerApiLoaded = false; var oauthToken; var googleAuth; -// additional variables 'developerKey', 'clientId', and 'appId' are +// additional variables 'developerKey', 'clientId', 'appId', and 'dummyChoiceTxt' are // already set in the 'import.html' template @@ -184,19 +187,25 @@ function createPicker() { // Function to call once the picker was successfully built. function pickerCallback(data) { if(data.action == google.picker.Action.PICKED) { - input_google_file.value = data.docs[0].id; + input_google_file_id.value = data.docs[0].id; + input_google_file_name.value = data.docs[0].name; input_google_oauth.value = oauthToken; span_import.innerHTML = data.docs[0].name + " (1st sheet)"; - /* display all subsheets + // display all subsheets gapi.client.sheets.spreadsheets.get({ spreadsheetId: data.docs[0].id }).then(function(response) { - console.log(response.result.sheets) + select_subsheet.innerHTML = ""; + for(let subsheet_meta of response.result.sheets) { + var opt = document.createElement("option"); + opt.text = subsheet_meta.properties.title; + select_subsheet.appendChild(opt); + console.log(subsheet_meta.properties.title) + } }).catch(function(response) { console.log('Error: ' + response.result.error.message); }); - */ } } diff --git a/gsheets_import/templates/admin/gsheets_import/import.html b/gsheets_import/templates/admin/gsheets_import/import.html index 4845f23..56242d6 100644 --- a/gsheets_import/templates/admin/gsheets_import/import.html +++ b/gsheets_import/templates/admin/gsheets_import/import.html @@ -231,6 +231,7 @@

{% trans "Preview" %}

const developerKey = '{{ gsheets_import_api_key }}'; const clientId = '{{ gsheets_import_client_id }}'; const appId = '{{ gsheets_import_app_id }}'; + const dummyChoiceTxt = '{{ dummy_choice_txt }}';