mirror of https://github.com/snachodog/mybuddy.git
Default to current user on timer POST (#134)
This commit is contained in:
parent
d6c2b48917
commit
4e5153daed
|
@ -135,13 +135,24 @@ class TimerSerializer(CoreModelSerializer):
|
|||
child = serializers.PrimaryKeyRelatedField(
|
||||
allow_null=True, allow_empty=True, queryset=models.Child.objects.all(),
|
||||
required=False)
|
||||
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
|
||||
user = serializers.PrimaryKeyRelatedField(
|
||||
allow_null=True, allow_empty=True, queryset=User.objects.all(),
|
||||
required=False)
|
||||
|
||||
class Meta:
|
||||
model = models.Timer
|
||||
fields = ('id', 'child', 'name', 'start', 'end', 'duration', 'active',
|
||||
'user')
|
||||
|
||||
def validate(self, attrs):
|
||||
attrs = super(TimerSerializer, self).validate(attrs)
|
||||
|
||||
# Set user to current user if no value is provided.
|
||||
if 'user' not in attrs or attrs['user'] is None:
|
||||
attrs['user'] = self.context['request'].user
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
class TummyTimeSerializer(CoreModelWithDurationSerializer):
|
||||
class Meta(CoreModelWithDurationSerializer.Meta):
|
||||
|
|
|
@ -368,6 +368,13 @@ class TimerAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
|
|||
obj = models.Timer.objects.get(pk=response.data['id'])
|
||||
self.assertEqual(obj.name, data['name'])
|
||||
|
||||
def test_post_default_user(self):
|
||||
user = User.objects.first()
|
||||
response = self.client.post(self.endpoint)
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
obj = models.Timer.objects.get(pk=response.data['id'])
|
||||
self.assertEqual(obj.user, user)
|
||||
|
||||
def test_patch(self):
|
||||
endpoint = '{}{}/'.format(self.endpoint, 1)
|
||||
response = self.client.get(endpoint)
|
||||
|
|
Loading…
Reference in New Issue