{"action":"created","comment":{"url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/69001972","id":69001972,"diff_hunk":"@@ -0,0 +1,68 @@\n+import logging\n+import re\n+\n+from django.core.management import call_command\n+from django.core.management.base import BaseCommand\n+\n+from corehq.toggles import all_toggles\n+from corehq.apps.domain.models import Domain\n+from corehq.apps.app_manager.dbaccessors import get_app, get_apps_in_domain\n+from corehq.apps.app_manager.util import ParentCasePropertyBuilder, save_xform\n+\n+logger = logging.getLogger('cmitfb_migrate_syntax')\n+logger.setLevel('DEBUG')\n+\n+\n+class Command(BaseCommand):\n+    help = '''\n+        Migrate apps using vellum case management from the old\n+        #case/type/property syntax to the new #case/relationship/property syntax.\n+    '''\n+\n+    affixes = [\"\", \"parent/\", \"grandparent/\"]\n+\n+    def _form_error(self, form, error=\"\"):\n+        logger.error(\"{} (domain {}, app {}, form {})\".format(error, form.get_app().domain,\n+                                                              form.get_app().id, form.unique_id))\n+\n+    def _replace_in_form(self, form, relationships, case_type, affix_index):\n+        if not case_type:\n+            return\n+        if affix_index < len(self.affixes):\n+            #logger.info(\"Replacing #case/{}/ with #case/{}\".format(case_type, self.affixes[affix_index]))\n+            form.source = form.source.replace(\"#case/{}/\".format(case_type), \"#case/{}\".format(self.affixes[affix_index]))\n+            parents = relationships[case_type].get(\"parent\", [])\n+            if len(parents) > 1:\n+                self._form_error(form, \"Multiple parents: {}\".format(\", \".join(parents)))\n+            elif len(parents) == 1:\n+                self._replace_in_form(form, relationships, parents[0], affix_index + 1)\n+        else:\n+            self._form_error(form, \"Hierarchy too deep\")\n+\n+    def handle(self, *args, **options):\n+        toggle_map = dict([(t.slug, t) for t in all_toggles()])\n+        domains = [row['key'] for row in Domain.get_all(include_docs=False)]\n+        for domain in domains:\n+            if toggle_map['rich_text'].enabled(domain) or toggle_map['experimental_ui'].enabled(domain):\n+                #logger.info('migrating domain {}'.format(domain))\n+                apps = get_apps_in_domain(domain, include_remote=False)\n+                for app in apps:\n+                    app_dirty = False\n+                    builder = ParentCasePropertyBuilder(app)\n+                    relationships = builder.get_parent_type_map(app.get_case_types(), allow_multiple_parents=True)\n+                    for module in app.modules:\n+                        for form in module.forms:\n+                            if form.doc_type != 'AdvancedForm' and form.requires_case():\n+                                #logger.info('migrating form {}'.format(form.name.get('en', form.name)))\n+                                base_case_type = form.get_module().case_type\n+                                self._replace_in_form(form, relationships, base_case_type, 0)\n+                                prefixes = re.findall(r'#case/\\w+/', form.source)\n+                                prefixes = set(prefixes)\n+                                for p in prefixes:\n+                                    if p != \"#case/parent/\" and p != \"#case/grandparent/\":\n+                                        self._form_error(form, \"Unknown prefix remaining: {}\".format(p))","path":"corehq/apps/app_manager/management/commands/cmitfb_migrate_syntax.py","position":73,"original_position":63,"commit_id":"a1feb40dbcd13c0408775efcf381aceb67842787","original_commit_id":"07a6ef278e0d6eab1834ba16d31b0d56626aa8a0","user":{"login":"orangejenny","id":1486591,"avatar_url":"https://avatars.githubusercontent.com/u/1486591?v=3","gravatar_id":"","url":"https://api.github.com/users/orangejenny","html_url":"https://github.com/orangejenny","followers_url":"https://api.github.com/users/orangejenny/followers","following_url":"https://api.github.com/users/orangejenny/following{/other_user}","gists_url":"https://api.github.com/users/orangejenny/gists{/gist_id}","starred_url":"https://api.github.com/users/orangejenny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/orangejenny/subscriptions","organizations_url":"https://api.github.com/users/orangejenny/orgs","repos_url":"https://api.github.com/users/orangejenny/repos","events_url":"https://api.github.com/users/orangejenny/events{/privacy}","received_events_url":"https://api.github.com/users/orangejenny/received_events","type":"User","site_admin":false},"body":"good though => https://github.com/dimagi/commcare-hq/pull/12252/commits/a1feb40dbcd13c0408775efcf381aceb67842787","created_at":"2016-06-29T18:29:55Z","updated_at":"2016-06-29T18:29:55Z","html_url":"https://github.com/dimagi/commcare-hq/pull/12252#discussion_r69001972","pull_request_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252","_links":{"self":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/69001972"},"html":{"href":"https://github.com/dimagi/commcare-hq/pull/12252#discussion_r69001972"},"pull_request":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252"}}},"pull_request":{"url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252","id":75390342,"html_url":"https://github.com/dimagi/commcare-hq/pull/12252","diff_url":"https://github.com/dimagi/commcare-hq/pull/12252.diff","patch_url":"https://github.com/dimagi/commcare-hq/pull/12252.patch","issue_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/12252","number":12252,"state":"open","locked":false,"title":"Update vellum's datasources structure","user":{"login":"orangejenny","id":1486591,"avatar_url":"https://avatars.githubusercontent.com/u/1486591?v=3","gravatar_id":"","url":"https://api.github.com/users/orangejenny","html_url":"https://github.com/orangejenny","followers_url":"https://api.github.com/users/orangejenny/followers","following_url":"https://api.github.com/users/orangejenny/following{/other_user}","gists_url":"https://api.github.com/users/orangejenny/gists{/gist_id}","starred_url":"https://api.github.com/users/orangejenny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/orangejenny/subscriptions","organizations_url":"https://api.github.com/users/orangejenny/orgs","repos_url":"https://api.github.com/users/orangejenny/repos","events_url":"https://api.github.com/users/orangejenny/events{/privacy}","received_events_url":"https://api.github.com/users/orangejenny/received_events","type":"User","site_admin":false},"body":"opening for tests","created_at":"2016-06-27T22:26:06Z","updated_at":"2016-06-29T18:29:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c0d93629da61b2ad3ff1989d392787922d3efc07","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252/commits","review_comments_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252/comments","review_comment_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/12252/comments","statuses_url":"https://api.github.com/repos/dimagi/commcare-hq/statuses/a1feb40dbcd13c0408775efcf381aceb67842787","head":{"label":"dimagi:vcm-datasources","ref":"vcm-datasources","sha":"a1feb40dbcd13c0408775efcf381aceb67842787","user":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"repo":{"id":247278,"name":"commcare-hq","full_name":"dimagi/commcare-hq","owner":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dimagi/commcare-hq","description":"A server-side tool to help manage community health workers, written in Django","fork":false,"url":"https://api.github.com/repos/dimagi/commcare-hq","forks_url":"https://api.github.com/repos/dimagi/commcare-hq/forks","keys_url":"https://api.github.com/repos/dimagi/commcare-hq/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dimagi/commcare-hq/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dimagi/commcare-hq/teams","hooks_url":"https://api.github.com/repos/dimagi/commcare-hq/hooks","issue_events_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/events{/number}","events_url":"https://api.github.com/repos/dimagi/commcare-hq/events","assignees_url":"https://api.github.com/repos/dimagi/commcare-hq/assignees{/user}","branches_url":"https://api.github.com/repos/dimagi/commcare-hq/branches{/branch}","tags_url":"https://api.github.com/repos/dimagi/commcare-hq/tags","blobs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dimagi/commcare-hq/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/refs{/sha}","trees_url":"https://api.github.com/repos/dimagi/commcare-hq/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dimagi/commcare-hq/statuses/{sha}","languages_url":"https://api.github.com/repos/dimagi/commcare-hq/languages","stargazers_url":"https://api.github.com/repos/dimagi/commcare-hq/stargazers","contributors_url":"https://api.github.com/repos/dimagi/commcare-hq/contributors","subscribers_url":"https://api.github.com/repos/dimagi/commcare-hq/subscribers","subscription_url":"https://api.github.com/repos/dimagi/commcare-hq/subscription","commits_url":"https://api.github.com/repos/dimagi/commcare-hq/commits{/sha}","git_commits_url":"https://api.github.com/repos/dimagi/commcare-hq/git/commits{/sha}","comments_url":"https://api.github.com/repos/dimagi/commcare-hq/comments{/number}","issue_comment_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/comments{/number}","contents_url":"https://api.github.com/repos/dimagi/commcare-hq/contents/{+path}","compare_url":"https://api.github.com/repos/dimagi/commcare-hq/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dimagi/commcare-hq/merges","archive_url":"https://api.github.com/repos/dimagi/commcare-hq/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dimagi/commcare-hq/downloads","issues_url":"https://api.github.com/repos/dimagi/commcare-hq/issues{/number}","pulls_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls{/number}","milestones_url":"https://api.github.com/repos/dimagi/commcare-hq/milestones{/number}","notifications_url":"https://api.github.com/repos/dimagi/commcare-hq/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dimagi/commcare-hq/labels{/name}","releases_url":"https://api.github.com/repos/dimagi/commcare-hq/releases{/id}","deployments_url":"https://api.github.com/repos/dimagi/commcare-hq/deployments","created_at":"2009-07-09T17:00:07Z","updated_at":"2016-05-16T15:23:03Z","pushed_at":"2016-06-29T18:29:25Z","git_url":"git://github.com/dimagi/commcare-hq.git","ssh_url":"git@github.com:dimagi/commcare-hq.git","clone_url":"https://github.com/dimagi/commcare-hq.git","svn_url":"https://github.com/dimagi/commcare-hq","homepage":"http://www.commcarehq.org","size":302470,"stargazers_count":72,"watchers_count":72,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":69,"mirror_url":null,"open_issues_count":44,"forks":69,"open_issues":44,"watchers":72,"default_branch":"master"}},"base":{"label":"dimagi:master","ref":"master","sha":"a9af20013327932e61a0308ff8192c6953240722","user":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"repo":{"id":247278,"name":"commcare-hq","full_name":"dimagi/commcare-hq","owner":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dimagi/commcare-hq","description":"A server-side tool to help manage community health workers, written in Django","fork":false,"url":"https://api.github.com/repos/dimagi/commcare-hq","forks_url":"https://api.github.com/repos/dimagi/commcare-hq/forks","keys_url":"https://api.github.com/repos/dimagi/commcare-hq/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dimagi/commcare-hq/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dimagi/commcare-hq/teams","hooks_url":"https://api.github.com/repos/dimagi/commcare-hq/hooks","issue_events_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/events{/number}","events_url":"https://api.github.com/repos/dimagi/commcare-hq/events","assignees_url":"https://api.github.com/repos/dimagi/commcare-hq/assignees{/user}","branches_url":"https://api.github.com/repos/dimagi/commcare-hq/branches{/branch}","tags_url":"https://api.github.com/repos/dimagi/commcare-hq/tags","blobs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dimagi/commcare-hq/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/refs{/sha}","trees_url":"https://api.github.com/repos/dimagi/commcare-hq/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dimagi/commcare-hq/statuses/{sha}","languages_url":"https://api.github.com/repos/dimagi/commcare-hq/languages","stargazers_url":"https://api.github.com/repos/dimagi/commcare-hq/stargazers","contributors_url":"https://api.github.com/repos/dimagi/commcare-hq/contributors","subscribers_url":"https://api.github.com/repos/dimagi/commcare-hq/subscribers","subscription_url":"https://api.github.com/repos/dimagi/commcare-hq/subscription","commits_url":"https://api.github.com/repos/dimagi/commcare-hq/commits{/sha}","git_commits_url":"https://api.github.com/repos/dimagi/commcare-hq/git/commits{/sha}","comments_url":"https://api.github.com/repos/dimagi/commcare-hq/comments{/number}","issue_comment_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/comments{/number}","contents_url":"https://api.github.com/repos/dimagi/commcare-hq/contents/{+path}","compare_url":"https://api.github.com/repos/dimagi/commcare-hq/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dimagi/commcare-hq/merges","archive_url":"https://api.github.com/repos/dimagi/commcare-hq/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dimagi/commcare-hq/downloads","issues_url":"https://api.github.com/repos/dimagi/commcare-hq/issues{/number}","pulls_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls{/number}","milestones_url":"https://api.github.com/repos/dimagi/commcare-hq/milestones{/number}","notifications_url":"https://api.github.com/repos/dimagi/commcare-hq/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dimagi/commcare-hq/labels{/name}","releases_url":"https://api.github.com/repos/dimagi/commcare-hq/releases{/id}","deployments_url":"https://api.github.com/repos/dimagi/commcare-hq/deployments","created_at":"2009-07-09T17:00:07Z","updated_at":"2016-05-16T15:23:03Z","pushed_at":"2016-06-29T18:29:25Z","git_url":"git://github.com/dimagi/commcare-hq.git","ssh_url":"git@github.com:dimagi/commcare-hq.git","clone_url":"https://github.com/dimagi/commcare-hq.git","svn_url":"https://github.com/dimagi/commcare-hq","homepage":"http://www.commcarehq.org","size":302470,"stargazers_count":72,"watchers_count":72,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":69,"mirror_url":null,"open_issues_count":44,"forks":69,"open_issues":44,"watchers":72,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252"},"html":{"href":"https://github.com/dimagi/commcare-hq/pull/12252"},"issue":{"href":"https://api.github.com/repos/dimagi/commcare-hq/issues/12252"},"comments":{"href":"https://api.github.com/repos/dimagi/commcare-hq/issues/12252/comments"},"review_comments":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252/comments"},"review_comment":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/12252/commits"},"statuses":{"href":"https://api.github.com/repos/dimagi/commcare-hq/statuses/a1feb40dbcd13c0408775efcf381aceb67842787"}}}}
{"actor":{"display_login":"orangejenny"}}