Commit 30f954e1 authored by Florent Chehab's avatar Florent Chehab

File generation cleaned

parent a97d8933
Pipeline #26856 passed with stages
in 2 minutes and 24 seconds
......@@ -5,10 +5,10 @@ install_backend:
pip install -r requirements.txt --quiet
generate_backend:
export PYTHONPATH=$$PWD ; python ./backend/generate/generate_all.py
export PYTHONPATH=$$PWD ; python ./backend/generate/generate_backend_files.py
generate_frontend:
python ./frontend/generate/generate_frontend_files.py
export PYTHONPATH=$$PWD ; python ./frontend/generate/generate_frontend_files.py
test_backend: generate_backend
pytest
......
#####
# This python file is used to generate js files for redux
# 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
......@@ -20,21 +20,15 @@ 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)
def render_and_save(template_path, context, output_path):
t = template.Template(read_file(template_path))
with open(template_path, 'r') as f:
t = f.read()
t = template.Template(t)
c = template.Context({'data': context})
output = t.render(c)
write_file(output_path, output)
with open(output_path, 'w') as f:
f.write(output)
current_dir = dirname(realpath(__file__))
......@@ -42,8 +36,6 @@ templates_dir = current_dir + '/templates/'
saving_dir = realpath(current_dir + "/../")
# API_BASE = "http://127.0.0.1:8000/api/"
api_config = get_api_config()
# Render urls.py
......@@ -55,12 +47,10 @@ render_and_save(template_path, api_config, output_path)
data = []
for obj in api_config:
if 'model' in obj and obj['model']:
if 'requires_testing' in obj and obj['requires_testing']:
if 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']:
if obj['ignore_in_admin']:
continue
if 'versionned' not in obj:
obj['versionned'] = False
data.append(obj)
template_path = join(templates_dir, 'admin.tpl')
......
{% autoescape off %}
# WARNING
# THIS FILE HAS BEEN AUTOMATICALLY GENERATED
# WITH /backend/generate/generate_all.py
# WITH /backend/generate/generate_backend_files.py
# MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
# THIS WARNING DOESN'T APPLY TO .tpl FILES...
......
{% autoescape off %}
# WARNING
# THIS FILE HAS BEEN AUTOMATICALLY GENERATED
# WITH /backend/generate/generate_all.py
# WITH /backend/generate/generate_backend_files.py
# MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
# THIS WARNING DOESN'T APPLY TO .tpl FILES...
......
#####
# This python file is used to generate js files for redux
import os
from os.path import join
from os import makedirs
from os.path import join, dirname, realpath, exists
from django import template
import re
import yaml
......@@ -23,12 +23,12 @@ django.setup()
##########
current_dir = os.path.dirname(os.path.realpath(__file__))
current_dir = dirname(realpath(__file__))
templates_dir = current_dir + '/templates/'
saving_dir = os.path.realpath(current_dir + "/../src/generated/")
saving_dir = realpath(current_dir + "/../src/generated/")
if not os.path.exists(saving_dir):
os.makedirs(saving_dir)
if not exists(saving_dir):
makedirs(saving_dir)
templates = [
'action-types',
......@@ -38,7 +38,7 @@ templates = [
]
API_BASE = "/api/"
with open(join(current_dir, '../../backend/generate/api_config.yml'), 'r') as f:
with open(join(current_dir, '../../general/api/api_config.yml'), 'r') as f:
data = f.read()
api_config = yaml.load(data)
......@@ -54,16 +54,6 @@ for api in api_config:
})
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)
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()
......@@ -72,13 +62,17 @@ def convert(name):
for c in contexts:
c['NAME'] = convert(c['name'])
# print(contexts)
for filename in templates:
t = template.Template(
read_file(os.path.join(templates_dir, filename + '.tpl')))
template_path = join(templates_dir, filename + '.tpl')
with open(template_path, "r") as f:
t = f.read()
t = template.Template(t)
c = template.Context({'data': contexts})
output = t.render(c)
write_file(os.path.join(saving_dir, filename + '.js'), output)
output_path = join(saving_dir, filename + '.js')
with open(output_path, 'w') as f:
f.write(output)
......@@ -20,7 +20,7 @@ export const {{obj.NAME}}_EL_INVALIDATED = '{{obj.NAME}}_EL_INVALIDATED';
export const {{obj.NAME}}_EL_IS_LOADING = '{{obj.NAME}}_IS_LOADING';
export const {{obj.NAME}}_EL_FETCH_DATA_SUCCESS = '{{obj.NAME}}_FETCH_DATA_SUCCESS';
{% if 'read_only' not in obj or not obj.read_only %}
{% if not obj.read_only %}
export const {{obj.NAME}}_EL_IS_SAVING = '{{obj.NAME}}_IS_POSTING';
export const {{obj.NAME}}_EL_SAVING_DATA_SUCCESS = '{{obj.NAME}}_POST_DATA_SUCCESS';
{% endif %}
......
......@@ -18,7 +18,7 @@ import {
{{obj.NAME}}_EL_IS_LOADING,
{{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
{% if 'read_only' not in obj or not obj.read_only %}
{% if not obj.read_only %}
{{obj.NAME}}_EL_IS_SAVING,
{{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
{% endif %}
......@@ -186,7 +186,7 @@ export function {{obj.name}}ElFetchData(pk) {
}
{% if 'read_only' not in obj or not obj.read_only %}
{% if not obj.read_only %}
export function {{obj.name}}ElIsSaving(bool) {
return {
......
......@@ -18,7 +18,7 @@ import {
{{obj.name}}ElIsLoading,
{{obj.name}}ElFetched,
{% if 'read_only' not in obj or not obj.read_only %}
{% if not obj.read_only %}
{{obj.name}}ElIsSaving,
{{obj.name}}ElSaved,
{% endif %}
......@@ -40,7 +40,7 @@ export const {{obj.name}}ElReducers = combineReducers({
isLoading: {{obj.name}}ElIsLoading,
fetched: {{obj.name}}ElFetched,
{% if 'read_only' not in obj or not obj.read_only %}
{% if not obj.read_only %}
isSaving: {{obj.name}}ElIsSaving,
isSaved: {{obj.name}}ElSaved,
{% endif %}
......
......@@ -16,7 +16,7 @@ import {
{{obj.NAME}}_EL_IS_LOADING,
{{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
{% if 'read_only' not in obj or not obj.read_only %}
{% if not obj.read_only %}
{{obj.NAME}}_EL_IS_SAVING,
{{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
{% endif %}
......@@ -114,7 +114,7 @@ export function {{obj.name}}ElFetched(state = { data: Object(), fetchedAt: null
}
{% if 'read_only' not in obj or not obj.read_only %}
{% if not obj.read_only %}
// Handling POST and PUT
......
......@@ -11,7 +11,9 @@ def get_api_config():
DEFAULT_SETTINGS = {
"ignore_in_admin": False,
"requires_testing": False,
"moderation_level": 2
"moderation_level": 2,
"versionned": False,
"read_only": False
}
for obj in api_config:
......
......@@ -4,9 +4,8 @@
"description": "[![build](/../badges/master/build.svg)](https://gitlab.utc.fr/chehabfl/outgoing_rex/pipelines) [![coverage](/../badges/master/coverage.svg)](https://chehabfl.gitlab.utc.fr/outgoing_rex/) [![License](https://img.shields.io/badge/License-BSD%202--Clause-green.svg)](https://opensource.org/licenses/BSD-2-Clause)",
"main": "manage.py",
"scripts": {
"gen": "python ./frontend/generate/generate_frontend_files.py",
"dev": "npm run gen && webpack --mode development ./frontend/src/index.js --output ./frontend/static/frontend/main.js",
"build": "npm run gen && webpack --mode production ./frontend/src/index.js --output ./frontend/static/frontend/main.js",
"dev": "make generate_frontend && webpack --mode development ./frontend/src/index.js --output ./frontend/static/frontend/main.js",
"build": "make generate_frontend && webpack --mode production ./frontend/src/index.js --output ./frontend/static/frontend/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment