{"action":"created","comment":{"url":"https://api.github.com/repos/pitzer/kobocat/pulls/comments/35623092","id":35623092,"diff_hunk":"@@ -0,0 +1,68 @@\n+import csv\n+\n+from django.core.files.temp import NamedTemporaryFile\n+\n+from onadata.libs.utils.common_tags import INDEX, PARENT_INDEX\n+from onadata.libs.utils.export_builder import ExportBuilder\n+\n+\n+class CsvExportBuilder(ExportBuilder):\n+    \n+    @classmethod\n+    def write_row(row, csv_writer, fields):\n+        csv_writer.writerow(\n+                [ExportBuilder.encode_if_str(row, field) for field in fields])\n+            \n+    def export(self, path, data, *args):\n+\n+\n+        csv_defs = {}\n+        for section in self.sections:\n+            csv_file = NamedTemporaryFile(suffix=\".csv\")\n+            csv_writer = csv.writer(csv_file)\n+            csv_defs[section['name']] = {\n+                'csv_file': csv_file, 'csv_writer': csv_writer}\n+\n+        # write headers\n+        for section in self.sections:\n+            fields = [element['title'] for element in section['elements']]\\\n+                + self.EXTRA_FIELDS\n+            csv_defs[section['name']]['csv_writer'].writerow(\n+                [f.encode('utf-8') for f in fields])\n+\n+        index = 1\n+        indices = {}\n+        survey_name = self.survey.name\n+        for d in data:\n+            # decode mongo section names\n+            joined_export = ExportBuilder.dict_to_joined_export(\n+                d, index, indices, survey_name)\n+            output = ExportBuilder.decode_mongo_encoded_section_names(\n+                joined_export)\n+            # attach meta fields (index, parent_index, parent_table)\n+            # output has keys for every section\n+            if survey_name not in output:\n+                output[survey_name] = {}\n+            output[survey_name][INDEX] = index\n+            output[survey_name][PARENT_INDEX] = -1\n+            for section in self.sections:\n+                # get data for this section and write to csv\n+                section_name = section['name']\n+                csv_def = csv_defs[section_name]\n+                fields = [\n+                    element['xpath'] for element in\n+                    section['elements']] + self.EXTRA_FIELDS","path":"onadata/libs/utils/csv_export.py","position":54,"original_position":54,"commit_id":"8651ad166e3d45096ff6fe50600b431619248631","original_commit_id":"8651ad166e3d45096ff6fe50600b431619248631","user":{"login":"sandipagr","id":351276,"avatar_url":"https://avatars.githubusercontent.com/u/351276?v=3","gravatar_id":"","url":"https://api.github.com/users/sandipagr","html_url":"https://github.com/sandipagr","followers_url":"https://api.github.com/users/sandipagr/followers","following_url":"https://api.github.com/users/sandipagr/following{/other_user}","gists_url":"https://api.github.com/users/sandipagr/gists{/gist_id}","starred_url":"https://api.github.com/users/sandipagr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sandipagr/subscriptions","organizations_url":"https://api.github.com/users/sandipagr/orgs","repos_url":"https://api.github.com/users/sandipagr/repos","events_url":"https://api.github.com/users/sandipagr/events{/privacy}","received_events_url":"https://api.github.com/users/sandipagr/received_events","type":"User","site_admin":false},"body":"You probably just copy/pasted existing code but the formatting is so unreadable. ","created_at":"2015-07-28T08:06:57Z","updated_at":"2015-07-28T08:06:57Z","html_url":"https://github.com/pitzer/kobocat/pull/1#discussion_r35623092","pull_request_url":"https://api.github.com/repos/pitzer/kobocat/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/pitzer/kobocat/pulls/comments/35623092"},"html":{"href":"https://github.com/pitzer/kobocat/pull/1#discussion_r35623092"},"pull_request":{"href":"https://api.github.com/repos/pitzer/kobocat/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/pitzer/kobocat/pulls/1","id":40982327,"html_url":"https://github.com/pitzer/kobocat/pull/1","diff_url":"https://github.com/pitzer/kobocat/pull/1.diff","patch_url":"https://github.com/pitzer/kobocat/pull/1.patch","issue_url":"https://api.github.com/repos/pitzer/kobocat/issues/1","number":1,"state":"open","locked":false,"title":"Google sheets export","user":{"login":"pitzer","id":1485919,"avatar_url":"https://avatars.githubusercontent.com/u/1485919?v=3","gravatar_id":"","url":"https://api.github.com/users/pitzer","html_url":"https://github.com/pitzer","followers_url":"https://api.github.com/users/pitzer/followers","following_url":"https://api.github.com/users/pitzer/following{/other_user}","gists_url":"https://api.github.com/users/pitzer/gists{/gist_id}","starred_url":"https://api.github.com/users/pitzer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pitzer/subscriptions","organizations_url":"https://api.github.com/users/pitzer/orgs","repos_url":"https://api.github.com/users/pitzer/repos","events_url":"https://api.github.com/users/pitzer/events{/privacy}","received_events_url":"https://api.github.com/users/pitzer/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-07-28T07:25:48Z","updated_at":"2015-07-28T08:06:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"633ce01aad75b63a8bac4fb1c5040784ce048e57","assignee":{"login":"sandipagr","id":351276,"avatar_url":"https://avatars.githubusercontent.com/u/351276?v=3","gravatar_id":"","url":"https://api.github.com/users/sandipagr","html_url":"https://github.com/sandipagr","followers_url":"https://api.github.com/users/sandipagr/followers","following_url":"https://api.github.com/users/sandipagr/following{/other_user}","gists_url":"https://api.github.com/users/sandipagr/gists{/gist_id}","starred_url":"https://api.github.com/users/sandipagr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sandipagr/subscriptions","organizations_url":"https://api.github.com/users/sandipagr/orgs","repos_url":"https://api.github.com/users/sandipagr/repos","events_url":"https://api.github.com/users/sandipagr/events{/privacy}","received_events_url":"https://api.github.com/users/sandipagr/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/pitzer/kobocat/pulls/1/commits","review_comments_url":"https://api.github.com/repos/pitzer/kobocat/pulls/1/comments","review_comment_url":"https://api.github.com/repos/pitzer/kobocat/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pitzer/kobocat/issues/1/comments","statuses_url":"https://api.github.com/repos/pitzer/kobocat/statuses/8651ad166e3d45096ff6fe50600b431619248631","head":{"label":"pitzer:google_sheets_export","ref":"google_sheets_export","sha":"8651ad166e3d45096ff6fe50600b431619248631","user":{"login":"pitzer","id":1485919,"avatar_url":"https://avatars.githubusercontent.com/u/1485919?v=3","gravatar_id":"","url":"https://api.github.com/users/pitzer","html_url":"https://github.com/pitzer","followers_url":"https://api.github.com/users/pitzer/followers","following_url":"https://api.github.com/users/pitzer/following{/other_user}","gists_url":"https://api.github.com/users/pitzer/gists{/gist_id}","starred_url":"https://api.github.com/users/pitzer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pitzer/subscriptions","organizations_url":"https://api.github.com/users/pitzer/orgs","repos_url":"https://api.github.com/users/pitzer/repos","events_url":"https://api.github.com/users/pitzer/events{/privacy}","received_events_url":"https://api.github.com/users/pitzer/received_events","type":"User","site_admin":false},"repo":{"id":39549187,"name":"kobocat","full_name":"pitzer/kobocat","owner":{"login":"pitzer","id":1485919,"avatar_url":"https://avatars.githubusercontent.com/u/1485919?v=3","gravatar_id":"","url":"https://api.github.com/users/pitzer","html_url":"https://github.com/pitzer","followers_url":"https://api.github.com/users/pitzer/followers","following_url":"https://api.github.com/users/pitzer/following{/other_user}","gists_url":"https://api.github.com/users/pitzer/gists{/gist_id}","starred_url":"https://api.github.com/users/pitzer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pitzer/subscriptions","organizations_url":"https://api.github.com/users/pitzer/orgs","repos_url":"https://api.github.com/users/pitzer/repos","events_url":"https://api.github.com/users/pitzer/events{/privacy}","received_events_url":"https://api.github.com/users/pitzer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/pitzer/kobocat","description":"Data collection for humanitarian emergencies","fork":true,"url":"https://api.github.com/repos/pitzer/kobocat","forks_url":"https://api.github.com/repos/pitzer/kobocat/forks","keys_url":"https://api.github.com/repos/pitzer/kobocat/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pitzer/kobocat/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pitzer/kobocat/teams","hooks_url":"https://api.github.com/repos/pitzer/kobocat/hooks","issue_events_url":"https://api.github.com/repos/pitzer/kobocat/issues/events{/number}","events_url":"https://api.github.com/repos/pitzer/kobocat/events","assignees_url":"https://api.github.com/repos/pitzer/kobocat/assignees{/user}","branches_url":"https://api.github.com/repos/pitzer/kobocat/branches{/branch}","tags_url":"https://api.github.com/repos/pitzer/kobocat/tags","blobs_url":"https://api.github.com/repos/pitzer/kobocat/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pitzer/kobocat/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pitzer/kobocat/git/refs{/sha}","trees_url":"https://api.github.com/repos/pitzer/kobocat/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pitzer/kobocat/statuses/{sha}","languages_url":"https://api.github.com/repos/pitzer/kobocat/languages","stargazers_url":"https://api.github.com/repos/pitzer/kobocat/stargazers","contributors_url":"https://api.github.com/repos/pitzer/kobocat/contributors","subscribers_url":"https://api.github.com/repos/pitzer/kobocat/subscribers","subscription_url":"https://api.github.com/repos/pitzer/kobocat/subscription","commits_url":"https://api.github.com/repos/pitzer/kobocat/commits{/sha}","git_commits_url":"https://api.github.com/repos/pitzer/kobocat/git/commits{/sha}","comments_url":"https://api.github.com/repos/pitzer/kobocat/comments{/number}","issue_comment_url":"https://api.github.com/repos/pitzer/kobocat/issues/comments{/number}","contents_url":"https://api.github.com/repos/pitzer/kobocat/contents/{+path}","compare_url":"https://api.github.com/repos/pitzer/kobocat/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pitzer/kobocat/merges","archive_url":"https://api.github.com/repos/pitzer/kobocat/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pitzer/kobocat/downloads","issues_url":"https://api.github.com/repos/pitzer/kobocat/issues{/number}","pulls_url":"https://api.github.com/repos/pitzer/kobocat/pulls{/number}","milestones_url":"https://api.github.com/repos/pitzer/kobocat/milestones{/number}","notifications_url":"https://api.github.com/repos/pitzer/kobocat/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pitzer/kobocat/labels{/name}","releases_url":"https://api.github.com/repos/pitzer/kobocat/releases{/id}","created_at":"2015-07-23T06:14:39Z","updated_at":"2015-07-23T06:14:42Z","pushed_at":"2015-07-28T07:33:14Z","git_url":"git://github.com/pitzer/kobocat.git","ssh_url":"git@github.com:pitzer/kobocat.git","clone_url":"https://github.com/pitzer/kobocat.git","svn_url":"https://github.com/pitzer/kobocat","homepage":"kobotoolbox.org","size":21999,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"pitzer:google","ref":"google","sha":"4fbdf168b2c598b8449dcc14dd7fcfd54efa206e","user":{"login":"pitzer","id":1485919,"avatar_url":"https://avatars.githubusercontent.com/u/1485919?v=3","gravatar_id":"","url":"https://api.github.com/users/pitzer","html_url":"https://github.com/pitzer","followers_url":"https://api.github.com/users/pitzer/followers","following_url":"https://api.github.com/users/pitzer/following{/other_user}","gists_url":"https://api.github.com/users/pitzer/gists{/gist_id}","starred_url":"https://api.github.com/users/pitzer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pitzer/subscriptions","organizations_url":"https://api.github.com/users/pitzer/orgs","repos_url":"https://api.github.com/users/pitzer/repos","events_url":"https://api.github.com/users/pitzer/events{/privacy}","received_events_url":"https://api.github.com/users/pitzer/received_events","type":"User","site_admin":false},"repo":{"id":39549187,"name":"kobocat","full_name":"pitzer/kobocat","owner":{"login":"pitzer","id":1485919,"avatar_url":"https://avatars.githubusercontent.com/u/1485919?v=3","gravatar_id":"","url":"https://api.github.com/users/pitzer","html_url":"https://github.com/pitzer","followers_url":"https://api.github.com/users/pitzer/followers","following_url":"https://api.github.com/users/pitzer/following{/other_user}","gists_url":"https://api.github.com/users/pitzer/gists{/gist_id}","starred_url":"https://api.github.com/users/pitzer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pitzer/subscriptions","organizations_url":"https://api.github.com/users/pitzer/orgs","repos_url":"https://api.github.com/users/pitzer/repos","events_url":"https://api.github.com/users/pitzer/events{/privacy}","received_events_url":"https://api.github.com/users/pitzer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/pitzer/kobocat","description":"Data collection for humanitarian emergencies","fork":true,"url":"https://api.github.com/repos/pitzer/kobocat","forks_url":"https://api.github.com/repos/pitzer/kobocat/forks","keys_url":"https://api.github.com/repos/pitzer/kobocat/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pitzer/kobocat/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pitzer/kobocat/teams","hooks_url":"https://api.github.com/repos/pitzer/kobocat/hooks","issue_events_url":"https://api.github.com/repos/pitzer/kobocat/issues/events{/number}","events_url":"https://api.github.com/repos/pitzer/kobocat/events","assignees_url":"https://api.github.com/repos/pitzer/kobocat/assignees{/user}","branches_url":"https://api.github.com/repos/pitzer/kobocat/branches{/branch}","tags_url":"https://api.github.com/repos/pitzer/kobocat/tags","blobs_url":"https://api.github.com/repos/pitzer/kobocat/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pitzer/kobocat/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pitzer/kobocat/git/refs{/sha}","trees_url":"https://api.github.com/repos/pitzer/kobocat/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pitzer/kobocat/statuses/{sha}","languages_url":"https://api.github.com/repos/pitzer/kobocat/languages","stargazers_url":"https://api.github.com/repos/pitzer/kobocat/stargazers","contributors_url":"https://api.github.com/repos/pitzer/kobocat/contributors","subscribers_url":"https://api.github.com/repos/pitzer/kobocat/subscribers","subscription_url":"https://api.github.com/repos/pitzer/kobocat/subscription","commits_url":"https://api.github.com/repos/pitzer/kobocat/commits{/sha}","git_commits_url":"https://api.github.com/repos/pitzer/kobocat/git/commits{/sha}","comments_url":"https://api.github.com/repos/pitzer/kobocat/comments{/number}","issue_comment_url":"https://api.github.com/repos/pitzer/kobocat/issues/comments{/number}","contents_url":"https://api.github.com/repos/pitzer/kobocat/contents/{+path}","compare_url":"https://api.github.com/repos/pitzer/kobocat/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pitzer/kobocat/merges","archive_url":"https://api.github.com/repos/pitzer/kobocat/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pitzer/kobocat/downloads","issues_url":"https://api.github.com/repos/pitzer/kobocat/issues{/number}","pulls_url":"https://api.github.com/repos/pitzer/kobocat/pulls{/number}","milestones_url":"https://api.github.com/repos/pitzer/kobocat/milestones{/number}","notifications_url":"https://api.github.com/repos/pitzer/kobocat/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pitzer/kobocat/labels{/name}","releases_url":"https://api.github.com/repos/pitzer/kobocat/releases{/id}","created_at":"2015-07-23T06:14:39Z","updated_at":"2015-07-23T06:14:42Z","pushed_at":"2015-07-28T07:33:14Z","git_url":"git://github.com/pitzer/kobocat.git","ssh_url":"git@github.com:pitzer/kobocat.git","clone_url":"https://github.com/pitzer/kobocat.git","svn_url":"https://github.com/pitzer/kobocat","homepage":"kobotoolbox.org","size":21999,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pitzer/kobocat/pulls/1"},"html":{"href":"https://github.com/pitzer/kobocat/pull/1"},"issue":{"href":"https://api.github.com/repos/pitzer/kobocat/issues/1"},"comments":{"href":"https://api.github.com/repos/pitzer/kobocat/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/pitzer/kobocat/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/pitzer/kobocat/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pitzer/kobocat/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/pitzer/kobocat/statuses/8651ad166e3d45096ff6fe50600b431619248631"}}}}