forked from nocproject/noc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv-export.py
78 lines (67 loc) · 2.16 KB
/
csv-export.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# ---------------------------------------------------------------------
# Export model to CSV
# ---------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import sys
import argparse
# Third-party modules
from django.apps import apps
# NOC modules
from noc.sa.models.managedobject import ManagedObject # noqa
from noc.core.management.base import BaseCommand
from noc.core.csvutils import csv_export
from noc.models import load_models
from noc.core.mongo.connection import connect
class Command(BaseCommand):
help = "Export model to CSV"
def add_arguments(self, parser):
parser.add_argument(
"-t",
"--template",
dest="template",
action="store_true",
default=False,
help="dump only header row",
),
parser.add_argument("args", nargs=argparse.REMAINDER, help="List of extractor names")
def _usage(self):
print("Usage:")
print("%s csv-export [-t] <model>" % (sys.argv[0]))
print("Where <model> is one of:")
load_models()
for m in apps.get_models():
t = m._meta.db_table
app, model = t.split("_", 1)
print("%s.%s" % (app, model))
sys.exit(1)
def get_queryset(self, model, args):
if not args:
return model.objects.all()
q = {}
for a in args:
if "=" in a:
k, v = a.split("=", 1)
q[k] = v
return model.objects.filter(**q)
def handle(self, *args, **options):
connect()
if len(args) < 1:
self._usage()
r = args[0].split(".")
if len(r) != 2:
self._usage()
app, model = r
load_models()
m = apps.get_model(app, model)
if not m:
return self._usage()
print(
csv_export(
m, queryset=self.get_queryset(m, args[1:]), first_row_only=options.get("template")
)
)
if __name__ == "__main__":
Command().run()