generate_backend_files.py 1.79 KB
Newer Older
1
#####
Florent Chehab's avatar
Florent Chehab committed
2
# This python file is used to generate some backend files
3
from django import template
4
from os.path import join, realpath, dirname
5
from general.api import get_api_config
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

############
# 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()
##########


23
def render_and_save(template_path, context, output_path):
Florent Chehab's avatar
Florent Chehab committed
24
25
26
    with open(template_path, 'r') as f:
        t = f.read()
    t = template.Template(t)
27
28
    c = template.Context({'data': context})
    output = t.render(c)
Florent Chehab's avatar
Florent Chehab committed
29
30
31

    with open(output_path, 'w') as f:
        f.write(output)
32
33


34
35
36
current_dir = dirname(realpath(__file__))
templates_dir = current_dir + '/templates/'
saving_dir = realpath(current_dir + "/../")
37
38


39
api_config = get_api_config()
40
41
42
43
44
45

# 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)

46
47
48
49
50
# render list_user_post_permission.py
template_path = join(templates_dir, 'list_user_post_permission.tpl')
output_path = join(saving_dir, './permissions/__list_user_post_permission.py')
render_and_save(template_path, api_config, output_path)

51
52
53
54
# Render admin.py
data = []
for obj in api_config:
    if 'model' in obj and obj['model']:
Florent Chehab's avatar
Florent Chehab committed
55
        if obj['requires_testing']:
56
            continue  # we don't want testing models to be register in admin
Florent Chehab's avatar
Florent Chehab committed
57
        if obj['ignore_in_admin']:
Florent Chehab's avatar
Florent Chehab committed
58
            continue
59
60
61
62
63
        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)