##### # This python file is used to generate some backend files from django import template from os.path import join, realpath, dirname from general.api import get_api_config ############ # 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 render_and_save(template_path, context, output_path): with open(template_path, 'r') as f: t = f.read() t = template.Template(t) c = template.Context({'data': context}) output = t.render(c) with open(output_path, 'w') as f: f.write(output) current_dir = dirname(realpath(__file__)) templates_dir = current_dir + '/templates/' saving_dir = realpath(current_dir + "/../") api_config = get_api_config() # 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 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) # Render admin.py data = [] for obj in api_config: if 'model' in obj and obj['model']: if obj['requires_testing']: continue # we don't want testing models to be register in admin if obj['ignore_in_admin']: continue 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)