{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31008145","id":31008145,"diff_hunk":"@@ -78,3 +88,108 @@ def revert(self, request, queryset):\n                 args=(target.id,),\n             )\n         )\n+\n+\n+class ShowHistoryFilter(ListFilter):\n+    \"\"\"\n+    Admin change view filter to show only the most recent (i.e. the \"current\") row for each\n+    unique key value.\n+    \"\"\"\n+    title = 'Status'\n+    parameter_name = 'show_history'\n+\n+    def __init__(self, request, params, model, model_admin):\n+        super(ShowHistoryFilter, self).__init__(request, params, model, model_admin)\n+        if self.parameter_name in params:\n+            value = params.pop(self.parameter_name)\n+            self.used_parameters[self.parameter_name] = value\n+\n+    def has_output(self):\n+        \"\"\" Should this filter be shown? \"\"\"\n+        return True\n+\n+    def choices(self, cl):\n+        \"\"\" Returns choices ready to be output in the template. \"\"\"\n+        show_all = self.used_parameters.get(self.parameter_name) == \"1\"\n+        return (\n+            {\n+                'display': 'Current Configuration',\n+                'selected': not show_all,\n+                'query_string': cl.get_query_string({}, [self.parameter_name]),\n+            },\n+            {\n+                'display': 'All (Show History)',\n+                'selected': show_all,\n+                'query_string': cl.get_query_string({self.parameter_name: \"1\"}, []),\n+            }\n+        )\n+\n+    def queryset(self, request, queryset):\n+        \"\"\" Filter the queryset. No-op since it's done by KeyedConfigurationModelAdmin \"\"\"\n+        return queryset\n+\n+    def expected_parameters(self):\n+        \"\"\" List the query string params used by this filter \"\"\"\n+        return [self.parameter_name]\n+\n+\n+class KeyedConfigurationModelAdmin(ConfigurationModelAdmin):\n+    \"\"\"\n+    :class:`~django.contrib.admin.ModelAdmin` for :class:`.ConfigurationModel` subclasses that\n+    use extra keys (i.e. they have KEY_FIELDS set).\n+    \"\"\"\n+    date_hierarchy = None\n+    list_filter = (ShowHistoryFilter, )\n+\n+    def queryset(self, request):\n+        \"\"\"\n+        Annote the queryset with an 'is_active' property that's true iff that row is the most\n+        recently added row for that particular set of KEY_FIELDS values.\n+        Filter the queryset to show only is_active rows by default.\n+        \"\"\"\n+        queryset = super(KeyedConfigurationModelAdmin, self).queryset(request)\n+        key_fields_escaped = [connection.ops.quote_name(name) for name in self.model.KEY_FIELDS]\n+        # The following custom SQL is like a \"SELECT DISTINCT ON {key_fields} ...\" query, which\n+        # is only supported on PostgreSQL.\n+        subquery = \"\"\"\n+            SELECT id FROM\n+                (SELECT id, {key_fields} FROM {table_name} AS ordered_table ORDER BY change_date DESC) AS grouped_table\n+            GROUP BY {key_fields}","path":"common/djangoapps/config_models/admin.py","position":97,"original_position":97,"commit_id":"dd89c90f5174583256d65cb86cd9fd9f70fadd49","original_commit_id":"dd89c90f5174583256d65cb86cd9fd9f70fadd49","user":{"login":"bradenmacdonald","id":945577,"avatar_url":"https://avatars.githubusercontent.com/u/945577?v=3","gravatar_id":"","url":"https://api.github.com/users/bradenmacdonald","html_url":"https://github.com/bradenmacdonald","followers_url":"https://api.github.com/users/bradenmacdonald/followers","following_url":"https://api.github.com/users/bradenmacdonald/following{/other_user}","gists_url":"https://api.github.com/users/bradenmacdonald/gists{/gist_id}","starred_url":"https://api.github.com/users/bradenmacdonald/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradenmacdonald/subscriptions","organizations_url":"https://api.github.com/users/bradenmacdonald/orgs","repos_url":"https://api.github.com/users/bradenmacdonald/repos","events_url":"https://api.github.com/users/bradenmacdonald/events{/privacy}","received_events_url":"https://api.github.com/users/bradenmacdonald/received_events","type":"User","site_admin":false},"body":"Ah, good point. I was overthinking it in order to sort by the date field without selecting that value - I didn't think to exploit that relationship with the ID. I will switch to this version, which is much better.","created_at":"2015-05-26T06:54:02Z","updated_at":"2015-05-26T06:54:02Z","html_url":"https://github.com/edx/edx-platform/pull/8077#discussion_r31008145","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/8077","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31008145"},"html":{"href":"https://github.com/edx/edx-platform/pull/8077#discussion_r31008145"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8077"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/8077","id":35545666,"html_url":"https://github.com/edx/edx-platform/pull/8077","diff_url":"https://github.com/edx/edx-platform/pull/8077.diff","patch_url":"https://github.com/edx/edx-platform/pull/8077.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/8077","number":8077,"state":"open","locked":false,"title":"Keyed ConfigurationModel + Admin functionality","user":{"login":"bradenmacdonald","id":945577,"avatar_url":"https://avatars.githubusercontent.com/u/945577?v=3","gravatar_id":"","url":"https://api.github.com/users/bradenmacdonald","html_url":"https://github.com/bradenmacdonald","followers_url":"https://api.github.com/users/bradenmacdonald/followers","following_url":"https://api.github.com/users/bradenmacdonald/following{/other_user}","gists_url":"https://api.github.com/users/bradenmacdonald/gists{/gist_id}","starred_url":"https://api.github.com/users/bradenmacdonald/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradenmacdonald/subscriptions","organizations_url":"https://api.github.com/users/bradenmacdonald/orgs","repos_url":"https://api.github.com/users/bradenmacdonald/repos","events_url":"https://api.github.com/users/bradenmacdonald/events{/privacy}","received_events_url":"https://api.github.com/users/bradenmacdonald/received_events","type":"User","site_admin":false},"body":"This extends @cpennington's work to allow keyed `ConfigurationModel`s (https://github.com/cpennington/edx-platform/commit/c1abce7dc0cece4fedc9fda6ee47cd801ff93214).\r\n\r\nI've developed it for use in the ongoing Shibboleth support branch. See https://github.com/edx/edx-platform/compare/feature/shibboleth-tpa...open-craft:shibboleth-2 for WIP example of real world use (PR not opened yet).\r\n\r\nThere is a demo at http://sandbox5.opencraft.com/admin/ - user name 'admin', ping Braden via HipChat or IRC or email for the password.\r\n\r\nScreenshot of the admin view:\r\n\r\n**Collapsed** - default admin view only shows the current values for each key:\r\n![screen shot 2015-05-15 at 1 11 47 pm](https://cloud.githubusercontent.com/assets/945577/7661020/06a5f3d4-fb04-11e4-9e56-5c80e7f82a35.png)\r\n\r\n**Expanded** - shows the complete change history:\r\n![screen shot 2015-05-15 at 1 11 15 pm](https://cloud.githubusercontent.com/assets/945577/7661015/03974602-fb04-11e4-9043-f699dc28e70a.png)\r\n\r\nBecause MySQL doesn't support `SELECT DISTINCT ON` queries (`queryset.distinct('column_name')` in Django ORM) or window functions,  there's a bit of an unfortunate custom SQL query required for the admin view that uses subqueries. However since it's only used for the admin view, it shouldn't be a database performance concern (I assume).\r\n","created_at":"2015-05-15T19:59:57Z","updated_at":"2015-05-26T06:54:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8a049538cfd524e920e2d7b2977f31780ab5808a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/8077/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/8077/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/8077/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/dd89c90f5174583256d65cb86cd9fd9f70fadd49","head":{"label":"open-craft:keyed-config-model-2","ref":"keyed-config-model-2","sha":"dd89c90f5174583256d65cb86cd9fd9f70fadd49","user":{"login":"open-craft","id":7414786,"avatar_url":"https://avatars.githubusercontent.com/u/7414786?v=3","gravatar_id":"","url":"https://api.github.com/users/open-craft","html_url":"https://github.com/open-craft","followers_url":"https://api.github.com/users/open-craft/followers","following_url":"https://api.github.com/users/open-craft/following{/other_user}","gists_url":"https://api.github.com/users/open-craft/gists{/gist_id}","starred_url":"https://api.github.com/users/open-craft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/open-craft/subscriptions","organizations_url":"https://api.github.com/users/open-craft/orgs","repos_url":"https://api.github.com/users/open-craft/repos","events_url":"https://api.github.com/users/open-craft/events{/privacy}","received_events_url":"https://api.github.com/users/open-craft/received_events","type":"Organization","site_admin":false},"repo":{"id":19940883,"name":"edx-platform","full_name":"open-craft/edx-platform","owner":{"login":"open-craft","id":7414786,"avatar_url":"https://avatars.githubusercontent.com/u/7414786?v=3","gravatar_id":"","url":"https://api.github.com/users/open-craft","html_url":"https://github.com/open-craft","followers_url":"https://api.github.com/users/open-craft/followers","following_url":"https://api.github.com/users/open-craft/following{/other_user}","gists_url":"https://api.github.com/users/open-craft/gists{/gist_id}","starred_url":"https://api.github.com/users/open-craft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/open-craft/subscriptions","organizations_url":"https://api.github.com/users/open-craft/orgs","repos_url":"https://api.github.com/users/open-craft/repos","events_url":"https://api.github.com/users/open-craft/events{/privacy}","received_events_url":"https://api.github.com/users/open-craft/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/open-craft/edx-platform","description":"the edX learning management system (LMS) and course authoring tool, Studio","fork":true,"url":"https://api.github.com/repos/open-craft/edx-platform","forks_url":"https://api.github.com/repos/open-craft/edx-platform/forks","keys_url":"https://api.github.com/repos/open-craft/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/open-craft/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/open-craft/edx-platform/teams","hooks_url":"https://api.github.com/repos/open-craft/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/open-craft/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/open-craft/edx-platform/events","assignees_url":"https://api.github.com/repos/open-craft/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/open-craft/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/open-craft/edx-platform/tags","blobs_url":"https://api.github.com/repos/open-craft/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/open-craft/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/open-craft/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/open-craft/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/open-craft/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/open-craft/edx-platform/languages","stargazers_url":"https://api.github.com/repos/open-craft/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/open-craft/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/open-craft/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/open-craft/edx-platform/subscription","commits_url":"https://api.github.com/repos/open-craft/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/open-craft/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/open-craft/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/open-craft/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/open-craft/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/open-craft/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/open-craft/edx-platform/merges","archive_url":"https://api.github.com/repos/open-craft/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/open-craft/edx-platform/downloads","issues_url":"https://api.github.com/repos/open-craft/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/open-craft/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/open-craft/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/open-craft/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/open-craft/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/open-craft/edx-platform/releases{/id}","created_at":"2014-05-19T12:28:25Z","updated_at":"2014-12-10T07:44:28Z","pushed_at":"2015-05-25T15:53:29Z","git_url":"git://github.com/open-craft/edx-platform.git","ssh_url":"git@github.com:open-craft/edx-platform.git","clone_url":"https://github.com/open-craft/edx-platform.git","svn_url":"https://github.com/open-craft/edx-platform","homepage":"http://code.edx.org/","size":432964,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":3,"forks":1,"open_issues":3,"watchers":1,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"b6164b570285a5d2ff67ed94e049a0b9e31d71a5","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-26T06:45:01Z","pushed_at":"2015-05-26T06:31:25Z","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":1947310,"stargazers_count":1942,"watchers_count":1942,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1119,"mirror_url":null,"open_issues_count":178,"forks":1119,"open_issues":178,"watchers":1942,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8077"},"html":{"href":"https://github.com/edx/edx-platform/pull/8077"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8077"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8077/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8077/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/8077/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/dd89c90f5174583256d65cb86cd9fd9f70fadd49"}}}}