def create(self, request, *args, **kwargs): # vd 조회 vd = self.vd if not vd: return Response(status=status.HTTP_401_UNAUTHORIZED) # guide 조회 guide = convert_to_json(request.data['guide']) # myself 변환 if 'vd' in guide and guide['vd'] == 'myself': guide['vd'] = vd.id # check validation 1 if 'type' in guide and guide['type'] == 'user': ru = RealUser.objects.get(email=guide['email']) if ru == self.vd.realOwner: return Response(status=status.HTTP_400_BAD_REQUEST) # proxy 조회 try: proxy = Proxy.objects.get(guide=guide) except Proxy.DoesNotExist: vd_publisher = VD() # TODO : type 이 images 가 아닌 경우에 대한 구현 if guide['type'] == 'images': vd_publisher.is_private = True vd_publisher.is_public = False vd_publisher.parent = vd vd_publisher.save() proxy = Proxy.objects.create(vd=vd_publisher, guide=guide) # check validation 2 if not proxy.vd: raise NotImplementedError if proxy.vd == vd: return Response(status=status.HTTP_400_BAD_REQUEST) if proxy.vd.is_private: if not proxy.vd.parent: raise NotImplementedError if proxy.vd.parent != vd: return Response(status=status.HTTP_403_FORBIDDEN) # importer 생성 및 celery task 처리 importer, is_created = Importer.objects.get_or_create(publisher=proxy, subscriber=vd) importer.start(high_priority=is_created) # 결과 처리 serializer = self.get_serializer(importer) return Response(serializer.data, status=status.HTTP_201_CREATED)
def test_create_images4_importer_private(self): self.assertEqual(Proxy.objects.count(), 1) guide_json2 = '{"type": "images", "vd": %d}' % self.vd_subscriber.id vd = VD() vd.is_private = True _vd = VD() _vd.save() vd.parent = _vd vd.save() proxy = Proxy.objects.create(vd=vd, guide=guide_json2) self.assertEqual(Proxy.objects.count(), 2) response = self.client.post('/importers/', dict(guide=guide_json2)) self.assertEqual(Proxy.objects.count(), 2) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)