blob: 683835249ca7d7b2c930e368f6f98533c5c3f5b8 [file] [log] [blame]
srikanth116e6e82014-08-19 07:22:37 -07001#
2# Copyright (c) 2013 Big Switch Networks, Inc.
3#
4# Licensed under the Eclipse Public License, Version 1.0 (the
5# "License"); you may not use this file except in compliance with the
6# License. You may obtain a copy of the License at
7#
8# http://www.eclipse.org/legal/epl-v10.html
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied. See the License for the specific language governing
14# permissions and limitations under the License.
15#
16
17#import time
18
19import datetime
20import django.core.management
21
22try:
23 import settings # Assumed to be in the same directory.
24except ImportError:
25 import sys
26 sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
27 sys.exit(1)
28
29django.core.management.setup_environ(settings)
30
31from django.db import models
32from django.db.models.fields import AutoField, BooleanField, IntegerField
33from django.db.models.fields.related import ForeignKey
34
35import sdncon.rest.views
36
37print "# Generated automatically from controller source"
38print "# via tools/extract_model.py date: %s" % datetime.datetime.now().ctime()
39
40# we iterate over rest_name_dict, limiting CLI access to those models
41# and model fields accessible via rest
42
43rest_map = {}
44for (model, model_info) in sdncon.rest.views.rest_model_info_dict.items():
45 rest_map[model] = {}
46 for (rest_field_name, rest_field_info) in model_info.rest_name_dict.items():
47 django_field_name = rest_field_info.name
48 rest_map[model][django_field_name] = rest_field_name
49
50model_info_dict = {}
51for (model, model_info) in sdncon.rest.views.rest_model_info_dict.items():
52 django_model_class = model_info.model_class
53 field_info_dict = {}
54 for (rest_field_name, rest_field_info) in model_info.rest_name_dict.items():
55 django_field_name = rest_field_info.name
56 django_field_info = rest_field_info.django_field_info
57 # now that we have the django info and our own rest info, create a field info to dump
58 json_serialize_string = type(django_field_info) not in (AutoField, BooleanField, IntegerField)
59 field_info = {}
60 field_info['json_serialize_string'] = json_serialize_string
61 if django_field_info.verbose_name != django_field_name:
62 # Check if this is a proxy class
63 if type(django_field_info.verbose_name) is str:
64 field_info['verbose-name'] = django_field_info.verbose_name
65 if django_field_info.primary_key == True:
66 field_info['primary_key'] = True
67 if django_field_info.help_text != "":
68 field_info['help_text'] = django_field_info.help_text
69 field_info['null'] = django_field_info.null
70 if type(django_field_info.default) in [int, bool, str]:
71 field_info['default'] = django_field_info.default
72 field_info['type'] = str(type(django_field_info)).split('.')[-1].replace("'>", "")
73 if field_info['type'] == 'AutoField':
74 # Re-label the cassandra compound key for easier consumption
75 if hasattr(django_model_class, 'CassandraSettings'):
76 cassandra_settings = django_model_class.CassandraSettings
77 if hasattr(cassandra_settings, 'COMPOUND_KEY_FIELDS'):
78 compound_key_fields = cassandra_settings.COMPOUND_KEY_FIELDS
79 rest_key_fields = [rest_map[model].get(x, x) for x in compound_key_fields]
80 field_info['compound_key_fields'] = rest_key_fields
81 field_info['type'] = 'compound-key'
82 field_info['help_text'] = '#|%s' % \
83 '|'.join(rest_key_fields)
84 if field_info['type'] == 'ForeignKey':
85 other_object = django_field_info.rel.to.Rest.NAME
86 if django_field_info.rel.field_name in rest_map[other_object]:
87 field_info['rel_field_name'] = \
88 rest_map[other_object][django_field_info.rel.field_name]
89 else:
90 field_info['rel_field_name'] = django_field_info.rel.field_name
91 field_info['rel_obj_type'] = django_field_info.rel.to.Rest.NAME
92 if field_info['type'] == 'CharField':
93 field_info['max_length'] = django_field_info.max_length
94 #if django_field_info.validators:
95 #field_info['validators'] = django_field_info.validators
96 #if isinstance(type, django.PositiveIntegerField):
97 #type_name = 'PositiveIntegerField'
98 #print type_name
99
100 field_info_dict[rest_field_name] = field_info
101 model_info_dict[model]={'fields':field_info_dict, 'has_rest_model': True}
102
103import pprint
104pp = pprint.PrettyPrinter(indent=2)
105print "model_info_dict = ",pp.pprint(model_info_dict)