Exemplo n.º 1
0
def copy_entry(self, job_id):
    job = Job.objects.get(id=job_id)

    if job.proceed_if_ready():
        # update job status
        job.update(Job.STATUS["PROCESSING"])

        user = User.objects.get(id=job.user.id)
        src_entry = Entry.objects.get(id=job.target.id)

        params = json.loads(job.params)
        total_count = len(params["new_name_list"])
        for (index, new_name) in enumerate(params["new_name_list"]):
            # abort processing when job is canceled
            if job.is_canceled():
                job.text = "Copy completed [%5d/%5d]" % (index, total_count)
                job.save(update_fields=["text"])
                return

            job.text = "Now copying... (progress: [%5d/%5d])" % (index + 1,
                                                                 total_count)
            job.save(update_fields=["text"])

            params["new_name"] = new_name
            job_do_copy_entry = Job.new_do_copy(user, src_entry, new_name,
                                                params)
            job_do_copy_entry.run(will_delay=False)

        # update job status and save it
        job.update(
            status=Job.STATUS["DONE"],
            text="Copy completed [%5d/%5d]" % (total_count, total_count),
        )
Exemplo n.º 2
0
    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_do_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)