Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import pdb |
| 5 | import sys |
| 6 | import json |
| 7 | |
| 8 | sys.path.append('.') |
| 9 | |
| 10 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 11 | |
| 12 | from django.db.models.fields.related import ForeignKey |
| 13 | from core.models import * |
| 14 | |
| 15 | try: |
| 16 | output = sys.args[1] |
| 17 | except: |
| 18 | output = '-json' |
| 19 | |
| 20 | g = globals() |
| 21 | model_classes = [] |
| 22 | class_names = [] |
| 23 | for c in g.values(): |
| 24 | if type(c)==type(PlCoreBase): |
| 25 | model_classes.append(c) |
| 26 | class_names.append(c.__name__) |
| 27 | |
| 28 | |
| 29 | if (output=='-dot'): |
| 30 | print "digraph plstack {"; |
| 31 | for c in model_classes: |
| 32 | fields = c._meta.fields |
| 33 | for f in fields: |
| 34 | if type(f)==ForeignKey and f.name.title() in class_names: |
| 35 | print '\t"%s"->"%s";'%(c.__name__,f.name.title()) |
| 36 | print "}\n"; |
| 37 | elif (output=='-json'): |
| 38 | d = {} |
| 39 | for c in model_classes: |
| 40 | fields = c._meta.fields |
| 41 | for f in fields: |
| 42 | if type(f)==ForeignKey and f.name.title() in class_names: |
| 43 | try: |
| 44 | d[c.__name__].append(f.name.title()) |
| 45 | except KeyError: |
| 46 | d[c.__name__]=[f.name.title()] |
| 47 | print json.dumps(d,indent=4) |
| 48 | |
| 49 | |