Beispiel #1
0
def google_xls_export(request, username, id_string):
    token = None
    if request.user.is_authenticated():
        try:
            ts = TokenStorageModel.objects.get(id=request.user)
        except TokenStorageModel.DoesNotExist:
            pass
        else:
            token = ts.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(redirect_uri)
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u"Not shared."))
    valid, dd = dd_for_params(id_string, owner, request)
    if not valid:
        return dd
    ddw = XlsWriter()
    tmp = NamedTemporaryFile(delete=False)
    ddw.set_file(tmp)
    ddw.set_data_dictionary(dd)
    temp_file = ddw.save_workbook_to_file()
    temp_file.close()
    url = google_export_xls(tmp.name, xform.title, token, blob=True)
    os.unlink(tmp.name)
    return HttpResponseRedirect(url)
Beispiel #2
0
def google_xls_export(request, username, id_string):
    token = None
    if request.user.is_authenticated():
        try:
            ts = TokenStorageModel.objects.get(id=request.user)
        except TokenStorageModel.DoesNotExist:
            pass
        else:
            token = ts.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.'))

    valid, dd = dd_for_params(id_string, owner, request)
    if not valid:
        return dd

    ddw = XlsWriter()
    tmp = NamedTemporaryFile(delete=False)
    ddw.set_file(tmp)
    ddw.set_data_dictionary(dd)
    temp_file = ddw.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)
Beispiel #3
0
def google_xls_export(request, username, id_string):
    token = None
    if request.user.is_authenticated():
        try:
            ts = TokenStorageModel.objects.get(id=request.user)
        except TokenStorageModel.DoesNotExist:
            pass
        else:
            token = ts.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(redirect_uri)

    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    valid, dd = dd_for_params(id_string, owner, request)
    if not valid:
        return dd

    ddw = XlsWriter()
    tmp = NamedTemporaryFile(delete=False)
    ddw.set_file(tmp)
    ddw.set_data_dictionary(dd)
    temp_file = ddw.save_workbook_to_file()
    temp_file.close()
    url = google_export_xls(tmp.name, xform.title, token, blob=True)
    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)
Beispiel #4
0
def xls_export(request, username, id_string):
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden("Not shared.")
    valid, dd = dd_for_params(id_string, owner, request)
    if not valid:
        return dd
    ddw = XlsWriter()
    ddw.set_data_dictionary(dd)
    temp_file = ddw.save_workbook_to_file()
    if request.GET.get("raw"):
        id_string = None
    response = response_with_mimetype_and_name("vnd.ms-excel", id_string, extension="xls")
    response.write(temp_file.getvalue())
    temp_file.seek(0, os.SEEK_END)
    response["Content-Length"] = temp_file.tell()
    temp_file.close()
    return response
Beispiel #5
0
def xls_export(request, username, id_string):
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden('Not shared.')
    valid, dd = dd_for_params(id_string, owner, request)
    if not valid: return dd
    ddw = XlsWriter()
    ddw.set_data_dictionary(dd)
    temp_file = ddw.save_workbook_to_file()
    if request.GET.get('raw'):
        id_string = None
    response = response_with_mimetype_and_name('vnd.ms-excel', id_string,
        extension='xls')
    response.write(temp_file.getvalue())
    temp_file.seek(0, os.SEEK_END)
    response['Content-Length'] = temp_file.tell()
    temp_file.close()
    return response
Beispiel #6
0
def xls_export(request, username, id_string):
    owner = User.objects.get(username=username)
    xform = XForm.objects.get(id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden('Not shared.')
    valid, dd = dd_for_params(id_string, owner, request)
    if not valid: return dd
    ddw = XlsWriter()
    ddw.set_data_dictionary(dd)
    temp_file = ddw.save_workbook_to_file()
    if request.GET.get('raw'):
        id_string = None
    response = response_with_mimetype_and_name('vnd.ms-excel', id_string,
        extension='xls')
    response.write(temp_file.getvalue())
    temp_file.seek(0, os.SEEK_END)
    response['Content-Length'] = temp_file.tell()
    temp_file.close()
    return response
Beispiel #7
0
def google_xls_export(request, username, id_string):
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden("Not shared.")
    valid, dd = dd_for_params(id_string, owner, request)
    if not valid:
        return dd
    ddw = XlsWriter()
    tmp = NamedTemporaryFile(delete=False)
    ddw.set_file(tmp)
    ddw.set_data_dictionary(dd)
    temp_file = ddw.save_workbook_to_file()
    temp_file.close()
    import gdata
    import gdata.gauth
    import gdata.docs
    import gdata.data
    import gdata.docs.client
    import gdata.docs.data
    from main.google_export import token, refresh_access_token, redirect_uri
    from main.models import TokenStorageModel

    try:
        ts = TokenStorageModel.objects.get(id=request.user)
    except TokenStorageModel.DoesNotExist:
        return HttpResponseRedirect(redirect_uri)
    else:
        stored_token = gdata.gauth.token_from_blob(ts.token)
        if stored_token.refresh_token is not None and stored_token.access_token is not None:
            token.refresh_token = stored_token.refresh_token
            working_token = refresh_access_token(token, request.user)
            docs_client = gdata.docs.client.DocsClient(source=token.user_agent)
            docs_client = working_token.authorize(docs_client)
            xls_doc = gdata.docs.data.Resource(type="spreadsheet", title=xform.title)
            media = gdata.data.MediaSource()
            media.SetFileHandle(tmp.name, "application/vnd.ms-excel")
            xls_doc = docs_client.CreateResource(xls_doc, media=media)
    os.unlink(tmp.name)
    return HttpResponseRedirect("https://docs.google.com")
Beispiel #8
0
def google_xls_export(request, username, id_string):
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden('Not shared.')
    valid, dd = dd_for_params(id_string, owner, request)
    if not valid: return dd
    ddw = XlsWriter()
    tmp = NamedTemporaryFile(delete=False)
    ddw.set_file(tmp)
    ddw.set_data_dictionary(dd)
    temp_file = ddw.save_workbook_to_file()
    temp_file.close()
    import gdata
    import gdata.gauth
    import gdata.docs
    import gdata.data
    import gdata.docs.client
    import gdata.docs.data
    from main.google_export import token, refresh_access_token, redirect_uri
    from main.models import TokenStorageModel
    try:
        ts = TokenStorageModel.objects.get(id=request.user)
    except TokenStorageModel.DoesNotExist:
        return HttpResponseRedirect(redirect_uri)
    else:
        stored_token = gdata.gauth.token_from_blob(ts.token)
        if stored_token.refresh_token is not None and\
           stored_token.access_token is not None:
            token.refresh_token = stored_token.refresh_token
            working_token = refresh_access_token(token, request.user)
            docs_client = gdata.docs.client.DocsClient(source=token.user_agent)
            docs_client = working_token.authorize(docs_client)
            xls_doc = gdata.docs.data.Resource(
                type='spreadsheet', title=xform.title)
            media = gdata.data.MediaSource()
            media.SetFileHandle(tmp.name, 'application/vnd.ms-excel')
            xls_doc = docs_client.CreateResource(xls_doc, media=media)
    os.unlink(tmp.name)
    return HttpResponseRedirect('https://docs.google.com')