<commit_before>from jinja2 import Environment, PackageLoader
import os
from skaffolder import Skaffolder


class FlaskSkaffolder(Skaffolder):
    # TODO
    pass


class DjangoSkaffolder(Skaffolder):

    def __init__(self, fixtures):

        self.data = []
        self.skeleton_root = 'django'
        self.fixtures = fixtures
        self.use_admin = self.fixtures['config']['use_admin']
        self.app_name = self.fixtures['config']['app_name'].lower()
        self.project_root = self.fixtures['config']['project_root'].lower()
        self.models = self.fixtures['models']
        self.templates = {
            'root': '',
            'layouts': '',
            'partials': '',
            'pages': '',
        }

        curr_dir = os.path.dirname(__file__)
        proj_dir = '{}/{}/{}'.format(
            self.project_root, self.project_root, self.app_name)
        self.app_root = os.path.abspath(os.path.join(curr_dir, proj_dir))

        # Setup template structure
        self.make_app_dirs()
        # Must set custom strings, since we want to keep some of the
        # jinja/django style syntax intact in some outputs (e.g. templates)
        self.env = Environment(
            loader=PackageLoader(self.skeleton_root, ''),
            trim_blocks=True,
            lstrip_blocks=True,
            block_start_string='{%%',
            block_end_string='%%}',
            variable_start_string='{{{',
            variable_end_string='}}}',)
        # Add custom filters to Jinja's context
        self.env.filters['pluralize'] = self.get_plural_inflection
        self.env.filters['singularize'] = self.get_singular_inflection
        self.env.filters['model_field'] = self.get_model_field_type
        self.env.filters['factory_field'] = self.get_modelfactory_field_type

    def get_model_field_type(self, prop):
        """Given a prop, returns the closest django model field type."""
        if type(prop) == bool:
            return 'models.NullBooleanField()'
        elif type(prop) == int:
            return 'models.IntegerField()'
        elif type(prop) == float:
            return 'models.FloatField()'
        elif type(prop) == str or type(prop) == unicode:
            return 'models.CharField(max_length=50)'

    def get_templates(self, filetype='.html'):
        return [template for template
                in self.env.list_templates() if template.endswith(filetype)]

    def generate_thing(self, thing, **kwargs):
        return self.env.get_template(thing).render(**kwargs)

    def generate_admin(self):
        return self.generate_thing('admin.py', all_models=self.models)

    def generate_models(self):
        return self.generate_thing('models.py', all_models=self.models)

    def generate_views(self):
        return self.generate_thing(<commit_msg>Default fields to blank in model generator<commit_after>from jinja2 import Environment, PackageLoader
import os
from skaffolder import Skaffolder


class FlaskSkaffolder(Skaffolder):
    # TODO
    pass


class DjangoSkaffolder(Skaffolder):

    def __init__(self, fixtures):

        self.data = []
        self.skeleton_root = 'django'
        self.fixtures = fixtures
        self.use_admin = self.fixtures['config']['use_admin']
        self.app_name = self.fixtures['config']['app_name'].lower()
        self.project_root = self.fixtures['config']['project_root'].lower()
        self.models = self.fixtures['models']
        self.templates = {
            'root': '',
            'layouts': '',
            'partials': '',
            'pages': '',
        }

        curr_dir = os.path.dirname(__file__)
        proj_dir = '{}/{}/{}'.format(
            self.project_root, self.project_root, self.app_name)
        self.app_root = os.path.abspath(os.path.join(curr_dir, proj_dir))

        # Setup template structure
        self.make_app_dirs()
        # Must set custom strings, since we want to keep some of the
        # jinja/django style syntax intact in some outputs (e.g. templates)
        self.env = Environment(
            loader=PackageLoader(self.skeleton_root, ''),
            trim_blocks=True,
            lstrip_blocks=True,
            block_start_string='{%%',
            block_end_string='%%}',
            variable_start_string='{{{',
            variable_end_string='}}}',)
        # Add custom filters to Jinja's context
        self.env.filters['pluralize'] = self.get_plural_inflection
        self.env.filters['singularize'] = self.get_singular_inflection
        self.env.filters['model_field'] = self.get_model_field_type
        self.env.filters['factory_field'] = self.get_modelfactory_field_type

    def get_model_field_type(self, prop):
        """Given a prop, returns the closest django model field type."""
        if type(prop) == bool:
            return 'models.NullBooleanField()'
        elif type(prop) == int:
            return 'models.IntegerField()'
        elif type(prop) == float:
            return 'models.FloatField()'
        elif type(prop) == str or type(prop) == unicode:
            return 'models.CharField(blank=True, max_length=50)'

    def get_templates(self, filetype='.html'):
        return [template for template
                in self.env.list_templates() if template.endswith(filetype)]

    def generate_thing(self, thing, **kwargs):
        return self.env.get_template(thing).render(**kwargs)

    def generate_admin(self):
        return self.generate_thing('admin.py', all_models=self.models)

    def generate_models(self):
        return self.generate_thing('models.py', all_models=self.models)

    def generate_views(self):
        return self.generate_thing(
{'fraction_non_alphanumeric': 0.12179146751637458, 'fraction_numerical': 0.0010621348911311736, 'mean_word_length': 2.6311053984575836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '454189', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1605, 'n_words': 332}