示例#1
0
    def test_hidden_jobs_is_not_shown(self):
        user = self.guest_login()

        entity = Entity.objects.create(name='entity', created_user=user)
        entry = Entry.objects.create(name='entry', schema=entity, created_user=user)

        # create a hidden job
        Job.new_register_referrals(user, entry)

        # create an unhidden job
        Job.new_create(user, entry)

        # access job list page and check only unhidden jobs are returned
        resp = self.client.get('/job/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.context['jobs']), 1)
        self.assertEqual(resp.context['jobs'][0]['operation'], JobOperation.CREATE_ENTRY.value)
示例#2
0
    def test_hidden_jobs_is_not_shown(self):
        user = self.guest_login()

        entity = Entity.objects.create(name="entity", created_user=user)
        entry = Entry.objects.create(name="entry", schema=entity, created_user=user)

        # create a hidden job
        Job.new_register_referrals(user, entry)

        # create an unhidden job
        Job.new_create(user, entry)

        resp = self.client.get("/api/v1/job/")
        self.assertEqual(resp.status_code, 200)

        # check API result doesn't contain hidden job
        resp_data = resp.json()
        self.assertEqual(len(resp_data["result"]), 1)
        self.assertEqual(resp_data["result"][0]["operation"], JobOperation.CREATE_ENTRY.value)
示例#3
0
def do_edit(request, entry_id, recv_data):
    entry, error = get_obj_with_check_perm(request.user, Entry, entry_id,
                                           ACLType.Writable)
    if error:
        return error

    # checks that a same name entry corresponding to the entity is existed.
    query = Q(schema=entry.schema,
              name=recv_data["entry_name"]) & ~Q(id=entry.id)
    if Entry.objects.filter(query).exists():
        return HttpResponse("Duplicate name entry is existed", status=400)

    # validate contexts of each attributes
    err = _validate_input(recv_data, entry)
    if err:
        return err

    if entry.get_status(Entry.STATUS_CREATING):
        return HttpResponse("Target entry is now under processing", status=400)

    if custom_view.is_custom("do_edit_entry", entry.schema.name):
        # resp is HttpReponse instance or its subclass (e.g. JsonResponse)
        resp = custom_view.call_custom("do_edit_entry", entry.schema.name,
                                       request, recv_data, request.user, entry)
        if resp:
            return resp

    # update name of Entry object. If name would be updated, the elasticsearch data of entries that
    # refers this entry also be updated by creating REGISTERED_REFERRALS task.
    job_register_referrals = None
    if entry.name != recv_data["entry_name"]:
        job_register_referrals = Job.new_register_referrals(
            request.user, entry)

    entry.name = recv_data["entry_name"]
    entry.save(update_fields=["name"])

    # set flags that indicates target entry is under processing
    entry.set_status(Entry.STATUS_EDITING)

    # Create new jobs to edit entry and notify it to registered webhook endpoint if it's necessary
    job_edit_entry = Job.new_edit(request.user, entry, params=recv_data)
    job_edit_entry.run()

    # running job of re-register referrals because of chaning entry's name
    if job_register_referrals:
        job_register_referrals.dependent_job = job_edit_entry
        job_register_referrals.run()

    return JsonResponse({
        "entry_id": entry.id,
        "entry_name": entry.name,
    })
示例#4
0
文件: views.py 项目: syucream/airone
def do_edit(request, entry_id, recv_data):
    user = User.objects.get(id=request.user.id)
    entry = Entry.objects.get(id=entry_id)
    tasks = []

    # checks that a same name entry corresponding to the entity is existed.
    query = Q(schema=entry.schema, name=recv_data['entry_name']) & ~Q(id=entry.id)
    if Entry.objects.filter(query).exists():
        return HttpResponse('Duplicate name entry is existed', status=400)

    # validate contexts of each attributes
    err = _validate_input(recv_data, entry)
    if err:
        return err

    if entry.get_status(Entry.STATUS_CREATING):
        return HttpResponse('Target entry is now under processing', status=400)

    if custom_view.is_custom("do_edit_entry", entry.schema.name):
        # resp is HttpReponse instance or its subclass (e.g. JsonResponse)
        resp = custom_view.call_custom(
            "do_edit_entry", entry.schema.name, request, recv_data, user, entry
        )
        if resp:
            return resp

    # update name of Entry object. If name would be updated, the elasticsearch data of entries that
    # refers this entry also be updated by creating REGISTERED_REFERRALS task.
    if entry.name != recv_data['entry_name']:
        tasks.append(Job.new_register_referrals(user, entry))

    entry.name = recv_data['entry_name']

    # set flags that indicates target entry is under processing
    entry.set_status(Entry.STATUS_EDITING)

    entry.save()

    # Create a new job to edit entry
    tasks.append(Job.new_edit(user, entry, params=recv_data))

    # Run all tasks which are created in this request
    [t.run() for t in tasks]

    return JsonResponse({
        'entry_id': entry.id,
        'entry_name': entry.name,
    })
示例#5
0
    def update(self, entry: Entry, validated_data):
        entry.set_status(Entry.STATUS_EDITING)
        user: User = self.context["request"].user

        entity_name = entry.schema.name
        if custom_view.is_custom("before_update_entry", entity_name):
            custom_view.call_custom("before_update_entry", entity_name, user,
                                    validated_data, entry)

        attrs_data = validated_data.pop("attrs", [])

        # update name of Entry object. If name would be updated, the elasticsearch data of entries
        # that refers this entry also be updated by creating REGISTERED_REFERRALS task.
        job_register_referrals: Optional[Job] = None
        if "name" in validated_data and entry.name != validated_data["name"]:
            entry.name = validated_data["name"]
            entry.save(update_fields=["name"])
            job_register_referrals = Job.new_register_referrals(user, entry)

        for entity_attr in entry.schema.attrs.filter(is_active=True):
            attr: Attribute = entry.attrs.filter(schema=entity_attr,
                                                 is_active=True).first()
            if not attr:
                attr = entry.add_attribute_from_base(entity_attr, user)

            # skip for unpermitted attributes
            if not user.has_permission(attr, ACLType.Writable):
                continue

            # make AttributeValue object if the value is specified
            attr_data = [
                x for x in attrs_data if int(x["id"]) == entity_attr.id
            ]
            if not attr_data:
                continue

            # Check a new update value is specified, or not
            if not attr.is_updated(attr_data[0]["value"]):
                continue

            attr.add_value(user, attr_data[0]["value"])

        if custom_view.is_custom("after_update_entry", entity_name):
            custom_view.call_custom("after_update_entry", entity_name, user,
                                    attrs_data, entry)

        # update entry information to Elasticsearch
        entry.register_es()

        # clear flag to specify this entry has been completed to edit
        entry.del_status(Entry.STATUS_EDITING)

        # running job of re-register referrals because of chaning entry's name
        if job_register_referrals:
            job_register_referrals.run()

        # running job to notify changing entry event
        job_notify_event: Job = Job.new_notify_update_entry(user, entry)
        job_notify_event.run()

        return entry