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

Avoid fail when file does not exist #175

Merged
merged 1 commit into from
Sep 4, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function controller($filter, $mdDialog, $state, adminService, alertService, data
.filter((value, index, array) => array.indexOf(value) === index)
.join('\n');

const text = `${warningsList.join(', ')}\n\n${filesList}`;
const text = `${warningsList.join('\n\n')}\n\n${filesList}`;

const dialog = $mdDialog
.confirm()
Expand Down
8 changes: 7 additions & 1 deletion montage/admin_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,13 @@ def import_entries(user_dao, round_id, request_dict):
'round_id': prev_round_id}
elif import_method == SELECTED_METHOD:
file_names = request_dict['file_names']
entries = coord_dao.add_entries_by_name(round_id, file_names)
entries, warnings = coord_dao.add_entries_by_name(round_id, file_names)
if warnings:
formatted_warnings = '\n'.join([
'- {}'.format(warning) for warning in warnings
])
msg = 'unable to load {} files:\n{}'.format(len(warnings), formatted_warnings)
import_warnings.append({'import issues', msg})
params = {'file_names': file_names}
else:
raise NotImplementedResponse()
Expand Down
5 changes: 3 additions & 2 deletions montage/labs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def get_file_info(filename):
params = (filename.replace(' ', '_'),)
results = fetchall_from_commonswiki(query, params)
if results:
results = results[0]
return results
return results[0]
else:
return None


if __name__ == '__main__':
Expand Down
21 changes: 15 additions & 6 deletions montage/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ def load_name_list(file_obj, source='local'):
edicts, warnings = get_by_filename_remote(rl)
else:
for filename in rl:
edict, warnings = get_file_info(filename)
edicts.append(edict)
file_info = get_file_info(filename)
if file_info is not None:
edict, warnings = file_info
edicts.append(edict)

for edict in edicts:
try:
Expand Down Expand Up @@ -182,18 +184,25 @@ def get_entries_from_gsheet(raw_url, source='local'):


def load_by_filename(filenames, source='local'):
ret = []
entries = []
warnings = []
if source == 'remote':
files, warnings = get_by_filename_remote(filenames)
else:
files = []
for filename in filenames:
file_info = get_file_info(filename)
files.append(file_info)
if file_info is not None:
files.append(file_info)
else:
warnings.append(
'file "%s" does not exist, please check that its name is spelled correctly, '
'that it has not been renamed or removed' % (filename,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good, actionable error message. However, we should keep an eye out for if there are many, many missing files. Assuming there are 0-10, I think the error display method defined in the JS above should be fine.

)
for edict in files:
entry = make_entry(edict)
ret.append(entry)
return ret
entries.append(entry)
return entries, warnings


def load_category(category_name, source='local'):
Expand Down
4 changes: 2 additions & 2 deletions montage/rdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,14 +1545,14 @@ def add_entries_by_name(self, round_id, file_names):
source = 'remote'
else:
source = 'local'
entries = loaders.load_by_filename(file_names, source=source)
entries, warnings = loaders.load_by_filename(file_names, source=source)
entries, new_entry_count = self.add_entries(rnd, entries)

msg = ('%s loaded %s entries from filenames, %s new entries added'
% (self.user.username, len(entries), new_entry_count))
self.log_action('add_entries', message=msg, round=rnd)

return entries
return entries, warnings

def add_entries_from_csv(self, round_id, csv_url):
# NOTE: this no longer creates RoundEntries, use
Expand Down