示例#1
0
def user_upload_job_poll(request, domain, download_id, template="users/mobile/partials/user_upload_status.html"):
    context = get_download_context(download_id, check_state=True)
    context.update({
        'on_complete_short': _('Bulk upload complete.'),
        'on_complete_long': _('Mobile Worker upload has finished'),

    })
    class _BulkUploadResponseWrapper(object):
        def __init__(self, context):
            results = context.get('result', defaultdict(lambda: []))
            self.response_rows = results['rows']
            self.response_errors = results['errors']
            self.problem_rows = [r for r in self.response_rows if r['flag'] not in ('updated', 'created')]

        def success_count(self):
            return len(self.response_rows) - len(self.problem_rows)

        def has_errors(self):
            return bool(self.response_errors or self.problem_rows)

        def errors(self):
            errors = []
            for row in self.problem_rows:
                if row['flag'] == 'missing-data':
                    errors.append(_('A row with no username was skipped'))
                else:
                    errors.append('{username}: {flag}'.format(**row))
            errors.extend(self.response_errors)
            return errors

    context['result'] = _BulkUploadResponseWrapper(context)
    return render(request, template, context)
示例#2
0
 def poll_custom_export_download(self, in_data):
     """Polls celery to see how the export download task is going.
     :param in_data: dict passed by the  angular js controller.
     :return: final response: {
         'success': True,
         'dropbox_url': '<url>',
         'download_url: '<url>',
         <task info>
     }
     """
     try:
         download_id = in_data["download_id"]
     except KeyError:
         return format_angular_error(_("Requires a download id"))
     try:
         context = get_download_context(download_id, check_state=True)
     except TaskFailedError:
         return format_angular_error(
             _("Download Task Failed to Start. It seems that the server " "might be under maintenance.")
         )
     if context.get("is_ready", False):
         context.update(
             {
                 "dropbox_url": reverse("dropbox_upload", args=(download_id,)),
                 "download_url": "{}?get_file".format(reverse("retrieve_download", args=(download_id,))),
             }
         )
     context["is_poll_successful"] = True
     return context
示例#3
0
def user_upload_job_poll(request, domain, download_id, template="users/mobile/partials/user_upload_status.html"):
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update(
        {"on_complete_short": _("Bulk upload complete."), "on_complete_long": _("Mobile Worker upload has finished")}
    )

    class _BulkUploadResponseWrapper(object):
        def __init__(self, context):
            results = context.get("result", defaultdict(lambda: []))
            self.response_rows = results["rows"]
            self.response_errors = results["errors"]
            self.problem_rows = [r for r in self.response_rows if r["flag"] not in ("updated", "created")]

        def success_count(self):
            return len(self.response_rows) - len(self.problem_rows)

        def has_errors(self):
            return bool(self.response_errors or self.problem_rows)

        def errors(self):
            errors = []
            for row in self.problem_rows:
                if row["flag"] == "missing-data":
                    errors.append(_("A row with no username was skipped"))
                else:
                    errors.append(u"{username}: {flag}".format(**row))
            errors.extend(self.response_errors)
            return errors

    context["result"] = _BulkUploadResponseWrapper(context)
    return render(request, template, context)
示例#4
0
def fixture_upload_job_poll(request, domain, download_id, template="fixtures/partials/fixture_upload_status.html"):
    try:
        context = get_download_context(download_id, require_result=True)
    except TaskFailedError:
        return HttpResponseServerError()

    return render(request, template, context)
示例#5
0
def ajax_job_poll(request, download_id, template="soil/partials/dl_status.html"):
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError as e:
        context = {'error': list(e)}
        return HttpResponseServerError(render(request, template, context))
    return render(request, template, context)
示例#6
0
def user_download_job_poll(request, domain, download_id, template="hqwebapp/partials/shared_download_status.html"):
    try:
        context = get_download_context(download_id, 'Preparing download')
        context.update({'link_text': _('Download Users')})
    except TaskFailedError as e:
        return HttpResponseServerError(e.errors)
    return render(request, template, context)
示例#7
0
def product_importer_job_poll(request, domain, download_id, template="hqwebapp/partials/download_status.html"):
    context = get_download_context(download_id, check_state=True)
    context.update({
        'on_complete_short': _('Import complete.'),
        'on_complete_long': _('Product importing has finished'),

    })
    return render(request, template, context)
示例#8
0
def fixture_upload_job_poll(request, domain, download_id, template="fixtures/partials/fixture_upload_status.html"):
    context = get_download_context(download_id, check_state=True)
    context.update({
        'on_complete_short': _('Upload complete.'),
        'on_complete_long': _('Lookup table upload has finished'),

    })
    return render(request, template, context)
示例#9
0
def ajax_job_poll(request, download_id, template="soil/partials/dl_status.html"):
    message = request.GET['message'] if 'message' in request.GET else None
    try:
        context = get_download_context(download_id, message=message)
    except TaskFailedError as e:
        context = {'error': list(e.errors) if e.errors else [_("An error occurred during the download.")]}
        return HttpResponseServerError(render(request, template, context))
    return render(request, template, context)
示例#10
0
def ajax_job_poll(request, download_id, template="soil/partials/bootstrap2/dl_status.html"):
    message = request.GET['message'] if 'message' in request.GET else None
    is_bootstrap3 = request.GET.get('is_bootstrap3', 'false') == 'true'
    template = 'soil/partials/bootstrap3/dl_status.html' if is_bootstrap3 else template
    try:
        context = get_download_context(download_id, check_state=True, message=message)
    except TaskFailedError as e:
        context = {'error': list(e.errors) if e.errors else [_("An error occurred during the download.")]}
        return HttpResponseServerError(render(request, template, context))
    return render(request, template, context)
示例#11
0
def location_importer_job_poll(request, domain, download_id, template="hqwebapp/partials/download_status.html"):
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Import complete.'),
        'on_complete_long': _('Location importing has finished'),

    })
    return render(request, template, context)
示例#12
0
def fixture_upload_job_poll(request, domain, download_id, template="fixtures/partials/fixture_upload_status.html"):
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Upload complete.'),
        'on_complete_long': _('Lookup table upload has finished'),

    })
    return render(request, template, context)
示例#13
0
def product_importer_job_poll(
    request, domain, download_id, template="products/manage/partials/product_upload_status.html"
):
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update(
        {"on_complete_short": _("Import complete."), "on_complete_long": _("Product importing has finished")}
    )
    return render(request, template, context)
示例#14
0
def location_importer_job_poll(request, domain, download_id):
    template = "locations/manage/partials/locations_upload_status.html"
    try:
        context = get_download_context(download_id)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Import complete.'),
        'on_complete_long': _('Organization Structure importing has finished'),

    })
    return render(request, template, context)
示例#15
0
def ucr_download_job_poll(request, domain,
                          download_id,
                          template="hqwebapp/partials/shared_download_status.html"):
    config_id = request.GET.get('config_id')
    if config_id and _has_permission(domain, request.couch_user, config_id):
        try:
            context = get_download_context(download_id, 'Preparing download')
            context.update({'link_text': _('Download Report')})
        except TaskFailedError as e:
            return HttpResponseServerError(e.errors)
        return render(request, template, context)
    else:
        raise Http403()
示例#16
0
def demo_restore_job_poll(request, domain, download_id, template="users/mobile/partials/demo_restore_status.html"):

    try:
        context = get_download_context(download_id)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Done'),
        'on_complete_long': _('User is now in Demo mode with latest restore!'),

    })
    return render(request, template, context)
示例#17
0
def xform_management_job_poll(request, domain, download_id,
                              template="data_interfaces/partials/xform_management_status.html"):
    mode = FormManagementMode(request.GET.get('mode'), validate=True)
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': mode.complete_short,
        'mode': mode,
        'form_management_url': reverse(EditDataInterfaceDispatcher.name(),
                                       args=[domain, BulkFormManagementInterface.slug])
    })
    return render(request, template, context)
示例#18
0
def product_importer_job_poll(
        request,
        domain,
        download_id,
        template="products/manage/partials/product_upload_status.html"):
    try:
        context = get_download_context(download_id)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Import complete.'),
        'on_complete_long': _('Product importing has finished'),
    })
    return render(request, template, context)
示例#19
0
def ucr_download_job_poll(
        request,
        domain,
        download_id,
        template="hqwebapp/partials/shared_download_status.html"):
    config_id = request.GET.get('config_id')
    if config_id and _has_permission(domain, request.couch_user, config_id):
        try:
            context = get_download_context(download_id, 'Preparing download')
            context.update({'link_text': _('Download Report')})
        except TaskFailedError as e:
            return HttpResponseServerError(e.errors)
        return render(request, template, context)
    else:
        raise Http403()
示例#20
0
def location_importer_job_poll(
        request,
        domain,
        download_id,
        template="style/bootstrap2/partials/download_status.html"):
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Import complete.'),
        'on_complete_long': _('Location importing has finished'),
    })
    return render(request, template, context)
示例#21
0
def xform_management_job_poll(request, domain, download_id,
                              template="data_interfaces/partials/xform_management_status.html"):
    mode = FormManagementMode(request.GET.get('mode'), validate=True)
    try:
        context = get_download_context(download_id)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': mode.complete_short,
        'mode': mode,
        'form_management_url': reverse(EditDataInterfaceDispatcher.name(),
                                       args=[domain, BulkFormManagementInterface.slug])
    })
    return render(request, template, context)
示例#22
0
def ajax_job_poll(request,
                  download_id,
                  template="soil/partials/dl_status.html"):
    message = request.GET['message'] if 'message' in request.GET else None
    try:
        context = get_download_context(download_id,
                                       check_state=True,
                                       message=message)
    except TaskFailedError as e:
        context = {
            'error':
            list(e.errors)
            if e.errors else [_("An error occurred during the download.")]
        }
        return HttpResponseServerError(render(request, template, context))
    return render(request, template, context)
示例#23
0
def poll_custom_export_download(request, domain):
    """Polls celery to see how the export download task is going.
    :return: final response: {
        'success': True,
        'dropbox_url': '<url>',
        'download_url: '<url>',
        <task info>
    }
    """
    form_or_case = request.GET.get('form_or_case')
    permissions = ExportsPermissionsManager(form_or_case, domain,
                                            request.couch_user)
    permissions.access_download_export_or_404()
    download_id = request.GET.get('download_id')
    try:
        context = get_download_context(download_id)
    except TaskFailedError as e:
        if e.exception_name == 'XlsLengthException':
            return JsonResponse({
                'error':
                _('This file has more than 256 columns, which is not supported by xls. '
                  'Please change the output type to csv or xlsx to export this file.'
                  )
            })
        else:
            notify_exception(request,
                             "Export download failed",
                             details={
                                 'download_id': download_id,
                                 'errors': e.errors,
                                 'exception_name': e.exception_name
                             })

            return JsonResponse({
                'error': _("Download task failed to start."),
            })

    if context.get('is_ready', False):
        context.update({
            'dropbox_url':
            reverse('dropbox_upload', args=(download_id, )),
            'download_url':
            "{}?get_file".format(
                reverse('retrieve_download', args=(download_id, ))),
        })
    context['is_poll_successful'] = True
    return json_response(context)
示例#24
0
def location_importer_job_poll(request,
                               domain,
                               download_id,
                               template="style/partials/download_status.html"):
    template = "locations/manage/partials/locations_upload_status.html"
    try:
        context = get_download_context(download_id)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short':
        _('Import complete.'),
        'on_complete_long':
        _('Organization Structure importing has finished'),
    })
    return render(request, template, context)
示例#25
0
def fixture_upload_job_poll(
        request,
        domain,
        download_id,
        template="fixtures/partials/fixture_upload_status.html"):
    try:
        context = get_download_context(download_id,
                                       check_state=True,
                                       require_result=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Upload complete.'),
        'on_complete_long': _('Lookup table upload has finished'),
    })
    return render(request, template, context)
示例#26
0
def pending_location_import_download_id(domain):
    # Check if there is an unfinished import_locations_async
    # If the task is pending returns download_id of the task else returns False
    key = import_locations_task_key(domain)
    download_id = cache.get(key)
    if download_id:
        try:
            context = get_download_context(download_id)
        except TaskFailedError:
            return False
        if context['is_ready']:
            return False
        else:
            # task hasn't finished
            return download_id
    else:
        return False
示例#27
0
def importer_job_poll(request, domain, download_id, template="importer/partials/import_status.html"):
    try:
        download_context = get_download_context(download_id, check_state=True)
    except TaskFailedError as e:
        error = e.errors
        if error == 'EXPIRED':
            return _spreadsheet_expired(request, domain)
        elif error == 'HAS_ERRORS':
            messages.error(request, _('The session containing the file you '
                                      'uploaded has expired - please upload '
                                      'a new one.'))
            return HttpResponseRedirect(base.ImportCases.get_url(domain=domain) + "?error=cache")
    else:
        context = RequestContext(request)
        context.update(download_context)
        context['url'] = base.ImportCases.get_url(domain=domain)
        return render_to_response(template, context_instance=context)
示例#28
0
def pending_location_import_download_id(domain):
    # Check if there is an unfinished import_locations_async
    # If the task is pending returns download_id of the task else returns False
    key = import_locations_task_key(domain)
    download_id = cache.get(key)
    if download_id:
        try:
            context = get_download_context(download_id)
        except TaskFailedError:
            return False
        if context['is_ready']:
            return False
        else:
            # task hasn't finished
            return download_id
    else:
        return False
示例#29
0
def demo_restore_job_poll(
        request,
        domain,
        download_id,
        template="users/mobile/partials/demo_restore_status.html"):

    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short':
        _('Done'),
        'on_complete_long':
        _('User is now in Demo mode with latest restore!'),
    })
    return render(request, template, context)
示例#30
0
def ajax_job_poll(request,
                  download_id,
                  template="soil/partials/dl_status.html"):
    message = request.GET['message'] if 'message' in request.GET else None
    try:
        context = get_download_context(download_id, message=message)
    except TaskFailedError as e:
        context = {
            'error':
            list(e.errors)
            if e.errors else [_("An error occurred during the download.")]
        }
        return HttpResponseServerError(render(request, template, context))

    download = DownloadBase.get(download_id)
    if download and download.owner_ids and request.couch_user.get_id not in download.owner_ids:
        return HttpResponseForbidden(_("You do not have access to this file"))

    return render(request, template, context)
示例#31
0
def add_export_email_request(request, domain):
    download_id = request.POST.get('download_id')
    user_id = request.couch_user.user_id
    if download_id is None or user_id is None:
        return HttpResponseBadRequest(ugettext_lazy('Download ID or User ID blank/not provided'))
    try:
        download_context = get_download_context(download_id)
    except TaskFailedError:
        return HttpResponseServerError(ugettext_lazy('Export failed'))
    if download_context.get('is_ready', False):
        try:
            couch_user = CouchUser.get_by_user_id(user_id, domain=domain)
        except CouchUser.AccountTypeError:
            return HttpResponseBadRequest(ugettext_lazy('Invalid user'))
        if couch_user is not None:
            process_email_request(domain, download_id, couch_user.get_email())
    else:
        EmailExportWhenDoneRequest.objects.create(domain=domain, download_id=download_id, user_id=user_id)
    return HttpResponse(ugettext_lazy('Export e-mail request sent.'))
示例#32
0
def add_export_email_request(request, domain):
    download_id = request.POST.get('download_id')
    user_id = request.couch_user.user_id
    if download_id is None or user_id is None:
        return HttpResponseBadRequest(gettext_lazy('Download ID or User ID blank/not provided'))
    try:
        download_context = get_download_context(download_id)
    except TaskFailedError:
        return HttpResponseServerError(gettext_lazy('Export failed'))
    if download_context.get('is_ready', False):
        try:
            couch_user = CouchUser.get_by_user_id(user_id, domain=domain)
        except CouchUser.AccountTypeError:
            return HttpResponseBadRequest(gettext_lazy('Invalid user'))
        if couch_user is not None:
            process_email_request(domain, download_id, couch_user.get_email())
    else:
        EmailExportWhenDoneRequest.objects.create(domain=domain, download_id=download_id, user_id=user_id)
    return HttpResponse(gettext_lazy('Export e-mail request sent.'))
示例#33
0
def user_upload_job_poll(
        request,
        domain,
        download_id,
        template="users/mobile/partials/user_upload_status.html"):
    try:
        context = get_download_context(download_id, check_state=True)
    except TaskFailedError:
        return HttpResponseServerError()

    context.update({
        'on_complete_short': _('Bulk upload complete.'),
        'on_complete_long': _('Mobile Worker upload has finished'),
    })

    class _BulkUploadResponseWrapper(object):
        def __init__(self, context):
            results = context.get('result', defaultdict(lambda: []))
            self.response_rows = results['rows']
            self.response_errors = results['errors']
            self.problem_rows = [
                r for r in self.response_rows
                if r['flag'] not in ('updated', 'created')
            ]

        def success_count(self):
            return len(self.response_rows) - len(self.problem_rows)

        def has_errors(self):
            return bool(self.response_errors or self.problem_rows)

        def errors(self):
            errors = []
            for row in self.problem_rows:
                if row['flag'] == 'missing-data':
                    errors.append(_('A row with no username was skipped'))
                else:
                    errors.append(u'{username}: {flag}'.format(**row))
            errors.extend(self.response_errors)
            return errors

    context['result'] = _BulkUploadResponseWrapper(context)
    return render(request, template, context)
示例#34
0
def poll_custom_export_download(request, domain):
    """Polls celery to see how the export download task is going.
    :return: final response: {
        'success': True,
        'dropbox_url': '<url>',
        'download_url: '<url>',
        <task info>
    }
    """
    form_or_case = request.GET.get('form_or_case')
    permissions = ExportsPermissionsManager(form_or_case, domain, request.couch_user)
    permissions.access_download_export_or_404()
    download_id = request.GET.get('download_id')
    try:
        context = get_download_context(download_id)
    except TaskFailedError as e:
        if e.exception_name == 'XlsLengthException':
            return JsonResponse({
                'error': _(
                    'This file has more than 256 columns, which is not supported by xls. '
                    'Please change the output type to csv or xlsx to export this file.')
            })
        else:
            notify_exception(
                request, "Export download failed",
                details={'download_id': download_id, 'errors': e.errors,
                         'exception_name': e.exception_name})

            return JsonResponse({
                'error': _("Download task failed to start."),
            })

    if context.get('is_ready', False):
        context.update({
            'dropbox_url': reverse('dropbox_upload', args=(download_id,)),
            'download_url': "{}?get_file".format(
                reverse('retrieve_download', args=(download_id,))
            ),
        })
    context['is_poll_successful'] = True
    return json_response(context)
示例#35
0
def importer_job_poll(request, domain, download_id, template="importer/partials/import_status.html"):
    try:
        download_context = get_download_context(download_id, check_state=True)
    except TaskFailedError as e:
        # todo: this is async, so it's totally inappropriate to be using messages.error
        # todo: and HttpResponseRedirect
        error = e.errors
        if error == 'EXPIRED':
            return _spreadsheet_expired(request, domain)
        elif error == 'HAS_ERRORS':
            messages.error(request, _('The session containing the file you '
                                      'uploaded has expired - please upload '
                                      'a new one.'))
        else:
            messages.error(request, _('Sorry something went wrong with that import. Please try again. '
                                      'Report an issue if you continue to have problems.'))
        return HttpResponseRedirect(base.ImportCases.get_url(domain=domain) + "?error=cache")
    else:
        context = RequestContext(request)
        context.update(download_context)
        context['url'] = base.ImportCases.get_url(domain=domain)
        return render_to_response(template, context_instance=context)
示例#36
0
def fixture_api_upload_status(request, domain, download_id, **kwargs):
    """
        Use following curl-command to test.
        > curl -v --digest http://127.0.0.1:8000/a/gsid/fixtures/fixapi/status/<download_id>/
               -u [email protected]:password
    """
    try:
        context = get_download_context(download_id, require_result=True)
    except TaskFailedError as e:
        notify_exception(request, message=six.text_type(e))
        response = {
            'message':
            _("Upload did not complete. Reason: '{}'".format(
                six.text_type(e))),
            'error':
            True,
        }
        return json_response(response)

    if context.get('is_ready', False):
        response = {
            'complete': True,
            'message': _("Upload complete."),
        }
    elif context.get('error'):
        response = {
            'error': True,
            'message': context.get('error') or _("An unknown error occurred."),
        }
    else:
        progress = context.get('progress', {}).get('percent')
        response = {
            'message': _("Task in progress. {}% complete").format(progress),
            'progress': progress,
        }
    return json_response(response)
示例#37
0
def poll_custom_export_download(request, domain):
    """Polls celery to see how the export download task is going.
    :return: final response: {
        'success': True,
        'dropbox_url': '<url>',
        'download_url: '<url>',
        <task info>
    }
    """
    form_or_case = request.GET.get('form_or_case')
    permissions = ExportsPermissionsManager(form_or_case, domain,
                                            request.couch_user)
    permissions.access_download_export_or_404()
    download_id = request.GET.get('download_id')
    try:
        context = get_download_context(download_id)
    except TaskFailedError as e:
        notify_exception(request,
                         "Export download failed",
                         details={
                             'download_id': download_id,
                             'errors': e.errors
                         })
        return JsonResponse({
            'error': _("Download task failed to start."),
        })
    if context.get('is_ready', False):
        context.update({
            'dropbox_url':
            reverse('dropbox_upload', args=(download_id, )),
            'download_url':
            "{}?get_file".format(
                reverse('retrieve_download', args=(download_id, ))),
        })
    context['is_poll_successful'] = True
    return JsonResponse(context)
示例#38
0
def user_download_job_poll(request, domain, download_id, template="users/mobile/partials/user_download_status.html"):
    try:
        context = get_download_context(download_id, 'Preparing download')
    except TaskFailedError as e:
        return HttpResponseServerError(e.errors)
    return render(request, template, context)