Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfe07f30

Browse files
committed
Remove 'PatchRelationSerializer'
This wasn't writeable for reasons I haven't been able to figure out.However, it's not actually needed: the 'PatchSerializer' can do the jobjust fine, given enough information. This exposes a bug in DRF, whichhas been reported upstream [1]. While we wait for that fix, or somevariant of it, to be merged, we must monkey patch the library.[1]encode/django-rest-framework#7550[2]encode/django-rest-framework#7574Signed-off-by: Stephen Finucane <stephen@that.guru>Reported-by: Ralf Ramsauer <ralf.ramsauer@oth-regensburg.de>Closes:#379Cc: Daniel Axtens <dja@axtens.net>Cc: Rohit Sarkar <rohitsarkar5398@gmail.com>
1 parent8092f8f commitfe07f30

File tree

4 files changed

+65
-42
lines changed

4 files changed

+65
-42
lines changed

‎patchwork/api/__init__.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Patchwork - automated patch tracking system
2+
# Copyright (C) 2020, Stephen Finucane <stephen@that.guru>
3+
#
4+
# SPDX-License-Identifier: GPL-2.0-or-later
5+
6+
fromrest_framework.fieldsimportempty
7+
fromrest_framework.fieldsimportget_attribute
8+
fromrest_framework.fieldsimportSkipField
9+
fromrest_framework.relationsimportManyRelatedField
10+
11+
12+
# monkey patch django-rest-framework to work around issue #7550 [1] until #7574
13+
# [2] or some other variant lands
14+
#
15+
# [1] https://github.com/encode/django-rest-framework/issues/7550
16+
# [2] https://github.com/encode/django-rest-framework/pull/7574
17+
18+
def_get_attribute(self,instance):
19+
# Can't have any relationships if not created
20+
ifhasattr(instance,'pk')andinstance.pkisNone:
21+
return []
22+
23+
try:
24+
relationship=get_attribute(instance,self.source_attrs)
25+
except (KeyError,AttributeError)asexc:
26+
ifself.defaultisnotempty:
27+
returnself.get_default()
28+
ifself.allow_null:
29+
returnNone
30+
ifnotself.required:
31+
raiseSkipField()
32+
msg= (
33+
'Got {exc_type} when attempting to get a value for field '
34+
'`{field}` on serializer `{serializer}`.\nThe serializer '
35+
'field might be named incorrectly and not match '
36+
'any attribute or key on the `{instance}` instance.\n'
37+
'Original exception text was: {exc}.'.format(
38+
exc_type=type(exc).__name__,
39+
field=self.field_name,
40+
serializer=self.parent.__class__.__name__,
41+
instance=instance.__class__.__name__,
42+
exc=exc
43+
)
44+
)
45+
raisetype(exc)(msg)
46+
47+
returnrelationship.all()ifhasattr(relationship,'all')elserelationship
48+
49+
50+
ManyRelatedField.get_attribute=_get_attribute

‎patchwork/api/embedded.py‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
fromcollectionsimportOrderedDict
1313

1414
fromrest_framework.serializersimportCharField
15-
fromrest_framework.serializersimportSerializerMethodField
1615
fromrest_framework.serializersimportPrimaryKeyRelatedField
17-
fromrest_framework.serializersimportValidationError
16+
fromrest_framework.serializersimportSerializerMethodField
1817

1918
frompatchwork.api.baseimportBaseHyperlinkedModelSerializer
2019
frompatchwork.api.baseimportCheckHyperlinkedIdentityField
@@ -139,31 +138,6 @@ class Meta:
139138
}
140139

141140

142-
classPatchRelationSerializer(BaseHyperlinkedModelSerializer):
143-
"""Hide the PatchRelation model, just show the list"""
144-
patches=PatchSerializer(many=True,
145-
style={'base_template':'input.html'})
146-
147-
defto_internal_value(self,data):
148-
ifnotisinstance(data,type([])):
149-
raiseValidationError(
150-
"Patch relations must be specified as a list of patch IDs"
151-
)
152-
result=super(PatchRelationSerializer,self).to_internal_value(
153-
{'patches':data}
154-
)
155-
returnresult
156-
157-
defto_representation(self,instance):
158-
data=super(PatchRelationSerializer,self).to_representation(instance)
159-
data=data['patches']
160-
returndata
161-
162-
classMeta:
163-
model=models.PatchRelation
164-
fields= ('patches',)
165-
166-
167141
classPersonSerializer(SerializedRelatedField):
168142

169143
class_Serializer(BaseHyperlinkedModelSerializer):

‎patchwork/api/event.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
frompatchwork.api.embeddedimportCheckSerializer
1414
frompatchwork.api.embeddedimportCoverSerializer
1515
frompatchwork.api.embeddedimportPatchSerializer
16-
frompatchwork.api.embeddedimportPatchRelationSerializer
1716
frompatchwork.api.embeddedimportProjectSerializer
1817
frompatchwork.api.embeddedimportSeriesSerializer
1918
frompatchwork.api.embeddedimportUserSerializer
@@ -34,8 +33,10 @@ class EventSerializer(ModelSerializer):
3433
current_delegate=UserSerializer()
3534
created_check=SerializerMethodField()
3635
created_check=CheckSerializer()
37-
previous_relation=PatchRelationSerializer(read_only=True)
38-
current_relation=PatchRelationSerializer(read_only=True)
36+
previous_relation=PatchSerializer(
37+
source='previous_relation.patches',many=True,default=None)
38+
current_relation=PatchSerializer(
39+
source='current_relation.patches',many=True,default=None)
3940

4041
_category_map= {
4142
Event.CATEGORY_COVER_CREATED: ['cover'],

‎patchwork/api/patch.py‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
fromdjango.core.exceptionsimportValidationError
1111
fromdjango.utils.textimportslugify
1212
fromdjango.utils.translationimportgettext_lazyas_
13-
fromrest_frameworkimportstatus
1413
fromrest_framework.exceptionsimportAPIException
1514
fromrest_framework.exceptionsimportPermissionDenied
1615
fromrest_framework.genericsimportListAPIView
1716
fromrest_framework.genericsimportRetrieveUpdateAPIView
1817
fromrest_framework.relationsimportRelatedField
1918
fromrest_framework.reverseimportreverse
2019
fromrest_framework.serializersimportSerializerMethodField
20+
fromrest_frameworkimportstatus
2121

2222
frompatchwork.api.baseimportBaseHyperlinkedModelSerializer
2323
frompatchwork.api.baseimportPatchworkPermission
24-
frompatchwork.api.filtersimportPatchFilterSet
25-
frompatchwork.api.embeddedimportPatchRelationSerializer
24+
frompatchwork.api.embeddedimportPatchSerializer
2625
frompatchwork.api.embeddedimportPersonSerializer
2726
frompatchwork.api.embeddedimportProjectSerializer
2827
frompatchwork.api.embeddedimportSeriesSerializer
2928
frompatchwork.api.embeddedimportUserSerializer
29+
frompatchwork.api.filtersimportPatchFilterSet
3030
frompatchwork.modelsimportPatch
3131
frompatchwork.modelsimportPatchRelation
3232
frompatchwork.modelsimportState
@@ -83,7 +83,8 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer):
8383
check=SerializerMethodField()
8484
checks=SerializerMethodField()
8585
tags=SerializerMethodField()
86-
related=PatchRelationSerializer()
86+
related=PatchSerializer(
87+
source='related.patches',many=True,default=[])
8788

8889
defget_web_url(self,instance):
8990
request=self.context.get('request')
@@ -127,14 +128,11 @@ def to_representation(self, instance):
127128
data=super(PatchListSerializer,self).to_representation(instance)
128129
data['series']= [data['series']]ifdata['series']else []
129130

130-
# stop the related serializer returning this patch in the list of
131-
# related patches. Also make it return an empty list, not null/None
132-
if'related'indata:
133-
ifdata['related']:
134-
data['related']= [pforpindata['related']
135-
ifp['id']!=instance.id]
136-
else:
137-
data['related']= []
131+
# Remove this patch from 'related'
132+
if'related'indataanddata['related']:
133+
data['related']= [
134+
xforxindata['related']ifx['id']!=data['id']
135+
]
138136

139137
returndata
140138

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp