{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31274173","id":31274173,"diff_hunk":"@@ -8,37 +8,88 @@\n from opaque_keys import InvalidKeyError\n from opaque_keys.edx.keys import CourseKey\n from opaque_keys.edx.locations import SlashSeparatedCourseKey\n+from xmodule.modulestore.django import modulestore\n \n from search.filter_generator import SearchFilterGenerator\n-from openedx.core.djangoapps.course_groups.partition_scheme import get_cohorted_user_partition\n+from openedx.core.djangoapps.user_api.partition_schemes import RandomUserPartitionScheme\n+from openedx.core.djangoapps.course_groups.partition_scheme import CohortPartitionScheme\n from courseware.access import get_user_role\n \n \n+INCLUDE_SCHEMES = [CohortPartitionScheme, RandomUserPartitionScheme, ]\n+SCHEME_SUPPORTS_ASSIGNMENT = [RandomUserPartitionScheme, ]\n+\n+\n class LmsSearchFilterGenerator(SearchFilterGenerator):\n     \"\"\" SearchFilterGenerator for LMS Search \"\"\"\n \n+    _user_enrollments = {}\n+\n+    def _enrollments_for_user(self, user):\n+        \"\"\" Return users course enrollments \"\"\"\n+        if user not in self._user_enrollments:\n+            self._user_enrollments[user] = CourseEnrollment.enrollments_for_user(user)\n+        return self._user_enrollments[user]\n+\n     def filter_dictionary(self, **kwargs):\n-        \"\"\" base implementation which filters via start_date \"\"\"\n-        filter_dictionary = super(LmsSearchFilterGenerator, self).filter_dictionary(**kwargs)\n-        if 'user' in kwargs and 'course_id' in kwargs and kwargs['course_id']:\n-            user = kwargs['user']\n-            try:\n-                course_key = CourseKey.from_string(kwargs['course_id'])\n-            except InvalidKeyError:\n-                course_key = SlashSeparatedCourseKey.from_deprecated_string(kwargs['course_id'])\n-\n-            # Staff user looking at course as staff user\n-            if get_user_role(user, course_key) == 'staff':\n-                return filter_dictionary\n-\n-            cohorted_user_partition = get_cohorted_user_partition(course_key)\n-            if cohorted_user_partition:\n-                partition_group = cohorted_user_partition.scheme.get_group_for_user(\n+        \"\"\" LMS implementation, adds filtering by content groups, course id and user \"\"\"\n+\n+        def get_group_for_user_partition(user_partition, course_key, user):\n+            \"\"\" Returns users group for user partition \"\"\"\n+            if user_partition.scheme in SCHEME_SUPPORTS_ASSIGNMENT:\n+                return user_partition.scheme.get_group_for_user(\n+                    course_key,\n+                    user,\n+                    user_partition,\n+                    assign=False,\n+                )\n+            else:\n+                return user_partition.scheme.get_group_for_user(\n                     course_key,\n                     user,\n-                    cohorted_user_partition,\n+                    user_partition,\n                 )\n-                filter_dictionary['content_groups'] = unicode(partition_group.id) if partition_group else None\n+\n+        def get_content_groups(course, user):\n+            \"\"\" Collect content groups for user for this course \"\"\"\n+            partition_groups = []\n+            for user_partition in course.user_partitions:\n+                if user_partition.scheme in INCLUDE_SCHEMES:\n+                    group = get_group_for_user_partition(user_partition, course.id, user)\n+                    if group:\n+                        partition_groups.append(group)\n+            content_groups = [unicode(partition_group.id) for partition_group in partition_groups]\n+            return content_groups if content_groups else None\n+\n+        filter_dictionary = super(LmsSearchFilterGenerator, self).filter_dictionary(**kwargs)\n+        if 'user' in kwargs:\n+            user = kwargs['user']\n+\n+            if 'course_id' in kwargs and kwargs['course_id']:\n+                try:\n+                    course_key = CourseKey.from_string(kwargs['course_id'])","path":"lms/lib/courseware_search/lms_filter_generator.py","position":82,"original_position":82,"commit_id":"7587fe2bb24a117cbb77eaf57ad326511bc1147a","original_commit_id":"7587fe2bb24a117cbb77eaf57ad326511bc1147a","user":{"login":"andy-armstrong","id":5985072,"avatar_url":"https://avatars.githubusercontent.com/u/5985072?v=3","gravatar_id":"","url":"https://api.github.com/users/andy-armstrong","html_url":"https://github.com/andy-armstrong","followers_url":"https://api.github.com/users/andy-armstrong/followers","following_url":"https://api.github.com/users/andy-armstrong/following{/other_user}","gists_url":"https://api.github.com/users/andy-armstrong/gists{/gist_id}","starred_url":"https://api.github.com/users/andy-armstrong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andy-armstrong/subscriptions","organizations_url":"https://api.github.com/users/andy-armstrong/orgs","repos_url":"https://api.github.com/users/andy-armstrong/repos","events_url":"https://api.github.com/users/andy-armstrong/events{/privacy}","received_events_url":"https://api.github.com/users/andy-armstrong/received_events","type":"User","site_admin":false},"body":"It seems that there should be a standard helper function that can handle both new and old style keys.","created_at":"2015-05-28T20:27:51Z","updated_at":"2015-05-28T20:27:51Z","html_url":"https://github.com/edx/edx-platform/pull/8229#discussion_r31274173","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/8229","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31274173"},"html":{"href":"https://github.com/edx/edx-platform/pull/8229#discussion_r31274173"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8229"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/8229","id":36396229,"html_url":"https://github.com/edx/edx-platform/pull/8229","diff_url":"https://github.com/edx/edx-platform/pull/8229.diff","patch_url":"https://github.com/edx/edx-platform/pull/8229.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/8229","number":8229,"state":"open","locked":false,"title":"Dcikatic/sol 536 [WIP]","user":{"login":"dino-cikatic","id":7403820,"avatar_url":"https://avatars.githubusercontent.com/u/7403820?v=3","gravatar_id":"","url":"https://api.github.com/users/dino-cikatic","html_url":"https://github.com/dino-cikatic","followers_url":"https://api.github.com/users/dino-cikatic/followers","following_url":"https://api.github.com/users/dino-cikatic/following{/other_user}","gists_url":"https://api.github.com/users/dino-cikatic/gists{/gist_id}","starred_url":"https://api.github.com/users/dino-cikatic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dino-cikatic/subscriptions","organizations_url":"https://api.github.com/users/dino-cikatic/orgs","repos_url":"https://api.github.com/users/dino-cikatic/repos","events_url":"https://api.github.com/users/dino-cikatic/events{/privacy}","received_events_url":"https://api.github.com/users/dino-cikatic/received_events","type":"User","site_admin":false},"body":"@martynjames @marjev I've merged your work into this PR. I've tried testing with different state accounts and it looks fine. My only concern is that we have to get whole course object for each enrolled course and filter it for content groups. I imagine these can get to double digits fairly easily.\r\n\r\nTicket with acceptance criteria: https://openedx.atlassian.net/browse/SOL-536","created_at":"2015-05-28T11:21:13Z","updated_at":"2015-05-28T20:27:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3bd2be6ee0db5908cf8389f197d84ba8d3d1ff8b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/8229/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/8229/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/8229/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/7587fe2bb24a117cbb77eaf57ad326511bc1147a","head":{"label":"edx:dcikatic/SOL-536","ref":"dcikatic/SOL-536","sha":"7587fe2bb24a117cbb77eaf57ad326511bc1147a","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","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://avatars.githubusercontent.com/u/3179841?v=3","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":"Open edX, the edX platform that powers http://edx.org","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}","created_at":"2013-05-30T20:20:38Z","updated_at":"2015-05-27T23:08:50Z","pushed_at":"2015-05-28T20:27:14Z","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://openedx.org/","size":1939225,"stargazers_count":1949,"watchers_count":1949,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1117,"mirror_url":null,"open_issues_count":199,"forks":1117,"open_issues":199,"watchers":1949,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"4a14ec694f5680ce9049b7d29f8ca0e496936eab","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","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://avatars.githubusercontent.com/u/3179841?v=3","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":"Open edX, the edX platform that powers http://edx.org","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}","created_at":"2013-05-30T20:20:38Z","updated_at":"2015-05-27T23:08:50Z","pushed_at":"2015-05-28T20:27:14Z","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://openedx.org/","size":1939225,"stargazers_count":1949,"watchers_count":1949,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1117,"mirror_url":null,"open_issues_count":199,"forks":1117,"open_issues":199,"watchers":1949,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8229"},"html":{"href":"https://github.com/edx/edx-platform/pull/8229"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8229"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8229/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8229/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/8229/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/7587fe2bb24a117cbb77eaf57ad326511bc1147a"}}}}