{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/153548420","pull_request_review_id":79569882,"id":153548420,"diff_hunk":"@@ -51,3 +54,105 @@ def perform_destroy(self, instance):\n             )\n         if save_model:\n             instance.save()\n+\n+\n+class EntitlementEnrollmentViewSet(viewsets.GenericViewSet):\n+    authentication_classes = (JwtAuthentication, SessionAuthentication,)\n+    permission_classes = (permissions.IsAuthenticated,)\n+    queryset = CourseEntitlement.objects.all()\n+    serializer_class = CourseEntitlementSerializer\n+\n+    def _enroll_entitlement(self, entitlement, course_session_key, user):\n+        enrollment = CourseEnrollment.enroll(\n+            user=user,\n+            course_key=course_session_key,\n+            mode=entitlement.mode,\n+        )\n+\n+        CourseEntitlement.set_enrollment(entitlement, enrollment)\n+\n+    def _unenroll_entitlement(self, entitlement, course_session_key, user):\n+        CourseEnrollment.unenroll(user, course_session_key, skip_refund=True)\n+        CourseEntitlement.set_enrollment(entitlement, None)\n+\n+    def create(self, request, uuid):\n+        course_session_id = request.data.get('course_run_id', None)\n+\n+        if not course_session_id:\n+            return Response(\n+                status=status.HTTP_400_BAD_REQUEST,\n+                data=\"The Course Run ID was not provided.\"\n+            )\n+\n+        # Verify that the user has an Entitlement for the provided Course UUID.\n+        try:\n+            entitlement = CourseEntitlement.objects.get(uuid=uuid, user=request.user, expired_at=None)\n+        except CourseEntitlement.DoesNotExist:\n+            return Response(\n+                status=status.HTTP_400_BAD_REQUEST,\n+                data=\"The Entitlement for this UUID does not exist or is Expired.\"\n+            )\n+\n+        # Verify the course run ID is of the same type as the Course entitlement.\n+        course_run_valid = False\n+        course_runs = get_course_runs_for_course(entitlement.course_uuid)\n+        for run in course_runs:\n+            if course_session_id == run.get('key', ''):\n+                course_run_valid = True\n+\n+        if not course_run_valid:\n+            return Response(\n+                status=status.HTTP_400_BAD_REQUEST,\n+                data=\"The Course Run ID is not a match for this Course Entitlement.\"\n+            )\n+\n+        # Determine if this is a Switch session or a simple enroll and handle both.\n+        if entitlement.enrollment_course_run is None:\n+            self._enroll_entitlement(\n+                entitlement=entitlement,\n+                course_session_key=CourseKey.from_string(course_session_id),\n+                user=request.user\n+            )\n+        else:\n+            if entitlement.enrollment_course_run.course_id != course_session_id:","path":"common/djangoapps/entitlements/api/v1/views.py","position":82,"original_position":82,"commit_id":"d1b03601ba43f50a0153fa402ea658b3a1928de4","original_commit_id":"d1b03601ba43f50a0153fa402ea658b3a1928de4","user":{"login":"amangano-edx","id":24230895,"avatar_url":"https://avatars2.githubusercontent.com/u/24230895?v=4","gravatar_id":"","url":"https://api.github.com/users/amangano-edx","html_url":"https://github.com/amangano-edx","followers_url":"https://api.github.com/users/amangano-edx/followers","following_url":"https://api.github.com/users/amangano-edx/following{/other_user}","gists_url":"https://api.github.com/users/amangano-edx/gists{/gist_id}","starred_url":"https://api.github.com/users/amangano-edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amangano-edx/subscriptions","organizations_url":"https://api.github.com/users/amangano-edx/orgs","repos_url":"https://api.github.com/users/amangano-edx/repos","events_url":"https://api.github.com/users/amangano-edx/events{/privacy}","received_events_url":"https://api.github.com/users/amangano-edx/received_events","type":"User","site_admin":false},"body":"Nit: this could be moved to line 116 and changed to `elif`","created_at":"2017-11-28T16:33:57Z","updated_at":"2017-11-28T16:33:57Z","html_url":"https://github.com/edx/edx-platform/pull/16617#discussion_r153548420","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/16617","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/153548420"},"html":{"href":"https://github.com/edx/edx-platform/pull/16617#discussion_r153548420"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/16617"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/16617","id":153390594,"html_url":"https://github.com/edx/edx-platform/pull/16617","diff_url":"https://github.com/edx/edx-platform/pull/16617.diff","patch_url":"https://github.com/edx/edx-platform/pull/16617.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/16617","number":16617,"state":"open","locked":false,"title":"Aj/entitlement session backend","user":{"login":"staubina","id":2854941,"avatar_url":"https://avatars2.githubusercontent.com/u/2854941?v=4","gravatar_id":"","url":"https://api.github.com/users/staubina","html_url":"https://github.com/staubina","followers_url":"https://api.github.com/users/staubina/followers","following_url":"https://api.github.com/users/staubina/following{/other_user}","gists_url":"https://api.github.com/users/staubina/gists{/gist_id}","starred_url":"https://api.github.com/users/staubina/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/staubina/subscriptions","organizations_url":"https://api.github.com/users/staubina/orgs","repos_url":"https://api.github.com/users/staubina/repos","events_url":"https://api.github.com/users/staubina/events{/privacy}","received_events_url":"https://api.github.com/users/staubina/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-11-17T20:48:04Z","updated_at":"2017-11-28T16:33:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9ca898d40ef5a148666e6bf81bc71d87280fe5dc","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/16617/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/16617/comments","review_comment_url":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/edx-platform/issues/16617/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/d1b03601ba43f50a0153fa402ea658b3a1928de4","head":{"label":"edx:aj/entitlement_session_backend","ref":"aj/entitlement_session_backend","sha":"d1b03601ba43f50a0153fa402ea658b3a1928de4","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars3.githubusercontent.com/u/3179841?v=4","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":10391073,"name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars3.githubusercontent.com/u/3179841?v=4","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-platform","description":"The Open edX platform, the software that powers edX!","fork":false,"url":"https://api.github.com/repos/edx/edx-platform","forks_url":"https://api.github.com/repos/edx/edx-platform/forks","keys_url":"https://api.github.com/repos/edx/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-platform/teams","hooks_url":"https://api.github.com/repos/edx/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-platform/events","assignees_url":"https://api.github.com/repos/edx/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-platform/tags","blobs_url":"https://api.github.com/repos/edx/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-platform/languages","stargazers_url":"https://api.github.com/repos/edx/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-platform/subscription","commits_url":"https://api.github.com/repos/edx/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-platform/merges","archive_url":"https://api.github.com/repos/edx/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-platform/downloads","issues_url":"https://api.github.com/repos/edx/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-platform/releases{/id}","deployments_url":"https://api.github.com/repos/edx/edx-platform/deployments","created_at":"2013-05-30T20:20:38Z","updated_at":"2017-11-27T21:27:36Z","pushed_at":"2017-11-28T16:26:20Z","git_url":"git://github.com/edx/edx-platform.git","ssh_url":"git@github.com:edx/edx-platform.git","clone_url":"https://github.com/edx/edx-platform.git","svn_url":"https://github.com/edx/edx-platform","homepage":"http://open.edx.org/","size":862729,"stargazers_count":3412,"watchers_count":3412,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1938,"mirror_url":null,"archived":false,"open_issues_count":210,"forks":1938,"open_issues":210,"watchers":3412,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"29f818076350eb425b7fdb66699484c85fb4499d","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars3.githubusercontent.com/u/3179841?v=4","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":10391073,"name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars3.githubusercontent.com/u/3179841?v=4","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-platform","description":"The Open edX platform, the software that powers edX!","fork":false,"url":"https://api.github.com/repos/edx/edx-platform","forks_url":"https://api.github.com/repos/edx/edx-platform/forks","keys_url":"https://api.github.com/repos/edx/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-platform/teams","hooks_url":"https://api.github.com/repos/edx/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-platform/events","assignees_url":"https://api.github.com/repos/edx/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-platform/tags","blobs_url":"https://api.github.com/repos/edx/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-platform/languages","stargazers_url":"https://api.github.com/repos/edx/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-platform/subscription","commits_url":"https://api.github.com/repos/edx/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-platform/merges","archive_url":"https://api.github.com/repos/edx/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-platform/downloads","issues_url":"https://api.github.com/repos/edx/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-platform/releases{/id}","deployments_url":"https://api.github.com/repos/edx/edx-platform/deployments","created_at":"2013-05-30T20:20:38Z","updated_at":"2017-11-27T21:27:36Z","pushed_at":"2017-11-28T16:26:20Z","git_url":"git://github.com/edx/edx-platform.git","ssh_url":"git@github.com:edx/edx-platform.git","clone_url":"https://github.com/edx/edx-platform.git","svn_url":"https://github.com/edx/edx-platform","homepage":"http://open.edx.org/","size":862729,"stargazers_count":3412,"watchers_count":3412,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1938,"mirror_url":null,"archived":false,"open_issues_count":210,"forks":1938,"open_issues":210,"watchers":3412,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/16617"},"html":{"href":"https://github.com/edx/edx-platform/pull/16617"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/16617"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/16617/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/16617/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/16617/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/d1b03601ba43f50a0153fa402ea658b3a1928de4"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"amangano-edx"}}