def entity_details(request, entity_id): entity = _get_entity_by_id(request, entity_id) template = loader.get_template('entity_details.html') similar_entities = entities.suggest_similar(entity) similar_entities = authorization.apply_filter(request.user, 'is_owner', similar_entities) relations_from = metadata.filter_relations(qs=entity.relations_from.all(), user=request.user) relations_from = [(g[0].predicate, g) for g in metadata.group_relations(relations_from)] relations_to = metadata.filter_relations(qs=entity.relations_to.all(), user=request.user) relations_to = [(g[0].predicate, g) for g in metadata.group_relations(relations_to)] context = RequestContext( request, { 'user_can_edit': request.user.is_staff, # TODO: change this! 'entity': entity, 'similar_entities': similar_entities, 'entity_type': ContentType.objects.get_for_model(ConceptEntity), 'relations_from': relations_from, 'relations_to': relations_to, }) return HttpResponse(template.render(context))
def test_filter_relations_with_source_name_and_target_name(self): Resource.objects.get_or_create(name='A Book') Resource.objects.get_or_create(name='A Value') qs = _get_mock_qs() result = metadata.filter_relations(source='A Book', target='A Value', qs=qs) self.assertEqual(qs.filter.call_count, 2)
def list_metadata(request): """ Users should be able to search/filter for metadata entries by subject, predicate, and/or object. """ source = request.GET.get('source', None) predicate = request.GET.get('predicate', None) target = request.GET.get('target', None) offset = int(request.GET.get('offset', 0)) size = int(request.GET.get('size', 20)) qs = metadata.filter_relations(source=source if source else None, predicate=predicate if predicate else None, target=target if target else None, user=request.user) max_results = qs.count() current_path = request.get_full_path().split('?')[0] params = request.GET.copy() if 'offset' in params: del params['offset'] base_path = current_path + '?' + params.urlencode() previous_offset = offset - size if offset - size >= 0 else -1 next_offset = offset + size if offset + size < max_results else None qs[offset:offset + size] context = RequestContext( request, { 'relations': qs[offset:offset + size], 'source': source, 'predicate': predicate, 'target': target, 'offset': offset, 'first_result': offset + 1, 'last_result': min(offset + size, max_results), 'next_url': base_path + '&offset=%i' % next_offset if next_offset else None, 'previous_url': base_path + '&offset=%i' % previous_offset if previous_offset >= 0 else None, 'size': size, 'max_results': max_results, }) template = loader.get_template('list_metadata.html') return HttpResponse(template.render(context))
def test_filter_by_predicate_id(self): """ When called with ``predicate=<int>``, :func:`metadata.filter_relations` should attempt to filter by pk. """ qs = _get_mock_qs() predicate_id = 1 relations = metadata.filter_relations(predicate=predicate_id, qs=qs) qs.filter.assert_called_once_with(predicate=predicate_id) self.assertEqual(qs.filter.call_count, 1) self.assertIsInstance(relations, MockQuerySet)
def test_filter_relations_with_source_object_and_target_name(self): qs = _get_mock_qs() mock_resource, _ = Resource.objects.get_or_create(name='A Book') mock_id = mock_resource.id Resource.objects.get_or_create(name='A Value') class MockObject(object): def __init__(self): self.id = mock_id mocksource = MockObject() result = metadata.filter_relations(source=mocksource, target='A Value', qs=qs) self.assertEqual(qs.filter.call_count, 2)
def test_filter_by_predicate_instance(self): """ When called with an object that has an ``id`` attribute, :func:`metadata.filter_relations` should attempt to filter by pk with the value of that attribute. """ mock_id = 'what' class MockObject(object): def __init__(self): self.id = mock_id qs = _get_mock_qs() mockPredicate = MockObject() relations = metadata.filter_relations(predicate=mockPredicate, qs=qs) qs.filter.assert_called_once_with(predicate=mock_id) self.assertEqual(qs.filter.call_count, 1) self.assertIsInstance(relations, MockQuerySet)