# Path: django_test_tools/app_manager.py
# class DjangoAppManager(object):
#     def __init__(self):
#         self.installed_apps = dict(self.get_installed_apps())
# 
#     def get_app(self, app_name):
#         return self.installed_apps.get(app_name)
# 
#     def get_installed_apps(self):
#         for app_config in apps.get_app_configs():
#             yield app_config.name, app_config
# 
#     def get_model(self, app_name, model_name):
#         app = self.get_app(app_name)
#         for model in app.get_models():
#             if model_name == model.__name__:
#                 return model
#         return None
# 
#     def get_project_apps(self, project_name):
#         project_apps = dict()
#         for app_name, app_config in self.installed_apps.items():
#             app_project = app_name.split('.')[0]
#             if app_project == project_name:
#                 project_apps[app_name] = app_config
#         return project_apps
# 
#     def get_app_data(self, app_name):
#         """
#         Read application data converts into a dictionary
# 
#         :param app_name: Application name
#         :return: Dictionary with application data
#         """
#         app = self.get_app(app_name)
#         app_dict = dict()
#         app_dict['app_name'] = app.name
#         app_dict['models'] = dict()
#         for key, model in app.models.items():
#             app_dict['models'][key] = dict()
#             app_dict['models'][key]['model_name'] = model.__name__
#             app_dict['models'][key]['original_attrs'] = model._meta.original_attrs
#             app_dict['models'][key]['fields'] = list()
#             for field in model._meta.fields:
#                 self._convert_field_to_dict(field, app_dict['models'][key]['fields'])
# 
#         return app_dict
# 
#     def _convert_field_to_dict(self, field, field_list):
#         field_dict = dict()
#         field_dict['field_name'] = field.name
#         field_dict['type'] = type(field).__name__
#         field_dict['unique'] = field.unique
#         field_dict['primary_key'] = field.primary_key
#         field_dict['editable'] = field.editable
#         if hasattr(field, 'choices') and field.choices is not None:
#             field_dict['choices_type'] = type(field.choices).__name__
#             if field_dict['choices_type'] == 'tuple':
#                 field_dict['choices'] = field.choices
#         if hasattr(field, 'max_length') and field.max_length is not None:
#             field_dict['max_length'] = field.max_length
#         if hasattr(field, 'max_digits') and field.max_digits is not None:
#             field_dict['max_digits'] = field.max_digits
#         if hasattr(field, 'decimal_places') and field.decimal_places is not None:
#             field_dict['decimal_places'] = field.decimal_places
#         if hasattr(field, 'remote_field') and field.remote_field is not None:
#             field_dict['remote_field'] = field.remote_field.model.__name__
#         field_list.append(field_dict)
#         return field_dict


from django.utils import timezone
from django_test_tools import __version__ as current_version
from ..app_manager import DjangoAppManager
    def get_genericipaddressfield_factory(self, *args, **kwargs):
        config = {'network': False}
        template = 'LazyAttribute(lambda o: faker.ipv4(network={network}))'
        return template.format(**config)

    def get_integerfield_factory(self, *args, **kwargs):
        config = {'min_value': kwargs.get('min_value', 3),
                  'max_value': kwargs.get('max_value', 3), }
        template = 'LazyAttribute(lambda o: randint({min_value}, {max_value}))'
        return template.format(**config)

    def get_moneyfield_factory(self, *args, **kwargs):
        factory = self.get_decimalfield_factory(*args, **kwargs)
        return factory

    def get_textfield_factory(self, *args, **kwargs):
        config = {'sentences': kwargs.get('sentences', 3)}
        template = 'LazyAttribute(lambda x: faker.paragraph(nb_sentences={sentences}, variable_nb_sentences=True))'
        return template.format(**config)

    def _is_number(self, field_name):
        for nv in self.numerical_identifiers:
            if nv in field_name.lower():
                return True
        return False


class ModelSerializerGenerator(object):

    def __init__(self, **kwargs):

# Path: django_test_tools/app_manager.py
# class DjangoAppManager(object):
#     def __init__(self):
#         self.installed_apps = dict(self.get_installed_apps())
# 
#     def get_app(self, app_name):
#         return self.installed_apps.get(app_name)
# 
#     def get_installed_apps(self):
#         for app_config in apps.get_app_configs():
#             yield app_config.name, app_config
# 
#     def get_model(self, app_name, model_name):
#         app = self.get_app(app_name)
#         for model in app.get_models():
#             if model_name == model.__name__:
#                 return model
#         return None
# 
#     def get_project_apps(self, project_name):
#         project_apps = dict()
#         for app_name, app_config in self.installed_apps.items():
#             app_project = app_name.split('.')[0]
#             if app_project == project_name:
#                 project_apps[app_name] = app_config
#         return project_apps
# 
#     def get_app_data(self, app_name):
#         """
#         Read application data converts into a dictionary
# 
#         :param app_name: Application name
#         :return: Dictionary with application data
#         """
#         app = self.get_app(app_name)
#         app_dict = dict()
#         app_dict['app_name'] = app.name
#         app_dict['models'] = dict()
#         for key, model in app.models.items():
#             app_dict['models'][key] = dict()
#             app_dict['models'][key]['model_name'] = model.__name__
#             app_dict['models'][key]['original_attrs'] = model._meta.original_attrs
#             app_dict['models'][key]['fields'] = list()
#             for field in model._meta.fields:
#                 self._convert_field_to_dict(field, app_dict['models'][key]['fields'])
# 
#         return app_dict
# 
#     def _convert_field_to_dict(self, field, field_list):
#         field_dict = dict()
#         field_dict['field_name'] = field.name
#         field_dict['type'] = type(field).__name__
#         field_dict['unique'] = field.unique
#         field_dict['primary_key'] = field.primary_key
#         field_dict['editable'] = field.editable
#         if hasattr(field, 'choices') and field.choices is not None:
#             field_dict['choices_type'] = type(field.choices).__name__
#             if field_dict['choices_type'] == 'tuple':
#                 field_dict['choices'] = field.choices
#         if hasattr(field, 'max_length') and field.max_length is not None:
#             field_dict['max_length'] = field.max_length
#         if hasattr(field, 'max_digits') and field.max_digits is not None:
#             field_dict['max_digits'] = field.max_digits
#         if hasattr(field, 'decimal_places') and field.decimal_places is not None:
#             field_dict['decimal_places'] = field.decimal_places
#         if hasattr(field, 'remote_field') and field.remote_field is not None:
#             field_dict['remote_field'] = field.remote_field.model.__name__
#         field_list.append(field_dict)
#         return field_dict


# Path: django_test_tools/generators/model_generator.py
from django.utils import timezone
from django_test_tools import __version__ as current_version
from ..app_manager import DjangoAppManager

    def get_genericipaddressfield_factory(self, *args, **kwargs):
        config = {'network': False}
        template = 'LazyAttribute(lambda o: faker.ipv4(network={network}))'
        return template.format(**config)

    def get_integerfield_factory(self, *args, **kwargs):
        config = {'min_value': kwargs.get('min_value', 3),
                  'max_value': kwargs.get('max_value', 3), }
        template = 'LazyAttribute(lambda o: randint({min_value}, {max_value}))'
        return template.format(**config)

    def get_moneyfield_factory(self, *args, **kwargs):
        factory = self.get_decimalfield_factory(*args, **kwargs)
        return factory

    def get_textfield_factory(self, *args, **kwargs):
        config = {'sentences': kwargs.get('sentences', 3)}
        template = 'LazyAttribute(lambda x: faker.paragraph(nb_sentences={sentences}, variable_nb_sentences=True))'
        return template.format(**config)

    def _is_number(self, field_name):
        for nv in self.numerical_identifiers:
            if nv in field_name.lower():
                return True
        return False


class ModelSerializerGenerator(object):

    def __init__(self, **kwargs):

        self.app_manager = DjangoAppManager()