forked from getpatchwork/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.py
More file actions
78 lines (59 loc) · 2.39 KB
/
Copy pathbundle.py
File metadata and controls
78 lines (59 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Patchwork - automated patch tracking system
# Copyright (C) 2017 Stephen Finucane <stephen@that.guru>
#
# SPDX-License-Identifier: GPL-2.0-or-later
from django.db.models import Q
from rest_framework.generics import ListAPIView
from rest_framework.generics import RetrieveAPIView
from rest_framework.serializers import SerializerMethodField
from patchwork.api.base import BaseHyperlinkedModelSerializer
from patchwork.api.base import PatchworkPermission
from patchwork.api.filters import BundleFilterSet
from patchwork.api.embedded import PatchSerializer
from patchwork.api.embedded import ProjectSerializer
from patchwork.api.embedded import UserSerializer
from patchwork.models import Bundle
class BundleSerializer(BaseHyperlinkedModelSerializer):
web_url = SerializerMethodField()
project = ProjectSerializer(read_only=True)
mbox = SerializerMethodField()
owner = UserSerializer(read_only=True)
patches = PatchSerializer(many=True, read_only=True)
def get_web_url(self, instance):
request = self.context.get('request')
return request.build_absolute_uri(instance.get_absolute_url())
def get_mbox(self, instance):
request = self.context.get('request')
return request.build_absolute_uri(instance.get_mbox_url())
class Meta:
model = Bundle
fields = ('id', 'url', 'web_url', 'project', 'name', 'owner',
'patches', 'public', 'mbox')
read_only_fields = ('owner', 'patches', 'mbox')
versioned_fields = {
'1.1': ('web_url', ),
}
extra_kwargs = {
'url': {'view_name': 'api-bundle-detail'},
}
class BundleMixin(object):
permission_classes = (PatchworkPermission,)
serializer_class = BundleSerializer
def get_queryset(self):
if self.request.user.is_authenticated:
bundle_filter = Q(owner=self.request.user) | Q(public=True)
else:
bundle_filter = Q(public=True)
return Bundle.objects\
.filter(bundle_filter)\
.prefetch_related('patches',)\
.select_related('owner', 'project')
class BundleList(BundleMixin, ListAPIView):
"""List bundles."""
filter_class = filterset_class = BundleFilterSet
search_fields = ('name',)
ordering_fields = ('id', 'name', 'owner')
ordering = 'id'
class BundleDetail(BundleMixin, RetrieveAPIView):
"""Show a bundle."""
pass