Ejemplo n.º 1
0
        def form_valid(self, form):
            from temba.msgs.tasks import export_sms_task

            user = self.request.user
            org = user.get_org()

            label_id = self.request.REQUEST.get('label', None)

            label = None
            if label_id:
                label = Label.objects.get(pk=label_id)

            host = self.request.branding['host']

            groups = form.cleaned_data['groups']
            start_date = form.cleaned_data['start_date']
            end_date = form.cleaned_data['end_date']

            export = ExportMessagesTask.objects.create(created_by=user, modified_by=user, org=org, host=host,
                                                  label=label, start_date=start_date, end_date=end_date)
            for group in groups:
                export.groups.add(group)

            export_sms_task.delay(export.pk)

            from django.contrib import messages
            if not getattr(settings, 'CELERY_ALWAYS_EAGER', False):
                messages.info(self.request, _("We are preparing your export. ") +
                                            _("We will e-mail you at %s when it is ready.") % self.request.user.username)

            else:
                export = ExportMessagesTask.objects.get(id=export.pk)
                dl_url = "file://%s/%s" % (settings.MEDIA_ROOT, export.filename)
                messages.info(self.request, _("Export complete, you can find it here: %s (production users will get an email)") % dl_url)


            try:
                messages.success(self.request, self.derive_success_message())

                if 'HTTP_X_PJAX' not in self.request.META:
                    return HttpResponseRedirect(self.get_success_url())
                else:  # pragma: no cover
                    response = self.render_to_response(self.get_context_data(form=form,
                                                                             success_url=self.get_success_url(),
                                                                             success_script=getattr(self, 'success_script', None)))
                    response['Temba-Success'] = self.get_success_url()
                    response['REDIRECT'] = self.get_success_url()
                    return response

            except IntegrityError as e:  # pragma: no cover
                message = str(e).capitalize()
                errors = self.form._errors.setdefault(forms.forms.NON_FIELD_ERRORS, forms.util.ErrorList())
                errors.append(message)
                return self.render_to_response(self.get_context_data(form=form))
Ejemplo n.º 2
0
        def form_valid(self, form):
            from temba.msgs.tasks import export_sms_task

            user = self.request.user
            org = user.get_org()

            label_id = self.request.REQUEST.get('label', None)

            label = None
            if label_id:
                label = Label.objects.get(pk=label_id)

            host = self.request.branding['host']

            groups = form.cleaned_data['groups']
            start_date = form.cleaned_data['start_date']
            end_date = form.cleaned_data['end_date']

            export = ExportMessagesTask.objects.create(created_by=user, modified_by=user, org=org, host=host,
                                                  label=label, start_date=start_date, end_date=end_date)
            for group in groups:
                export.groups.add(group)

            export_sms_task.delay(export.pk)

            from django.contrib import messages
            if not getattr(settings, 'CELERY_ALWAYS_EAGER', False):
                messages.info(self.request, _("We are preparing your export. ") +
                                            _("We will e-mail you at %s when it is ready.") % self.request.user.username)

            else:
                export = ExportMessagesTask.objects.get(id=export.pk)
                dl_url = "file://%s/%s" % (settings.MEDIA_ROOT, export.filename)
                messages.info(self.request, _("Export complete, you can find it here: %s (production users will get an email)") % dl_url)


            try:
                messages.success(self.request, self.derive_success_message())

                if 'HTTP_X_PJAX' not in self.request.META:
                    return HttpResponseRedirect(self.get_success_url())
                else:  # pragma: no cover
                    response = self.render_to_response(self.get_context_data(form=form,
                                                                             success_url=self.get_success_url(),
                                                                             success_script=getattr(self, 'success_script', None)))
                    response['Temba-Success'] = self.get_success_url()
                    response['REDIRECT'] = self.get_success_url()
                    return response

            except IntegrityError as e:  # pragma: no cover
                message = str(e).capitalize()
                errors = self.form._errors.setdefault(forms.forms.NON_FIELD_ERRORS, forms.utils.ErrorList())
                errors.append(message)
                return self.render_to_response(self.get_context_data(form=form))
Ejemplo n.º 3
0
        def form_valid(self, form):
            from temba.msgs.tasks import export_sms_task

            user = self.request.user
            org = user.get_org()

            label_id = self.request.GET.get('label', None)

            label = None
            if label_id:  # pragma: needs cover
                label = Label.label_objects.get(pk=label_id)

            groups = form.cleaned_data['groups']
            start_date = form.cleaned_data['start_date']
            end_date = form.cleaned_data['end_date']

            # is there already an export taking place?
            existing = ExportMessagesTask.objects.filter(org=org, is_finished=False,
                                                         created_on__gt=timezone.now() - timedelta(hours=24))\
                                                 .order_by('-created_on').first()

            # if there is an existing export, don't allow it
            if existing:
                messages.info(
                    self.request,
                    _("There is already an export in progress, started by %s. You must wait "
                      "for that export to complete before starting another." %
                      existing.created_by.username))

            # otherwise, off we go
            else:  # pragma: needs cover
                export = ExportMessagesTask.objects.create(
                    created_by=user,
                    modified_by=user,
                    org=org,
                    label=label,
                    start_date=start_date,
                    end_date=end_date)
                for group in groups:
                    export.groups.add(group)

                on_transaction_commit(lambda: export_sms_task.delay(export.pk))

                if not getattr(settings, 'CELERY_ALWAYS_EAGER', False):
                    messages.info(
                        self.request,
                        _("We are preparing your export. We will e-mail you at %s when "
                          "it is ready.") % self.request.user.username)

                else:
                    dl_url = reverse('assets.download',
                                     kwargs=dict(type='message_export',
                                                 pk=export.pk))
                    messages.info(
                        self.request,
                        _("Export complete, you can find it here: %s (production users "
                          "will get an email)") % dl_url)

            try:
                messages.success(self.request, self.derive_success_message())

                if 'HTTP_X_PJAX' not in self.request.META:
                    return HttpResponseRedirect(self.get_success_url())
                else:  # pragma: no cover
                    response = self.render_to_response(
                        self.get_context_data(
                            form=form,
                            success_url=self.get_success_url(),
                            success_script=getattr(self, 'success_script',
                                                   None)))
                    response['Temba-Success'] = self.get_success_url()
                    response['REDIRECT'] = self.get_success_url()
                    return response

            except IntegrityError as e:  # pragma: no cover
                message = str(e).capitalize()
                errors = self.form._errors.setdefault(
                    forms.forms.NON_FIELD_ERRORS, forms.utils.ErrorList())
                errors.append(message)
                return self.render_to_response(
                    self.get_context_data(form=form))
Ejemplo n.º 4
0
        def form_valid(self, form):
            from temba.msgs.tasks import export_sms_task

            user = self.request.user
            org = user.get_org()

            label_id = self.request.REQUEST.get('label', None)

            label = None
            if label_id:
                label = Label.label_objects.get(pk=label_id)

            host = self.request.branding['host']

            groups = form.cleaned_data['groups']
            start_date = form.cleaned_data['start_date']
            end_date = form.cleaned_data['end_date']

            # is there already an export taking place?
            existing = ExportMessagesTask.objects.filter(org=org, is_finished=False,
                                                         created_on__gt=timezone.now() - timedelta(hours=24))\
                                                 .order_by('-created_on').first()

            # if there is an existing export, don't allow it
            if existing:
                messages.info(self.request,
                              _("There is already an export in progress, started by %s. You must wait "
                                "for that export to complete before starting another." % existing.created_by.username))

            # otherwise, off we go
            else:
                export = ExportMessagesTask.objects.create(created_by=user, modified_by=user, org=org, host=host,
                                                           label=label, start_date=start_date, end_date=end_date)
                for group in groups:
                    export.groups.add(group)

                export_sms_task.delay(export.pk)

                if not getattr(settings, 'CELERY_ALWAYS_EAGER', False):
                    messages.info(self.request, _("We are preparing your export. We will e-mail you at %s when "
                                                  "it is ready.") % self.request.user.username)

                else:
                    export = ExportMessagesTask.objects.get(id=export.pk)
                    dl_url = reverse('assets.download', kwargs=dict(type='message_export', pk=export.pk))
                    messages.info(self.request, _("Export complete, you can find it here: %s (production users "
                                                  "will get an email)") % dl_url)

            try:
                messages.success(self.request, self.derive_success_message())

                if 'HTTP_X_PJAX' not in self.request.META:
                    return HttpResponseRedirect(self.get_success_url())
                else:  # pragma: no cover
                    response = self.render_to_response(self.get_context_data(form=form,
                                                                             success_url=self.get_success_url(),
                                                                             success_script=getattr(self, 'success_script', None)))
                    response['Temba-Success'] = self.get_success_url()
                    response['REDIRECT'] = self.get_success_url()
                    return response

            except IntegrityError as e:  # pragma: no cover
                message = str(e).capitalize()
                errors = self.form._errors.setdefault(forms.forms.NON_FIELD_ERRORS, forms.utils.ErrorList())
                errors.append(message)
                return self.render_to_response(self.get_context_data(form=form))