usr bin env python coding utf-8 from __future__ import division print_

 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
#!/usr/bin/env python
# coding: utf-8
from __future__ import division, print_function, unicode_literals
import glob
from os.path import join
import yaml
from django.contrib import admin
from django.db import models
try:
from django.conf import YAML_DIR
except:
YAML_DIR = '.'
def capitalize(name):
return ''.join(x.capitalize() for x in name.split('_'))
def get_field(name):
d = {
'int': models.IntegerField,
'str': models.CharField,
'date': models.DateTimeField,
}
return d[name]
def create_model(name, fields={}):
attrs = {}
capitalized = capitalize(name)
for field in fields:
field_attrs = {}
filed_type = field['type']
if filed_type == 'str':
field_attrs['max_length'] = 80
attrs[field['id']] = get_field(field['id'])(field['title'],
**field_attrs)
model = type(capitalized, (models.Model,), attrs)
admin.site.register(model)
import ipdb
ipdb.set_trace()
return capitalized
def aggr_yamls():
response = {}
for filename in glob.glob1(YAML_DIR, '*.y?ml'):
full_path = join(YAML_DIR, filename)
response.update(yaml.loaf(open(full_path, 'r')))
return response
def generate_models():
response = []
for key, value in aggr_yamls().items():
response.append(create_model(key, value))
return response