Exemplo n.º 1
0
    def get_context_data(self, **kwargs):
        """Update context with Intents list and Entities formsets"""

        context = super(IntentsEditView, self).get_context_data(**kwargs)

        # Get entities
        entities = get_entities_list(self.request.session.get('token', False),
                                     self.kwargs['aiid']).get('entities')

        # Custom entities goes first, sort alphabetically
        entities.sort(
            key=lambda entity: (entity['is_system'], entity['entity_name']))

        intents = [
            intent['intent_name'] for intent in get_intent_list(
                self.request.session.get('token', False),
                self.kwargs['aiid']).get('intents', [])
        ]

        # And pass it to formsets initial choice
        context['formsets'] = {
            'conditions_in':
            kwargs.get(
                'conditions_in',
                self.get_formset(
                    prefix='CONDITIONS_IN',
                    initial=self.initial.get('conditions_in', []),
                )),
            'entities':
            kwargs.get(
                'entities',
                self.get_formset(
                    prefix='ENTITIES',
                    initial=self.initial.get('variables', []),
                    form_kwargs={'entities': entities},
                )),
            'context_in':
            kwargs.get(
                'context_in',
                self.get_formset(prefix='CONTEXT_IN',
                                 initial=self.initial.get('context_in', []))),
            'context_out':
            kwargs.get(
                'context_out',
                self.get_formset(prefix='CONTEXT_OUT',
                                 initial=self.initial.get('context_out', []))),
            'conditions_out':
            kwargs.get(
                'conditions_out',
                self.get_formset(
                    prefix='CONDITIONS_OUT',
                    initial=self.initial.get('conditions_out', []),
                    form_kwargs={
                        'intents': intents,
                        'current_intent': self.initial
                    },
                ))
        }

        return context
Exemplo n.º 2
0
    def get_context_data(self, **kwargs):
        """Update context with Entities list"""

        context = super(EntitiesView, self).get_context_data(**kwargs)

        # Get entities
        context['entities'] = get_entities_list(
            self.request.session.get('token', False),
            self.kwargs.get('aiid')).get('entities')

        context['allow_regex'] = get_experiments_list(
            self.request.session.get('token', False),
            self.kwargs.get('aiid', False), 'regex-entity').get('state')

        return context
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        """Custom post for handling both Intent form and Entities entities_formset"""

        # Get entities
        entities = get_entities_list(self.request.session.get('token', False),
                                     self.kwargs.get('aiid')).get('entities')

        intents = [
            intent['intent_name'] for intent in get_intent_list(
                self.request.session.get('token', False),
                self.kwargs.get('aiid')).get('intents')
        ]

        form = self.get_form()

        formsets = {
            'conditions_in':
            self.get_formset(prefix='CONDITIONS_IN'),
            'entities':
            self.get_formset(prefix='ENTITIES',
                             form_kwargs={'entities': entities}),
            'context_in':
            self.get_formset(prefix='CONTEXT_IN'),
            'context_out':
            self.get_formset(prefix='CONTEXT_OUT'),
            'conditions_out':
            self.get_formset(prefix='CONDITIONS_OUT',
                             form_kwargs={
                                 'intents': intents,
                                 'current_intent': self.initial
                             })
        }

        if form.is_valid() and all(formset.is_valid()
                                   for key, formset in formsets.items()):
            return self.form_valid(form, formsets)
        else:
            return self.form_invalid(form, formsets)