def do_copy(request, entry_id, recv_data): user = User.objects.get(id=request.user.id) # validation check if 'entries' not in recv_data: return HttpResponse('Malformed data is specified (%s)' % recv_data, status=400) if not Entry.objects.filter(id=entry_id).exists(): return HttpResponse('Failed to get an Entry object of specified id', status=400) ret = [] entry = Entry.objects.get(id=entry_id) for new_name in [x for x in recv_data['entries'].split('\n') if x]: if Entry.objects.filter(schema=entry.schema, name=new_name).exists(): ret.append({ 'status': 'fail', 'msg': 'A same named entry (%s) already exists' % new_name, }) continue if custom_view.is_custom("do_copy_entry", entry.schema.name): (is_continue, status, msg) = custom_view.call_custom( "do_copy_entry", entry.schema.name, request, user, new_name) if not is_continue: ret.append({ 'status': 'success' if status else 'fail', 'msg': msg, }) continue params = { 'new_name': new_name, 'post_data': recv_data, } # Check another COPY job that targets same name entry is under processing if Job.objects.filter( operation=JobOperation.COPY_ENTRY.value, target=entry, status__in=[Job.STATUS['PREPARING'], Job.STATUS['PROCESSING']], params=json.dumps(params, sort_keys=True)): ret.append({ 'status': 'fail', 'msg': 'There is another job that targets same name(%s) is existed' % new_name, }) continue # make a new job to copy entry and run it job = Job.new_copy(user, entry, text=new_name, params=params) job.run() ret.append({ 'status': 'success', 'msg': "Success to create new entry '%s'" % new_name, }) return JsonResponse({'results': ret})
def do_copy(request, entry_id, recv_data): entry, error = get_obj_with_check_perm(request.user, Entry, entry_id, ACLType.Full) if error: return error ret = [] params = { "new_name_list": [], "post_data": recv_data, } for new_name in [x for x in recv_data["entries"].split("\n") if x]: if (new_name in params["new_name_list"] or Entry.objects.filter( schema=entry.schema, name=new_name).exists()): ret.append({ "status": "fail", "msg": "A same named entry (%s) already exists" % new_name, }) continue if custom_view.is_custom("do_copy_entry", entry.schema.name): (is_continue, status, msg) = custom_view.call_custom( "do_copy_entry", entry.schema.name, request, entry, recv_data, request.user, new_name, ) if not is_continue: ret.append({ "status": "success" if status else "fail", "msg": msg, }) continue params["new_name_list"].append(new_name) ret.append({ "status": "success", "msg": "Success to create new entry '%s'" % new_name, }) # if there is no entry to copy, do not create a job. if params["new_name_list"]: # make a new job to copy entry and run it job = Job.new_copy(request.user, entry, text="Preparing to copy entry", params=params) job.run() return JsonResponse({"results": ret})
def copy(self, request, pk): src_entry: Entry = self.get_object() if not src_entry.is_active: raise ValidationError("specified entry is not active") # validate post parameter serializer = self.get_serializer(src_entry, data=request.data) serializer.is_valid(raise_exception=True) # TODO Conversion to support the old UI params = { "new_name_list": request.data["copy_entry_names"], "post_data": request.data, } # run copy job job = Job.new_copy(request.user, src_entry, text="Preparing to copy entry", params=params) job.run() return Response({}, status=status.HTTP_200_OK)
def test_rerun_jobs(self): user = self.guest_login() entity = Entity.objects.create(name='entity', created_user=user) attr = EntityAttr.objects.create(name='attr', created_user=user, type=AttrTypeValue['string'], parent_entity=entity) entity.attrs.add(attr) # make a job to create an entry entry = Entry.objects.create(name='entry', schema=entity, created_user=user) job = Job.new_create(user, entry, params={ 'attrs': [ {'id': str(attr.id), 'value': [{'data': 'hoge', 'index': 0}], 'referral_key': []} ] }) # send request to run job resp = self.client.post('/api/v1/job/run/%d' % job.id) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content, b'"Success to run command"') job = Job.objects.get(id=job.id) self.assertEqual(job.status, Job.STATUS['DONE']) self.assertEqual(entry.attrs.count(), 1) attrv = entry.attrs.first().get_latest_value() self.assertEqual(attrv.value, 'hoge') # send request to run job with finished job-id resp = self.client.post('/api/v1/job/run/%d' % job.id) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content, b'"Target job has already been done"') # send request to run job with invalid job-id resp = self.client.post('/api/v1/job/run/%d' % 9999) self.assertEqual(resp.status_code, 400) # make and send a job to update entry job = Job.new_edit(user, entry, params={ 'attrs': [ {'id': str(entry.attrs.first().id), 'value': [{'data': 'fuga', 'index': 0}], 'referral_key': []} ] }) resp = self.client.post('/api/v1/job/run/%d' % job.id) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content, b'"Success to run command"') self.assertEqual(Job.objects.get(id=job.id).status, Job.STATUS['DONE']) self.assertEqual(entry.attrs.first().get_latest_value().value, 'fuga') # make and send a job to copy entry job = Job.new_copy(user, entry, params={'new_name': 'new_entry'}) resp = self.client.post('/api/v1/job/run/%d' % job.id) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content, b'"Success to run command"') self.assertEqual(Job.objects.get(id=job.id).status, Job.STATUS['DONE']) # checks it's success to clone entry new_entry = Entry.objects.get(name='new_entry', schema=entity) self.assertEqual(new_entry.attrs.first().get_latest_value().value, 'fuga') # make and send a job to delete entry job = Job.new_delete(user, entry) resp = self.client.post('/api/v1/job/run/%d' % job.id) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content, b'"Success to run command"') self.assertFalse(Entry.objects.get(id=entry.id).is_active)