|
| 1 | +# Patchwork - automated patch tracking system |
| 2 | +# Copyright (C) 2018 Red Hat |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | + |
| 6 | + |
| 7 | +from rest_framework import permissions |
| 8 | +from rest_framework.generics import get_object_or_404 |
| 9 | +from rest_framework.generics import CreateAPIView |
| 10 | +from rest_framework.generics import RetrieveUpdateDestroyAPIView |
| 11 | +from rest_framework.generics import ListAPIView |
| 12 | +from patchwork.api.patch import PatchSerializer |
| 13 | +from patchwork.api.user import UserDetailSerializer |
| 14 | +from patchwork.api.base import BaseHyperlinkedModelSerializer |
| 15 | +from patchwork.models import Note |
| 16 | +from patchwork.models import Patch |
| 17 | + |
| 18 | + |
| 19 | +class NoteSerializer(BaseHyperlinkedModelSerializer): |
| 20 | + submitter = UserDetailSerializer(read_only=True) |
| 21 | + patch = PatchSerializer(read_only=True) |
| 22 | + |
| 23 | + class Meta: |
| 24 | + model = Note |
| 25 | + fields = [ |
| 26 | + 'id', |
| 27 | + 'patch', |
| 28 | + 'submitter', |
| 29 | + 'content', |
| 30 | + 'last_modified', |
| 31 | + 'maintainer_only', |
| 32 | + ] |
| 33 | + read_only_fields = [ |
| 34 | + 'id', |
| 35 | + 'patch', |
| 36 | + 'submitter', |
| 37 | + 'last_modified', |
| 38 | + 'maintainer_only', |
| 39 | + ] |
| 40 | + |
| 41 | + |
| 42 | +class NoteDetailPermission(permissions.BasePermission): |
| 43 | + def has_permission(self, request, view): |
| 44 | + if not request.user.is_authenticated: |
| 45 | + return False |
| 46 | + |
| 47 | + if request.method == 'POST': |
| 48 | + patch = Patch.objects.get(id=view.kwargs['patch_id']) |
| 49 | + return ( |
| 50 | + patch.project in request.user.profile.maintainer_projects.all() |
| 51 | + ) |
| 52 | + |
| 53 | + note = Note.objects.get(id=view.kwargs['note_id']) |
| 54 | + return ( |
| 55 | + note.patch.project |
| 56 | + in request.user.profile.maintainer_projects.all() |
| 57 | + ) |
| 58 | + |
| 59 | + def has_object_permission(self, request, view, obj): |
| 60 | + if ( |
| 61 | + not obj.maintainer_only |
| 62 | + and request.method in permissions.SAFE_METHODS |
| 63 | + ): |
| 64 | + return True |
| 65 | + |
| 66 | + if request.method == 'POST': |
| 67 | + patch = Patch.objects.get(id=view.kwargs['patch_id']) |
| 68 | + return ( |
| 69 | + patch.project in request.user.profile.maintainer_projects.all() |
| 70 | + ) |
| 71 | + |
| 72 | + note = Note.objects.get(id=view.kwargs['note_id']) |
| 73 | + return ( |
| 74 | + note.patch.project |
| 75 | + in request.user.profile.maintainer_projects.all() |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +class NoteListPermission(permissions.BasePermission): |
| 80 | + def has_permission(self, request, view): |
| 81 | + if request.method in permissions.SAFE_METHODS: |
| 82 | + return True |
| 83 | + if not request.user.is_authenticated: |
| 84 | + return False |
| 85 | + patch = Patch.objects.get(id=view.kwargs['patch_id']) |
| 86 | + return patch.project in request.user.profile.maintainer_projects.all() |
| 87 | + |
| 88 | + def has_object_permission(self, request, view, obj): |
| 89 | + if request.method in permissions.SAFE_METHODS: |
| 90 | + return True |
| 91 | + |
| 92 | + |
| 93 | +class NoteMixin(object): |
| 94 | + queryset = Note.objects.all() |
| 95 | + serializer_class = NoteSerializer |
| 96 | + lookup_field = 'patch_id' |
| 97 | + |
| 98 | + def get_queryset(self): |
| 99 | + patch_id = self.kwargs['patch_id'] |
| 100 | + get_object_or_404(Patch, id=patch_id) |
| 101 | + |
| 102 | + return Note.objects.filter(patch=patch_id) |
| 103 | + |
| 104 | + |
| 105 | +class NoteDetail(NoteMixin, RetrieveUpdateDestroyAPIView): |
| 106 | + permission_classes = [NoteDetailPermission] |
| 107 | + |
| 108 | + def get_object(self): |
| 109 | + queryset = self.filter_queryset(self.get_queryset()) |
| 110 | + note_id = self.kwargs.get('note_id') |
| 111 | + instance = get_object_or_404(queryset, id=note_id) |
| 112 | + self.check_object_permissions(self.request, instance) |
| 113 | + return instance |
| 114 | + |
| 115 | + |
| 116 | +class NoteList(NoteMixin, CreateAPIView, ListAPIView): |
| 117 | + ordering = 'id' |
| 118 | + permission_classes = [NoteListPermission] |
| 119 | + |
| 120 | + def get_queryset(self): |
| 121 | + patch_queryset = Patch.objects.all() |
| 122 | + |
| 123 | + queryset = super(NoteMixin, self).get_queryset() |
| 124 | + public_notes = queryset.filter(maintainer_only=False) |
| 125 | + user_patches = patch_queryset.filter( |
| 126 | + project__in=list( |
| 127 | + self.request.user.profile.maintainer_projects.all() |
| 128 | + ) |
| 129 | + ) |
| 130 | + maintainer_notes = queryset.filter( |
| 131 | + maintainer_only=True, patch__in=list(user_patches) |
| 132 | + ) |
| 133 | + |
| 134 | + return public_notes | maintainer_notes |
| 135 | + |
| 136 | + def perform_create(self, serializer): |
| 137 | + serializer.save( |
| 138 | + submitter=self.request.user, |
| 139 | + patch=Patch.objects.get(id=self.kwargs['patch_id']), |
| 140 | + ) |
| 141 | + return super().perform_create(serializer) |
0 commit comments