Esempio n. 1
0
    def test_raises_error_when_create_copy_of_instance_uses_an_invalid_attrs_value(
        self, ):
        instance = Library.objects.create(name="First library",
                                          user=self.user1)

        with self.assertRaises(ValueError):
            create_copy_of_instance(instance, attrs="user")
Esempio n. 2
0
def copy_observable_element(element, block, elements_set=None):
    new_obs = create_copy_of_instance(element.observable_hq)

    attrs = {"block_id": block.id, "observable_hq_id": new_obs.id}

    if elements_set:
        attrs["custom_element_set_id"] = elements_set.id

    return create_copy_of_instance(element, attrs=attrs)
Esempio n. 3
0
    def test_cloning_an_invalid_object_is_invalid(self):
        class InvalidObj:
            def __init__(self):
                pass

        instance = InvalidObj()

        with self.assertRaises(ValueError):
            create_copy_of_instance(instance, attrs={"created_by": self.user2})
Esempio n. 4
0
    def test_cloning_excluded_field_without_a_fallback_value_is_invalid(self):
        name = "New Library"
        instance = Book.objects.create(name=name,
                                       created_by=self.user1,
                                       slug=slugify(name))

        with self.assertRaises(IntegrityError):
            create_copy_of_instance(instance,
                                    exclude={"slug"},
                                    attrs={"created_by": self.user2})
Esempio n. 5
0
def copy_page(obj, attrs):
    new_page = create_copy_of_instance(obj, attrs=attrs)

    for block in obj.page_blocks.all():
        copied_block = create_copy_of_instance(block,
                                               attrs={"page_id": new_page.id})

        for element in block.elements.all():
            copy_elements(element, copied_block)

    return new_page
Esempio n. 6
0
def clone_custom_element(element, block):
    new_ele = create_copy_of_instance(element, attrs={"block_id": block.id})

    for element_set in element.elements_sets.all():
        new_set = create_copy_of_instance(
            element_set, attrs={"custom_element_id": new_ele.id})

        for set_element in element_set.elements.all():
            if set_element.type == "observable_hq":
                copy_observable_element(set_element, block, new_set)
            else:
                copy_simple_element(set_element, block, elements_set=new_set)

    return new_ele
Esempio n. 7
0
def copy_simple_element(element, block, elements_set=None):
    attrs = {"block_id": block.id}

    if elements_set:
        attrs["custom_element_set_id"] = elements_set.id

    return create_copy_of_instance(element, attrs=attrs)
Esempio n. 8
0
    def test_cloning_model_with_custom_id(self):
        instance = Library.objects.create(name="First library",
                                          user=self.user1)
        clone = create_copy_of_instance(instance, attrs={"user": self.user2})

        self.assertNotEqual(instance.pk, clone.pk)
        self.assertEqual(clone.user, self.user2)
Esempio n. 9
0
    def test_cloning_model_with_a_different_db_alias_is_valid(self):
        new_user = User(username="******")
        new_user.save(using=self.REPLICA_DB_ALIAS)
        instance = Library(name="First library", user=self.user1)
        instance.save(using=DEFAULT_DB_ALIAS)
        clone = create_copy_of_instance(instance,
                                        attrs={"user": new_user},
                                        using=self.REPLICA_DB_ALIAS)

        self.assertNotEqual(instance.pk, clone.pk)
        self.assertEqual(clone.user, new_user)
        self.assertNotEqual(instance._state.db, clone._state.db)
Esempio n. 10
0
def copy_tags(tags, page_id):
    for tag in tags:
        create_copy_of_instance(tag, attrs={"page_id": page_id})
Esempio n. 11
0
    def test_cloning_unique_fk_field_without_a_fallback_value_is_invalid(self):
        name = "New Library"
        instance = Library.objects.create(name=name, user=self.user1)

        with self.assertRaises(ValidationError):
            create_copy_of_instance(instance)