{"action":"created","comment":{"url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/24057846","id":24057846,"diff_hunk":"@@ -87,4 +93,102 @@ def get_open_sms_session(cls, domain, contact_id):\n                            include_docs=True).one()\n         return session\n \n+\n+class SQLXFormsSession(models.Model):\n+    \"\"\"\n+    Keeps information about an SMS XForm session.\n+    \"\"\"\n+    # generic properties\n+    couch_id = models.CharField(null=True, blank=True, max_length=50)\n+    connection_id = models.CharField(null=True, blank=True, db_index=True, max_length=50)\n+    session_id = models.CharField(null=True, blank=True, max_length=50)\n+    form_xmlns = models.CharField(null=True, blank=True, max_length=100)\n+    start_time = models.DateTimeField()\n+    modified_time = models.DateTimeField()\n+    end_time = models.DateTimeField(null=True)\n+    completed = models.BooleanField()\n+\n+    # HQ specific properties\n+    domain = models.CharField(null=True, blank=True, db_index=True, max_length=100)\n+    user_id = models.CharField(null=True, blank=True, max_length=50)\n+    app_id = models.CharField(null=True, blank=True, max_length=50)\n+    submission_id = models.CharField(null=True, blank=True, max_length=50)\n+    survey_incentive = models.CharField(null=True, blank=True, max_length=100)\n+    session_type = models.CharField(max_length=10, choices=zip(XFORMS_SESSION_TYPES, XFORMS_SESSION_TYPES),\n+                                    default=XFORMS_SESSION_SMS)\n+    workflow = models.CharField(null=True, blank=True, max_length=20)\n+    reminder_id = models.CharField(null=True, blank=True, max_length=50)\n+\n+    def __unicode__(self):\n+        return 'Form %(form)s in domain %(domain)s. Last modified: %(mod)s' % \\\n+            {\"form\": self.form_xmlns,\n+             \"domain\": self.domain,\n+             \"mod\": self.modified_time}\n+\n+    def end(self, completed):\n+        \"\"\"\n+        Marks this as ended (by setting end time).\n+        \"\"\"\n+        self.completed = completed\n+        self.modified_time = self.end_time = datetime.utcnow()\n+\n+    @property\n+    def is_open(self):\n+        \"\"\"\n+        True if this session is still open, False otherwise.\n+        \"\"\"\n+        return self.end_time is None\n+\n+    @classmethod\n+    def get_all_open_sms_sessions(cls, domain, contact_id):\n+        return cls.objects.filter(\n+            Q(session_type__isnull=True) | Q(session_type=XFORMS_SESSION_SMS),\n+            domain=domain,\n+            connection_id=contact_id,\n+            end_time__isnull=True,\n+        )\n+\n+    @classmethod\n+    def close_all_open_sms_sessions(cls, domain, contact_id):\n+        sessions = cls.get_all_open_sms_sessions(domain, contact_id)\n+        for session in sessions:\n+            session.end(False)\n+            session.save()\n+\n+    @classmethod\n+    def by_session_id(cls, id):\n+        return cls.get(session_id=id)\n+\n+    @classmethod\n+    def get_open_sms_session(cls, domain, contact_id):\n+        \"\"\"\n+        Looks up the open sms survey session for the given domain and contact_id.\n+        Only one session is expected to be open at a time.\n+        Raises MultipleResultsFound if more than one session is open.\n+        \"\"\"\n+        objs = cls.get_all_open_sms_sessions(domain, contact_id).all()\n+        if len(objs) > 1:\n+            raise MultipleResultsFound('more than 1 ({}) session found for domain {} and contact {}'.format(\n+                len(objs), domain, contact_id\n+            ))\n+        elif len(objs) == 0:\n+            raise cls.DoesNotExist('no sessions found for domain {} and contact {}'.format(","path":"corehq/apps/smsforms/models.py","position":113,"original_position":113,"commit_id":"c416894072e5b9d52d2e97471e00a9e68a4e3f3e","original_commit_id":"c416894072e5b9d52d2e97471e00a9e68a4e3f3e","user":{"login":"gcapalbo","id":1028814,"avatar_url":"https://avatars.githubusercontent.com/u/1028814?v=3","gravatar_id":"","url":"https://api.github.com/users/gcapalbo","html_url":"https://github.com/gcapalbo","followers_url":"https://api.github.com/users/gcapalbo/followers","following_url":"https://api.github.com/users/gcapalbo/following{/other_user}","gists_url":"https://api.github.com/users/gcapalbo/gists{/gist_id}","starred_url":"https://api.github.com/users/gcapalbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gcapalbo/subscriptions","organizations_url":"https://api.github.com/users/gcapalbo/orgs","repos_url":"https://api.github.com/users/gcapalbo/repos","events_url":"https://api.github.com/users/gcapalbo/events{/privacy}","received_events_url":"https://api.github.com/users/gcapalbo/received_events","type":"User","site_admin":false},"body":"We shouldn't raise DoesNotExist here since that wasn't the behavior that couch had. Instead, we should just return None","created_at":"2015-02-04T02:00:06Z","updated_at":"2015-02-04T02:00:06Z","html_url":"https://github.com/dimagi/commcare-hq/pull/5427#discussion_r24057846","pull_request_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/5427","_links":{"self":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/24057846"},"html":{"href":"https://github.com/dimagi/commcare-hq/pull/5427#discussion_r24057846"},"pull_request":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/5427"}}},"pull_request":{"url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/5427","id":28567044,"html_url":"https://github.com/dimagi/commcare-hq/pull/5427","diff_url":"https://github.com/dimagi/commcare-hq/pull/5427.diff","patch_url":"https://github.com/dimagi/commcare-hq/pull/5427.patch","issue_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/5427","number":5427,"state":"open","locked":false,"title":"save touchforms sessions in SQL","user":{"login":"czue","id":66555,"avatar_url":"https://avatars.githubusercontent.com/u/66555?v=3","gravatar_id":"","url":"https://api.github.com/users/czue","html_url":"https://github.com/czue","followers_url":"https://api.github.com/users/czue/followers","following_url":"https://api.github.com/users/czue/following{/other_user}","gists_url":"https://api.github.com/users/czue/gists{/gist_id}","starred_url":"https://api.github.com/users/czue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/czue/subscriptions","organizations_url":"https://api.github.com/users/czue/orgs","repos_url":"https://api.github.com/users/czue/repos","events_url":"https://api.github.com/users/czue/events{/privacy}","received_events_url":"https://api.github.com/users/czue/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-02-03T16:35:30Z","updated_at":"2015-02-04T02:00:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3c5d06235d7306f431f65dc574ba176eecbcf984","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/5427/commits","review_comments_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/5427/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/5427/comments","statuses_url":"https://api.github.com/repos/dimagi/commcare-hq/statuses/c416894072e5b9d52d2e97471e00a9e68a4e3f3e","head":{"label":"dimagi:session-to-sql","ref":"session-to-sql","sha":"c416894072e5b9d52d2e97471e00a9e68a4e3f3e","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}","created_at":"2009-07-09T17:00:07Z","updated_at":"2015-02-03T20:10:08Z","pushed_at":"2015-02-03T23:38:51Z","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":192719,"stargazers_count":43,"watchers_count":43,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":57,"mirror_url":null,"open_issues_count":40,"forks":57,"open_issues":40,"watchers":43,"default_branch":"master"}},"base":{"label":"dimagi:master","ref":"master","sha":"f003b8cc8bfa588c0f98648131154fc01a0770c5","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}","created_at":"2009-07-09T17:00:07Z","updated_at":"2015-02-03T20:10:08Z","pushed_at":"2015-02-03T23:38:51Z","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":192719,"stargazers_count":43,"watchers_count":43,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":57,"mirror_url":null,"open_issues_count":40,"forks":57,"open_issues":40,"watchers":43,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/5427"},"html":{"href":"https://github.com/dimagi/commcare-hq/pull/5427"},"issue":{"href":"https://api.github.com/repos/dimagi/commcare-hq/issues/5427"},"comments":{"href":"https://api.github.com/repos/dimagi/commcare-hq/issues/5427/comments"},"review_comments":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/5427/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/5427/commits"},"statuses":{"href":"https://api.github.com/repos/dimagi/commcare-hq/statuses/c416894072e5b9d52d2e97471e00a9e68a4e3f3e"}}}}