{"action":"created","comment":{"url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/comments/45965534","id":45965534,"diff_hunk":"@@ -1,22 +1,121 @@\n+# encoding: utf-8\n from django.db import models\n from django.contrib.postgres.fields import JSONField\n-import uuid\n from django.conf import settings\n+from django.db.models.query import QuerySet\n+import os\n+import tempfile\n+import subprocess\n+\n+\n+# Based on django-queryset-transform.\n+# This object however, operates on a per-object instance,\n+# so it doesn't break the result generators\n+\n+\n+class DecoratingQuerySet(QuerySet):\n+\n+    \"\"\"\n+    An enhancement of the QuerySet which allows objects to be decorated\n+    with extra properties before they are returned.\n+    \"\"\"\n+\n+    def __init__(self, *args, **kwargs):\n+        super(DecoratingQuerySet, self).__init__(*args, **kwargs)\n+        self._decorate_funcs = []\n+\n+    def _clone(self, **kw):\n+        c = super(DecoratingQuerySet, self)._clone(**kw)\n+        c._decorate_funcs = self._decorate_funcs[:]\n+        return c\n+\n+    def decorate(self, fn):\n+        \"\"\"\n+        Register a function which will decorate a retrieved object before it's returned.\n+        \"\"\"\n+        if fn not in self._decorate_funcs:\n+            self._decorate_funcs.append(fn)\n+        return self\n+\n+    def iterator(self):\n+        \"\"\"\n+        Overwritten iterator which will apply the decorate functions before returning it.\n+        \"\"\"\n+        base_iterator = super(DecoratingQuerySet, self).iterator()\n+        import ast\n+        # TODO: Do not run this if there are no functions to run, also pull\n+        # this out to it's own function.\n+        with tempfile.TemporaryDirectory(dir='/tmp/') as tmpdirname:\n+            print('created temporary directory', tmpdirname)\n+            with tempfile.NamedTemporaryFile(dir=tmpdirname, suffix='.py') as fp:\n+                print(fp.name)\n+                for obj in base_iterator:\n+                    mapped_data = obj.data\n+                    if self._decorate_funcs:\n+                        for fn in self._decorate_funcs:\n+                            fp.truncate()\n+                            code = '''\n+data={mapped_data}\n+\n+{fn}\n+'''.format(mapped_data=mapped_data, fn=fn)\n+                            fp.write(bytes(code, 'UTF-8'))\n+                            fp.seek(0)\n+                            raw_mapped_data, raw_err = subprocess.Popen([\"pypy-sandbox\", \"--timeout\", \"1\", \"--tmp\", tmpdirname, os.path.basename(fp.name)],\n+                                                                        stdout=subprocess.PIPE,\n+                                                                        stderr=subprocess.PIPE\n+                                                                        ).communicate()\n+                            err = '\\n'.join(\n+                                (raw_err.decode(\"utf-8\")).splitlines()[1:-1]) or None\n+\n+                            try:\n+                                if raw_mapped_data:\n+                                    mapped_data = (\n+                                        ast.literal_eval(raw_mapped_data.decode(\"utf-8\").strip()))\n+                                else:\n+                                    mapped_data = None\n+                            except (ValueError, SyntaxError) as e:\n+                                print(e, raw_mapped_data)\n+                                mapped_data = raw_mapped_data.decode(\n+                                    \"utf-8\").strip()\n+                        obj.mapped_data = mapped_data\n+                        obj.mapped_err = err\n+\n+                    else:\n+                        obj.mapped_data = None\n+\n+                    yield obj\n+\n+\n+class DecoratorManager(models.Manager):\n+\n+    \"\"\"\n+    The manager class which ensures the enhanced DecoratorQuerySet object is used.\n+    \"\"\"\n+\n+    def get_queryset(self):\n+        return DecoratingQuerySet(self.model)\n \n \n class Survey(models.Model):\n     name = models.CharField(max_length=15)\n-    schema = JSONField(blank=False, null=False)\n-    created_by = models.ForeignKey(\n-        settings.AUTH_USER_MODEL)\n+    schema = JSONField(blank=False, null=False, default=\"{}\")\n+    created = models.DateTimeField(auto_now_add=True, db_index=True)\n+    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True)\n \n     def __str__(self):","path":"gather2-core/core/models.py","position":110,"original_position":110,"commit_id":"e97a1313334a4f6b2c335a752e21069de3e855c1","original_commit_id":"e97a1313334a4f6b2c335a752e21069de3e855c1","user":{"login":"nkhumphreys","id":1306787,"avatar_url":"https://avatars.githubusercontent.com/u/1306787?v=3","gravatar_id":"","url":"https://api.github.com/users/nkhumphreys","html_url":"https://github.com/nkhumphreys","followers_url":"https://api.github.com/users/nkhumphreys/followers","following_url":"https://api.github.com/users/nkhumphreys/following{/other_user}","gists_url":"https://api.github.com/users/nkhumphreys/gists{/gist_id}","starred_url":"https://api.github.com/users/nkhumphreys/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nkhumphreys/subscriptions","organizations_url":"https://api.github.com/users/nkhumphreys/orgs","repos_url":"https://api.github.com/users/nkhumphreys/repos","events_url":"https://api.github.com/users/nkhumphreys/events{/privacy}","received_events_url":"https://api.github.com/users/nkhumphreys/received_events","type":"User","site_admin":false},"body":"You should also have a `__unicode__` method on every model for safety","created_at":"2015-11-26T10:49:21Z","updated_at":"2015-11-26T10:49:21Z","html_url":"https://github.com/eHealthAfrica/gather2/pull/16#discussion_r45965534","pull_request_url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16","_links":{"self":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/comments/45965534"},"html":{"href":"https://github.com/eHealthAfrica/gather2/pull/16#discussion_r45965534"},"pull_request":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16"}}},"pull_request":{"url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16","id":51833563,"html_url":"https://github.com/eHealthAfrica/gather2/pull/16","diff_url":"https://github.com/eHealthAfrica/gather2/pull/16.diff","patch_url":"https://github.com/eHealthAfrica/gather2/pull/16.patch","issue_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues/16","number":16,"state":"open","locked":false,"title":"Sandbox","user":{"login":"readevalprint","id":118430,"avatar_url":"https://avatars.githubusercontent.com/u/118430?v=3","gravatar_id":"","url":"https://api.github.com/users/readevalprint","html_url":"https://github.com/readevalprint","followers_url":"https://api.github.com/users/readevalprint/followers","following_url":"https://api.github.com/users/readevalprint/following{/other_user}","gists_url":"https://api.github.com/users/readevalprint/gists{/gist_id}","starred_url":"https://api.github.com/users/readevalprint/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/readevalprint/subscriptions","organizations_url":"https://api.github.com/users/readevalprint/orgs","repos_url":"https://api.github.com/users/readevalprint/repos","events_url":"https://api.github.com/users/readevalprint/events{/privacy}","received_events_url":"https://api.github.com/users/readevalprint/received_events","type":"User","site_admin":false},"body":"This is the first version of the map function in the pypy-sandbox. Added test coverage.\r\nThis is a super naive implementation with no caching.","created_at":"2015-11-25T18:58:40Z","updated_at":"2015-11-26T10:49:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"983b0814b0845d19565663825a6094c079bae47b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16/commits","review_comments_url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16/comments","review_comment_url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues/16/comments","statuses_url":"https://api.github.com/repos/eHealthAfrica/gather2/statuses/e97a1313334a4f6b2c335a752e21069de3e855c1","head":{"label":"eHealthAfrica:sandbox","ref":"sandbox","sha":"e97a1313334a4f6b2c335a752e21069de3e855c1","user":{"login":"eHealthAfrica","id":3214042,"avatar_url":"https://avatars.githubusercontent.com/u/3214042?v=3","gravatar_id":"","url":"https://api.github.com/users/eHealthAfrica","html_url":"https://github.com/eHealthAfrica","followers_url":"https://api.github.com/users/eHealthAfrica/followers","following_url":"https://api.github.com/users/eHealthAfrica/following{/other_user}","gists_url":"https://api.github.com/users/eHealthAfrica/gists{/gist_id}","starred_url":"https://api.github.com/users/eHealthAfrica/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eHealthAfrica/subscriptions","organizations_url":"https://api.github.com/users/eHealthAfrica/orgs","repos_url":"https://api.github.com/users/eHealthAfrica/repos","events_url":"https://api.github.com/users/eHealthAfrica/events{/privacy}","received_events_url":"https://api.github.com/users/eHealthAfrica/received_events","type":"Organization","site_admin":false},"repo":{"id":39896572,"name":"gather2","full_name":"eHealthAfrica/gather2","owner":{"login":"eHealthAfrica","id":3214042,"avatar_url":"https://avatars.githubusercontent.com/u/3214042?v=3","gravatar_id":"","url":"https://api.github.com/users/eHealthAfrica","html_url":"https://github.com/eHealthAfrica","followers_url":"https://api.github.com/users/eHealthAfrica/followers","following_url":"https://api.github.com/users/eHealthAfrica/following{/other_user}","gists_url":"https://api.github.com/users/eHealthAfrica/gists{/gist_id}","starred_url":"https://api.github.com/users/eHealthAfrica/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eHealthAfrica/subscriptions","organizations_url":"https://api.github.com/users/eHealthAfrica/orgs","repos_url":"https://api.github.com/users/eHealthAfrica/repos","events_url":"https://api.github.com/users/eHealthAfrica/events{/privacy}","received_events_url":"https://api.github.com/users/eHealthAfrica/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/eHealthAfrica/gather2","description":"Survey collection and analytics ","fork":false,"url":"https://api.github.com/repos/eHealthAfrica/gather2","forks_url":"https://api.github.com/repos/eHealthAfrica/gather2/forks","keys_url":"https://api.github.com/repos/eHealthAfrica/gather2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/eHealthAfrica/gather2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/eHealthAfrica/gather2/teams","hooks_url":"https://api.github.com/repos/eHealthAfrica/gather2/hooks","issue_events_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues/events{/number}","events_url":"https://api.github.com/repos/eHealthAfrica/gather2/events","assignees_url":"https://api.github.com/repos/eHealthAfrica/gather2/assignees{/user}","branches_url":"https://api.github.com/repos/eHealthAfrica/gather2/branches{/branch}","tags_url":"https://api.github.com/repos/eHealthAfrica/gather2/tags","blobs_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/refs{/sha}","trees_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/eHealthAfrica/gather2/statuses/{sha}","languages_url":"https://api.github.com/repos/eHealthAfrica/gather2/languages","stargazers_url":"https://api.github.com/repos/eHealthAfrica/gather2/stargazers","contributors_url":"https://api.github.com/repos/eHealthAfrica/gather2/contributors","subscribers_url":"https://api.github.com/repos/eHealthAfrica/gather2/subscribers","subscription_url":"https://api.github.com/repos/eHealthAfrica/gather2/subscription","commits_url":"https://api.github.com/repos/eHealthAfrica/gather2/commits{/sha}","git_commits_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/commits{/sha}","comments_url":"https://api.github.com/repos/eHealthAfrica/gather2/comments{/number}","issue_comment_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues/comments{/number}","contents_url":"https://api.github.com/repos/eHealthAfrica/gather2/contents/{+path}","compare_url":"https://api.github.com/repos/eHealthAfrica/gather2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/eHealthAfrica/gather2/merges","archive_url":"https://api.github.com/repos/eHealthAfrica/gather2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/eHealthAfrica/gather2/downloads","issues_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues{/number}","pulls_url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls{/number}","milestones_url":"https://api.github.com/repos/eHealthAfrica/gather2/milestones{/number}","notifications_url":"https://api.github.com/repos/eHealthAfrica/gather2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/eHealthAfrica/gather2/labels{/name}","releases_url":"https://api.github.com/repos/eHealthAfrica/gather2/releases{/id}","created_at":"2015-07-29T14:02:49Z","updated_at":"2015-09-28T16:05:54Z","pushed_at":"2015-11-25T20:37:25Z","git_url":"git://github.com/eHealthAfrica/gather2.git","ssh_url":"git@github.com:eHealthAfrica/gather2.git","clone_url":"https://github.com/eHealthAfrica/gather2.git","svn_url":"https://github.com/eHealthAfrica/gather2","homepage":null,"size":1648,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"eHealthAfrica:master","ref":"master","sha":"7c009ef9555f7f8de50693e360fb90d20a55c8f5","user":{"login":"eHealthAfrica","id":3214042,"avatar_url":"https://avatars.githubusercontent.com/u/3214042?v=3","gravatar_id":"","url":"https://api.github.com/users/eHealthAfrica","html_url":"https://github.com/eHealthAfrica","followers_url":"https://api.github.com/users/eHealthAfrica/followers","following_url":"https://api.github.com/users/eHealthAfrica/following{/other_user}","gists_url":"https://api.github.com/users/eHealthAfrica/gists{/gist_id}","starred_url":"https://api.github.com/users/eHealthAfrica/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eHealthAfrica/subscriptions","organizations_url":"https://api.github.com/users/eHealthAfrica/orgs","repos_url":"https://api.github.com/users/eHealthAfrica/repos","events_url":"https://api.github.com/users/eHealthAfrica/events{/privacy}","received_events_url":"https://api.github.com/users/eHealthAfrica/received_events","type":"Organization","site_admin":false},"repo":{"id":39896572,"name":"gather2","full_name":"eHealthAfrica/gather2","owner":{"login":"eHealthAfrica","id":3214042,"avatar_url":"https://avatars.githubusercontent.com/u/3214042?v=3","gravatar_id":"","url":"https://api.github.com/users/eHealthAfrica","html_url":"https://github.com/eHealthAfrica","followers_url":"https://api.github.com/users/eHealthAfrica/followers","following_url":"https://api.github.com/users/eHealthAfrica/following{/other_user}","gists_url":"https://api.github.com/users/eHealthAfrica/gists{/gist_id}","starred_url":"https://api.github.com/users/eHealthAfrica/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eHealthAfrica/subscriptions","organizations_url":"https://api.github.com/users/eHealthAfrica/orgs","repos_url":"https://api.github.com/users/eHealthAfrica/repos","events_url":"https://api.github.com/users/eHealthAfrica/events{/privacy}","received_events_url":"https://api.github.com/users/eHealthAfrica/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/eHealthAfrica/gather2","description":"Survey collection and analytics ","fork":false,"url":"https://api.github.com/repos/eHealthAfrica/gather2","forks_url":"https://api.github.com/repos/eHealthAfrica/gather2/forks","keys_url":"https://api.github.com/repos/eHealthAfrica/gather2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/eHealthAfrica/gather2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/eHealthAfrica/gather2/teams","hooks_url":"https://api.github.com/repos/eHealthAfrica/gather2/hooks","issue_events_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues/events{/number}","events_url":"https://api.github.com/repos/eHealthAfrica/gather2/events","assignees_url":"https://api.github.com/repos/eHealthAfrica/gather2/assignees{/user}","branches_url":"https://api.github.com/repos/eHealthAfrica/gather2/branches{/branch}","tags_url":"https://api.github.com/repos/eHealthAfrica/gather2/tags","blobs_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/refs{/sha}","trees_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/eHealthAfrica/gather2/statuses/{sha}","languages_url":"https://api.github.com/repos/eHealthAfrica/gather2/languages","stargazers_url":"https://api.github.com/repos/eHealthAfrica/gather2/stargazers","contributors_url":"https://api.github.com/repos/eHealthAfrica/gather2/contributors","subscribers_url":"https://api.github.com/repos/eHealthAfrica/gather2/subscribers","subscription_url":"https://api.github.com/repos/eHealthAfrica/gather2/subscription","commits_url":"https://api.github.com/repos/eHealthAfrica/gather2/commits{/sha}","git_commits_url":"https://api.github.com/repos/eHealthAfrica/gather2/git/commits{/sha}","comments_url":"https://api.github.com/repos/eHealthAfrica/gather2/comments{/number}","issue_comment_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues/comments{/number}","contents_url":"https://api.github.com/repos/eHealthAfrica/gather2/contents/{+path}","compare_url":"https://api.github.com/repos/eHealthAfrica/gather2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/eHealthAfrica/gather2/merges","archive_url":"https://api.github.com/repos/eHealthAfrica/gather2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/eHealthAfrica/gather2/downloads","issues_url":"https://api.github.com/repos/eHealthAfrica/gather2/issues{/number}","pulls_url":"https://api.github.com/repos/eHealthAfrica/gather2/pulls{/number}","milestones_url":"https://api.github.com/repos/eHealthAfrica/gather2/milestones{/number}","notifications_url":"https://api.github.com/repos/eHealthAfrica/gather2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/eHealthAfrica/gather2/labels{/name}","releases_url":"https://api.github.com/repos/eHealthAfrica/gather2/releases{/id}","created_at":"2015-07-29T14:02:49Z","updated_at":"2015-09-28T16:05:54Z","pushed_at":"2015-11-25T20:37:25Z","git_url":"git://github.com/eHealthAfrica/gather2.git","ssh_url":"git@github.com:eHealthAfrica/gather2.git","clone_url":"https://github.com/eHealthAfrica/gather2.git","svn_url":"https://github.com/eHealthAfrica/gather2","homepage":null,"size":1648,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16"},"html":{"href":"https://github.com/eHealthAfrica/gather2/pull/16"},"issue":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/issues/16"},"comments":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/issues/16/comments"},"review_comments":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16/comments"},"review_comment":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/pulls/16/commits"},"statuses":{"href":"https://api.github.com/repos/eHealthAfrica/gather2/statuses/e97a1313334a4f6b2c335a752e21069de3e855c1"}}}}