{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/203290606","pull_request_review_id":138147092,"id":203290606,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwMzI5MDYwNg==","diff_hunk":"@@ -0,0 +1,111 @@\n+\"\"\"\n+Management command to change many user enrollments in many courses using\n+csv file.\n+\"\"\"\n+import csv\n+import logging\n+from os import path\n+\n+from django.core.management.base import BaseCommand, CommandError\n+from django.db import transaction\n+from opaque_keys import InvalidKeyError\n+from opaque_keys.edx.keys import CourseKey\n+\n+from student.models import CourseEnrollment, CourseEnrollmentAttribute, User\n+\n+logger = logging.getLogger(__name__)  # pylint: disable=invalid-name\n+\n+\n+class Command(BaseCommand):\n+    \"\"\"\n+        Management command to change many user enrollments in many\n+        courses using the csv file\n+    \"\"\"\n+\n+    help = \"\"\"\n+        Change the enrollment status of all the users specified in\n+        the csv file in the specified course to specified course\n+        mode.\n+        Could be used to update effected users by order\n+        placement issues. If large number of students are effected\n+        in different courses.\n+        Similar to bulk_change_enrollment but uses the csv file\n+        input format and can enroll students in multiple courses.\n+\n+        Example:\n+            $ ... bulk_change_enrollment_csv csv_file_path\n+        \"\"\"\n+\n+    def add_arguments(self, parser):\n+        \"\"\" Add argument to the command parser. \"\"\"\n+        parser.add_argument(\n+            '--csv_file_path',\n+            required=True,\n+            help='Csv file path'\n+        )\n+\n+    def handle(self, *args, **options):\n+        \"\"\" Main handler for the command.\"\"\"\n+        file_path = options['csv_file_path']\n+\n+        if not path.isfile(file_path):\n+            raise CommandError(\"File not found.\")\n+\n+        with open(file_path, 'rb') as file:\n+            file_reader = csv.DictReader(file)\n+            for row in file_reader:\n+                try:\n+                    course_key = CourseKey.from_string(row['course_id'])\n+                except InvalidKeyError:\n+                    raise CommandError('Invalid or non-existant course id {}'.format(row['course_id']))\n+\n+                try:\n+                    user = User.objects.get(username=row['user'])\n+                except:\n+                    raise CommandError('Invalid or non-existant user {}'.format(row['user']))\n+\n+                try:\n+                    course_enrollment = CourseEnrollment.get_enrollment(user, course_key)\n+                    #  If student is not enrolled in course enroll the student in free mode\n+                    if not course_enrollment:\n+                        # try to create a enroll user in default course enrollment mode in case of\n+                        # professional it will break because of no default course mode.\n+                        try:\n+                            course_enrollment = CourseEnrollment.get_or_create_enrollment(user=user,\n+                                                                                          course_key=course_key)\n+                        except Exception:  # pylint: disable=broad-except\n+                            # In case if no free mode is available.\n+                            course_enrollment = None\n+                    # if student already had a enrollment and its mode is same as the provided one\n+                    if course_enrollment and course_enrollment.mode == row['mode']:\n+                        logger.info(\"Student [%s] is already enrolled in Course [%s] in mode [%s].\", user.username,\n+                                    course_key, course_enrollment.mode)\n+                        # set the enrollment to active if its not already active.\n+                        if not course_enrollment.is_active:\n+                            course_enrollment.is_active = True\n+                        course_enrollment.save()\n+                    elif course_enrollment:\n+                        # if student enrollment exists update it to new mode.\n+                        with transaction.atomic():\n+                            course_enrollment.update_enrollment(\n+                                mode=row['mode'],\n+                                is_active=True,\n+                                skip_refund=True\n+                            )\n+                            course_enrollment.save()\n+                            enrollment_attrs = []","path":"common/djangoapps/student/management/commands/bulk_change_enrollment_csv.py","position":96,"original_position":96,"commit_id":"616be99980f2e5e1a76d5fd4b20336fee3cb9053","original_commit_id":"616be99980f2e5e1a76d5fd4b20336fee3cb9053","user":{"login":"tasawernawaz","id":4245618,"node_id":"MDQ6VXNlcjQyNDU2MTg=","avatar_url":"https://avatars2.githubusercontent.com/u/4245618?v=4","gravatar_id":"","url":"https://api.github.com/users/tasawernawaz","html_url":"https://github.com/tasawernawaz","followers_url":"https://api.github.com/users/tasawernawaz/followers","following_url":"https://api.github.com/users/tasawernawaz/following{/other_user}","gists_url":"https://api.github.com/users/tasawernawaz/gists{/gist_id}","starred_url":"https://api.github.com/users/tasawernawaz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tasawernawaz/subscriptions","organizations_url":"https://api.github.com/users/tasawernawaz/orgs","repos_url":"https://api.github.com/users/tasawernawaz/repos","events_url":"https://api.github.com/users/tasawernawaz/events{/privacy}","received_events_url":"https://api.github.com/users/tasawernawaz/received_events","type":"User","site_admin":false},"body":"you can remove this line, and instead of appending you can simply create list at line 98.","created_at":"2018-07-18T08:23:09Z","updated_at":"2018-07-18T08:24:13Z","html_url":"https://github.com/edx/edx-platform/pull/18583#discussion_r203290606","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/18583","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/203290606"},"html":{"href":"https://github.com/edx/edx-platform/pull/18583#discussion_r203290606"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/18583"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/18583","id":201270378,"node_id":"MDExOlB1bGxSZXF1ZXN0MjAxMjcwMzc4","html_url":"https://github.com/edx/edx-platform/pull/18583","diff_url":"https://github.com/edx/edx-platform/pull/18583.diff","patch_url":"https://github.com/edx/edx-platform/pull/18583.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/18583","number":18583,"state":"open","locked":false,"title":"(WIP) bulk change enrollment using csv file","user":{"login":"Ayub-Khan","id":7721119,"node_id":"MDQ6VXNlcjc3MjExMTk=","avatar_url":"https://avatars1.githubusercontent.com/u/7721119?v=4","gravatar_id":"","url":"https://api.github.com/users/Ayub-Khan","html_url":"https://github.com/Ayub-Khan","followers_url":"https://api.github.com/users/Ayub-Khan/followers","following_url":"https://api.github.com/users/Ayub-Khan/following{/other_user}","gists_url":"https://api.github.com/users/Ayub-Khan/gists{/gist_id}","starred_url":"https://api.github.com/users/Ayub-Khan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Ayub-Khan/subscriptions","organizations_url":"https://api.github.com/users/Ayub-Khan/orgs","repos_url":"https://api.github.com/users/Ayub-Khan/repos","events_url":"https://api.github.com/users/Ayub-Khan/events{/privacy}","received_events_url":"https://api.github.com/users/Ayub-Khan/received_events","type":"User","site_admin":false},"body":"Enables us to enroll students using a cv file. \r\ncsv format 'user, mode, course_id'\r\ncan enroll multiple users in multiple courses.\r\n\r\nI think testing needed so keeping it in (WIP)\r\n","created_at":"2018-07-13T11:58:42Z","updated_at":"2018-07-18T08:24:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9ad5f9cfb810a60ce52eb2e4ce4b647e1dcdc653","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/18583/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/18583/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/18583/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/616be99980f2e5e1a76d5fd4b20336fee3cb9053","head":{"label":"edx:LEARNER-5603-managment-command","ref":"LEARNER-5603-managment-command","sha":"616be99980f2e5e1a76d5fd4b20336fee3cb9053","user":{"login":"edx","id":3179841,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMxNzk4NDE=","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,"node_id":"MDEwOlJlcG9zaXRvcnkxMDM5MTA3Mw==","name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMxNzk4NDE=","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":"2018-07-18T07:29:43Z","pushed_at":"2018-07-18T06:38: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":946952,"stargazers_count":3773,"watchers_count":3773,"language":"Python","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2124,"mirror_url":null,"archived":false,"open_issues_count":194,"license":{"key":"agpl-3.0","name":"GNU Affero General Public License v3.0","spdx_id":"AGPL-3.0","url":"https://api.github.com/licenses/agpl-3.0","node_id":"MDc6TGljZW5zZTE="},"forks":2124,"open_issues":194,"watchers":3773,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"cf6e2d9f894dd2aa6c367bda02fd9f9e4784bba4","user":{"login":"edx","id":3179841,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMxNzk4NDE=","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,"node_id":"MDEwOlJlcG9zaXRvcnkxMDM5MTA3Mw==","name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMxNzk4NDE=","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":"2018-07-18T07:29:43Z","pushed_at":"2018-07-18T06:38: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":946952,"stargazers_count":3773,"watchers_count":3773,"language":"Python","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2124,"mirror_url":null,"archived":false,"open_issues_count":194,"license":{"key":"agpl-3.0","name":"GNU Affero General Public License v3.0","spdx_id":"AGPL-3.0","url":"https://api.github.com/licenses/agpl-3.0","node_id":"MDc6TGljZW5zZTE="},"forks":2124,"open_issues":194,"watchers":3773,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/18583"},"html":{"href":"https://github.com/edx/edx-platform/pull/18583"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/18583"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/18583/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/18583/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/18583/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/616be99980f2e5e1a76d5fd4b20336fee3cb9053"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"tasawernawaz"}}