generate_all.py 1.86 KB
Newer Older
1
2
3
4
#####
# This python file is used to generate js files for redux
from django import template
import yaml
5
from os.path import join, realpath, dirname
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

############
# Need to do this first so that Django template engine is working
import django
from django.conf import settings

settings.configure(TEMPLATES=[
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['.'],  # if you want the templates from a file
        'APP_DIRS': False,  # we have no apps
    },
])
django.setup()
##########


def read_file(file):
    with open(file, "r") as myfile:
        return myfile.read()


def write_file(file, data):
    with open(file, 'w') as the_file:
        the_file.write(data)


33
34
35
36
37
def render_and_save(template_path, context, output_path):
    t = template.Template(read_file(template_path))
    c = template.Context({'data': context})
    output = t.render(c)
    write_file(output_path, output)
38
39


40
41
42
current_dir = dirname(realpath(__file__))
templates_dir = current_dir + '/templates/'
saving_dir = realpath(current_dir + "/../")
43
44


45
# API_BASE = "http://127.0.0.1:8000/api/"
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
api_config = yaml.load(read_file(join(current_dir, 'api_config.yml')))

# Render urls.py
template_path = join(templates_dir, 'urls.tpl')
output_path = join(saving_dir, 'urls.py')
render_and_save(template_path, api_config, output_path)

# Render admin.py
data = []
for obj in api_config:
    if 'model' in obj and obj['model']:
        if 'requires_testing' in obj and obj['requires_testing']:
            continue  # we don't want testing models to be register in admin
        if 'ignore_in_admin' in obj and obj['ignore_in_admin']:
            continue 
        if 'versionned' not in obj:
            obj['versionned'] = False
        data.append(obj)

template_path = join(templates_dir, 'admin.tpl')
output_path = join(saving_dir, 'admin.py')
render_and_save(template_path, data, output_path)