{"action":"created","comment":{"url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/comments/64358527","id":64358527,"diff_hunk":"@@ -61,17 +70,149 @@ class DomainViewSet(OwnerViewSet):\n \n     queryset = Domain.objects.all().select_related('owner')\n     serializer_class = DomainSerializer\n-    filter_fields = ('name', 'type')\n+    filter_fields = ('name', 'type', 'owner',)\n     permission_classes = (DomainPermission,)\n \n \n+class RecordRequestsViewSet(ReadOnlyModelViewSet):\n+    queryset = RecordRequest.objects.all().select_related(\n+        'owner', 'domain', 'record', 'target_owner',\n+    )\n+    filter_fields = ('owner',)\n+    serializer_class = RecordRequestSerializer\n+\n+\n class RecordViewSet(OwnerViewSet):\n \n     queryset = Record.objects.all().select_related('owner', 'domain')\n     serializer_class = RecordSerializer\n-    filter_fields = ('name', 'content', 'domain')\n+    filter_fields = ('name', 'content', 'domain', 'owner',)\n     search_fields = filter_fields\n \n+    def get_object(self):\n+        obj = super().get_object()\n+        if self.request.version == 'v1':\n+            if (\n+                self.request._request.method in ('PATCH', 'PUT') and\n+                not can_edit(self.request.user, obj)\n+            ):\n+                raise exceptions.PermissionDenied(detail='No permission')\n+            if (\n+                self.request._request.method == 'DELETE' and\n+                not can_delete(self.request.user, obj)\n+            ):\n+                raise exceptions.PermissionDenied(detail='No permission')\n+        return obj\n+\n+    def get_serializer_class(self):\n+        if self.request.version == 'v1':\n+            serializer_class = RecordSerializer\n+        if self.request.version == 'v2':\n+            serializer_class = RecordSerializerV2\n+        return serializer_class\n+\n+    def create(self, request, *args, **kwargs):\n+        if self.request.version == 'v1':\n+            return super().create(request, *args, **kwargs)\n+        serializer = self.get_serializer(data=request.data)\n+        serializer.is_valid(raise_exception=True)\n+\n+        record_request = RecordRequest(\n+            domain_id=serializer.validated_data['domain'],\n+            owner_id=request.user.id,\n+        )\n+        record_request.copy_records_data(serializer.validated_data.items())\n+        record_request.save()\n+\n+        auto_accept = (\n+            self.request.user.is_superuser or\n+            serializer.validated_data['domain'].unrestricted or\n+            self.request.user == serializer.validated_data['domain'].owner or\n+            request.user.id in serializer.validated_data['domain'].authorisations.values_list('authorised', flat=True) # noqa\n+        )\n+        if auto_accept:\n+            serializer.save()\n+            record_request.record = serializer.instance\n+            record_request.accept()\n+            data = serializer.data\n+            code = status.HTTP_201_CREATED\n+            headers = {}\n+        else:\n+            data = {}\n+            code = status.HTTP_202_ACCEPTED\n+            headers = {\n+                'Location': reverse(\n+                    'recordrequest-detail', kwargs={'pk': record_request.id}\n+                )\n+            }\n+        return Response(data, status=code, headers=headers)\n+\n+    def update(self, request, *args, **kwargs):\n+        if self.request.version == 'v1':\n+            return super().update(request, *args, **kwargs)\n+        partial = kwargs.pop('partial', False)\n+        instance = self.get_object()\n+        serializer = self.get_serializer(instance, data=request.data, partial=partial)\n+        serializer.is_valid(raise_exception=True)\n+\n+        record_request = RecordRequest(\n+            domain_id=serializer.instance.domain_id,\n+            owner_id=request.user.id,\n+            record_id=serializer.instance.id,\n+        )\n+        # in validated_data lands fields required by model, even if they\n+        # weren't changed, so filter it by matching validated vs initial\n+        data_to_copy = [\n+            (field_name, serializer.validated_data[field_name])\n+            for field_name in (\n+                serializer.validated_data.keys() &\n+                serializer.initial_data.keys()\n+            )\n+        ]\n+        record_request.copy_records_data(data_to_copy)","path":"powerdns/views.py","position":148,"original_position":148,"commit_id":"f8a6edfb30e43d7881e40b541a02a5dd0e26b917","original_commit_id":"f8a6edfb30e43d7881e40b541a02a5dd0e26b917","user":{"login":"mkurek","id":107437,"avatar_url":"https://avatars.githubusercontent.com/u/107437?v=3","gravatar_id":"","url":"https://api.github.com/users/mkurek","html_url":"https://github.com/mkurek","followers_url":"https://api.github.com/users/mkurek/followers","following_url":"https://api.github.com/users/mkurek/following{/other_user}","gists_url":"https://api.github.com/users/mkurek/gists{/gist_id}","starred_url":"https://api.github.com/users/mkurek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mkurek/subscriptions","organizations_url":"https://api.github.com/users/mkurek/orgs","repos_url":"https://api.github.com/users/mkurek/repos","events_url":"https://api.github.com/users/mkurek/events{/privacy}","received_events_url":"https://api.github.com/users/mkurek/received_events","type":"User","site_admin":false},"body":"maybe we could use https://docs.djangoproject.com/en/1.9/ref/models/instances/#specifying-which-fields-to-save here?","created_at":"2016-05-24T09:34:11Z","updated_at":"2016-05-24T09:34:11Z","html_url":"https://github.com/allegro/django-powerdns-dnssec/pull/79#discussion_r64358527","pull_request_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79","_links":{"self":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/comments/64358527"},"html":{"href":"https://github.com/allegro/django-powerdns-dnssec/pull/79#discussion_r64358527"},"pull_request":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79"}}},"pull_request":{"url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79","id":70692179,"html_url":"https://github.com/allegro/django-powerdns-dnssec/pull/79","diff_url":"https://github.com/allegro/django-powerdns-dnssec/pull/79.diff","patch_url":"https://github.com/allegro/django-powerdns-dnssec/pull/79.patch","issue_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/issues/79","number":79,"state":"open","locked":false,"title":"API resource Record creates Request objects in v2","user":{"login":"xliiv","id":298994,"avatar_url":"https://avatars.githubusercontent.com/u/298994?v=3","gravatar_id":"","url":"https://api.github.com/users/xliiv","html_url":"https://github.com/xliiv","followers_url":"https://api.github.com/users/xliiv/followers","following_url":"https://api.github.com/users/xliiv/following{/other_user}","gists_url":"https://api.github.com/users/xliiv/gists{/gist_id}","starred_url":"https://api.github.com/users/xliiv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xliiv/subscriptions","organizations_url":"https://api.github.com/users/xliiv/orgs","repos_url":"https://api.github.com/users/xliiv/repos","events_url":"https://api.github.com/users/xliiv/events{/privacy}","received_events_url":"https://api.github.com/users/xliiv/received_events","type":"User","site_admin":false},"body":"API is now versioned (default version is 'v1').\r\nIn version 'v2' resource Record creates always Requests (RecordRequest for\r\ncreation and update, DeleteRequest for deletion)\r\nBeside that Record endpoint also checks if requested changes can be\r\nautoaccepted, which instantly changes Records in powerdns table.","created_at":"2016-05-19T13:17:57Z","updated_at":"2016-05-24T09:34:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bf16a13954b6325aec2e4b157d830c000737ddf6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79/commits","review_comments_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79/comments","review_comment_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/comments{/number}","comments_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/issues/79/comments","statuses_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/statuses/f8a6edfb30e43d7881e40b541a02a5dd0e26b917","head":{"label":"xliiv:new-api","ref":"new-api","sha":"f8a6edfb30e43d7881e40b541a02a5dd0e26b917","user":{"login":"xliiv","id":298994,"avatar_url":"https://avatars.githubusercontent.com/u/298994?v=3","gravatar_id":"","url":"https://api.github.com/users/xliiv","html_url":"https://github.com/xliiv","followers_url":"https://api.github.com/users/xliiv/followers","following_url":"https://api.github.com/users/xliiv/following{/other_user}","gists_url":"https://api.github.com/users/xliiv/gists{/gist_id}","starred_url":"https://api.github.com/users/xliiv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xliiv/subscriptions","organizations_url":"https://api.github.com/users/xliiv/orgs","repos_url":"https://api.github.com/users/xliiv/repos","events_url":"https://api.github.com/users/xliiv/events{/privacy}","received_events_url":"https://api.github.com/users/xliiv/received_events","type":"User","site_admin":false},"repo":{"id":53501845,"name":"django-powerdns-dnssec","full_name":"xliiv/django-powerdns-dnssec","owner":{"login":"xliiv","id":298994,"avatar_url":"https://avatars.githubusercontent.com/u/298994?v=3","gravatar_id":"","url":"https://api.github.com/users/xliiv","html_url":"https://github.com/xliiv","followers_url":"https://api.github.com/users/xliiv/followers","following_url":"https://api.github.com/users/xliiv/following{/other_user}","gists_url":"https://api.github.com/users/xliiv/gists{/gist_id}","starred_url":"https://api.github.com/users/xliiv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xliiv/subscriptions","organizations_url":"https://api.github.com/users/xliiv/orgs","repos_url":"https://api.github.com/users/xliiv/repos","events_url":"https://api.github.com/users/xliiv/events{/privacy}","received_events_url":"https://api.github.com/users/xliiv/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/xliiv/django-powerdns-dnssec","description":"Django application managing PowerDNS database","fork":true,"url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec","forks_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/forks","keys_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/keys{/key_id}","collaborators_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/teams","hooks_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/hooks","issue_events_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/issues/events{/number}","events_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/events","assignees_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/assignees{/user}","branches_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/branches{/branch}","tags_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/tags","blobs_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/git/refs{/sha}","trees_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/git/trees{/sha}","statuses_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/statuses/{sha}","languages_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/languages","stargazers_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/stargazers","contributors_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/contributors","subscribers_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/subscribers","subscription_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/subscription","commits_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/commits{/sha}","git_commits_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/git/commits{/sha}","comments_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/comments{/number}","issue_comment_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/issues/comments{/number}","contents_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/contents/{+path}","compare_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/compare/{base}...{head}","merges_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/merges","archive_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/downloads","issues_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/issues{/number}","pulls_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/pulls{/number}","milestones_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/milestones{/number}","notifications_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/labels{/name}","releases_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/releases{/id}","deployments_url":"https://api.github.com/repos/xliiv/django-powerdns-dnssec/deployments","created_at":"2016-03-09T13:59:19Z","updated_at":"2016-03-09T13:59:20Z","pushed_at":"2016-05-24T08:35:35Z","git_url":"git://github.com/xliiv/django-powerdns-dnssec.git","ssh_url":"git@github.com:xliiv/django-powerdns-dnssec.git","clone_url":"https://github.com/xliiv/django-powerdns-dnssec.git","svn_url":"https://github.com/xliiv/django-powerdns-dnssec","homepage":"http://django-powerdns-dnssec.readthedocs.org","size":342,"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":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"develop"}},"base":{"label":"allegro:develop","ref":"develop","sha":"570ce196df355892c3542561a222c6c27bd08507","user":{"login":"allegro","id":562236,"avatar_url":"https://avatars.githubusercontent.com/u/562236?v=3","gravatar_id":"","url":"https://api.github.com/users/allegro","html_url":"https://github.com/allegro","followers_url":"https://api.github.com/users/allegro/followers","following_url":"https://api.github.com/users/allegro/following{/other_user}","gists_url":"https://api.github.com/users/allegro/gists{/gist_id}","starred_url":"https://api.github.com/users/allegro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allegro/subscriptions","organizations_url":"https://api.github.com/users/allegro/orgs","repos_url":"https://api.github.com/users/allegro/repos","events_url":"https://api.github.com/users/allegro/events{/privacy}","received_events_url":"https://api.github.com/users/allegro/received_events","type":"Organization","site_admin":false},"repo":{"id":37307631,"name":"django-powerdns-dnssec","full_name":"allegro/django-powerdns-dnssec","owner":{"login":"allegro","id":562236,"avatar_url":"https://avatars.githubusercontent.com/u/562236?v=3","gravatar_id":"","url":"https://api.github.com/users/allegro","html_url":"https://github.com/allegro","followers_url":"https://api.github.com/users/allegro/followers","following_url":"https://api.github.com/users/allegro/following{/other_user}","gists_url":"https://api.github.com/users/allegro/gists{/gist_id}","starred_url":"https://api.github.com/users/allegro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allegro/subscriptions","organizations_url":"https://api.github.com/users/allegro/orgs","repos_url":"https://api.github.com/users/allegro/repos","events_url":"https://api.github.com/users/allegro/events{/privacy}","received_events_url":"https://api.github.com/users/allegro/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/allegro/django-powerdns-dnssec","description":"Django application managing PowerDNS database","fork":false,"url":"https://api.github.com/repos/allegro/django-powerdns-dnssec","forks_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/forks","keys_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/keys{/key_id}","collaborators_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/teams","hooks_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/hooks","issue_events_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/issues/events{/number}","events_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/events","assignees_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/assignees{/user}","branches_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/branches{/branch}","tags_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/tags","blobs_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/git/refs{/sha}","trees_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/git/trees{/sha}","statuses_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/statuses/{sha}","languages_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/languages","stargazers_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/stargazers","contributors_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/contributors","subscribers_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/subscribers","subscription_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/subscription","commits_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/commits{/sha}","git_commits_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/git/commits{/sha}","comments_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/comments{/number}","issue_comment_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/issues/comments{/number}","contents_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/contents/{+path}","compare_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/compare/{base}...{head}","merges_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/merges","archive_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/downloads","issues_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/issues{/number}","pulls_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls{/number}","milestones_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/milestones{/number}","notifications_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/labels{/name}","releases_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/releases{/id}","deployments_url":"https://api.github.com/repos/allegro/django-powerdns-dnssec/deployments","created_at":"2015-06-12T07:12:29Z","updated_at":"2016-05-16T13:55:33Z","pushed_at":"2016-05-24T08:35:35Z","git_url":"git://github.com/allegro/django-powerdns-dnssec.git","ssh_url":"git@github.com:allegro/django-powerdns-dnssec.git","clone_url":"https://github.com/allegro/django-powerdns-dnssec.git","svn_url":"https://github.com/allegro/django-powerdns-dnssec","homepage":"http://django-powerdns-dnssec.readthedocs.org","size":389,"stargazers_count":13,"watchers_count":13,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":6,"forks":5,"open_issues":6,"watchers":13,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79"},"html":{"href":"https://github.com/allegro/django-powerdns-dnssec/pull/79"},"issue":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/issues/79"},"comments":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/issues/79/comments"},"review_comments":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79/comments"},"review_comment":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/pulls/79/commits"},"statuses":{"href":"https://api.github.com/repos/allegro/django-powerdns-dnssec/statuses/f8a6edfb30e43d7881e40b541a02a5dd0e26b917"}}}}
{"actor":{"display_login":"mkurek"}}