handler.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    def update(self, request, validate=False, form=None, *args, **kwargs):

        try:
            object_id = getattr(getattr(self, self._object_name().lower()), 'id')
        except AttributeError:
            raise NoIdGiven

        model_attrs = self._set_put_attrs(request, 'model_attrs', **kwargs)
        form_attrs = self._set_put_attrs(request, 'form_attrs', **kwargs)

        inst = getattr(self, self._object_name().lower())
        form = form(form_attrs, instance=inst)

        if form.is_valid():
            for k,v in model_attrs.iteritems():
                setattr(inst, k, v)
            inst.save()
            if request.GET.get('return'):
                return inst
            return HttpMsg("%s with ID `%s` was updated." % (self._object_name(), object_id)).all_ok()
        return self._form_errors_to_json(form.errors.items())

tests.py

1
2
3
4
5
6
7
8
9
10
11
    def test_update_with_return(self):
        response = self.client.put('/api/1.0/task/2/update/?return=true', data={
            'name': 'Show me!'
        }, **self.auth2)
        self.assertEqual(response.content, 'here comes reaaaaaalllly long json ;)')

    def test_update_without_return(self):
        response = self.client.put('/api/1.0/task/1/update/', data={
            'name': 'Get child from school'
        }, **self.auth1)
        self.assertEqual(response.content, 'OK : Task with ID `1` was updated.')