예제 #1
0
    def from_template(cls, view):
        model = ''
        # workaround for state variance. No default value is set for object
        # django/views/generic/detail.py:144
        # django 1.11
        if hasattr(view, 'model'):
            view.object = None
            view.object_list = view.model.objects.all()
            model = view.model.__name__

        app = get_app_name(view)
        candidates = view.get_template_names()

        use_generic_html = True
        try:
            template_path = loader.select_template(candidates).template.name
            use_generic_html = False
            log('Creating component from template:')
        except TemplateDoesNotExist:
            log('No Template found. Blindly creating component from first '
                'template candidate')
            template_path = candidates[0]

        name = convert_file_to_component_name(template_path)
        component = ModuleComponent('generic', app, name, renderer=CBVRenderer)

        model and component.add_context({'model': model})

        if not use_generic_html:
            component.renderer.html_template = template_path

        return component
예제 #2
0
    def from_cbv(cls, view: View):
        if not isinstance(view, TemplateResponseMixin):
            log("CBV's must be of type TemplateResponseMixin.")
            log(f"Nothing created for:{TemplateResponseMixin}")
            return None, None

        log(f'Creating from CBV: {view.__module__}.{view.__class__}')
        form = None
        if isinstance(view, ModelFormMixin):
            form_class = view.get_form_class()
            form = cls.from_form(form_class)

        model = ''

        # workaround for state variance. No default value is set for object
        # django/views/generic/detail.py:144
        # django 1.11
        if hasattr(view, 'model'):
            view.object = None
            view.object_list = view.model.objects.all()
            model = view.model.__name__

        app = get_app_name(view)
        name = view.__class__.__name__ + 'Component'

        if isinstance(view, generic.CreateView):
            action = 'create'
        elif isinstance(view, generic.DeleteView):
            action = 'destroy'
        elif isinstance(view, generic.ListView):
            action = 'list'
        elif isinstance(view, generic.UpdateView):
            action = 'update'
        elif isinstance(view, generic.DetailView):
            action = 'retrieve'
        else:
            log(f'Using generic template for {view}')
            action = 'generic'

        component = ModuleComponent(action, app, name, renderer=CBVRenderer)

        model and component.add_context({'model': model})
        form and component.add_context({'component': form})

        candidates = view.get_template_names()

        try:
            template_path = loader.select_template(candidates).template.name
            component.renderer.html_template = template_path
            log('Using HTML template: ' + template_path)
        except TemplateDoesNotExist:
            log('Using generic template for action: ' + action)

        component.add_context({
            'context_obj_name':
            view.get_context_object_name(None) or 'object'
        })

        return component, form
예제 #3
0
    def from_form(cls, form_class):
        log('Creating form_class component')
        form_class.as_vue = as_vue
        name = form_class.__name__
        model = form_class._meta.model.__name__
        app = get_app_name(form_class)

        component = ModuleComponent('component', app, name)
        component.add_context({'model': model, 'form': form_class()})
        return component
예제 #4
0
    def from_viewset(cls, viewset):
        name = viewset.cls.__name__ + viewset.suffix
        app = get_app_name(viewset)

        components = [
            ComponentFactory.from_junk(viewset, method, action)[0]
            for method, action in viewset.actions.items()
        ]

        return View(components, app, name)
예제 #5
0
    def from_model_and_fields(cls, model: Model, fields):
        props = []
        for name, field in fields.items():
            f = model._meta.get_field(name)
            validators = [v.__class__.__name__ for v in f.validators]
            f.validator_names = validators
            props.append(f)

        app = get_app_name(model)

        return Store(app, model.__name__, props)
예제 #6
0
    def from_serializer(cls, serializer):
        model = serializer.Meta.model
        fields = serializer.Meta.fields
        form_class = modelform_factory(model, fields=fields)
        app = get_app_name(serializer)

        form_class.as_vue = as_vue
        name = serializer.__name__

        form = ModuleComponent('component', app, name)
        form.add_context({'form': form_class(), 'model': model.__name__})

        return form
예제 #7
0
    def from_callback(cls, callback):
        if hasattr(callback, 'actions'):
            return cls.from_viewset(callback)

        components = [ComponentFactory.from_callback(callback)]
        app = get_app_name(callback)

        if components in [[None], []]:
            log(f'No component generated for {callback.__name__}')
            return
        if hasattr(callback, 'view_class'):
            name = callback.view_class.__name__
        else:
            name = convert_to_camelcase(callback.__name__)

        return View(components, app, name)
예제 #8
0
    def from_junk(cls, callback, method, action):
        app = get_app_name(callback)
        action_cls = convert_to_pascalcase(action)
        model = callback.cls.__name__
        name = model + action_cls + 'Component'

        form_methods = ['post', 'put', 'patch']
        serializer = callback.cls.serializer_class

        if method in form_methods:
            form = cls.from_serializer(serializer)
        else:
            form = None

        model = serializer.Meta.model.__name__
        comp = ModuleComponent(action, app, name)
        comp.add_context({'model': model, 'component': form})

        return comp, form
예제 #9
0
    def from_callback(cls, callback):
        log("Creating from callback")
        if callback is None:
            log('Something strange happened. NoneType passed as callback. '
                'GTFO!')
            return

        if not hasattr(callback, 'view_class'):
            name = callback.__name__
            app = get_app_name(callback)

            log(f'Creating Anonymous component for functional view: {name}')
            return ModuleComponent('generic', app, name)

        view = callback.view_class()

        if isinstance(view, APIView):
            return cls.from_drf_view(view)

        return cls.from_cbv(view)
예제 #10
0
    def from_view(cls, view):
        name = view.__name__
        app = get_app_name(view)
        components = [ComponentFactory.from_cbv(view)]

        return View(components, app, name)