def test_descubre_padres_hijos(self): """ Este caso de prueba se encarga de que los utils descubre_padres y descubre_hijos detectan perfectamente las entidades padre y entidades hijas de una determinada entidad """ ent1 = Entidad.objects.get(nombre="father") ent2 = Entidad.objects.get(nombre="son") # Set ent1 as father of ent2 ent2.padres.add(ent1) ent2.save() # Comprobar que tiene a su padre padres_ent2 = descubre_padres(ent2) self.assertIn(ent1, padres_ent2) # Comprobar que tiene a su hijo hijos_ent1 = descubre_hijos(ent1) self.assertIn(ent2, hijos_ent1)
def publish_type(request, format, names, tipo): """ Publish the information of all instances which their models are mapped with the entity given :param request: the request of the view :param format: the format to publish the data (xml, nt or ttl) :param names: is the namespace short name :param tipo: the name of the entity :return: return a file in xml, ttl or nt format, with the information """ entities = Entidad.objects.all() entities = entities.filter(nombre=tipo) entities = entities.filter(namespace__short_name=names) #Create the graph to generate the rdf graph = Graph() #Captamos el limite de instancias maximas a publicar limit = getattr(settings, 'EASYDATA_PUBLISH_LIMIT', 50) #Create dictionary for namespaces namespaces = dict() #Bind the namespaces with their names for name in NameSpace.objects.all(): if name.url != "http://www.w3.org/1999/02/22-rdf-syntax-ns#": graph.bind(name.short_name, name.url) namespaces[name.url] = Namespace(name.url) for ent in entities: #Get all the ancients of the entitie hijos = descubre_hijos(ent) hijos.add(ent) #Get all the models related with this entities mods = Modelo.objects.all() mods = mods.filter(visibilidad='V') mods = mods.filter(entidad__in=hijos) for mod in mods: #Get the model class model_class = mod.devolver_modelo() if not model_class is None: #Get all the instances model_instances = model_class._default_manager.all() #Miro si se ha sobrepasado el limite de instancias a publicar if model_instances.count() <= limit: limit = limit - model_instances.count() else: model_instances = model_instances[:limit] limit = 0 for instance in model_instances: #Generate the graph with the model instance info generate_unique(mod, instance.pk, graph, namespaces) if format == 'nt': return HttpResponse(graph.serialize(format="nt"), mimetype='text/plain') elif format == 'ttl': return HttpResponse(graph.serialize(format="turtle"), mimetype='text/turtle') else: return HttpResponse(graph.serialize(format="xml"), mimetype='application/rdf+xml')