{"action":"created","comment":{"url":"https://api.github.com/repos/torchbox/wagtail/pulls/comments/37406863","id":37406863,"diff_hunk":"@@ -2,171 +2,268 @@\n \n from collections import OrderedDict\n \n-from django.db import models\n-from django.utils.encoding import force_text\n-\n from modelcluster.models import get_all_child_relations\n \n-from rest_framework.serializers import BaseSerializer\n+from taggit.managers import _TaggableManager\n+\n+from rest_framework import serializers\n+from rest_framework.fields import Field\n+from rest_framework import relations\n \n from wagtail.utils.compat import get_related_model\n-from wagtail.wagtailcore.models import Page\n+from wagtail.wagtailcore import fields as wagtailcore_fields\n \n-from .utils import ObjectDetailURL, URLPath, BadRequestError, pages_for_site\n+from .utils import ObjectDetailURL, URLPath, pages_for_site\n \n \n-def get_api_data(obj, fields):\n-    # Find any child relations (pages only)\n-    child_relations = {}\n-    if isinstance(obj, Page):\n-        child_relations = {\n-            child_relation.field.rel.related_name: get_related_model(child_relation)\n-            for child_relation in get_all_child_relations(type(obj))\n-        }\n+class MetaField(Field):\n+    \"\"\"\n+    Serializes the \"meta\" section of each object.\n+\n+    This section is used for storing non-field data such as model name, urls, etc.\n+\n+    Example:\n \n-    # Loop through fields\n-    for field_name in fields:\n-        # Check child relations\n-        if field_name in child_relations and hasattr(child_relations[field_name], 'api_fields'):\n-            yield field_name, [\n-                dict(get_api_data(child_object, child_relations[field_name].api_fields))\n-                for child_object in getattr(obj, field_name).all()\n-            ]\n-            continue\n-\n-        # Check django fields\n-        try:\n-            field = obj._meta.get_field(field_name)\n-\n-            if field.rel and isinstance(field.rel, models.ManyToOneRel):\n-                # Foreign key\n-                val = field._get_val_from_obj(obj)\n-\n-                if val:\n-                    yield field_name, OrderedDict([\n-                        ('id', field._get_val_from_obj(obj)),\n-                        ('meta', OrderedDict([\n-                             ('type', field.rel.to._meta.app_label + '.' + field.rel.to.__name__),\n-                             ('detail_url', ObjectDetailURL(field.rel.to, val)),\n-                        ])),\n-                    ])\n-                else:\n-                    yield field_name, None\n-            else:\n-                yield field_name, field._get_val_from_obj(obj)\n-\n-            continue\n-        except models.fields.FieldDoesNotExist:\n-            pass\n-\n-        # Check attributes\n-        if hasattr(obj, field_name):\n-            value = getattr(obj, field_name)\n-            yield field_name, force_text(value, strings_only=True)\n-            continue\n-\n-\n-class WagtailSerializer(BaseSerializer):\n-    def to_representation(self, instance):\n-        request = self.context['request']\n-        fields = self.context.get('fields', frozenset())\n-        all_fields = self.context.get('all_fields', False)\n-        show_details = self.context.get('show_details', False)\n-        return self.serialize_object(\n-            request,\n-            instance,\n-            fields=fields,\n-            all_fields=all_fields,\n-            show_details=show_details\n-        )\n-\n-    def serialize_object_metadata(self, request, obj, show_details=False):\n-        \"\"\"\n-        This returns a JSON-serialisable dict to use for the \"meta\"\n-        section of a particlular object.\n-        \"\"\"\n-        data = OrderedDict()\n-\n-        # Add type\n-        data['type'] = type(obj)._meta.app_label + '.' + type(obj).__name__\n-        data['detail_url'] = ObjectDetailURL(type(obj), obj.pk)\n+    \"meta\": {\n+        \"type\": \"wagtailimages.Image\",\n+        \"detail_url\": \"http://api.example.com/v1/images/1/\"\n+    }\n+    \"\"\"\n+    def get_attribute(self, instance):\n+        return instance\n+\n+    def to_representation(self, obj):\n+        return OrderedDict([\n+            ('type', type(obj)._meta.app_label + '.' + type(obj).__name__),\n+            ('detail_url', ObjectDetailURL(type(obj), obj.pk)),","path":"wagtail/contrib/wagtailapi/serializers.py","position":117,"original_position":117,"commit_id":"634136fb78a6526027e88c6f4a811221e15219a9","original_commit_id":"634136fb78a6526027e88c6f4a811221e15219a9","user":{"login":"kaedroho","id":1093808,"avatar_url":"https://avatars.githubusercontent.com/u/1093808?v=3","gravatar_id":"","url":"https://api.github.com/users/kaedroho","html_url":"https://github.com/kaedroho","followers_url":"https://api.github.com/users/kaedroho/followers","following_url":"https://api.github.com/users/kaedroho/following{/other_user}","gists_url":"https://api.github.com/users/kaedroho/gists{/gist_id}","starred_url":"https://api.github.com/users/kaedroho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaedroho/subscriptions","organizations_url":"https://api.github.com/users/kaedroho/orgs","repos_url":"https://api.github.com/users/kaedroho/repos","events_url":"https://api.github.com/users/kaedroho/events{/privacy}","received_events_url":"https://api.github.com/users/kaedroho/received_events","type":"User","site_admin":false},"body":"I guess the only advantage is it doesn't require the request object in the serializer context which would make unit testing and rendering APIs to static files *slightly* easier to implement (but we have the request object in the context anyway...).\r\n\r\nSo maybe you're right. It would be great to use DRFs built in renderers and that change should make the code a bit easier to follow too.","created_at":"2015-08-19T12:10:21Z","updated_at":"2015-08-19T12:10:21Z","html_url":"https://github.com/torchbox/wagtail/pull/1579#discussion_r37406863","pull_request_url":"https://api.github.com/repos/torchbox/wagtail/pulls/1579","_links":{"self":{"href":"https://api.github.com/repos/torchbox/wagtail/pulls/comments/37406863"},"html":{"href":"https://github.com/torchbox/wagtail/pull/1579#discussion_r37406863"},"pull_request":{"href":"https://api.github.com/repos/torchbox/wagtail/pulls/1579"}}},"pull_request":{"url":"https://api.github.com/repos/torchbox/wagtail/pulls/1579","id":41464997,"html_url":"https://github.com/torchbox/wagtail/pull/1579","diff_url":"https://github.com/torchbox/wagtail/pull/1579.diff","patch_url":"https://github.com/torchbox/wagtail/pull/1579.patch","issue_url":"https://api.github.com/repos/torchbox/wagtail/issues/1579","number":1579,"state":"open","locked":false,"title":"Use rest frameworks serializers in API","user":{"login":"kaedroho","id":1093808,"avatar_url":"https://avatars.githubusercontent.com/u/1093808?v=3","gravatar_id":"","url":"https://api.github.com/users/kaedroho","html_url":"https://github.com/kaedroho","followers_url":"https://api.github.com/users/kaedroho/followers","following_url":"https://api.github.com/users/kaedroho/following{/other_user}","gists_url":"https://api.github.com/users/kaedroho/gists{/gist_id}","starred_url":"https://api.github.com/users/kaedroho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaedroho/subscriptions","organizations_url":"https://api.github.com/users/kaedroho/orgs","repos_url":"https://api.github.com/users/kaedroho/repos","events_url":"https://api.github.com/users/kaedroho/events{/privacy}","received_events_url":"https://api.github.com/users/kaedroho/received_events","type":"User","site_admin":false},"body":"This PR replaces the ``get_api_data`` function with REST frameworks serializers/fields APIs","created_at":"2015-08-03T13:09:30Z","updated_at":"2015-08-19T12:10:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2d3397445c68a366f04d680ce7f9a71e06f3cfca","assignee":null,"milestone":{"url":"https://api.github.com/repos/torchbox/wagtail/milestones/9","html_url":"https://github.com/torchbox/wagtail/milestones/1.1","labels_url":"https://api.github.com/repos/torchbox/wagtail/milestones/9/labels","id":861608,"number":9,"title":"1.1","description":"","creator":{"login":"davecranwell","id":352835,"avatar_url":"https://avatars.githubusercontent.com/u/352835?v=3","gravatar_id":"","url":"https://api.github.com/users/davecranwell","html_url":"https://github.com/davecranwell","followers_url":"https://api.github.com/users/davecranwell/followers","following_url":"https://api.github.com/users/davecranwell/following{/other_user}","gists_url":"https://api.github.com/users/davecranwell/gists{/gist_id}","starred_url":"https://api.github.com/users/davecranwell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davecranwell/subscriptions","organizations_url":"https://api.github.com/users/davecranwell/orgs","repos_url":"https://api.github.com/users/davecranwell/repos","events_url":"https://api.github.com/users/davecranwell/events{/privacy}","received_events_url":"https://api.github.com/users/davecranwell/received_events","type":"User","site_admin":false},"open_issues":111,"closed_issues":55,"state":"open","created_at":"2014-11-11T11:56:09Z","updated_at":"2015-08-19T11:23:23Z","due_on":"2015-08-30T23:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/torchbox/wagtail/pulls/1579/commits","review_comments_url":"https://api.github.com/repos/torchbox/wagtail/pulls/1579/comments","review_comment_url":"https://api.github.com/repos/torchbox/wagtail/pulls/comments{/number}","comments_url":"https://api.github.com/repos/torchbox/wagtail/issues/1579/comments","statuses_url":"https://api.github.com/repos/torchbox/wagtail/statuses/634136fb78a6526027e88c6f4a811221e15219a9","head":{"label":"kaedroho:rest-framework-serializers","ref":"rest-framework-serializers","sha":"634136fb78a6526027e88c6f4a811221e15219a9","user":{"login":"kaedroho","id":1093808,"avatar_url":"https://avatars.githubusercontent.com/u/1093808?v=3","gravatar_id":"","url":"https://api.github.com/users/kaedroho","html_url":"https://github.com/kaedroho","followers_url":"https://api.github.com/users/kaedroho/followers","following_url":"https://api.github.com/users/kaedroho/following{/other_user}","gists_url":"https://api.github.com/users/kaedroho/gists{/gist_id}","starred_url":"https://api.github.com/users/kaedroho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaedroho/subscriptions","organizations_url":"https://api.github.com/users/kaedroho/orgs","repos_url":"https://api.github.com/users/kaedroho/repos","events_url":"https://api.github.com/users/kaedroho/events{/privacy}","received_events_url":"https://api.github.com/users/kaedroho/received_events","type":"User","site_admin":false},"repo":{"id":16483819,"name":"wagtail","full_name":"kaedroho/wagtail","owner":{"login":"kaedroho","id":1093808,"avatar_url":"https://avatars.githubusercontent.com/u/1093808?v=3","gravatar_id":"","url":"https://api.github.com/users/kaedroho","html_url":"https://github.com/kaedroho","followers_url":"https://api.github.com/users/kaedroho/followers","following_url":"https://api.github.com/users/kaedroho/following{/other_user}","gists_url":"https://api.github.com/users/kaedroho/gists{/gist_id}","starred_url":"https://api.github.com/users/kaedroho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaedroho/subscriptions","organizations_url":"https://api.github.com/users/kaedroho/orgs","repos_url":"https://api.github.com/users/kaedroho/repos","events_url":"https://api.github.com/users/kaedroho/events{/privacy}","received_events_url":"https://api.github.com/users/kaedroho/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kaedroho/wagtail","description":"Django-based content management system","fork":true,"url":"https://api.github.com/repos/kaedroho/wagtail","forks_url":"https://api.github.com/repos/kaedroho/wagtail/forks","keys_url":"https://api.github.com/repos/kaedroho/wagtail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kaedroho/wagtail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kaedroho/wagtail/teams","hooks_url":"https://api.github.com/repos/kaedroho/wagtail/hooks","issue_events_url":"https://api.github.com/repos/kaedroho/wagtail/issues/events{/number}","events_url":"https://api.github.com/repos/kaedroho/wagtail/events","assignees_url":"https://api.github.com/repos/kaedroho/wagtail/assignees{/user}","branches_url":"https://api.github.com/repos/kaedroho/wagtail/branches{/branch}","tags_url":"https://api.github.com/repos/kaedroho/wagtail/tags","blobs_url":"https://api.github.com/repos/kaedroho/wagtail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kaedroho/wagtail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kaedroho/wagtail/git/refs{/sha}","trees_url":"https://api.github.com/repos/kaedroho/wagtail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kaedroho/wagtail/statuses/{sha}","languages_url":"https://api.github.com/repos/kaedroho/wagtail/languages","stargazers_url":"https://api.github.com/repos/kaedroho/wagtail/stargazers","contributors_url":"https://api.github.com/repos/kaedroho/wagtail/contributors","subscribers_url":"https://api.github.com/repos/kaedroho/wagtail/subscribers","subscription_url":"https://api.github.com/repos/kaedroho/wagtail/subscription","commits_url":"https://api.github.com/repos/kaedroho/wagtail/commits{/sha}","git_commits_url":"https://api.github.com/repos/kaedroho/wagtail/git/commits{/sha}","comments_url":"https://api.github.com/repos/kaedroho/wagtail/comments{/number}","issue_comment_url":"https://api.github.com/repos/kaedroho/wagtail/issues/comments{/number}","contents_url":"https://api.github.com/repos/kaedroho/wagtail/contents/{+path}","compare_url":"https://api.github.com/repos/kaedroho/wagtail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kaedroho/wagtail/merges","archive_url":"https://api.github.com/repos/kaedroho/wagtail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kaedroho/wagtail/downloads","issues_url":"https://api.github.com/repos/kaedroho/wagtail/issues{/number}","pulls_url":"https://api.github.com/repos/kaedroho/wagtail/pulls{/number}","milestones_url":"https://api.github.com/repos/kaedroho/wagtail/milestones{/number}","notifications_url":"https://api.github.com/repos/kaedroho/wagtail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kaedroho/wagtail/labels{/name}","releases_url":"https://api.github.com/repos/kaedroho/wagtail/releases{/id}","created_at":"2014-02-03T15:46:45Z","updated_at":"2015-05-07T09:34:19Z","pushed_at":"2015-08-19T11:16:25Z","git_url":"git://github.com/kaedroho/wagtail.git","ssh_url":"git@github.com:kaedroho/wagtail.git","clone_url":"https://github.com/kaedroho/wagtail.git","svn_url":"https://github.com/kaedroho/wagtail","homepage":null,"size":25439,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":7,"forks":0,"open_issues":7,"watchers":0,"default_branch":"master"}},"base":{"label":"torchbox:master","ref":"master","sha":"371c9dc9a5963b34fcf888d67376f7eaba5d9795","user":{"login":"torchbox","id":84015,"avatar_url":"https://avatars.githubusercontent.com/u/84015?v=3","gravatar_id":"","url":"https://api.github.com/users/torchbox","html_url":"https://github.com/torchbox","followers_url":"https://api.github.com/users/torchbox/followers","following_url":"https://api.github.com/users/torchbox/following{/other_user}","gists_url":"https://api.github.com/users/torchbox/gists{/gist_id}","starred_url":"https://api.github.com/users/torchbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/torchbox/subscriptions","organizations_url":"https://api.github.com/users/torchbox/orgs","repos_url":"https://api.github.com/users/torchbox/repos","events_url":"https://api.github.com/users/torchbox/events{/privacy}","received_events_url":"https://api.github.com/users/torchbox/received_events","type":"Organization","site_admin":false},"repo":{"id":16479108,"name":"wagtail","full_name":"torchbox/wagtail","owner":{"login":"torchbox","id":84015,"avatar_url":"https://avatars.githubusercontent.com/u/84015?v=3","gravatar_id":"","url":"https://api.github.com/users/torchbox","html_url":"https://github.com/torchbox","followers_url":"https://api.github.com/users/torchbox/followers","following_url":"https://api.github.com/users/torchbox/following{/other_user}","gists_url":"https://api.github.com/users/torchbox/gists{/gist_id}","starred_url":"https://api.github.com/users/torchbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/torchbox/subscriptions","organizations_url":"https://api.github.com/users/torchbox/orgs","repos_url":"https://api.github.com/users/torchbox/repos","events_url":"https://api.github.com/users/torchbox/events{/privacy}","received_events_url":"https://api.github.com/users/torchbox/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/torchbox/wagtail","description":"A Django content management system focused on flexibility and user experience","fork":false,"url":"https://api.github.com/repos/torchbox/wagtail","forks_url":"https://api.github.com/repos/torchbox/wagtail/forks","keys_url":"https://api.github.com/repos/torchbox/wagtail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/torchbox/wagtail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/torchbox/wagtail/teams","hooks_url":"https://api.github.com/repos/torchbox/wagtail/hooks","issue_events_url":"https://api.github.com/repos/torchbox/wagtail/issues/events{/number}","events_url":"https://api.github.com/repos/torchbox/wagtail/events","assignees_url":"https://api.github.com/repos/torchbox/wagtail/assignees{/user}","branches_url":"https://api.github.com/repos/torchbox/wagtail/branches{/branch}","tags_url":"https://api.github.com/repos/torchbox/wagtail/tags","blobs_url":"https://api.github.com/repos/torchbox/wagtail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/torchbox/wagtail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/torchbox/wagtail/git/refs{/sha}","trees_url":"https://api.github.com/repos/torchbox/wagtail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/torchbox/wagtail/statuses/{sha}","languages_url":"https://api.github.com/repos/torchbox/wagtail/languages","stargazers_url":"https://api.github.com/repos/torchbox/wagtail/stargazers","contributors_url":"https://api.github.com/repos/torchbox/wagtail/contributors","subscribers_url":"https://api.github.com/repos/torchbox/wagtail/subscribers","subscription_url":"https://api.github.com/repos/torchbox/wagtail/subscription","commits_url":"https://api.github.com/repos/torchbox/wagtail/commits{/sha}","git_commits_url":"https://api.github.com/repos/torchbox/wagtail/git/commits{/sha}","comments_url":"https://api.github.com/repos/torchbox/wagtail/comments{/number}","issue_comment_url":"https://api.github.com/repos/torchbox/wagtail/issues/comments{/number}","contents_url":"https://api.github.com/repos/torchbox/wagtail/contents/{+path}","compare_url":"https://api.github.com/repos/torchbox/wagtail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/torchbox/wagtail/merges","archive_url":"https://api.github.com/repos/torchbox/wagtail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/torchbox/wagtail/downloads","issues_url":"https://api.github.com/repos/torchbox/wagtail/issues{/number}","pulls_url":"https://api.github.com/repos/torchbox/wagtail/pulls{/number}","milestones_url":"https://api.github.com/repos/torchbox/wagtail/milestones{/number}","notifications_url":"https://api.github.com/repos/torchbox/wagtail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/torchbox/wagtail/labels{/name}","releases_url":"https://api.github.com/repos/torchbox/wagtail/releases{/id}","created_at":"2014-02-03T12:41:59Z","updated_at":"2015-08-18T22:10:42Z","pushed_at":"2015-08-19T11:22:47Z","git_url":"git://github.com/torchbox/wagtail.git","ssh_url":"git@github.com:torchbox/wagtail.git","clone_url":"https://github.com/torchbox/wagtail.git","svn_url":"https://github.com/torchbox/wagtail","homepage":"wagtail.io","size":35790,"stargazers_count":2827,"watchers_count":2827,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":416,"mirror_url":null,"open_issues_count":338,"forks":416,"open_issues":338,"watchers":2827,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/torchbox/wagtail/pulls/1579"},"html":{"href":"https://github.com/torchbox/wagtail/pull/1579"},"issue":{"href":"https://api.github.com/repos/torchbox/wagtail/issues/1579"},"comments":{"href":"https://api.github.com/repos/torchbox/wagtail/issues/1579/comments"},"review_comments":{"href":"https://api.github.com/repos/torchbox/wagtail/pulls/1579/comments"},"review_comment":{"href":"https://api.github.com/repos/torchbox/wagtail/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/torchbox/wagtail/pulls/1579/commits"},"statuses":{"href":"https://api.github.com/repos/torchbox/wagtail/statuses/634136fb78a6526027e88c6f4a811221e15219a9"}}}}