scibi / django-teryt

Django app that implements TERYT database
MIT License
3 stars 7 forks source link

Django 1.8+ version command options #16

Open rexopl opened 7 years ago

rexopl commented 7 years ago

New django version does not support option_list. AttributeError: type object 'BaseCommand' has no attribute 'option_list' Reference: https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/#django.core.management.BaseCommand.option_list

It should get something like: def add_arguments(self, parser): parser.add_argument( '--update', action='store_true', dest='update', default=False, help='Update exisitng data' )

rexopl commented 7 years ago

Here is working example with some fixes (django 1.10+):

` from django.core.management.base import BaseCommand, CommandError from django.db import transaction, DatabaseError, IntegrityError

from teryt.models import ( RodzajMiejscowosci, JednostkaAdministracyjna, Miejscowosc, Ulica ) from teryt.utils import parse

import os.path

class Command(BaseCommand): args = '[xml file list]' help = 'Import TERYT data from XML files prepared by GUS'

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']

    fn_dict = {
        'WMRODZ.xml': RodzajMiejscowosci,
        'TERC.xml': JednostkaAdministracyjna,
        'SIMC.xml': Miejscowosc,
        'ULIC.xml': Ulica,
    }

    if not files or len(files) == 0:
        raise CommandError('At least 1 file name required')

    for a in files:
        try:
            c = fn_dict[os.path.basename(a)]
        except KeyError as e:
            raise CommandError('Unknown filename: {}'.format(e))

        try:
            with transaction.atomic():
                c.objects.all().update(aktywny=False)

                row_list = parse(a)

                # MySQL doesn't support deferred checking of foreign key
                # constraints. As a workaround we sort data placing rows
                # with no a parent row at the begining.
                if c is Miejscowosc:
                    row_list = sorted(row_list, key=lambda x: '0000000'
                                      if x['SYM'] == x['SYMPOD']
                                      else x['SYM'])

                for vals in row_list:
                    if len(vals) == 0:
                        continue
                    t = c()
                    t.set_val(vals)
                    t.aktywny = True
                    t.save(force_insert=force_ins)
        except IntegrityError as e:
            raise CommandError("Database integrity error: {}".format(e))
        except DatabaseError as e:
            raise CommandError("General database error: {}\n"
                               "Make sure you run syncdb or migrate before"
                               "importing data!".format(e))

`

Grejeru commented 5 years ago

@rexopl can You create Pull Request with changes to have it working on Django 1.10+?

rexopl commented 5 years ago

Done @ https://github.com/scibi/django-teryt/pull/17