def test_simple(self): related_name = "relate26" data = { "desc": "some", related_name: { "dummy1": 1, "dummy2": "value1" } } prefix = "provider" root_name = "model_test26" model_builder = DynamicModelBuilder(model_name_prefix=prefix) model_builder.create_models_from_data(data, root_name) Object = get_dynamic_model(root_name) Objrelate = get_dynamic_model(related_name) prefixed_related_name = add_prefix_to_model_label(related_name, prefix) self.assertEqual(Object.__name__, add_prefix_to_model_label(root_name, prefix)) self.assertEqual(Objrelate.__name__, prefixed_related_name) instance_builder = DynamicDataInstances(model_name_prefix=prefix) inst = instance_builder.create_instances_from_data(data, root_name) rel_inst = getattr(inst, prefixed_related_name) self.assertTrue(hasattr(inst, prefixed_related_name)) self.assertTrue(hasattr(rel_inst, "dummy1")) self.assertEqual(rel_inst.dummy1, 1) self.assertEqual(rel_inst.dummy2, "value1")
def test_create_data_from_random_json(self): data = {"obj2": {"title": "title1", "obj3": {"title": "title2"}}} root_name = "instance_test2" model_builder = DynamicModelBuilder() model_builder.create_models_from_data(data, root_name) instance_builder = DynamicDataInstances() instances = instance_builder.create_instances_from_data( data, root_name) obj2 = get_dynamic_model("obj2").objects.first() obj3 = get_dynamic_model("obj3").objects.first() assert obj3.title == "title2" assert obj2.title == "title1"
def test_instances_are_being_assigned_nested(self, mock): data = {"obj55": {"title8": "title32", "obj66": {"title7": "title44"}}} root_name = "user_test4" model_builder = DynamicModelBuilder() model_builder.create_models_from_data(data, root_name) instance_builder = DynamicDataInstances(self.user.pk) instances = instance_builder.create_instances_from_data( data, root_name) obj2 = get_dynamic_model("obj55").objects.first() obj3 = get_dynamic_model("obj66").objects.first() self.assertEqual(obj3.user_pk, self.user.pk) self.assertEqual(obj2.user_pk, self.user.pk)
def test_create_dynamic_list_of_objects(self): data = { "prop2": 1, "prop1": "test2", "related_obj": [ { "name": "name1", "value": 2 }, { "name": "name2", "value": 3 }, ] } root_name = "model_test1" model_builder = DynamicModelBuilder() ModelObject = model_builder.create_models_from_data(root_name, data) instance = ModelObject(prop2=3) instance.save() # instance_builder = DynamicDataInstances() # instances = instance_builder.create_instances_from_data(root_name, data) RelObj = get_dynamic_model("related_obj") inst1 = RelObj(name="Peter Meyer", value=3, model_test1=instance) inst1.save() inst1 = RelObj(name="Anders Rikvold", value=3, model_test1=instance) inst1.save()
def test_build_model_with_user_pk(self, mock): data = { "prop2": 1, } root_name = "user_test_1" model_builder = DynamicModelBuilder() model_builder.create_models_from_data(data, root_name) ModelObject = get_dynamic_model(root_name) inst = ModelObject.objects.create(prop2=3, user_pk=self.user.pk)
def test_build_data_from_data_fetch(self): with patch(get_method_path( DataFetch.execute_on_save_methods)) as mock_method: data, data_fetch, user = self.create_data_fetch_objects() TransformFilesToData().create_data_file(data, user, data_fetch) dynamic_model_tasks.build_models_from_data_files( pk=data_fetch.refined_data_file.pk) model = get_dynamic_model("activity") self.assertIsNotNone(model)
def create_view_instances(selected_model_name, search_query, user_pk, page): if selected_model_name == "": return [] models = [get_dynamic_model(selected_model_name) ] if selected_model_name != "ALL" else get_all_dynamic_models() view_instances = [] for model in models: instances = query_instances(model, search_query, user_pk) # TODO figure out a smart way to only create dynamicViewObjects of instances in page view_instances.extend([DynamicViewObject(inst) for inst in instances]) paged_view_instances = make_paginator(page, view_instances) return paged_view_instances
def test_instances_are_being_assigned(self, mock): data = { "obj22": { "title": "title324", } } root_name = "user_test3" model_builder = DynamicModelBuilder() model_builder.create_models_from_data(data, root_name) self.assertTrue(len(model_builder.failed_objects) is 0) instance_builder = DynamicDataInstances(self.user.pk) instances = instance_builder.create_instances_from_data( data, root_name) obj2 = get_dynamic_model("obj22").objects.first() self.assertEqual(obj2.user_pk, self.user.pk)
def test_create_dynamic_simple(self): data = {"desc": "some", "relate": {"dummy1": 1, "dummy2": "value1"}} root_name = "model_test0" model_builder = DynamicModelBuilder() Object = model_builder.create_models_from_data(root_name, data) inst = Object(desc="soemthing") inst.save() Objrelate = get_dynamic_model("relate") inst2 = Objrelate(model_test0=inst, dummy1=2, dummy2="value2") inst2.save() assert hasattr(inst, "relate") assert hasattr(inst.relate, "dummy1") assert hasattr(inst.relate, "dummy2") assert inst.relate.dummy1 == 2 assert inst.relate.dummy2 == "value2"
def test_if_list_of_objects_with_some_errors_are_caught_correctly(self): data = { "newobj3": { "related2": [{ "attribute": 0, }, { "attribute": 1, }, { "attribute": 0, }, { "attribute": 1, }] } } root_name = "model_test4" model_builder = DynamicModelBuilder() model_builder.handle_attribute = Mock( side_effect=make_attributes_with_value_0_fail) ModelObject = model_builder.create_models_from_data(root_name, data) assert len(model_builder.failed_objects) == 2 relatedOb = get_dynamic_model("related2") assert relatedOb is not None
def test_get_dynamic_model(self): obj = dynamic_model_utils.get_dynamic_model("newobj1") self.assertIsNotNone(obj)
def build_object_list_view(request, provider_name, object_label): # object_label = request.GET.get("object_label") model = get_dynamic_model(object_label) attrs = {"model": model} list_view = type(f"{model.__name__}ListView", (generic.ListView,), attrs) return list_view
def model_list_view(request, object_label): model = get_dynamic_model(object_label) fields = model._meta.fields instances = model.objects.all() return render(request, 'object_list.html', {'object_label': object_label, 'instances': instances})
def build_object_detail_view(request, provider_name, object_label): # object_label = request.GET.get("object_label") model = get_dynamic_model(object_label) attrs = {"model": model} return type(f"{model.__name__}DetailView", (generic.DetailView,), attrs) # .as_view()
def dynamic_data_detail_view(request, model_name, pk): user_pk = request.user.pk model = get_dynamic_model(model_name) instance = model.objects.get(pk=pk, user_pk=user_pk) html_params = {"instance": DynamicViewObject(instance)} return render(request, 'dynamic_data_detail.html', html_params)
def pre_handle_object(self, parent_ref: Model, object_label: str, data): DjangoModel = dm_utils.get_dynamic_model(model_name=object_label, prefix=self.model_name_prefix) instance = DjangoModel() return instance