don't save ticket state in multiple locations
This commit is contained in:
parent
fd7847993b
commit
7b77c183fb
3 changed files with 62 additions and 43 deletions
|
@ -24,6 +24,17 @@ class IssueSerializer(serializers.ModelSerializer):
|
|||
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity')
|
||||
read_only_fields = ('id', 'timeline', 'last_activity')
|
||||
|
||||
def to_internal_value(self, data):
|
||||
ret = super().to_internal_value(data)
|
||||
if 'state' in data:
|
||||
ret['state'] = data['state']
|
||||
return ret
|
||||
|
||||
def validate(self, attrs):
|
||||
if 'state' in attrs:
|
||||
if attrs['state'] not in [x[0] for x in STATE_CHOICES]:
|
||||
raise serializers.ValidationError('invalid state')
|
||||
return attrs
|
||||
@staticmethod
|
||||
def get_timeline(obj):
|
||||
timeline = []
|
||||
|
@ -53,16 +64,6 @@ class IssueSerializer(serializers.ModelSerializer):
|
|||
})
|
||||
return sorted(timeline, key=lambda x: x['timestamp'])
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
if 'state' in validated_data:
|
||||
instance.state = validated_data['state']
|
||||
instance.save()
|
||||
StateChange.objects.create(
|
||||
issue_thread=instance,
|
||||
state=validated_data['state'],
|
||||
)
|
||||
return instance
|
||||
|
||||
|
||||
class IssueViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = IssueSerializer
|
||||
|
@ -154,6 +155,7 @@ def get_available_states(request):
|
|||
def get_state_choices():
|
||||
for state in STATE_CHOICES:
|
||||
yield {'value': list(state)[0], 'text': list(state)[1]}
|
||||
|
||||
return Response(get_state_choices())
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue