Beispiel #1
0
    def post(self, request, *args, **kwargs):
        if self.keyword_form.is_valid():
            with transaction.atomic():
                self.keyword.keyword = self.keyword_form.cleaned_data['keyword']
                self.keyword.description = self.keyword_form.cleaned_data['description']
                self.keyword.delimiter = self.keyword_form.cleaned_data['delimiter']
                self.keyword.override_open_sessions = self.keyword_form.cleaned_data['override_open_sessions']

                self.keyword.initiator_doc_type_filter = []
                if self.keyword_form.cleaned_data['allow_keyword_use_by'] == 'users':
                    self.keyword.initiator_doc_type_filter.append('CommCareUser')
                if self.keyword_form.cleaned_data['allow_keyword_use_by'] == 'cases':
                    self.keyword.initiator_doc_type_filter.append('CommCareCase')

                self.keyword.save()

                self.keyword.keywordaction_set.all().delete()
                if self.keyword_form.cleaned_data['sender_content_type'] != NO_RESPONSE:
                    app_id, form_unique_id = split_combined_id(
                        self.keyword_form.cleaned_data['sender_app_and_form_unique_id']
                    )
                    self.keyword.keywordaction_set.create(
                        recipient=KeywordAction.RECIPIENT_SENDER,
                        action=self.keyword_form.cleaned_data['sender_content_type'],
                        message_content=self.keyword_form.cleaned_data['sender_message'],
                        app_id=app_id,
                        form_unique_id=form_unique_id,
                    )
                if self.process_structured_message:
                    app_id, form_unique_id = split_combined_id(
                        self.keyword_form.cleaned_data['structured_sms_app_and_form_unique_id']
                    )
                    self.keyword.keywordaction_set.create(
                        recipient=KeywordAction.RECIPIENT_SENDER,
                        action=KeywordAction.ACTION_STRUCTURED_SMS,
                        app_id=app_id,
                        form_unique_id=form_unique_id,
                        use_named_args=self.keyword_form.cleaned_data['use_named_args'],
                        named_args=self.keyword_form.cleaned_data['named_args'],
                        named_args_separator=self.keyword_form.cleaned_data['named_args_separator'],
                    )
                if self.keyword_form.cleaned_data['other_recipient_content_type'] != NO_RESPONSE:
                    app_id, form_unique_id = split_combined_id(
                        self.keyword_form.cleaned_data['other_recipient_app_and_form_unique_id']
                    )
                    self.keyword.keywordaction_set.create(
                        recipient=self.keyword_form.cleaned_data['other_recipient_type'],
                        recipient_id=self.keyword_form.cleaned_data['other_recipient_id'],
                        action=self.keyword_form.cleaned_data['other_recipient_content_type'],
                        message_content=self.keyword_form.cleaned_data['other_recipient_message'],
                        app_id=app_id,
                        form_unique_id=form_unique_id,
                    )

                return HttpResponseRedirect(reverse(KeywordsListView.urlname, args=[self.domain]))
        return self.get(request, *args, **kwargs)
Beispiel #2
0
def validate_app_and_form_unique_id(app_and_form_unique_id, domain):
    error_msg = _('Invalid form chosen.')
    try:
        app_id, form_unique_id = split_combined_id(app_and_form_unique_id)
        app = get_app(domain, app_id)
    except Exception:
        raise ValidationError(error_msg)

    if app.domain != domain:
        raise ValidationError(error_msg)

    return app_and_form_unique_id