summaryrefslogtreecommitdiff
path: root/archaeological_operations/management/commands/import_operations.py
blob: 3cf4a569d443fa4345ffe4ab264a4850cd31e407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012  Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

from django.core.management.base import BaseCommand, CommandError
from archaeological_operations.import_from_csv import import_from_csv
from archaeological_operations.import_from_dbf import import_from_dbf

IMPORTERS = {'csv':import_from_csv,
             'dbf':import_from_dbf,
             'db3':import_from_dbf,
             'fp':import_from_dbf,
             'vfp':import_from_dbf}

class Command(BaseCommand):
    args = '<filename> [<update> <csv|dbf>]'
    help = "Import archaelogical operations"

    def handle(self, *args, **options):
        if not args or not args[0]:
            raise CommandError("No file provided.")
        filename = args[0]
        update = len(args) > 1 and args[1]
        file_type = len(args) > 1 and args[2]
        if not file_type:
            suffix = filename.split('.')[-1].lower()
            if suffix in IMPORTERS.keys():
                file_type = suffix
            else:
                raise CommandError("This file extension is not managed. "\
                                    "Specify manualy the file type.")
        elif file_type not in IMPORTERS.keys():
            raise CommandError("This file type is not managed.")
        nb_ops, errors = IMPORTERS[file_type](filename,
                                              update=update,
                                              stdout=self.stdout)
        self.stdout.write('\n* %d operation treated\n' % nb_ops)
        if errors:
            self.stderr.write('\n'.join(errors))