From e28c92f0c2e58abe2ab2eaa2aa37a38c96fe4c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mosi=C5=84ski?= Date: Sat, 15 Dec 2018 14:18:08 +0100 Subject: [PATCH 1/2] Fix for django 1.8+ (parsing args) and files in utf8 --- .../management/commands/teryt_auto_update.py | 1 - teryt/management/commands/teryt_parse.py | 25 +++++++++++-------- teryt/utils_zip.py | 2 ++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/teryt/management/commands/teryt_auto_update.py b/teryt/management/commands/teryt_auto_update.py index fceb45e..92d7367 100755 --- a/teryt/management/commands/teryt_auto_update.py +++ b/teryt/management/commands/teryt_auto_update.py @@ -17,7 +17,6 @@ class Command(BaseCommand): help = 'Import TERYT data from ZIP files prepared by GUS,\ auto download, unpack and update with them.' - option_list = BaseCommand.option_list def handle(self, *args, **options): # download zip files from GUS site diff --git a/teryt/management/commands/teryt_parse.py b/teryt/management/commands/teryt_parse.py index 2ca40a8..00f2ff0 100644 --- a/teryt/management/commands/teryt_parse.py +++ b/teryt/management/commands/teryt_parse.py @@ -7,7 +7,6 @@ already existing in database """ -from optparse import make_option import zipfile from django.core.management.base import BaseCommand, CommandError @@ -18,21 +17,25 @@ class Command(BaseCommand): args = '[xml/zip file list]' help = 'Import TERYT data from XML/ZIP files prepared by GUS' - option_list = BaseCommand.option_list + ( - make_option('--update', - action='store_true', - dest='update', - default=False, - help='Update exisitng data'), - ) + + def add_arguments(self, parser): + parser.add_argument('file', nargs='+', type=str) + parser.add_argument( + '--update', + action='store_true', + dest='update', + default=False, + help='Update exisitng data' + ) def handle(self, *args, **options): + files = options['file'] force_ins = not options['update'] - if not args: + if not files: raise CommandError('At least 1 file name required') - for data_file in args: + for data_file in files: self.stdout.write('Working on {}'.format(data_file)) if zipfile.is_zipfile(data_file): zfile = zipfile.ZipFile(data_file) @@ -40,7 +43,7 @@ def handle(self, *args, **options): with zfile.open(fname) as xml_file: update_database(xml_file, fname, force_ins) else: - with open(data_file) as xml_file: + with open(data_file, encoding="utf8") as xml_file: update_database(xml_file, data_file, force_ins) self.stdout.write('File {} uploaded'.format(data_file)) diff --git a/teryt/utils_zip.py b/teryt/utils_zip.py index 11f9888..2e60c6f 100755 --- a/teryt/utils_zip.py +++ b/teryt/utils_zip.py @@ -66,6 +66,8 @@ def update_database(xml_stream, fname, force_flag): else x['SYM']) for vals in row_list: + if len(vals) == 0: + continue instance = teryt_class() instance.set_val(vals) instance.aktywny = True From 8ffcca2123bed80b414635f679c3eb9267912efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mosi=C5=84ski?= Date: Sat, 15 Dec 2018 22:35:25 +0100 Subject: [PATCH 2/2] Fix for python2.7 --- teryt/management/commands/teryt_parse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/teryt/management/commands/teryt_parse.py b/teryt/management/commands/teryt_parse.py index 00f2ff0..f0f99a0 100644 --- a/teryt/management/commands/teryt_parse.py +++ b/teryt/management/commands/teryt_parse.py @@ -43,7 +43,7 @@ def handle(self, *args, **options): with zfile.open(fname) as xml_file: update_database(xml_file, fname, force_ins) else: - with open(data_file, encoding="utf8") as xml_file: + with open(data_file) as xml_file: update_database(xml_file, data_file, force_ins) self.stdout.write('File {} uploaded'.format(data_file))