{"action":"created","comment":{"url":"https://api.github.com/repos/edx/credentials/pulls/comments/48566014","id":48566014,"diff_hunk":"@@ -1,4 +1,101 @@\n-# Serializers that can be shared across multiple versions of the API\n-# should be created here. As the API evolves, serializers may become more\n-# specific to a particular version of the API. In this case, the serializers\n-# in question should be moved to versioned sub-package.\n+\"\"\"\n+Serializers for data manipulated by the credentials service APIs.\n+\"\"\"\n+from rest_framework import serializers\n+from rest_framework.exceptions import ValidationError\n+\n+from credentials.apps.api.accreditor import Accreditor\n+from credentials.apps.credentials.models import (\n+    CourseCertificate, ProgramCertificate,\n+    UserCredential, UserCredentialAttribute\n+)\n+\n+\n+class CredentialField(serializers.Field):\n+    \"\"\"\n+    A custom field to use for the user credential generic relationship.\n+    \"\"\"\n+\n+    def to_internal_value(self, data):\n+        try:\n+            if 'program_id' in data:\n+                return ProgramCertificate.objects.get(program_id=data['program_id'])\n+            elif 'course_id' in data:\n+                return CourseCertificate.objects.get(\n+                    course_id=data['course_id'],\n+                    certificate_type=data['certificate_type'])\n+        except (ProgramCertificate.DoesNotExist, CourseCertificate.DoesNotExist) as ex:\n+            raise ValidationError(ex.message)\n+\n+    def to_representation(self, value):\n+        \"\"\"\n+        Serialize objects to a according to model content-type.\n+        \"\"\"\n+        credential = {\n+            'credential_id': value.id\n+        }\n+        if isinstance(value, ProgramCertificate):\n+            credential.update({\n+                'program_id': value.program_id,\n+            })\n+        elif isinstance(value, CourseCertificate):\n+            credential.update({\n+                'course_id': value.course_id,\n+                'certificate_type': value.certificate_type\n+            })\n+\n+        return credential\n+\n+\n+class UserCredentialAttributeSerializer(serializers.ModelSerializer):\n+    \"\"\" Serializer for CredentialAttribute objects \"\"\"\n+\n+    class Meta(object):\n+        model = UserCredentialAttribute\n+        fields = ('namespace', 'name', 'value')\n+\n+\n+class UserCredentialSerializer(serializers.ModelSerializer):\n+    \"\"\" Serializer for User Credential objects. \"\"\"\n+\n+    credential = CredentialField()\n+    attributes = UserCredentialAttributeSerializer(many=True)\n+\n+    class Meta(object):\n+        model = UserCredential\n+        exclude = ('credential_content_type', 'credential_id')\n+        read_only_fields = ('credential', 'username', 'download_url', 'uuid')","path":"credentials/apps/api/serializers.py","position":71,"original_position":71,"commit_id":"5a5889488bd5a6eab44b5688f925080dd15d8715","original_commit_id":"5a5889488bd5a6eab44b5688f925080dd15d8715","user":{"login":"clintonb","id":910510,"avatar_url":"https://avatars.githubusercontent.com/u/910510?v=3","gravatar_id":"","url":"https://api.github.com/users/clintonb","html_url":"https://github.com/clintonb","followers_url":"https://api.github.com/users/clintonb/followers","following_url":"https://api.github.com/users/clintonb/following{/other_user}","gists_url":"https://api.github.com/users/clintonb/gists{/gist_id}","starred_url":"https://api.github.com/users/clintonb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clintonb/subscriptions","organizations_url":"https://api.github.com/users/clintonb/orgs","repos_url":"https://api.github.com/users/clintonb/repos","events_url":"https://api.github.com/users/clintonb/events{/privacy}","received_events_url":"https://api.github.com/users/clintonb/received_events","type":"User","site_admin":false},"body":"Right now `status` is the only editable field. Say you added a new field called `secret_field`. That field will automatically be exposed as a writeable field. If that field is intended to be read-only, a developer (and reviewers) could easily miss that. However, if the developer has to update `fields`, he/she will also see `readonly_fields`, which should (hopefully) trigger the developer to thinking about if the field should be read-only.\r\n\r\ntl;dr we should make it difficult for developers to do the wrong thing (in this case, making a future read-only field writeable).","created_at":"2015-12-29T20:03:12Z","updated_at":"2015-12-29T20:03:12Z","html_url":"https://github.com/edx/credentials/pull/2#discussion_r48566014","pull_request_url":"https://api.github.com/repos/edx/credentials/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/edx/credentials/pulls/comments/48566014"},"html":{"href":"https://github.com/edx/credentials/pull/2#discussion_r48566014"},"pull_request":{"href":"https://api.github.com/repos/edx/credentials/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/edx/credentials/pulls/2","id":53697609,"html_url":"https://github.com/edx/credentials/pull/2","diff_url":"https://github.com/edx/credentials/pull/2.diff","patch_url":"https://github.com/edx/credentials/pull/2.patch","issue_url":"https://api.github.com/repos/edx/credentials/issues/2","number":2,"state":"open","locked":false,"title":"User Credentials api","user":{"login":"awais786","id":445320,"avatar_url":"https://avatars.githubusercontent.com/u/445320?v=3","gravatar_id":"","url":"https://api.github.com/users/awais786","html_url":"https://github.com/awais786","followers_url":"https://api.github.com/users/awais786/followers","following_url":"https://api.github.com/users/awais786/following{/other_user}","gists_url":"https://api.github.com/users/awais786/gists{/gist_id}","starred_url":"https://api.github.com/users/awais786/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awais786/subscriptions","organizations_url":"https://api.github.com/users/awais786/orgs","repos_url":"https://api.github.com/users/awais786/repos","events_url":"https://api.github.com/users/awais786/events{/privacy}","received_events_url":"https://api.github.com/users/awais786/received_events","type":"User","site_admin":false},"body":"ECOM-2999\r\n\r\n* Adding the end-points to create the user credentials.\r\n* Patch functionality to update the status.\r\n* Classes implementation.\r\n* /api/v1/credentials/: List all credentials\r\n* /api/v1/credentials/:id/: Retrieve a single credential\r\n* Added filtering to your list endpoint: /api/v1/credentials/?username=<username>&status=<status>.\r\n\r\nReference: https://github.com/edx/edx-platform/commit/84699260e06199b8a41c3765260dc35cf619e4c1\r\n\r\nFiltering for Programs and Courses will be added in separate story [ECOM-3263](https://openedx.atlassian.net/browse/ECOM-3263).","created_at":"2015-12-15T12:36:53Z","updated_at":"2015-12-29T20:03:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"388c7757df7071e0346a3ee6862b98bf5242f831","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/credentials/pulls/2/commits","review_comments_url":"https://api.github.com/repos/edx/credentials/pulls/2/comments","review_comment_url":"https://api.github.com/repos/edx/credentials/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/credentials/issues/2/comments","statuses_url":"https://api.github.com/repos/edx/credentials/statuses/5a5889488bd5a6eab44b5688f925080dd15d8715","head":{"label":"edx:awais786/ECOM-2999-user-credential-apis","ref":"awais786/ECOM-2999-user-credential-apis","sha":"5a5889488bd5a6eab44b5688f925080dd15d8715","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":47830104,"name":"credentials","full_name":"edx/credentials","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/credentials","description":"Django application used as the backend to support Course or Program based credentials.","fork":false,"url":"https://api.github.com/repos/edx/credentials","forks_url":"https://api.github.com/repos/edx/credentials/forks","keys_url":"https://api.github.com/repos/edx/credentials/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/credentials/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/credentials/teams","hooks_url":"https://api.github.com/repos/edx/credentials/hooks","issue_events_url":"https://api.github.com/repos/edx/credentials/issues/events{/number}","events_url":"https://api.github.com/repos/edx/credentials/events","assignees_url":"https://api.github.com/repos/edx/credentials/assignees{/user}","branches_url":"https://api.github.com/repos/edx/credentials/branches{/branch}","tags_url":"https://api.github.com/repos/edx/credentials/tags","blobs_url":"https://api.github.com/repos/edx/credentials/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/credentials/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/credentials/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/credentials/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/credentials/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/credentials/languages","stargazers_url":"https://api.github.com/repos/edx/credentials/stargazers","contributors_url":"https://api.github.com/repos/edx/credentials/contributors","subscribers_url":"https://api.github.com/repos/edx/credentials/subscribers","subscription_url":"https://api.github.com/repos/edx/credentials/subscription","commits_url":"https://api.github.com/repos/edx/credentials/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/credentials/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/credentials/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/credentials/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/credentials/contents/{+path}","compare_url":"https://api.github.com/repos/edx/credentials/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/credentials/merges","archive_url":"https://api.github.com/repos/edx/credentials/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/credentials/downloads","issues_url":"https://api.github.com/repos/edx/credentials/issues{/number}","pulls_url":"https://api.github.com/repos/edx/credentials/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/credentials/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/credentials/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/credentials/labels{/name}","releases_url":"https://api.github.com/repos/edx/credentials/releases{/id}","created_at":"2015-12-11T14:09:03Z","updated_at":"2015-12-14T20:00:22Z","pushed_at":"2015-12-29T19:10:20Z","git_url":"git://github.com/edx/credentials.git","ssh_url":"git@github.com:edx/credentials.git","clone_url":"https://github.com/edx/credentials.git","svn_url":"https://github.com/edx/credentials","homepage":"http://open-edx-credentials-guide.readthedocs.org/en/latest/ ","size":1951,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"15aed79898ba65465b8d193d5e6f66f7e27e72a4","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":47830104,"name":"credentials","full_name":"edx/credentials","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/credentials","description":"Django application used as the backend to support Course or Program based credentials.","fork":false,"url":"https://api.github.com/repos/edx/credentials","forks_url":"https://api.github.com/repos/edx/credentials/forks","keys_url":"https://api.github.com/repos/edx/credentials/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/credentials/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/credentials/teams","hooks_url":"https://api.github.com/repos/edx/credentials/hooks","issue_events_url":"https://api.github.com/repos/edx/credentials/issues/events{/number}","events_url":"https://api.github.com/repos/edx/credentials/events","assignees_url":"https://api.github.com/repos/edx/credentials/assignees{/user}","branches_url":"https://api.github.com/repos/edx/credentials/branches{/branch}","tags_url":"https://api.github.com/repos/edx/credentials/tags","blobs_url":"https://api.github.com/repos/edx/credentials/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/credentials/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/credentials/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/credentials/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/credentials/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/credentials/languages","stargazers_url":"https://api.github.com/repos/edx/credentials/stargazers","contributors_url":"https://api.github.com/repos/edx/credentials/contributors","subscribers_url":"https://api.github.com/repos/edx/credentials/subscribers","subscription_url":"https://api.github.com/repos/edx/credentials/subscription","commits_url":"https://api.github.com/repos/edx/credentials/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/credentials/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/credentials/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/credentials/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/credentials/contents/{+path}","compare_url":"https://api.github.com/repos/edx/credentials/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/credentials/merges","archive_url":"https://api.github.com/repos/edx/credentials/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/credentials/downloads","issues_url":"https://api.github.com/repos/edx/credentials/issues{/number}","pulls_url":"https://api.github.com/repos/edx/credentials/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/credentials/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/credentials/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/credentials/labels{/name}","releases_url":"https://api.github.com/repos/edx/credentials/releases{/id}","created_at":"2015-12-11T14:09:03Z","updated_at":"2015-12-14T20:00:22Z","pushed_at":"2015-12-29T19:10:20Z","git_url":"git://github.com/edx/credentials.git","ssh_url":"git@github.com:edx/credentials.git","clone_url":"https://github.com/edx/credentials.git","svn_url":"https://github.com/edx/credentials","homepage":"http://open-edx-credentials-guide.readthedocs.org/en/latest/ ","size":1951,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/credentials/pulls/2"},"html":{"href":"https://github.com/edx/credentials/pull/2"},"issue":{"href":"https://api.github.com/repos/edx/credentials/issues/2"},"comments":{"href":"https://api.github.com/repos/edx/credentials/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/credentials/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/credentials/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/credentials/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/edx/credentials/statuses/5a5889488bd5a6eab44b5688f925080dd15d8715"}}}}