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 = [] |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 23 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 24 | for c in g.values(): |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 25 | if type(c)==type(PlCoreBase): |
| 26 | model_classes.append(c) |
| 27 | class_names.append(c.__name__) |
| 28 | |
| 29 | |
| 30 | if (output=='-dot'): |
| 31 | print "digraph plstack {"; |
| 32 | for c in model_classes: |
| 33 | fields = c._meta.fields |
| 34 | for f in fields: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 35 | if type(f)==ForeignKey and f.name.title() in class_names: |
| 36 | print '\t"%s"->"%s";'%(c.__name__,f.name.title()) |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 37 | print "}\n"; |
| 38 | elif (output=='-json'): |
| 39 | d = {} |
| 40 | for c in model_classes: |
| 41 | fields = c._meta.fields |
| 42 | for f in fields: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 43 | if type(f)==ForeignKey and f.name.title() in class_names: |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 44 | try: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 45 | d[c.__name__].append(f.name.title()) |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 46 | except KeyError: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 47 | d[c.__name__]=[f.name.title()] |
Sapan Bhatia | d742545 | 2013-09-03 11:45:15 -0400 | [diff] [blame] | 48 | print json.dumps(d,indent=4) |
| 49 | |
| 50 | |