|
|
@@ -23,7 +23,6 @@ import logging
|
|
|
import re
|
|
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
-from django.forms.util import ErrorDict
|
|
|
from django.http import QueryDict
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
@@ -168,10 +167,20 @@ def import_wizard(request, database='default'):
|
|
|
# Go back to define columns
|
|
|
do_s3_column_def, do_hive_create = True, False
|
|
|
|
|
|
+ load_data = s1_file_form.cleaned_data.get('load_data', 'IMPORT').upper()
|
|
|
+ path = s1_file_form.cleaned_data['path']
|
|
|
+
|
|
|
#
|
|
|
# Go to step 2: We've just picked the file. Preview it.
|
|
|
#
|
|
|
if do_s2_auto_delim:
|
|
|
+ if load_data == 'IMPORT':
|
|
|
+ if not request.fs.isfile(path):
|
|
|
+ raise PopupException(_('Path location must refer to a file if "Import Data" is selected.'))
|
|
|
+ elif load_data == 'EXTERNAL':
|
|
|
+ if not request.fs.isdir(path):
|
|
|
+ raise PopupException(_('Path location must refer to a directory if "Create External Table" is selected.'))
|
|
|
+
|
|
|
delim_is_auto = True
|
|
|
fields_list, n_cols, s2_delim_form = _delim_preview(request.fs, s1_file_form, encoding, [reader.TYPE for reader in FILE_READERS], DELIMITERS)
|
|
|
|
|
|
@@ -231,13 +240,16 @@ def import_wizard(request, database='default'):
|
|
|
if do_hive_create:
|
|
|
delim = s2_delim_form.cleaned_data['delimiter']
|
|
|
table_name = s1_file_form.cleaned_data['name']
|
|
|
+
|
|
|
proposed_query = django_mako.render_to_string("create_table_statement.mako", {
|
|
|
'table': {
|
|
|
'name': table_name,
|
|
|
'comment': s1_file_form.cleaned_data['comment'],
|
|
|
'row_format': 'Delimited',
|
|
|
'field_terminator': delim,
|
|
|
- 'file_format': 'TextFile'
|
|
|
+ 'file_format': 'TextFile',
|
|
|
+ 'load_data': load_data,
|
|
|
+ 'path': path,
|
|
|
},
|
|
|
'columns': [ f.cleaned_data for f in s3_col_formset.forms ],
|
|
|
'partition_columns': [],
|
|
|
@@ -245,11 +257,8 @@ def import_wizard(request, database='default'):
|
|
|
'databases': databases
|
|
|
}
|
|
|
)
|
|
|
-
|
|
|
- do_load_data = s1_file_form.cleaned_data.get('do_import')
|
|
|
- path = s1_file_form.cleaned_data['path']
|
|
|
try:
|
|
|
- return _submit_create_and_load(request, proposed_query, table_name, path, do_load_data, database=database)
|
|
|
+ return _submit_create_and_load(request, proposed_query, table_name, path, load_data, database=database)
|
|
|
except QueryServerException, e:
|
|
|
raise PopupException(_('The table could not be created.'), detail=e.message)
|
|
|
else:
|
|
|
@@ -263,14 +272,14 @@ def import_wizard(request, database='default'):
|
|
|
})
|
|
|
|
|
|
|
|
|
-def _submit_create_and_load(request, create_hql, table_name, path, do_load, database):
|
|
|
+def _submit_create_and_load(request, create_hql, table_name, path, load_data, database):
|
|
|
"""
|
|
|
- Submit the table creation, and setup the load to happen (if ``do_load``).
|
|
|
+ Submit the table creation, and setup the load to happen (if ``load_data`` == IMPORT).
|
|
|
"""
|
|
|
on_success_params = QueryDict('', mutable=True)
|
|
|
app_name = get_app_name(request)
|
|
|
|
|
|
- if do_load:
|
|
|
+ if load_data == 'IMPORT':
|
|
|
on_success_params['table'] = table_name
|
|
|
on_success_params['path'] = path
|
|
|
on_success_params['removeHeader'] = request.POST.get('removeHeader')
|
|
|
@@ -296,6 +305,11 @@ def _delim_preview(fs, file_form, encoding, file_types, delimiters):
|
|
|
|
|
|
path = file_form.cleaned_data['path']
|
|
|
try:
|
|
|
+ # If path is a directory, find first file object
|
|
|
+ if fs.isdir(path):
|
|
|
+ children = fs.listdir(path)
|
|
|
+ if children:
|
|
|
+ path = '%s/%s' % (path, children[0])
|
|
|
file_obj = fs.open(path)
|
|
|
delim, file_type, fields_list = _parse_fields(path, file_obj, encoding, file_types, delimiters)
|
|
|
file_obj.close()
|
|
|
@@ -477,6 +491,7 @@ def load_after_create(request, database):
|
|
|
|
|
|
if is_remove_header:
|
|
|
try:
|
|
|
+ path = path.rstrip('/') # need to remove trailing slash before overwrite
|
|
|
remove_header(request.fs, path)
|
|
|
except Exception, e:
|
|
|
msg = "The headers of the file could not be removed."
|