Ejemplo n.º 1
0
def kml_export(request, username, id_string):
    """
    KML export view.
    """
    # read the locations from the database
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    data = {'data': kml_export_data(id_string, user=owner, xform=xform)}
    response = render(
        request,
        "survey.kml",
        data,
        content_type="application/vnd.google-earth.kml+xml")
    response['Content-Disposition'] = \
        generate_content_disposition_header(id_string, 'kml')
    audit = {"xform": xform.id_string, "export_type": Export.KML_EXPORT}
    audit_log(Actions.EXPORT_CREATED, request.user, owner,
              _("Created KML export on '%(id_string)s'.") % {
                  'id_string': xform.id_string,
              }, audit, request)
    # log download as well
    audit_log(Actions.EXPORT_DOWNLOADED, request.user, owner,
              _("Downloaded KML export on '%(id_string)s'.") % {
                  'id_string': xform.id_string,
              }, audit, request)

    return response
Ejemplo n.º 2
0
def download_jsonform(request, username, id_string):
    """
    XForm JSON view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })

    if request.method == "OPTIONS":
        response = HttpResponse()
        add_cors_headers(response)
        return response
    helper_auth_helper(request)
    if not has_permission(xform, owner, request, xform.shared):
        response = HttpResponseForbidden(_(u'Not shared.'))
        add_cors_headers(response)
        return response
    response = response_with_mimetype_and_name(
        'json', id_string, show_date=False)
    if 'callback' in request.GET and request.GET.get('callback') != '':
        callback = request.GET.get('callback')
        response.content = "%s(%s)" % (callback, xform.json)
    else:
        add_cors_headers(response)
        response.content = xform.json
    return response
Ejemplo n.º 3
0
def edit_data(request, username, id_string, data_id):
    """
    Redirects to Enketo webform to edit a submission with the data_id.
    """
    context = RequestContext(request)
    owner = User.objects.get(username__iexact=username)
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    instance = get_object_or_404(Instance, pk=data_id, xform=xform)
    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))
    if not hasattr(settings, 'ENKETO_URL'):
        return HttpResponseRedirect(
            reverse('form-show',
                    kwargs={
                        'username': username,
                        'id_string': id_string
                    }))

    url = '%sdata/edit_url' % settings.ENKETO_URL
    # see commit 220f2dad0e for tmp file creation
    injected_xml = inject_instanceid(instance.xml, instance.uuid)
    return_url = request.build_absolute_uri(
        reverse('submission-instance',
                kwargs={
                    'username': username,
                    'id_string': id_string
                }) + "#/" + text(instance.id))
    form_url = get_form_url(request, username, settings.ENKETO_PROTOCOL)

    try:
        url = get_enketo_urls(form_url,
                              xform.id_string,
                              instance_xml=injected_xml,
                              instance_id=instance.uuid,
                              return_url=return_url)
    except EnketoError as e:
        context.message = {
            'type': 'alert-error',
            'text': u"Enketo error, reason: %s" % e
        }
        messages.add_message(request,
                             messages.WARNING,
                             _("Enketo error: enketo replied %s") % e,
                             fail_silently=True)
    else:
        if url:
            url = url['edit_url']
            context.enketo = url
            return HttpResponseRedirect(url)
    return HttpResponseRedirect(
        reverse('form-show',
                kwargs={
                    'username': username,
                    'id_string': id_string
                }))
Ejemplo n.º 4
0
    def test_get_form(self):
        # non existent id_string
        with self.assertRaises(Http404):
            get_form({'id_string': 'non_existent_form'})

        self._publish_transportation_form()

        # valid xform id_string
        kwarg = {'id_string__iexact': self.xform.id_string}
        xform = get_form(kwarg)
        self.assertIsInstance(xform, XForm)

        # pass a queryset
        kwarg['queryset'] = XForm.objects.all()
        xform = get_form(kwarg)
        self.assertIsInstance(xform, XForm)
Ejemplo n.º 5
0
def xformsManifest(request, username, id_string):  # pylint: disable=C0103
    """
    XFormManifest view, part of OpenRosa Form Discovery API 1.0.
    """
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    formlist_user = xform.user
    profile, __ = UserProfile.objects.get_or_create(user=formlist_user)

    if profile.require_auth:
        authenticator = HttpDigestAuthenticator()
        if not authenticator.authenticate(request):
            return authenticator.build_challenge_response()

    response = render(
        request,
        "xformsManifest.xml", {
            'host':
            request.build_absolute_uri().replace(request.get_full_path(), ''),
            'media_files':
            MetaData.media_upload(xform, download=True)
        },
        content_type="text/xml; charset=utf-8")
    response['X-OpenRosa-Version'] = '1.0'
    response['Date'] = datetime.now(pytz.timezone(settings.TIME_ZONE))\
        .strftime('%a, %d %b %Y %H:%M:%S %Z')

    return response
Ejemplo n.º 6
0
def xformsManifest(request, username, id_string):  # pylint: disable=C0103
    """
    XFormManifest view, part of OpenRosa Form Discovery API 1.0.
    """
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    formlist_user = xform.user
    profile, __ = UserProfile.objects.get_or_create(user=formlist_user)

    if profile.require_auth:
        authenticator = HttpDigestAuthenticator()
        if not authenticator.authenticate(request):
            return authenticator.build_challenge_response()

    response = render(
        request,
        "xformsManifest.xml", {
            'host':
            request.build_absolute_uri().replace(request.get_full_path(), ''),
            'media_files':
            MetaData.media_upload(xform, download=True)
        },
        content_type="text/xml; charset=utf-8")
    response['X-OpenRosa-Version'] = '1.0'
    response['Date'] = datetime.now(pytz.timezone(settings.TIME_ZONE))\
        .strftime('%a, %d %b %Y %H:%M:%S %Z')

    return response
Ejemplo n.º 7
0
def download_jsonform(request, username, id_string):
    """
    XForm JSON view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })

    if request.method == "OPTIONS":
        response = HttpResponse()
        add_cors_headers(response)
        return response
    helper_auth_helper(request)
    if not has_permission(xform, owner, request, xform.shared):
        response = HttpResponseForbidden(_(u'Not shared.'))
        add_cors_headers(response)
        return response
    response = response_with_mimetype_and_name('json',
                                               id_string,
                                               show_date=False)
    if 'callback' in request.GET and request.GET.get('callback') != '':
        callback = request.GET.get('callback')
        response.content = "%s(%s)" % (callback, xform.json)
    else:
        add_cors_headers(response)
        response.content = xform.json
    return response
Ejemplo n.º 8
0
def delete_export(request, username, id_string, export_type):
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    export_id = request.POST.get('export_id')

    # find the export entry in the db
    export = get_object_or_404(Export, id=export_id)

    export.delete()
    audit = {"xform": xform.id_string, "export_type": export.export_type}
    audit_log(
        Actions.EXPORT_DOWNLOADED, request.user, owner,
        _("Deleted %(export_type)s export '%(filename)s'"
          " on '%(id_string)s'.") % {
              'export_type': export.export_type.upper(),
              'filename': export.filename,
              'id_string': xform.id_string,
          }, audit, request)
    return HttpResponseRedirect(
        reverse(export_list,
                kwargs={
                    "username": username,
                    "id_string": id_string,
                    "export_type": export_type
                }))
Ejemplo n.º 9
0
def kml_export(request, username, id_string):
    # read the locations from the database
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    data = {'data': kml_export_data(id_string, user=owner, xform=xform)}
    response = \
        render(request, "survey.kml", data,
               content_type="application/vnd.google-earth.kml+xml")
    response['Content-Disposition'] = \
        generate_content_disposition_header(id_string, 'kml')
    audit = {"xform": xform.id_string, "export_type": Export.KML_EXPORT}
    audit_log(
        Actions.EXPORT_CREATED, request.user, owner,
        _("Created KML export on '%(id_string)s'.") % {
            'id_string': xform.id_string,
        }, audit, request)
    # log download as well
    audit_log(
        Actions.EXPORT_DOWNLOADED, request.user, owner,
        _("Downloaded KML export on '%(id_string)s'.") % {
            'id_string': xform.id_string,
        }, audit, request)

    return response
Ejemplo n.º 10
0
def delete_export(request, username, id_string, export_type):
    """
    Delete export view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    export_id = request.POST.get('export_id')

    # find the export entry in the db
    export = get_object_or_404(Export, id=export_id)

    export.delete()
    audit = {"xform": xform.id_string, "export_type": export.export_type}
    audit_log(Actions.EXPORT_DOWNLOADED, request.user, owner,
              _("Deleted %(export_type)s export '%(filename)s'"
                " on '%(id_string)s'.") % {
                    'export_type': export.export_type.upper(),
                    'filename': export.filename,
                    'id_string': xform.id_string,
                }, audit, request)
    return HttpResponseRedirect(
        reverse(
            export_list,
            kwargs={
                "username": username,
                "id_string": id_string,
                "export_type": export_type
            }))
Ejemplo n.º 11
0
def add_service(request, username, id_string):
    data = {}
    form = RestServiceForm()
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    if request.method == 'POST':
        form = RestServiceForm(request.POST)
        restservice = None
        if form.is_valid():
            service_name = form.cleaned_data['service_name']
            service_url = form.cleaned_data['service_url']
            try:
                rs = RestService(service_url=service_url,
                                 name=service_name,
                                 xform=xform)
                rs.save()
            except IntegrityError:
                message = _(u"Service already defined.")
                status = 'fail'
            else:
                status = 'success'
                message = (_(u"Successfully added service %(name)s.") % {
                    'name': service_name
                })
                service_tpl = render_to_string(
                    "service.html", {
                        "sv": rs,
                        "username": xform.user.username,
                        "id_string": xform.id_string
                    })
                restservice = service_tpl
        else:
            status = 'fail'
            message = _(u"Please fill in all required fields")

            if form.errors:
                for field in form:
                    message += Template(u"{{ field.errors }}")\
                        .render(Context({'field': field}))
        if request.is_ajax():
            response = {'status': status, 'message': message}
            if restservice:
                response["restservice"] = u"%s" % restservice

            return HttpResponse(json.dumps(response))

        data['status'] = status
        data['message'] = message

    data['list_services'] = RestService.objects.filter(xform=xform)
    data['form'] = form
    data['username'] = username
    data['id_string'] = id_string

    return render(request, "add-service.html", data)
Ejemplo n.º 12
0
def check_and_set_form_by_id_string(username, id_string, request):
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    return xform if has_permission(xform, xform.user, request)\
        else False
Ejemplo n.º 13
0
def edit_data(request, username, id_string, data_id):
    """
    Redirects to Enketo webform to edit a submission with the data_id.
    """
    context = RequestContext(request)
    owner = User.objects.get(username__iexact=username)
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    instance = get_object_or_404(Instance, pk=data_id, xform=xform)
    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))
    if not hasattr(settings, 'ENKETO_URL'):
        return HttpResponseRedirect(
            reverse(
                'form-show',
                kwargs={'username': username,
                        'id_string': id_string}))

    url = '%sdata/edit_url' % settings.ENKETO_URL
    # see commit 220f2dad0e for tmp file creation
    injected_xml = inject_instanceid(instance.xml, instance.uuid)
    return_url = request.build_absolute_uri(
        reverse(
            'submission-instance',
            kwargs={'username': username,
                    'id_string': id_string}) + "#/" + text(instance.id))
    form_url = get_form_url(request, username, settings.ENKETO_PROTOCOL)

    try:
        url = enketo_url(
            form_url,
            xform.id_string,
            instance_xml=injected_xml,
            instance_id=instance.uuid,
            return_url=return_url)
    except EnketoError as e:
        context.message = {
            'type': 'alert-error',
            'text': u"Enketo error, reason: %s" % e
        }
        messages.add_message(
            request,
            messages.WARNING,
            _("Enketo error: enketo replied %s") % e,
            fail_silently=True)
    else:
        if url:
            context.enketo = url
            return HttpResponseRedirect(url)
    return HttpResponseRedirect(
        reverse(
            'form-show', kwargs={'username': username,
                                 'id_string': id_string}))
Ejemplo n.º 14
0
def check_and_set_form_by_id_string(username, id_string, request):
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    return xform if has_permission(xform, xform.user, request)\
        else False
Ejemplo n.º 15
0
def check_and_set_user_and_form(username, id_string, request):
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    owner = User.objects.get(username=username)
    return [xform, owner] if has_permission(xform, owner, request)\
        else [False, False]
Ejemplo n.º 16
0
def check_and_set_user_and_form(username, id_string, request):
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    owner = User.objects.get(username=username)
    return [xform, owner] if has_permission(xform, owner, request)\
        else [False, False]
Ejemplo n.º 17
0
def add_service(request, username, id_string):
    data = {}
    form = RestServiceForm()
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    if request.method == 'POST':
        form = RestServiceForm(request.POST)
        restservice = None
        if form.is_valid():
            service_name = form.cleaned_data['service_name']
            service_url = form.cleaned_data['service_url']
            try:
                rs = RestService(service_url=service_url,
                                 name=service_name, xform=xform)
                rs.save()
            except IntegrityError:
                message = _(u"Service already defined.")
                status = 'fail'
            else:
                status = 'success'
                message = (_(u"Successfully added service %(name)s.")
                           % {'name': service_name})
                service_tpl = render_to_string("service.html", {
                    "sv": rs, "username": xform.user.username,
                    "id_string": xform.id_string})
                restservice = service_tpl
        else:
            status = 'fail'
            message = _(u"Please fill in all required fields")

            if form.errors:
                for field in form:
                    message += Template(u"{{ field.errors }}")\
                        .render(Context({'field': field}))
        if request.is_ajax():
            response = {'status': status, 'message': message}
            if restservice:
                response["restservice"] = u"%s" % restservice

            return HttpResponse(json.dumps(response))

        data['status'] = status
        data['message'] = message

    data['list_services'] = RestService.objects.filter(xform=xform)
    data['form'] = form
    data['username'] = username
    data['id_string'] = id_string

    return render(request, "add-service.html", data)
Ejemplo n.º 18
0
    def filter_queryset(self, queryset):
        username = self.kwargs.get('username')
        if username is None and self.request.user.is_anonymous():
            # raises a permission denied exception, forces authentication
            self.permission_denied(self.request)

        if username is not None and self.request.user.is_anonymous():
            profile = get_object_or_404(UserProfile,
                                        user__username=username.lower())

            if profile.require_auth:
                # raises a permission denied exception, forces authentication
                self.permission_denied(self.request)
            else:
                queryset = queryset.filter(user=profile.user)
        else:
            queryset = super(BriefcaseViewset, self).filter_queryset(queryset)

        formId = self.request.GET.get('formId', '')

        if formId.find('[') != -1:
            formId = _extract_id_string(formId)

        xform_kwargs = {'queryset': queryset, 'id_string__iexact': formId}
        if username:
            xform_kwargs['user__username__iexact'] = username
        xform = get_form(xform_kwargs)
        self.check_object_permissions(self.request, xform)
        instances = Instance.objects.filter(xform=xform,
                                            deleted_at__isnull=True)
        if xform.encrypted:
            instances = instances.filter(media_all_received=True)
        instances = instances.order_by('pk')
        num_entries = self.request.GET.get('numEntries')
        cursor = self.request.GET.get('cursor')

        cursor = _parse_int(cursor)
        if cursor:
            instances = instances.filter(pk__gt=cursor)

        num_entries = _parse_int(num_entries)
        if num_entries:
            instances = instances[:num_entries]

        if instances.count():
            last_instance = instances[instances.count() - 1]
            self.resumptionCursor = last_instance.pk
        elif instances.count() == 0 and cursor:
            self.resumptionCursor = cursor
        else:
            self.resumptionCursor = 0

        return instances
Ejemplo n.º 19
0
def google_xls_export(request, username, id_string):
    """
    Google export view, uploads an excel export to google drive and then
    redirects to the uploaded google sheet.
    """
    token = None
    if request.user.is_authenticated():
        try:
            token_storage = TokenStorageModel.objects.get(id=request.user)
        except TokenStorageModel.DoesNotExist:
            pass
        else:
            token = token_storage.token
    elif request.session.get('access_token'):
        token = request.session.get('access_token')

    if token is None:
        request.session["google_redirect_url"] = reverse(google_xls_export,
                                                         kwargs={
                                                             'username':
                                                             username,
                                                             'id_string':
                                                             id_string
                                                         })
        return HttpResponseRedirect(google_flow.step1_get_authorize_url())

    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    is_valid, data_dictionary = set_instances_for_export(
        id_string, owner, request)
    if not is_valid:
        return data_dictionary

    xls_writer = XlsWriter()
    tmp = NamedTemporaryFile(delete=False)
    xls_writer.set_file(tmp)
    xls_writer.set_data_dictionary(data_dictionary)
    temp_file = xls_writer.save_workbook_to_file()
    temp_file.close()
    url = None
    os.unlink(tmp.name)
    audit = {"xform": xform.id_string, "export_type": "google"}
    audit_log(
        Actions.EXPORT_CREATED, request.user, owner,
        _("Created Google Docs export on '%(id_string)s'.") % {
            'id_string': xform.id_string,
        }, audit, request)

    return HttpResponseRedirect(url)
Ejemplo n.º 20
0
def map_view(request, username, id_string, template='map.html'):
    """
    Map view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    data = {'content_user': owner, 'xform': xform}
    data['profile'], __ = UserProfile.objects.get_or_create(user=owner)

    data['form_view'] = True
    data['jsonform_url'] = reverse(download_jsonform,
                                   kwargs={
                                       "username": username,
                                       "id_string": id_string
                                   })
    data['enketo_edit_url'] = reverse('edit_data',
                                      kwargs={
                                          "username": username,
                                          "id_string": id_string,
                                          "data_id": 0
                                      })
    data['enketo_add_url'] = reverse('enter_data',
                                     kwargs={
                                         "username": username,
                                         "id_string": id_string
                                     })

    data['enketo_add_with_url'] = reverse('add_submission_with',
                                          kwargs={
                                              "username": username,
                                              "id_string": id_string
                                          })
    data['mongo_api_url'] = reverse('mongo_view_api',
                                    kwargs={
                                        "username": username,
                                        "id_string": id_string
                                    })
    data['delete_data_url'] = reverse('delete_data',
                                      kwargs={
                                          "username": username,
                                          "id_string": id_string
                                      })
    data['mapbox_layer'] = MetaData.mapbox_layer_upload(xform)
    audit = {"xform": xform.id_string}
    audit_log(
        Actions.FORM_MAP_VIEWED, request.user, owner,
        _("Requested map on '%(id_string)s'.") %
        {'id_string': xform.id_string}, audit, request)
    return render(request, template, data)
Ejemplo n.º 21
0
def get_xform_and_perms(username, id_string, request):
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    is_owner = xform.user == request.user
    can_edit = is_owner or\
        request.user.has_perm('logger.change_xform', xform)
    can_view = can_edit or\
        request.user.has_perm('logger.view_xform', xform)
    return [xform, is_owner, can_edit, can_view]
Ejemplo n.º 22
0
def get_xform_and_perms(username, id_string, request):
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    is_owner = xform.user == request.user
    can_edit = is_owner or\
        request.user.has_perm('logger.change_xform', xform)
    can_view = can_edit or\
        request.user.has_perm('logger.view_xform', xform)
    return [xform, is_owner, can_edit, can_view]
Ejemplo n.º 23
0
def enter_data(request, username, id_string):
    """
    Redirects to Enketo webform view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })

    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))

    form_url = get_form_url(request, username, settings.ENKETO_PROTOCOL)

    try:
        enketo_urls = get_enketo_urls(form_url, xform.id_string)
        url = enketo_urls.get('url')
        if not url:
            return HttpResponseRedirect(
                reverse('form-show',
                        kwargs={
                            'username': username,
                            'id_string': id_string
                        }))
        return HttpResponseRedirect(url)
    except EnketoError as e:
        data = {}
        owner = User.objects.get(username__iexact=username)
        data['profile'], __ = UserProfile.objects.get_or_create(user=owner)
        data['xform'] = xform
        data['content_user'] = owner
        data['form_view'] = True
        data['message'] = {
            'type': 'alert-error',
            'text': u"Enketo error, reason: %s" % e
        }
        messages.add_message(request,
                             messages.WARNING,
                             _("Enketo error: enketo replied %s") % e,
                             fail_silently=True)
        return render(request, "profile.html", data)

    return HttpResponseRedirect(
        reverse('form-show',
                kwargs={
                    'username': username,
                    'id_string': id_string
                }))
Ejemplo n.º 24
0
def google_xls_export(request, username, id_string):
    """
    Google export view, uploads an excel export to google drive and then
    redirects to the uploaded google sheet.
    """
    token = None
    if request.user.is_authenticated:
        try:
            token_storage = TokenStorageModel.objects.get(id=request.user)
        except TokenStorageModel.DoesNotExist:
            pass
        else:
            token = token_storage.token
    elif request.session.get('access_token'):
        token = request.session.get('access_token')

    if token is None:
        request.session["google_redirect_url"] = reverse(
            google_xls_export,
            kwargs={'username': username,
                    'id_string': id_string})
        return HttpResponseRedirect(google_flow.step1_get_authorize_url())

    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    is_valid, data_dictionary = set_instances_for_export(
        id_string, owner, request)
    if not is_valid:
        return data_dictionary

    xls_writer = XlsWriter()
    tmp = NamedTemporaryFile(delete=False)
    xls_writer.set_file(tmp)
    xls_writer.set_data_dictionary(data_dictionary)
    temp_file = xls_writer.save_workbook_to_file()
    temp_file.close()
    url = None
    os.unlink(tmp.name)
    audit = {"xform": xform.id_string, "export_type": "google"}
    audit_log(Actions.EXPORT_CREATED, request.user, owner,
              _("Created Google Docs export on '%(id_string)s'.") % {
                  'id_string': xform.id_string,
              }, audit, request)

    return HttpResponseRedirect(url)
Ejemplo n.º 25
0
def view_submission_list(request, username):
    """
    Submission list view.

    Briefcase Aggregate API view/submissionList.
    """
    form_user = get_object_or_404(User, username__iexact=username)
    __, ___ = UserProfile.objects.get_or_create(user=form_user)
    authenticator = HttpDigestAuthenticator()
    if not authenticator.authenticate(request):
        return authenticator.build_challenge_response()
    id_string = request.GET.get('formId', None)
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    if not has_permission(xform, form_user, request, xform.shared_data):
        return HttpResponseForbidden('Not shared.')
    num_entries = request.GET.get('numEntries', None)
    cursor = request.GET.get('cursor', None)
    instances = xform.instances.filter(deleted_at=None).order_by('pk')

    cursor = _parse_int(cursor)
    if cursor:
        instances = instances.filter(pk__gt=cursor)

    num_entries = _parse_int(num_entries)
    if num_entries:
        instances = instances[:num_entries]

    data = {'instances': instances}

    resumption_cursor = 0
    if instances.count():
        last_instance = instances[instances.count() - 1]
        resumption_cursor = last_instance.pk
    elif instances.count() == 0 and cursor:
        resumption_cursor = cursor

    data['resumptionCursor'] = resumption_cursor

    return render(
        request,
        'submissionList.xml',
        data,
        content_type="text/xml; charset=utf-8")
Ejemplo n.º 26
0
def view_submission_list(request, username):
    """
    Submission list view.

    Briefcase Aggregate API view/submissionList.
    """
    form_user = get_object_or_404(User, username__iexact=username)
    __, ___ = UserProfile.objects.get_or_create(user=form_user)
    authenticator = HttpDigestAuthenticator()
    if not authenticator.authenticate(request):
        return authenticator.build_challenge_response()
    id_string = request.GET.get('formId', None)
    xform_kwargs = {
        'id_string__iexact': id_string,
        'user__username__iexact': username
    }

    xform = get_form(xform_kwargs)
    if not has_permission(xform, form_user, request, xform.shared_data):
        return HttpResponseForbidden('Not shared.')
    num_entries = request.GET.get('numEntries', None)
    cursor = request.GET.get('cursor', None)
    instances = xform.instances.filter(deleted_at=None).order_by('pk')

    cursor = _parse_int(cursor)
    if cursor:
        instances = instances.filter(pk__gt=cursor)

    num_entries = _parse_int(num_entries)
    if num_entries:
        instances = instances[:num_entries]

    data = {'instances': instances}

    resumption_cursor = 0
    if instances.count():
        last_instance = instances[instances.count() - 1]
        resumption_cursor = last_instance.pk
    elif instances.count() == 0 and cursor:
        resumption_cursor = cursor

    data['resumptionCursor'] = resumption_cursor

    return render(
        request,
        'submissionList.xml',
        data,
        content_type="text/xml; charset=utf-8")
Ejemplo n.º 27
0
def delete_xform(request, username, id_string):
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })

    # delete xform and submissions
    remove_xform(xform)

    audit = {}
    audit_log(
        Actions.FORM_DELETED, request.user, xform.user,
        _("Deleted form '%(id_string)s'.") % {
            'id_string': xform.id_string,
        }, audit, request)
    return HttpResponseRedirect('/')
Ejemplo n.º 28
0
def export_progress(request, username, id_string, export_type):
    """
    Async export progress view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    # find the export entry in the db
    export_ids = request.GET.getlist('export_ids')
    exports = Export.objects.filter(xform=xform,
                                    id__in=export_ids,
                                    export_type=export_type)
    statuses = []
    for export in exports:
        status = {
            'complete': False,
            'url': None,
            'filename': None,
            'export_id': export.id
        }

        if export.status == Export.SUCCESSFUL:
            status['url'] = reverse(export_download,
                                    kwargs={
                                        'username': owner.username,
                                        'id_string': xform.id_string,
                                        'export_type': export.export_type,
                                        'filename': export.filename
                                    })
            status['filename'] = export.filename
            if export.export_type == Export.GOOGLE_SHEETS_EXPORT and \
                    export.export_url is None:
                status['url'] = None
            if export.export_type == Export.EXTERNAL_EXPORT \
                    and export.export_url is None:
                status['url'] = None
        # mark as complete if it either failed or succeeded but NOT pending
        if export.status == Export.SUCCESSFUL \
                or export.status == Export.FAILED:
            status['complete'] = True
        statuses.append(status)

    return HttpResponse(json.dumps(statuses), content_type='application/json')
Ejemplo n.º 29
0
def enter_data(request, username, id_string):
    """
    Redirects to Enketo webform view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })

    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))

    form_url = get_form_url(request, username, settings.ENKETO_PROTOCOL)

    try:
        url = enketo_url(form_url, xform.id_string)
        if not url:
            return HttpResponseRedirect(
                reverse(
                    'form-show',
                    kwargs={'username': username,
                            'id_string': id_string}))
        return HttpResponseRedirect(url)
    except EnketoError as e:
        data = {}
        owner = User.objects.get(username__iexact=username)
        data['profile'], __ = UserProfile.objects.get_or_create(user=owner)
        data['xform'] = xform
        data['content_user'] = owner
        data['form_view'] = True
        data['message'] = {
            'type': 'alert-error',
            'text': u"Enketo error, reason: %s" % e
        }
        messages.add_message(
            request,
            messages.WARNING,
            _("Enketo error: enketo replied %s") % e,
            fail_silently=True)
        return render(request, "profile.html", data)

    return HttpResponseRedirect(
        reverse(
            'form-show', kwargs={'username': username,
                                 'id_string': id_string}))
Ejemplo n.º 30
0
def data_view(request, username, id_string):
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'id_string__iexact': id_string, 'user': owner})
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    data = {'owner': owner, 'xform': xform}
    audit = {
        "xform": xform.id_string,
    }
    audit_log(
        Actions.FORM_DATA_VIEWED, request.user, owner,
        _("Requested data view for '%(id_string)s'.") % {
            'id_string': xform.id_string,
        }, audit, request)

    return render(request, "data_view.html", data)
Ejemplo n.º 31
0
def export_progress(request, username, id_string, export_type):
    """
    Async export progress view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    # find the export entry in the db
    export_ids = request.GET.getlist('export_ids')
    exports = Export.objects.filter(
        xform=xform, id__in=export_ids, export_type=export_type)
    statuses = []
    for export in exports:
        status = {
            'complete': False,
            'url': None,
            'filename': None,
            'export_id': export.id
        }

        if export.status == Export.SUCCESSFUL:
            status['url'] = reverse(
                export_download,
                kwargs={
                    'username': owner.username,
                    'id_string': xform.id_string,
                    'export_type': export.export_type,
                    'filename': export.filename
                })
            status['filename'] = export.filename
            if export.export_type == Export.GOOGLE_SHEETS_EXPORT and \
                    export.export_url is None:
                status['url'] = None
            if export.export_type == Export.EXTERNAL_EXPORT \
                    and export.export_url is None:
                status['url'] = None
        # mark as complete if it either failed or succeeded but NOT pending
        if export.status == Export.SUCCESSFUL \
                or export.status == Export.FAILED:
            status['complete'] = True
        statuses.append(status)

    return HttpResponse(json.dumps(statuses), content_type='application/json')
Ejemplo n.º 32
0
def download_xlsform(request, username, id_string):
    """
    Download XLSForm view.
    """
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })
    owner = User.objects.get(username__iexact=username)

    helper_auth_helper(request)

    if not has_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden('Not shared.')

    file_path = xform.xls.name
    default_storage = get_storage_class()()

    if file_path != '' and default_storage.exists(file_path):
        audit = {"xform": xform.id_string}
        audit_log(
            Actions.FORM_XLS_DOWNLOADED, request.user, xform.user,
            _("Downloaded XLS file for form '%(id_string)s'.") %
            {"id_string": xform.id_string}, audit, request)
        split_path = file_path.split(os.extsep)
        extension = 'xls'

        if len(split_path) > 1:
            extension = split_path[len(split_path) - 1]

        response = response_with_mimetype_and_name('vnd.ms-excel',
                                                   id_string,
                                                   show_date=False,
                                                   extension=extension,
                                                   file_path=file_path)

        return response

    else:
        messages.add_message(
            request, messages.WARNING,
            _(u'No XLS file for your form '
              u'<strong>%(id)s</strong>') % {'id': id_string})

        return HttpResponseRedirect("/%s" % username)
Ejemplo n.º 33
0
def delete_xform(request, username, id_string):
    """
    Delete XForm view.
    """
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })

    # delete xform and submissions
    remove_xform(xform)

    audit = {}
    audit_log(Actions.FORM_DELETED, request.user, xform.user,
              _("Deleted form '%(id_string)s'.") % {
                  'id_string': xform.id_string,
              }, audit, request)
    return HttpResponseRedirect('/')
Ejemplo n.º 34
0
def download_xlsform(request, username, id_string):
    """
    Download XLSForm view.
    """
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })
    owner = User.objects.get(username__iexact=username)

    helper_auth_helper(request)

    if not has_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden('Not shared.')

    file_path = xform.xls.name
    default_storage = get_storage_class()()

    if file_path != '' and default_storage.exists(file_path):
        audit = {"xform": xform.id_string}
        audit_log(Actions.FORM_XLS_DOWNLOADED, request.user, xform.user,
                  _("Downloaded XLS file for form '%(id_string)s'.") %
                  {"id_string": xform.id_string}, audit, request)
        split_path = file_path.split(os.extsep)
        extension = 'xls'

        if len(split_path) > 1:
            extension = split_path[len(split_path) - 1]

        response = response_with_mimetype_and_name(
            'vnd.ms-excel',
            id_string,
            show_date=False,
            extension=extension,
            file_path=file_path)

        return response

    else:
        messages.add_message(request, messages.WARNING,
                             _(u'No XLS file for your form '
                               u'<strong>%(id)s</strong>') % {'id': id_string})

        return HttpResponseRedirect("/%s" % username)
Ejemplo n.º 35
0
def export_download(request, username, id_string, export_type, filename):
    """
    Export download view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    # find the export entry in the db
    export = get_object_or_404(Export, xform=xform, filename=filename)

    if (export_type == Export.GOOGLE_SHEETS_EXPORT or
            export_type == Export.EXTERNAL_EXPORT) and \
            export.export_url is not None:
        return HttpResponseRedirect(export.export_url)

    ext, mime_type = export_def_from_filename(export.filename)

    audit = {"xform": xform.id_string, "export_type": export.export_type}
    audit_log(
        Actions.EXPORT_DOWNLOADED, request.user, owner,
        _("Downloaded %(export_type)s export '%(filename)s' "
          "on '%(id_string)s'.") % {
              'export_type': export.export_type.upper(),
              'filename': export.filename,
              'id_string': xform.id_string,
          }, audit, request)
    if request.GET.get('raw'):
        id_string = None

    default_storage = get_storage_class()()
    if not isinstance(default_storage, FileSystemStorage):
        return HttpResponseRedirect(default_storage.url(export.filepath))
    basename = os.path.splitext(export.filename)[0]
    response = response_with_mimetype_and_name(mime_type,
                                               name=basename,
                                               extension=ext,
                                               file_path=export.filepath,
                                               show_date=False)
    return response
Ejemplo n.º 36
0
def map_view(request, username, id_string, template='map.html'):
    """
    Map view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    data = {'content_user': owner, 'xform': xform}
    data['profile'], __ = UserProfile.objects.get_or_create(user=owner)

    data['form_view'] = True
    data['jsonform_url'] = reverse(
        download_jsonform,
        kwargs={"username": username,
                "id_string": id_string})
    data['enketo_edit_url'] = reverse(
        'edit_data',
        kwargs={"username": username,
                "id_string": id_string,
                "data_id": 0})
    data['enketo_add_url'] = reverse(
        'enter_data', kwargs={"username": username,
                              "id_string": id_string})

    data['enketo_add_with_url'] = reverse(
        'add_submission_with',
        kwargs={"username": username,
                "id_string": id_string})
    data['mongo_api_url'] = reverse(
        'mongo_view_api',
        kwargs={"username": username,
                "id_string": id_string})
    data['delete_data_url'] = reverse(
        'delete_data', kwargs={"username": username,
                               "id_string": id_string})
    data['mapbox_layer'] = MetaData.mapbox_layer_upload(xform)
    audit = {"xform": xform.id_string}
    audit_log(Actions.FORM_MAP_VIEWED, request.user, owner,
              _("Requested map on '%(id_string)s'.") %
              {'id_string': xform.id_string}, audit, request)
    return render(request, template, data)
Ejemplo n.º 37
0
def export_download(request, username, id_string, export_type, filename):
    """
    Export download view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    # find the export entry in the db
    export = get_object_or_404(Export, xform=xform, filename=filename)

    if (export_type == Export.GOOGLE_SHEETS_EXPORT or
            export_type == Export.EXTERNAL_EXPORT) and \
            export.export_url is not None:
        return HttpResponseRedirect(export.export_url)

    ext, mime_type = export_def_from_filename(export.filename)

    audit = {"xform": xform.id_string, "export_type": export.export_type}
    audit_log(Actions.EXPORT_DOWNLOADED, request.user, owner,
              _("Downloaded %(export_type)s export '%(filename)s' "
                "on '%(id_string)s'.") % {
                    'export_type': export.export_type.upper(),
                    'filename': export.filename,
                    'id_string': xform.id_string,
                }, audit, request)
    if request.GET.get('raw'):
        id_string = None

    default_storage = get_storage_class()()
    if not isinstance(default_storage, FileSystemStorage):
        return HttpResponseRedirect(default_storage.url(export.filepath))
    basename = os.path.splitext(export.filename)[0]
    response = response_with_mimetype_and_name(
        mime_type,
        name=basename,
        extension=ext,
        file_path=export.filepath,
        show_date=False)
    return response
Ejemplo n.º 38
0
def data_view(request, username, id_string):
    """
    Data view displays submission data.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'id_string__iexact': id_string, 'user': owner})
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    data = {'owner': owner, 'xform': xform}
    audit = {
        "xform": xform.id_string,
    }
    audit_log(Actions.FORM_DATA_VIEWED, request.user, owner,
              _("Requested data view for '%(id_string)s'.") % {
                  'id_string': xform.id_string,
              }, audit, request)

    return render(request, "data_view.html", data)
Ejemplo n.º 39
0
def zip_export(request, username, id_string):
    """
    Zip export view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    if request.GET.get('raw'):
        id_string = None

    attachments = Attachment.objects.filter(instance__xform=xform)
    zip_file = None

    try:
        zip_file = create_attachments_zipfile(attachments)
        audit = {"xform": xform.id_string, "export_type": Export.ZIP_EXPORT}
        audit_log(
            Actions.EXPORT_CREATED, request.user, owner,
            _("Created ZIP export on '%(id_string)s'.") % {
                'id_string': xform.id_string,
            }, audit, request)
        # log download as well
        audit_log(
            Actions.EXPORT_DOWNLOADED, request.user, owner,
            _("Downloaded ZIP export on '%(id_string)s'.") % {
                'id_string': xform.id_string,
            }, audit, request)
        if request.GET.get('raw'):
            id_string = None

        response = response_with_mimetype_and_name('zip', id_string)
        response.write(FileWrapper(zip_file))
        response['Content-Length'] = zip_file.tell()
        zip_file.seek(0)
    finally:
        if zip_file:
            zip_file.close()

    return response
Ejemplo n.º 40
0
def toggle_downloadable(request, username, id_string):
    """
    Toggle XForm view, changes downloadable status of a form.
    """
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })
    xform.downloadable = not xform.downloadable
    xform.save()
    audit = {}
    audit_log(
        Actions.FORM_UPDATED, request.user, xform.user,
        _("Made form '%(id_string)s' %(downloadable)s.") % {
            'id_string':
            xform.id_string,
            'downloadable':
            _("downloadable") if xform.downloadable else _("un-downloadable")
        }, audit, request)
    return HttpResponseRedirect("/%s" % username)
Ejemplo n.º 41
0
def download_xform(request, username, id_string):
    user = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': user, 'id_string__iexact': id_string})
    profile, created =\
        UserProfile.objects.get_or_create(user=user)

    if profile.require_auth:
        authenticator = HttpDigestAuthenticator()
        if not authenticator.authenticate(request):
            return authenticator.build_challenge_response()
    audit = {"xform": xform.id_string}
    audit_log(
        Actions.FORM_XML_DOWNLOADED, request.user, xform.user,
        _("Downloaded XML for form '%(id_string)s'.") %
        {"id_string": xform.id_string}, audit, request)
    response = response_with_mimetype_and_name('xml',
                                               id_string,
                                               show_date=False)
    response.content = xform.xml
    return response
Ejemplo n.º 42
0
def toggle_downloadable(request, username, id_string):
    """
    Toggle XForm view, changes downloadable status of a form.
    """
    xform = get_form({
        'user__username__iexact': username,
        'id_string__iexact': id_string
    })
    xform.downloadable = not xform.downloadable
    xform.save()
    audit = {}
    audit_log(Actions.FORM_UPDATED, request.user, xform.user,
              _("Made form '%(id_string)s' %(downloadable)s.") % {
                  'id_string':
                  xform.id_string,
                  'downloadable':
                  _("downloadable")
                  if xform.downloadable else _("un-downloadable")
              }, audit, request)
    return HttpResponseRedirect("/%s" % username)
Ejemplo n.º 43
0
def download_xform(request, username, id_string):
    """
    Download XForm XML view.
    """
    user = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': user, 'id_string__iexact': id_string})
    profile, __ = UserProfile.objects.get_or_create(user=user)

    if profile.require_auth:
        authenticator = HttpDigestAuthenticator()
        if not authenticator.authenticate(request):
            return authenticator.build_challenge_response()
    audit = {"xform": xform.id_string}
    audit_log(Actions.FORM_XML_DOWNLOADED, request.user, xform.user,
              _("Downloaded XML for form '%(id_string)s'.") %
              {"id_string": xform.id_string}, audit, request)
    response = response_with_mimetype_and_name(
        'xml', id_string, show_date=False)
    response.content = xform.xml
    return response
Ejemplo n.º 44
0
def zip_export(request, username, id_string):
    """
    Zip export view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    if request.GET.get('raw'):
        id_string = None

    attachments = Attachment.objects.filter(instance__xform=xform)
    zip_file = None

    try:
        zip_file = create_attachments_zipfile(attachments)
        audit = {"xform": xform.id_string, "export_type": Export.ZIP_EXPORT}
        audit_log(Actions.EXPORT_CREATED, request.user, owner,
                  _("Created ZIP export on '%(id_string)s'.") % {
                      'id_string': xform.id_string,
                  }, audit, request)
        # log download as well
        audit_log(Actions.EXPORT_DOWNLOADED, request.user, owner,
                  _("Downloaded ZIP export on '%(id_string)s'.") % {
                      'id_string': xform.id_string,
                  }, audit, request)
        if request.GET.get('raw'):
            id_string = None

        response = response_with_mimetype_and_name('zip', id_string)
        response.write(FileWrapper(zip_file))
        response['Content-Length'] = zip_file.tell()
        zip_file.seek(0)
    finally:
        if zip_file:
            zip_file.close()

    return response
Ejemplo n.º 45
0
    def test_get_form(self):
        """Test get_form()."""
        # non existent id_string
        with self.assertRaises(Http404):
            get_form({'id_string': 'non_existent_form'})

        self._publish_transportation_form()

        # valid xform id_string
        kwarg = {'id_string__iexact': self.xform.id_string}
        xform = get_form(kwarg)
        self.assertIsInstance(xform, XForm)

        # pass a queryset
        kwarg['queryset'] = XForm.objects.all()
        xform = get_form(kwarg)
        self.assertIsInstance(xform, XForm)

        # deleted form
        xform.deleted_at = timezone.now()
        xform.save()
        with self.assertRaises(Http404):
            get_form(kwarg)
Ejemplo n.º 46
0
def export_list(request, username, id_string, export_type):
    """
    Export list view.
    """
    credential = None

    if export_type not in Export.EXPORT_TYPE_DICT:
        return HttpResponseBadRequest(
            _(u'Export type "%s" is not supported.' % export_type))

    if export_type == Export.GOOGLE_SHEETS_EXPORT:
        # Retrieve  google creds or redirect to google authorization page
        credential = _get_google_credential(request)
        if isinstance(credential, HttpResponseRedirect):
            return credential
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))
    # Get meta and token
    export_token = request.GET.get('token')
    export_meta = request.GET.get('meta')
    options = {
        'group_delimiter': DEFAULT_GROUP_DELIMITER,
        'remove_group_name': False,
        'split_select_multiples': True,
        'binary_select_multiples': False,
        'meta': export_meta,
        'token': export_token,
        'google_credentials': credential
    }

    if should_create_new_export(xform, export_type, options):
        try:
            create_async_export(
                xform,
                export_type,
                query=None,
                force_xlsx=True,
                options=options)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    metadata_qs = MetaData.objects.filter(object_id=xform.id,
                                          data_type="external_export")\
        .values('id', 'data_value')

    for metadata in metadata_qs:
        metadata['data_value'] = metadata.get('data_value').split('|')[0]

    data = {
        'username': owner.username,
        'xform': xform,
        'export_type': export_type,
        'export_type_name': Export.EXPORT_TYPE_DICT[export_type],
        'exports': Export.objects.filter(
            xform=xform, export_type=export_type).order_by('-created_on'),
        'metas': metadata_qs
    }  # yapf: disable

    return render(request, 'export_list.html', data)
Ejemplo n.º 47
0
def create_export(request, username, id_string, export_type):
    """
    Create async export tasks view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))

    credential = None
    if export_type == Export.GOOGLE_SHEETS_EXPORT:

        credential = _get_google_credential(request)
        if isinstance(credential, HttpResponseRedirect):
            return credential

    query = request.POST.get("query")
    force_xlsx = request.POST.get('xls') != 'true'

    # export options
    group_delimiter = request.POST.get("options[group_delimiter]", '/')
    if group_delimiter not in ['.', '/']:
        return HttpResponseBadRequest(
            _("%s is not a valid delimiter" % group_delimiter))

    # default is True, so when dont_.. is yes
    # split_select_multiples becomes False
    split_select_multiples = request.POST.get(
        "options[dont_split_select_multiples]", "no") == "no"

    binary_select_multiples = getattr(settings, 'BINARY_SELECT_MULTIPLES',
                                      False)
    remove_group_name = request.POST.get("options[remove_group_name]", "false")
    value_select_multiples = request.POST.get(
        "options[value_select_multiples]", "false")

    # external export option
    meta = request.POST.get("meta")
    options = {
        'group_delimiter': group_delimiter,
        'split_select_multiples': split_select_multiples,
        'binary_select_multiples': binary_select_multiples,
        'value_select_multiples': str_to_bool(value_select_multiples),
        'remove_group_name': str_to_bool(remove_group_name),
        'meta': meta.replace(",", "") if meta else None,
        'google_credentials': credential
    }

    try:
        create_async_export(xform, export_type, query, force_xlsx, options)
    except ExportTypeError:
        return HttpResponseBadRequest(
            _("%s is not a valid export type" % export_type))
    else:
        audit = {"xform": xform.id_string, "export_type": export_type}
        audit_log(Actions.EXPORT_CREATED, request.user, owner,
                  _("Created %(export_type)s export on '%(id_string)s'.") % {
                      'export_type': export_type.upper(),
                      'id_string': xform.id_string,
                  }, audit, request)
        return HttpResponseRedirect(
            reverse(
                export_list,
                kwargs={
                    "username": username,
                    "id_string": id_string,
                    "export_type": export_type
                }))
Ejemplo n.º 48
0
def export_list(request, username, id_string, export_type):
    credential = None

    if export_type not in Export.EXPORT_TYPE_DICT:
        return HttpResponseBadRequest(
            _(u'Export type "%s" is not supported.' % export_type))

    if export_type == Export.GOOGLE_SHEETS_EXPORT:
        # Retrieve  google creds or redirect to google authorization page
        credential = _get_google_credential(request)
        if isinstance(credential, HttpResponseRedirect):
            return credential
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))
    # Get meta and token
    export_token = request.GET.get('token')
    export_meta = request.GET.get('meta')
    options = {
        'group_delimiter': DEFAULT_GROUP_DELIMITER,
        'remove_group_name': False,
        'split_select_multiples': True,
        'binary_select_multiples': False,
        'meta': export_meta,
        'token': export_token,
        'google_credentials': credential
    }

    if should_create_new_export(xform, export_type, options):
        try:
            create_async_export(xform,
                                export_type,
                                query=None,
                                force_xlsx=True,
                                options=options)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    metadata = MetaData.objects.filter(object_id=xform.id,
                                       data_type="external_export")\
        .values('id', 'data_value')

    for m in metadata:
        m['data_value'] = m.get('data_value').split('|')[0]

    data = {
        'username':
        owner.username,
        'xform':
        xform,
        'export_type':
        export_type,
        'export_type_name':
        Export.EXPORT_TYPE_DICT[export_type],
        'exports':
        Export.objects.filter(xform=xform,
                              export_type=export_type).order_by('-created_on'),
        'metas':
        metadata
    }

    return render(request, 'export_list.html', data)
Ejemplo n.º 49
0
def create_export(request, username, id_string, export_type):
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))

    credential = None
    if export_type == Export.GOOGLE_SHEETS_EXPORT:

        credential = _get_google_credential(request)
        if isinstance(credential, HttpResponseRedirect):
            return credential

    query = request.POST.get("query")
    force_xlsx = request.POST.get('xls') != 'true'

    # export options
    group_delimiter = request.POST.get("options[group_delimiter]", '/')
    if group_delimiter not in ['.', '/']:
        return HttpResponseBadRequest(
            _("%s is not a valid delimiter" % group_delimiter))

    # default is True, so when dont_.. is yes
    # split_select_multiples becomes False
    split_select_multiples = request.POST.get(
        "options[dont_split_select_multiples]", "no") == "no"

    binary_select_multiples = getattr(settings, 'BINARY_SELECT_MULTIPLES',
                                      False)
    remove_group_name = request.POST.get("options[remove_group_name]", "false")

    # external export option
    meta = request.POST.get("meta")
    options = {
        'group_delimiter': group_delimiter,
        'split_select_multiples': split_select_multiples,
        'binary_select_multiples': binary_select_multiples,
        'remove_group_name': str_to_bool(remove_group_name),
        'meta': meta.replace(",", "") if meta else None,
        'google_credentials': credential
    }

    try:
        create_async_export(xform, export_type, query, force_xlsx, options)
    except Export.ExportTypeError:
        return HttpResponseBadRequest(
            _("%s is not a valid export type" % export_type))
    else:
        audit = {"xform": xform.id_string, "export_type": export_type}
        audit_log(
            Actions.EXPORT_CREATED, request.user, owner,
            _("Created %(export_type)s export on '%(id_string)s'.") % {
                'export_type': export_type.upper(),
                'id_string': xform.id_string,
            }, audit, request)
        return HttpResponseRedirect(
            reverse(export_list,
                    kwargs={
                        "username": username,
                        "id_string": id_string,
                        "export_type": export_type
                    }))
Ejemplo n.º 50
0
def data_export(request, username, id_string, export_type):
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    query = request.GET.get("query")
    extension = export_type

    # check if we should force xlsx
    force_xlsx = request.GET.get('xls') != 'true'
    if export_type == Export.XLS_EXPORT and force_xlsx:
        extension = 'xlsx'
    elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]:
        extension = 'zip'

    audit = {"xform": xform.id_string, "export_type": export_type}

    options = {
        "extension": extension,
        "username": username,
        "id_string": id_string,
        "query": query
    }

    # check if we need to re-generate,
    # we always re-generate if a filter is specified
    if should_create_new_export(xform, export_type, options) or query or\
            'start' in request.GET or 'end' in request.GET:
        # check for start and end params
        start, end = _get_start_end_submission_time(request)
        options.update({"start": start, "end": end})

        try:
            export = generate_export(export_type, xform, None, options)
            audit_log(
                Actions.EXPORT_CREATED, request.user, owner,
                _("Created %(export_type)s export on '%(id_string)s'.") % {
                    'id_string': xform.id_string,
                    'export_type': export_type.upper()
                }, audit, request)
        except NoRecordsFoundError:
            return HttpResponseNotFound(_("No records found to export"))
        except SPSSIOError as e:
            return HttpResponseBadRequest(str(e))
    else:
        export = newest_export_for(xform, export_type, options)

    # log download as well
    audit_log(
        Actions.EXPORT_DOWNLOADED, request.user, owner,
        _("Downloaded %(export_type)s export on '%(id_string)s'.") % {
            'id_string': xform.id_string,
            'export_type': export_type.upper()
        }, audit, request)

    if not export.filename and not export.error_message:
        # tends to happen when using newset_export_for.
        return HttpResponseNotFound("File does not exist!")
    elif not export.filename and export.error_message:
        return HttpResponseBadRequest(str(export.error_message))

    # get extension from file_path, exporter could modify to
    # xlsx if it exceeds limits
    path, ext = os.path.splitext(export.filename)
    ext = ext[1:]
    if request.GET.get('raw'):
        id_string = None

    response = response_with_mimetype_and_name(Export.EXPORT_MIMES[ext],
                                               id_string,
                                               extension=ext,
                                               file_path=export.filepath)

    return response
Ejemplo n.º 51
0
def data_export(request, username, id_string, export_type):
    """
    Data export view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    query = request.GET.get("query")
    extension = export_type

    # check if we should force xlsx
    force_xlsx = request.GET.get('xls') != 'true'
    if export_type == Export.XLS_EXPORT and force_xlsx:
        extension = 'xlsx'
    elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]:
        extension = 'zip'

    audit = {"xform": xform.id_string, "export_type": export_type}

    options = {
        "extension": extension,
        "username": username,
        "id_string": id_string
    }
    if query:
        options['query'] = query

    # check if we need to re-generate,
    # we always re-generate if a filter is specified
    if should_create_new_export(xform, export_type, options) or query or\
            'start' in request.GET or 'end' in request.GET:
        # check for start and end params
        start, end = _get_start_end_submission_time(request)
        options.update({"start": start, "end": end})

        try:
            export = generate_export(export_type, xform, None, options)
            audit_log(
                Actions.EXPORT_CREATED, request.user, owner,
                _("Created %(export_type)s export on '%(id_string)s'.") % {
                    'id_string': xform.id_string,
                    'export_type': export_type.upper()
                }, audit, request)
        except NoRecordsFoundError:
            return HttpResponseNotFound(_("No records found to export"))
        except SPSSIOError as e:
            return HttpResponseBadRequest(str(e))
    else:
        export = newest_export_for(xform, export_type, options)

    # log download as well
    audit_log(
        Actions.EXPORT_DOWNLOADED, request.user, owner,
        _("Downloaded %(export_type)s export on '%(id_string)s'.") %
        {'id_string': xform.id_string,
         'export_type': export_type.upper()}, audit, request)

    if not export.filename and not export.error_message:
        # tends to happen when using newset_export_for.
        return HttpResponseNotFound("File does not exist!")
    elif not export.filename and export.error_message:
        return HttpResponseBadRequest(str(export.error_message))

    # get extension from file_path, exporter could modify to
    # xlsx if it exceeds limits
    __, extension = os.path.splitext(export.filename)
    extension = extension[1:]
    if request.GET.get('raw'):
        id_string = None

    response = response_with_mimetype_and_name(
        Export.EXPORT_MIMES[extension],
        id_string,
        extension=extension,
        file_path=export.filepath)

    return response