def test_google_docs_export(self):
        self._publish_transportation_form()
        self._make_submissions()

        initial_token_count = TokenStorageModel.objects.all().count()
        self.token = refresh_access_token(self.token, self.user)
        self.assertIsNotNone(self.token.access_token)
        self.assertEqual(TokenStorageModel.objects.all().count(),
                         initial_token_count + 1)

        response = self.client.get(
            reverse(google_xls_export,
                    kwargs={
                        'username': self.user.username,
                        'id_string': self.xform.id_string
                    }))
        self.assertEqual(response.status_code, 302)
        # share the data, log out, and check the export
        self._share_form_data()
        self._logout()
        response = self.client.get(
            reverse(google_xls_export,
                    kwargs={
                        'username': self.user.username,
                        'id_string': self.xform.id_string
                    }))
        self.assertEqual(response.status_code, 302)
Exemple #2
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")
Exemple #3
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')
    def test_google_docs_export(self):
        self._publish_transportation_form()
        self._make_submissions()

        initial_token_count = TokenStorageModel.objects.all().count()
        self.token = refresh_access_token(self.token, self.user)
        self.assertIsNotNone(self.token.access_token)
        self.assertEqual(
            TokenStorageModel.objects.all().count(), initial_token_count + 1)

        response = self.client.get(reverse(google_xls_export, kwargs={
            'username': self.user.username,
            'id_string': self.xform.id_string
        }))
        self.assertEqual(response.status_code, 302)
        # share the data, log out, and check the export
        self._share_form_data()
        self._logout()
        response = self.client.get(reverse(google_xls_export, kwargs={
            'username': self.user.username,
            'id_string': self.xform.id_string
        }))
        self.assertEqual(response.status_code, 302)
 def _refresh_token(self):
     self.assertEqual(TokenStorageModel.objects.all().count(), 0)
     self.assertIsNone(self.token.access_token)
     self.token = refresh_access_token(self.token, self.user)
     self.assertIsNotNone(self.token.access_token)
     self.assertEqual(TokenStorageModel.objects.all().count(), 1)
 def _refresh_token(self):
     self.assertEqual(TokenStorageModel.objects.all().count(), 0)
     self.assertIsNone(self.token.access_token)
     self.token = refresh_access_token(self.token, self.user)
     self.assertIsNotNone(self.token.access_token)
     self.assertEqual(TokenStorageModel.objects.all().count(), 1)