From 40af77b2677f94d81521d6668d1d3aefd12376c9 Mon Sep 17 00:00:00 2001 From: Christophe Boulanger Date: Tue, 28 Feb 2017 16:19:49 +0100 Subject: [PATCH] Add management for ts1 terms (manage.py). usage : bin/manage.py ts1_terms_manage [options] --- .../management/__init__.py | 0 .../management/commands/__init__.py | 0 .../management/commands/ts1_terms_manage.py | 48 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 passerelle_imio_ts1_datasources/management/__init__.py create mode 100644 passerelle_imio_ts1_datasources/management/commands/__init__.py create mode 100644 passerelle_imio_ts1_datasources/management/commands/ts1_terms_manage.py diff --git a/passerelle_imio_ts1_datasources/management/__init__.py b/passerelle_imio_ts1_datasources/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/passerelle_imio_ts1_datasources/management/commands/__init__.py b/passerelle_imio_ts1_datasources/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/passerelle_imio_ts1_datasources/management/commands/ts1_terms_manage.py b/passerelle_imio_ts1_datasources/management/commands/ts1_terms_manage.py new file mode 100644 index 0000000..684709b --- /dev/null +++ b/passerelle_imio_ts1_datasources/management/commands/ts1_terms_manage.py @@ -0,0 +1,48 @@ +from django.core.management.base import BaseCommand, CommandError +from passerelle_imio_ts1_datasources.models import MotivationTerm, DestinationTerm +import json + + +class Command(BaseCommand): + help = 'Import TS1 Motivationterms in TS2.' + + def add_arguments(self, parser): + parser.add_argument('--motivationterms_filepath', type=str) + parser.add_argument('--destinationterms_filepath', type=str) + parser.add_argument('--remove_all_terms', action='store_true') + + def handle(self, *args, **options): + motivationterms_filepath = options['motivationterms_filepath'] + destinationterms_filepath = options['destinationterms_filepath'] + remove_all_terms = options['remove_all_terms'] + if remove_all_terms is True: + self.remove_all_terms() + if motivationterms_filepath is not None: + self.motivationterms_import(motivationterms_filepath) + if destinationterms_filepath is not None: + self.destinationterms_import(destinationterms_filepath) + + def remove_all_terms(self): + MotivationTerm.objects.all().delete() + DestinationTerm.objects.all().delete() + + def motivationterms_import(self, path): + data = {} + with open(path) as data_file: + data = json.load(data_file) + for motivationterm in data: + mt_object = MotivationTerm(text=motivationterm["text"], + price=motivationterm["price"], + description=motivationterm["description"]) + mt_object.save() + + def destinationterms_import(self, path): + data = {} + with open(path) as data_file: + data = json.load(data_file) + for destinationterm in data: + mt_object = DestinationTerm(text=destinationterm["text"], + price=destinationterm["price"], + description=destinationterm["description"], + paymentrequired=destinationterm["paymentRequired"]) + mt_object.save()