# Path: django_make_app/renderers.py
# class TemplateRenderer(object):
#     def __init__(self, templates_directory, template_name, item):
#         """
#         :type templates_directory: os.path
#         :type template_name: unicode
#         :type item: dict
#         """
#         self._templates_directory = templates_directory
#         self.template_name = template_name
#         self.item = item
# 
#     def _render_from_template(self, template_name, **kwargs):
#         loader = FileSystemLoader(self._templates_directory)
#         env = Environment(loader=loader)
#         template = env.get_template(template_name)
# 
#         render = template.render(**kwargs)
# 
#         render = render.replace("[[", "{{")
#         render = render.replace("]]", "}}")
# 
#         render = render.replace("[%", "{%")
#         render = render.replace("%]", "%}")
# 
#         return render
# 
#     def render(self, context):
#         """
# 
#         :type context: dict
#         """
#         if "_model" in self.item:
#             context.update({
#                 "current_model": self.item.get('_model')
#             })
# 
#         return self._render_from_template(self.template_name, **context)
# 
# Path: django_make_app/structure.py
# def prepare_structure(structure_item, app_data, simple_folder_path=SIMPLE_ROOT):
#     models_list = app_data.get(YamlSchemaKeywords.MODELS)
#     app_name = app_data.get(YamlSchemaKeywords.APP_NAME)
#     item_type = structure_item.get(StructureKeyword.TYPE)
#     is_folder = item_type == StructureKeyword.FOLDER
#     is_file = item_type == StructureKeyword.FILE
# 
#     nodes = []
# 
#     for node_obj in structure_item.get(StructureKeyword.ITEMS, []):
#         if is_callable(node_obj):
#             nodes = nodes + [generated_node for generated_node in node_obj(models_list)]
#         else:
#             nodes.append(node_obj)
# 
#     new_item_name = structure_item.get(StructureKeyword.NAME).replace('__app__', app_name)
#     next_item_simple_path = get_next_item_name(is_folder, new_item_name, app_name, simple_folder_path)
# 
#     return_dict = dict(structure_item)
#     return_dict.update({
#         StructureKeyword.NAME: new_item_name
#     })
# 
#     if StructureKeyword.TEMPLATE_NAME not in return_dict and is_file:
#         return_dict.update({
#             StructureKeyword.TEMPLATE_NAME: "{}{}.jinja2.html".format(next_item_simple_path, new_item_name)
#         })
# 
#     if nodes:
#         # Prevent empty [] for better readability
#         return_dict.update({
#             StructureKeyword.ITEMS: [prepare_structure(node, app_data, next_item_simple_path) for node in nodes]
#         })
# 
#     return return_dict
# 
# SIMPLE_ROOT = ""
# 
# def get_next_item_name(is_folder, item_name, app_name, current_root):
#     """
#     :type is_folder: bool
#     :type item_name: unicode
#     :type app_name: unicode
#     :type current_root: unicode
#     :rtype: unicode
#     """
#     if not is_folder:
#         return current_root
# 
#     if item_name == app_name and current_root == SIMPLE_ROOT:
#         # app directory only in root is root
#         return SIMPLE_ROOT
# 
#     return "{}{}/".format(current_root, item_name)
# 
# def generate_model_items(model_list):
#     for model in model_list:
#         yield {
#             "type": "file",
#             "name": "test_{}.py".format(model.get('name').lower()),
#             "template_name": "tests/model_test.jinja2.html",
#             "renderer": TemplateRenderer,
#             "_model": model
#         }
# 
# def generate_template_items(model_list):
#     operations = ["detail", "form", "list", "delete"]
# 
#     for model in model_list:
#         for operation in operations:
#             yield {
#                 "type": "file",
#                 "name": "{}_{}.html".format(model.get('name').lower(), operation),
#                 "template_name": "templates/{}.jinja2.html".format(operation),
#                 "renderer": TemplateRenderer,
#                 "_model": model
#             }


from unittest import TestCase
from django_make_app.renderers import TemplateRenderer
from django_make_app.structure import prepare_structure, SIMPLE_ROOT, get_next_item_name, generate_model_items, generate_template_items
                        {
                            "type": "file",
                            "name": "__init__.py",
                            "template_name": "tests/__init__.py.jinja2.html",
                            "renderer": DummyTemplateRenderer
                        }
                    ]
                }
            ]
        }

        in_app_schema = {
            "name": "web_app",
            "models": [
                {"name": "Library"},
                {"name": "Book"}
            ]
        }

        self.assertDictEqual(expected_structure, prepare_structure(in_structure, in_app_schema))

    def test_generate_model_items(self):
        model_list = [
            {
                "name": "Book"
            }, {
                "name": "Library"
            }
        ]


# Path: django_make_app/renderers.py
# class TemplateRenderer(object):
#     def __init__(self, templates_directory, template_name, item):
#         """
#         :type templates_directory: os.path
#         :type template_name: unicode
#         :type item: dict
#         """
#         self._templates_directory = templates_directory
#         self.template_name = template_name
#         self.item = item
# 
#     def _render_from_template(self, template_name, **kwargs):
#         loader = FileSystemLoader(self._templates_directory)
#         env = Environment(loader=loader)
#         template = env.get_template(template_name)
# 
#         render = template.render(**kwargs)
# 
#         render = render.replace("[[", "{{")
#         render = render.replace("]]", "}}")
# 
#         render = render.replace("[%", "{%")
#         render = render.replace("%]", "%}")
# 
#         return render
# 
#     def render(self, context):
#         """
# 
#         :type context: dict
#         """
#         if "_model" in self.item:
#             context.update({
#                 "current_model": self.item.get('_model')
#             })
# 
#         return self._render_from_template(self.template_name, **context)
# 
# Path: django_make_app/structure.py
# def prepare_structure(structure_item, app_data, simple_folder_path=SIMPLE_ROOT):
#     models_list = app_data.get(YamlSchemaKeywords.MODELS)
#     app_name = app_data.get(YamlSchemaKeywords.APP_NAME)
#     item_type = structure_item.get(StructureKeyword.TYPE)
#     is_folder = item_type == StructureKeyword.FOLDER
#     is_file = item_type == StructureKeyword.FILE
# 
#     nodes = []
# 
#     for node_obj in structure_item.get(StructureKeyword.ITEMS, []):
#         if is_callable(node_obj):
#             nodes = nodes + [generated_node for generated_node in node_obj(models_list)]
#         else:
#             nodes.append(node_obj)
# 
#     new_item_name = structure_item.get(StructureKeyword.NAME).replace('__app__', app_name)
#     next_item_simple_path = get_next_item_name(is_folder, new_item_name, app_name, simple_folder_path)
# 
#     return_dict = dict(structure_item)
#     return_dict.update({
#         StructureKeyword.NAME: new_item_name
#     })
# 
#     if StructureKeyword.TEMPLATE_NAME not in return_dict and is_file:
#         return_dict.update({
#             StructureKeyword.TEMPLATE_NAME: "{}{}.jinja2.html".format(next_item_simple_path, new_item_name)
#         })
# 
#     if nodes:
#         # Prevent empty [] for better readability
#         return_dict.update({
#             StructureKeyword.ITEMS: [prepare_structure(node, app_data, next_item_simple_path) for node in nodes]
#         })
# 
#     return return_dict
# 
# SIMPLE_ROOT = ""
# 
# def get_next_item_name(is_folder, item_name, app_name, current_root):
#     """
#     :type is_folder: bool
#     :type item_name: unicode
#     :type app_name: unicode
#     :type current_root: unicode
#     :rtype: unicode
#     """
#     if not is_folder:
#         return current_root
# 
#     if item_name == app_name and current_root == SIMPLE_ROOT:
#         # app directory only in root is root
#         return SIMPLE_ROOT
# 
#     return "{}{}/".format(current_root, item_name)
# 
# def generate_model_items(model_list):
#     for model in model_list:
#         yield {
#             "type": "file",
#             "name": "test_{}.py".format(model.get('name').lower()),
#             "template_name": "tests/model_test.jinja2.html",
#             "renderer": TemplateRenderer,
#             "_model": model
#         }
# 
# def generate_template_items(model_list):
#     operations = ["detail", "form", "list", "delete"]
# 
#     for model in model_list:
#         for operation in operations:
#             yield {
#                 "type": "file",
#                 "name": "{}_{}.html".format(model.get('name').lower(), operation),
#                 "template_name": "templates/{}.jinja2.html".format(operation),
#                 "renderer": TemplateRenderer,
#                 "_model": model
#             }


# Path: tests/test_structure.py
from unittest import TestCase
from django_make_app.renderers import TemplateRenderer
from django_make_app.structure import prepare_structure, SIMPLE_ROOT, get_next_item_name, generate_model_items, generate_template_items

                        {
                            "type": "file",
                            "name": "__init__.py",
                            "template_name": "tests/__init__.py.jinja2.html",
                            "renderer": DummyTemplateRenderer
                        }
                    ]
                }
            ]
        }

        in_app_schema = {
            "name": "web_app",
            "models": [
                {"name": "Library"},
                {"name": "Book"}
            ]
        }

        self.assertDictEqual(expected_structure, prepare_structure(in_structure, in_app_schema))

    def test_generate_model_items(self):
        model_list = [
            {
                "name": "Book"
            }, {
                "name": "Library"
            }
        ]


        expected = [{"type": "file", "name": "test_book.py", "template_name": "tests/model_test.jinja2.html", "renderer": TemplateRenderer, "_model": {"name": "Book"}},