Commit 0b93bd25 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

Start update from list in viewset

parent 056c92a2
Pipeline #14347 failed with stages
in 1 minute and 58 seconds
......@@ -5,6 +5,10 @@ from rest_framework.response import Response
class BulkViewSet(ModelViewSet):
def get_objects(self, instance_ids):
return self.queryset.in_bulk(instance_ids, field_name=self.lookup_field)
def _created_payload(self, serializer, request):
if isinstance(request.data, list):
return {
......@@ -29,11 +33,17 @@ class BulkViewSet(ModelViewSet):
)
def update(self, request, *args, **kwargs):
print(self.lookup_field)
if self.lookup_field in kwargs.keys():
# perform the classic update
return super().update(request, *args, **kwargs)
if isinstance(request.data, list):
print("this is a list man")
instance_ids = set([element[self.lookup_field] for element in request.data])
instances_set = self.get_objects(instance_ids)
instances = [instance for instance in instances_set.values()]
serializer = self.get_serializer(instances, data=request.data, many=True)
serializer.is_valid(raise_exception=True)
# instance = self.get_object()
# serializer = self.get_serializer(instance, data=request.data, partial=partial)
# serializer.is_valid(raise_exception=True)
......
......@@ -62,16 +62,22 @@ class TestOperationsBulkViewSet(APITestCase):
self.assertDictEqual(self.function_api.get(function.function_id), data)
def test_update_existing_functions(self):
functions = FunctionFactory.create_batch(2)
data = [
{
"function_id": "k_test1",
"source": "kegg",
"name": "Kegg Test 1"
"function_id": functions[0].function_id,
"source": functions[0].source,
"name": "Test 1"
},
{
"function_id": "k_test2",
"source": "kegg",
"name": "Kegg Test 2"
"function_id": functions[1].function_id,
"source": functions[1].source,
"name": "Test 2"
}
]
print(self.function_api.put(data))
for element in data:
self.assertNotEqual(self.function_api.get(element['function_id']), element)
self.function_api.put(data)
self.assertEqual(self.function_api.get_all()['count'], 2)
for element in data:
self.assertDictEqual(self.function_api.get(element['function_id']), element)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment