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 |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 13 | from core.models import * |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 14 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 15 | try: |
| 16 | output = sys.args[1] |
| 17 | except: |
| 18 | output = '-json' |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 19 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 20 | g = globals() |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 21 | model_classes = [] |
| 22 | class_names = [] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 23 | for c in g.values(): |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 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: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 34 | if type(f)==ForeignKey and f.name.title() in class_names: |
| 35 | print '\t"%s"->"%s";'%(c.__name__,f.name.title()) |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 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: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 42 | if type(f)==ForeignKey and f.name.title() in class_names: |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 43 | try: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 44 | d[c.__name__].append(f.name.title()) |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 45 | except KeyError: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 46 | d[c.__name__]=[f.name.title()] |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 47 | print json.dumps(d,indent=4) |
| 48 | |
| 49 | |