This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from cmc333333/rewrite-import
Rewrite/rename import_reg management command
- Loading branch information
Showing
6 changed files
with
147 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import logging | ||
import os | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.test import Client, override_settings | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def scoped_files(root): | ||
"""Find all of the files which will need to be "uploaded"; trim them down | ||
to their suffix and separate their path components. We'll assume that | ||
`root` has no trailing slash""" | ||
for path, _, file_names in os.walk(root): | ||
for file_name in file_names: | ||
file_path = os.path.join(path, file_name) | ||
trimmed = file_path[len(root):] | ||
yield trimmed.split(os.sep) | ||
|
||
|
||
def save_file(root, file_parts): | ||
"""Given a file (indicated by a root file path and a set of file path | ||
components), read the file from disk and write it to the database. Log | ||
results.""" | ||
file_path = os.path.join(root, *file_parts) | ||
with open(file_path, 'rb') as f: | ||
content = f.read() | ||
result = Client().put('/'.join(file_parts), data=content, | ||
content_type='application/json') | ||
if result.status_code == 204: | ||
logger.info('Saved %s', file_path) | ||
else: | ||
logger.error('Failed to save %s: (%s), %s', | ||
file_path, result.status_code, result.content[:100]) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Import a collection of JSON files into the database." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'base_dir', default=os.getcwd(), nargs='?', | ||
help='the base filesystem path for importing JSON files' | ||
) | ||
|
||
@override_settings(ROOT_URLCONF='regcore.urls', ALLOWED_HOSTS=['*']) | ||
def handle(self, *args, **options): | ||
root = options['base_dir'].rstrip(os.sep) | ||
|
||
for file_parts in scoped_files(root): | ||
save_file(root, file_parts) |
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from mock import Mock | ||
|
||
from regcore.management.commands import import_docs | ||
|
||
|
||
def test_scoped_files(tmpdir): | ||
"""We get the file path components for all created files, regardless of | ||
how deep in the directory structure""" | ||
a = tmpdir.mkdir('a') | ||
b = tmpdir.mkdir('b') | ||
a.mkdir('1') | ||
a.mkdir('2') | ||
b.mkdir('1').mkdir('i') | ||
|
||
tmpdir.ensure('a', '1', 'i') | ||
tmpdir.ensure('a', '1', 'ii') | ||
tmpdir.ensure('a', '2', 'i') | ||
tmpdir.ensure('b', '1', 'i', 'A') | ||
tmpdir.ensure('b', '1', 'i', 'B') | ||
tmpdir.ensure('b', '1', 'i', 'C') | ||
|
||
result = {tuple(file_parts) | ||
for file_parts in import_docs.scoped_files(str(tmpdir))} | ||
# These should always begin with an empty string due to leaving in the | ||
# trailing slash | ||
assert result == { | ||
('', 'a', '1', 'i'), ('', 'a', '1', 'ii'), | ||
('', 'a', '2', 'i'), | ||
('', 'b', '1', 'i', 'A'), ('', 'b', '1', 'i', 'B'), | ||
('', 'b', '1', 'i', 'C'), | ||
} | ||
|
||
|
||
def test_save_file(monkeypatch, tmpdir): | ||
"""Saving a file should send it to a corresponding url""" | ||
monkeypatch.setattr(import_docs, 'Client', Mock()) | ||
monkeypatch.setattr(import_docs, 'logger', Mock()) | ||
# Client().put | ||
put = import_docs.Client.return_value.put | ||
put.return_value.status_code = 204 | ||
|
||
tmpdir.mkdir('a').mkdir('1').join('i').write(b'content') | ||
import_docs.save_file(str(tmpdir), ['', 'a', '1', 'i']) | ||
assert put.call_args == ( | ||
('/a/1/i',), {'data': b'content', 'content_type': 'application/json'}) | ||
assert import_docs.logger.info.called | ||
|
||
put.reset_mock() | ||
put.return_value.status_code = 404 | ||
put.return_value.content = 'a'*1000 | ||
import_docs.save_file(str(tmpdir), ['', 'a', '1', 'i']) | ||
assert import_docs.logger.error.called | ||
assert import_docs.logger.error.call_args[0][3] == 'a'*100 # trimmed |