generate_frontend_files.py 1.94 KB
Newer Older
1 2
#####
# This python file is used to generate js files for redux
Florent Chehab's avatar
Florent Chehab committed
3 4
from os import makedirs
from os.path import join, dirname, realpath, exists
5
from django import template
6
import re
7
from general.api import get_api_config
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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


Florent Chehab's avatar
Florent Chehab committed
26
current_dir = dirname(realpath(__file__))
27
templates_dir = current_dir + '/templates/'
Florent Chehab's avatar
Florent Chehab committed
28
saving_dir = realpath(current_dir + "/../src/generated/")
29

Florent Chehab's avatar
Florent Chehab committed
30 31
if not exists(saving_dir):
    makedirs(saving_dir)
32 33 34 35

templates = [
    'action-types',
    'actions',
36 37
    'reducers',
    'combinedReducers'
38 39
]

40
api_config = get_api_config()
41 42 43 44
API_BASE = "/api/"

contexts = []
for api in api_config:
45 46
    if "requires_testing" in api and api["requires_testing"]:
        continue
47 48 49
    name = api['viewset'].split('ViewSet')[0]
    name = name[0].lower() + name[1:]
    contexts.append({
50 51
        "name": api["api_end_point"],
        "api_end_point": API_BASE + api["api_end_point"] + '/',
52
    })
53

54 55 56 57 58 59
# API outside rest framework here
contexts.append({
    "name": 'serverModerationStatus',
    "api_end_point": API_BASE + 'serverModerationStatus' + '/',
})

60

61 62 63 64 65 66 67 68 69
def convert(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()


for c in contexts:
    c['NAME'] = convert(c['name'])


70
for filename in templates:
Florent Chehab's avatar
Florent Chehab committed
71 72 73 74 75
    template_path = join(templates_dir, filename + '.tpl')
    with open(template_path, "r") as f:
        t = f.read()

    t = template.Template(t)
76

77 78
    c = template.Context({'data': contexts})
    output = t.render(c)
79

Florent Chehab's avatar
Florent Chehab committed
80 81 82
    output_path = join(saving_dir, filename + '.js')
    with open(output_path, 'w') as f:
        f.write(output)