Beispiel #1
0
def overview(request):

    collections = filter_by_access(request.user,
                                   Collection.objects.exclude(hidden=True))
    collections = apply_collection_visibility_preferences(
        request.user, collections)
    collections = collections.annotate(num_records=Count('records'))
    children = dict()
    overview_thumbs = dict()
    for coll in collections:
        c = filter_by_access(request.user, coll.children.exclude(hidden=True))
        c = apply_collection_visibility_preferences(request.user, c)
        children[coll.id] = c

    for record in Record.objects.filter(id__in=FieldValue.objects.filter(
            field=get_system_field(),
            value='overview',
            index_value='overview',
    ).values('record'), ):
        for coll in record.collection_set.all().values_list('id', flat=True):
            overview_thumbs[coll] = record

    return render_to_response('overview.html', {
        'collections': [(coll, children[coll.id], overview_thumbs.get(coll.id))
                        for coll in collections]
    },
                              context_instance=RequestContext(request))
Beispiel #2
0
def overview(request):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)
    collections = collections.order_by("title").annotate(num_records=Count("records"))

    return render_to_response("overview.html", {"collections": collections}, context_instance=RequestContext(request))
Beispiel #3
0
def overview(request):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)
    collections = collections.order_by('title').annotate(num_records=Count('records'))

    return render_to_response('overview.html',
                              {'collections': collections,},
                              context_instance=RequestContext(request))
Beispiel #4
0
def overview(request):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)
    collections = collections.order_by('title').annotate(num_records=Count('records'))

    return render_to_response('overview.html',
                              {'collections': collections,},
                              context_instance=RequestContext(request))
Beispiel #5
0
def browse(request, id=None, name=None):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)
    collections = collections.annotate(num_records=Count('records')).filter(num_records__gt=0).order_by('title')

    if not collections:
        return render_to_response('browse.html',
                              {},
                              context_instance=RequestContext(request))

    if request.GET.has_key('c'):
        collection = get_object_or_404(collections, name=request.GET['c'])
        return HttpResponseRedirect(reverse('solr-browse-collection',
                                            kwargs={'id': collection.id, 'name': collection.name}))

    collection = id and get_object_or_404(collections, id=id) or collections[0]
    
    fields = cache.get('browse_fields_%s' % collection.id)
    if fields:
        fields = list(Field.objects.filter(id__in=fields))
    else:
        fields = list(Field.objects.filter(fieldvalue__record__collection=collection).distinct())
        cache.set('browse_fields_%s' % collection.id, [f.id for f in fields], 60)

    if not fields:
        raise Http404()

    if request.GET.has_key('f'):
        try:
            field = get_object_or_404(Field, id=request.GET['f'], id__in=(f.id for f in fields))
        except ValueError:
            # GET['f'] was text previously and external links exist that are no longer valid
            return HttpResponseRedirect(reverse('solr-browse-collection',
                                        kwargs={'id': collection.id, 'name': collection.name}))
    else:
        field = fields[0]

    values = FieldValue.objects.filter(field=field, record__collection=collection).values('value').annotate(freq=Count('value', distinct=False)).order_by('value')
    #print "solr.views 741 collection %s field %s manager %s" %(collection, field.__class__.__name__, FieldValue.objects)

    if request.GET.has_key('s'):
        start = values.filter(value__lt=request.GET['s']).count() / 50 + 1
        return HttpResponseRedirect(reverse('solr-browse-collection',
                                            kwargs={'id': collection.id, 'name': collection.name}) +
                                    "?f=%s&page=%s" % (field.id, start))

    return render_to_response('browse.html',
                              {'collections': collections,
                               'selected_collection': collection and collection or None,
                               'fields': fields,
                               'selected_field': field,
                               'values': values,},
                              context_instance=RequestContext(request))
Beispiel #6
0
def browse(request, id=None, name=None):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)
    collections = collections.annotate(num_records=Count('records')).filter(num_records__gt=0).order_by('title')

    if not collections:
        return render_to_response('browse.html',
                              {},
                              context_instance=RequestContext(request))

    if request.GET.has_key('c'):
        collection = get_object_or_404(collections, name=request.GET['c'])
        return HttpResponseRedirect(reverse('solr-browse-collection',
                                            kwargs={'id': collection.id, 'name': collection.name}))

    collection = id and get_object_or_404(collections, id=id) or collections[0]
    
    fields = cache.get('browse_fields_%s' % collection.id)
    if fields:
        fields = list(Field.objects.filter(id__in=fields))
    else:
        fields = list(Field.objects.filter(fieldvalue__record__collection=collection).distinct())
        cache.set('browse_fields_%s' % collection.id, [f.id for f in fields], 60)

    if not fields:
        raise Http404()

    if request.GET.has_key('f'):
        try:
            field = get_object_or_404(Field, id=request.GET['f'], id__in=(f.id for f in fields))
        except ValueError:
            # GET['f'] was text previously and external links exist that are no longer valid
            return HttpResponseRedirect(reverse('solr-browse-collection',
                                        kwargs={'id': collection.id, 'name': collection.name}))
    else:
        field = fields[0]

    values = FieldValue.objects.filter(field=field, record__collection=collection).values('value').annotate(freq=Count('value', distinct=False)).order_by('value')
    print "solr.views 741 collection %s field %s manager %s" %(collection, field.__class__.__name__, FieldValue.objects)

    if request.GET.has_key('s'):
        start = values.filter(value__lt=request.GET['s']).count() / 50 + 1
        return HttpResponseRedirect(reverse('solr-browse-collection',
                                            kwargs={'id': collection.id, 'name': collection.name}) +
                                    "?f=%s&page=%s" % (field.id, start))

    return render_to_response('browse.html',
                              {'collections': collections,
                               'selected_collection': collection and collection or None,
                               'fields': fields,
                               'selected_field': field,
                               'values': values,},
                              context_instance=RequestContext(request))
Beispiel #7
0
def overview(request):

    collections = filter_by_access(
        request.user, Collection.objects.exclude(hidden=True))
    collections = apply_collection_visibility_preferences(
        request.user, collections)
    collections = collections.annotate(
        num_records=Count('records'))
    children = dict()
    overview_thumbs = dict()
    for coll in collections:
        c = filter_by_access(request.user, coll.children.exclude(hidden=True))
        c = apply_collection_visibility_preferences(
            request.user, c
        )
        children[coll.id] = c

    for record in Record.objects.filter(
        id__in=FieldValue.objects.filter(
            field=get_system_field(),
            value='overview',
            index_value='overview',
        ).values('record'),
    ):
        for coll in record.collection_set.all().values_list('id', flat=True):
            overview_thumbs[coll] = record

    return render_to_response(
        'overview.html',
        {
            'collections': [
                (coll, children[coll.id], overview_thumbs.get(coll.id))
                for coll in collections
            ]
        },
        context_instance=RequestContext(request))
Beispiel #8
0
def search_form(request):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)
    if not collections:
        raise Http404()

    def _get_fields():
        return Field.objects.select_related('standard').all().order_by('standard__title', 'name')

    def _cmp(x, y):
        if x == "Other": return 1
        if y == "Other": return -1
        return cmp(x, y)

    def _field_choices():
        grouped = {}
        for f in _get_fields():
            grouped.setdefault(f.standard and f.standard.title or 'Other', []).append(f)
        return [('', 'Any')] + [(g, [(f.id, f.label) for f in grouped[g]]) for g in sorted(grouped, _cmp)]

    class SearchForm(forms.Form):
        TYPE_CHOICES = (('t', 'in'), ('T', 'not in'))
        criteria = forms.CharField(required=False)
        type = forms.ChoiceField(choices=TYPE_CHOICES, required=False, label='')
        field = forms.ChoiceField(choices=_field_choices(), required=False, label='')

    def _collection_choices():
        result = []
        for c in collections:
            title = c.title
            children = c.all_child_collections
            if children:
                title += " (including %s sub-collections)" % len(children)
            result.append((c.id, title))
        return result

    class CollectionForm(forms.Form):
        collections = forms.MultipleChoiceField(choices=_collection_choices(),
                                                widget=forms.CheckboxSelectMultiple,
                                                required=False)

    SearchFormFormSet = formset_factory(form=SearchForm, extra=5)

    if request.method == "POST":
        collectionform = CollectionForm(request.POST, prefix='coll')
        formset = SearchFormFormSet(request.POST, prefix='crit')
        if formset.is_valid() and collectionform.is_valid():
            core_fields = dict((f, f.get_equivalent_fields()) for f in Field.objects.filter(standard__prefix='dc'))
            query = []
            keywords = []
            for form in formset.forms:
                field = form.cleaned_data['field']
                type = form.cleaned_data['type']
                criteria = form.cleaned_data['criteria']
                if criteria:
                    if field:
                        field = Field.objects.get(id=field)
                        for cf, cfe in core_fields.iteritems():
                            if field == cf or field in cfe:
                                field = cf
                                break
                        query.append('c=%s%s_%s:"%s"' % (type.isupper() and '-' or '', field.name, type.lower(), urlquote(criteria)))
                    else:
                        keywords.append('%s"%s"' % (type.isupper() and '-' or '', urlquote(criteria)))
            collections = collectionform.cleaned_data['collections']
            if collections:
                query.append('c=allcollections:%s' % '|'.join(collections))
            if query or keywords:
                qs = 'kw=%s&' % '+'.join(keywords) + '&'.join(query)
                return HttpResponseRedirect(reverse('solr-search') + '?' + qs)
    else:
        collectionform = CollectionForm(prefix='coll')
        formset = SearchFormFormSet(prefix='crit')

    return render_to_response('search.html',
                              {'collectionform': collectionform,
                               'formset': formset,
                               },
                              context_instance=RequestContext(request))
Beispiel #9
0
def search_form(request):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)

    def _get_fields():
        return Field.objects.select_related("standard").all().order_by("standard__title", "name")

    def _cmp(x, y):
        if x == "Other":
            return 1
        if y == "Other":
            return -1
        return cmp(x, y)

    def _field_choices():
        grouped = {}
        for f in _get_fields():
            grouped.setdefault(f.standard.title if f.standard else "Other", []).append(f)
        return [("", "Any")] + [(g, [(f.id, f.label) for f in grouped[g]]) for g in sorted(grouped, _cmp)]

    class SearchForm(forms.Form):
        TYPE_CHOICES = (("t", "in"), ("T", "not in"))
        criteria = forms.CharField(required=False)
        type = forms.ChoiceField(choices=TYPE_CHOICES, required=False, label="")
        field = forms.ChoiceField(choices=_field_choices(), required=False, label="")

    def _collection_choices():
        result = []
        for c in collections:
            title = c.title
            children = c.all_child_collections
            if children:
                title += " (including %s sub-collections)" % len(children)
            result.append((c.id, title))
        return result

    class CollectionForm(forms.Form):
        collections = forms.MultipleChoiceField(
            choices=_collection_choices(), widget=forms.CheckboxSelectMultiple, required=False
        )

    SearchFormFormSet = formset_factory(form=SearchForm, extra=5)

    if request.method == "POST":
        collectionform = CollectionForm(request.POST, prefix="coll")
        formset = SearchFormFormSet(request.POST, prefix="crit")
        if formset.is_valid() and collectionform.is_valid():
            core_fields = dict((f, f.get_equivalent_fields()) for f in Field.objects.filter(standard__prefix="dc"))
            query = []
            keywords = []
            for form in formset.forms:
                field = form.cleaned_data["field"]
                type = form.cleaned_data["type"]
                criteria = form.cleaned_data["criteria"]
                if criteria:
                    if field:
                        field = Field.objects.get(id=field)
                        for cf, cfe in core_fields.iteritems():
                            if field == cf or field in cfe:
                                field = cf
                                break
                        query.append(
                            'c=%s%s_%s:"%s"'
                            % (type.isupper() and "-" or "", field.name, type.lower(), urlquote(criteria))
                        )
                    else:
                        keywords.append('%s"%s"' % (type.isupper() and "-" or "", urlquote(criteria)))
            collections = collectionform.cleaned_data["collections"]
            if collections:
                query.append("c=allcollections:%s" % "|".join(collections))
            if query or keywords:
                qs = "kw=%s&" % "+".join(keywords) + "&".join(query)
                return HttpResponseRedirect(reverse("solr-search") + "?" + qs)
    else:
        collectionform = CollectionForm(prefix="coll")
        formset = SearchFormFormSet(prefix="crit")

    return render_to_response(
        "search.html",
        {"collectionform": collectionform, "formset": formset, "collections": collections},
        context_instance=RequestContext(request),
    )
Beispiel #10
0
def browse(request, id=None, name=None):
    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user, collections)
    collections = collections.annotate(num_records=Count("records")).filter(num_records__gt=0).order_by("title")

    if not collections:
        return render_to_response("browse.html", {}, context_instance=RequestContext(request))

    if "c" in request.GET:
        collection = get_object_or_404(collections, name=request.GET["c"])
        return HttpResponseRedirect(
            reverse("solr-browse-collection", kwargs={"id": collection.id, "name": collection.name})
        )

    collection = id and get_object_or_404(collections, id=id) or collections[0]

    fields = cache.get("browse_fields_%s" % collection.id)
    if fields:
        fields = list(Field.objects.filter(id__in=fields))
    else:
        ids = list(
            FieldValue.objects.filter(record__collection=collection)
            .order_by()
            .distinct()
            .values_list("field_id", flat=True)
        )
        fields = list(Field.objects.filter(id__in=ids))
        cache.set("browse_fields_%s" % collection.id, [f.id for f in fields], 60)

    if not fields:
        raise Http404()

    if "f" in request.GET:
        try:
            field = get_object_or_404(Field, id=request.GET["f"], id__in=(f.id for f in fields))
        except ValueError:
            # GET['f'] was text previously and external links exist
            # that are no longer valid
            return HttpResponseRedirect(
                reverse("solr-browse-collection", kwargs={"id": collection.id, "name": collection.name})
            )
    else:
        field = fields[0]

    values = (
        FieldValue.objects.filter(field=field, record__collection=collection)
        .values("value")
        .annotate(freq=Count("value", distinct=False))
        .order_by("value")
    )

    if "s" in request.GET:
        start = values.filter(value__lt=request.GET["s"]).count() / 50 + 1
        return HttpResponseRedirect(
            reverse("solr-browse-collection", kwargs={"id": collection.id, "name": collection.name})
            + "?f=%s&page=%s" % (field.id, start)
        )

    return render_to_response(
        "browse.html",
        {
            "collections": collections,
            "selected_collection": collection and collection or None,
            "fields": fields,
            "selected_field": field,
            "values": values,
        },
        context_instance=RequestContext(request),
    )
Beispiel #11
0
def search_form(request):

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(
        request.user, collections)

    def _get_fields():
        return Field.objects.select_related('standard').all().order_by(
            'standard__title', 'name')

    def _cmp(x, y):
        if x == "Other":
            return 1
        if y == "Other":
            return -1
        return cmp(x, y)

    def _field_choices():
        grouped = {}
        for f in _get_fields():
            grouped.setdefault(
                f.standard.title if f.standard else 'Other', []).append(f)
        return [('', 'Any')] + [
            (g, [(f.id, f.label) for f in grouped[g]])
            for g in sorted(grouped, _cmp)
            ]

    class SearchForm(forms.Form):
        TYPE_CHOICES = (('t', 'in'), ('T', 'not in'))
        criteria = forms.CharField(required=False)
        type = forms.ChoiceField(
            choices=TYPE_CHOICES, required=False, label='')
        field = forms.ChoiceField(
            choices=_field_choices(), required=False, label='')

    def _collection_choices():
        result = []
        for c in collections:
            title = c.title
            children = c.all_child_collections
            if children:
                title += " (including %s sub-collections)" % len(children)
            result.append((c.id, title))
        return result

    class CollectionForm(forms.Form):
        collections = forms.MultipleChoiceField(
            choices=_collection_choices(),
            widget=forms.CheckboxSelectMultiple,
            required=False)

    SearchFormFormSet = formset_factory(form=SearchForm, extra=5)

    if request.method == "POST":
        collectionform = CollectionForm(request.POST, prefix='coll')
        formset = SearchFormFormSet(request.POST, prefix='crit')
        if formset.is_valid() and collectionform.is_valid():
            core_fields = dict(
                (f, f.get_equivalent_fields())
                for f in Field.objects.filter(standard__prefix='dc'))
            query = []
            keywords = []
            for form in formset.forms:
                field = form.cleaned_data['field']
                type = form.cleaned_data['type']
                criteria = form.cleaned_data['criteria']
                if criteria:
                    if field:
                        field = Field.objects.get(id=field)
                        for cf, cfe in core_fields.iteritems():
                            if field == cf or field in cfe:
                                field = cf
                                break
                        query.append('c=%s%s_%s:"%s"' % (
                            type.isupper() and '-' or '',
                            field.name,
                            type.lower(),
                            urlquote(criteria)))
                    else:
                        keywords.append('%s"%s"' % (
                            type.isupper() and '-' or '',
                            urlquote(criteria)))
            collections = collectionform.cleaned_data['collections']
            if collections:
                query.append('c=allcollections:%s' % '|'.join(collections))
            if query or keywords:
                qs = 'kw=%s&' % '+'.join(keywords) + '&'.join(query)
                return HttpResponseRedirect(reverse('solr-search') + '?' + qs)
    else:
        collectionform = CollectionForm(prefix='coll')
        formset = SearchFormFormSet(prefix='crit')

    return render_to_response('search.html',
                              {'collectionform': collectionform,
                               'formset': formset,
                               'collections': collections,
                               },
                              context_instance=RequestContext(request))
Beispiel #12
0
def browse(request, id=None, name=None):

    browse_children = getattr(settings, 'BROWSE_CHILDREN', False)

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user,
                                                          collections)

    if not browse_children:
        # Filter out empty collections.  When browsing child collections
        # as well, we don't do this
        collections = collections.annotate(
            num_records=Count('records')).filter(
            num_records__gt=0)

    collections = collections.order_by('order', 'title')

    if not collections:
        return render(
            request,
            'browse.html'
        )

    if 'c' in request.GET:
        collection = get_object_or_404(collections, name=request.GET['c'])
        return HttpResponseRedirect(reverse(
            'solr-browse-collection',
            kwargs={'id': collection.id, 'name': collection.name}))

    fields = None
    if id:
        collection = get_object_or_404(collections, id=id)
        if collection:
            fields = _get_browse_fields(
                collection.id, list(collection.all_child_collections))
            if not fields:
                return HttpResponseRedirect(reverse('solr-browse'))

    for ic in collections:
        if fields:
            break
        collection = ic
        fields = _get_browse_fields(collection.id)

    if not fields:
        raise Http404()

    collection_and_children = \
        [collection] + list(collection.all_child_collections)

    if 'f' in request.GET:
        try:
            field = get_object_or_404(Field, id=request.GET['f'],
                                      id__in=(f.id for f in fields))
        except ValueError:
            # GET['f'] was text previously and external links exist
            # that are no longer valid
            return HttpResponseRedirect(
                reverse('solr-browse-collection',
                        kwargs={'id': collection.id, 'name': collection.name}))
    else:
        field = fields[0]

    ivalues = FieldValue.objects.filter(
        field=field,
        record__collection__in=collection_and_children,
    ).values('browse_value').distinct().order_by('browse_value')

    if 's' in request.GET:
        start = ivalues.filter(
            browse_value__lt=request.GET['s']).count() // 50 + 1
        return HttpResponseRedirect(reverse(
            'solr-browse-collection',
            kwargs={'id': collection.id, 'name': collection.name}) +
            "?f=%s&page=%s" % (field.id, start))

    try:
        page = int(request.GET['page'])
    except:
        page = 1

    start = (page - 1) * 50
    ivalues_list = [
        row['browse_value']
        for row in ivalues[start:start + 50]
        if row['browse_value'] != ''
    ]

    ivalues_length = ivalues.count()

    values = list(FieldValue.objects.filter(
        field=field,
        record__collection__in=collection_and_children,
        browse_value__in=ivalues_list,
    ).values('value', 'browse_value').annotate(
        freq=Count('record', distinct=True)
    ).order_by('browse_value', 'value'))

    collection_filter = '|'.join(
        str(c.id) for c in collection_and_children
    )

    dummyvalues = DummyContent(ivalues_length)
    paginator = Paginator(dummyvalues, 50)
    page = request.GET.get('page')
    try:
        dummyvalues = paginator.page(page)
    except PageNotAnInteger:
        dummyvalues = paginator.page(1)
    except EmptyPage:
        dummyvalues = paginator.page(paginator.num_pages)

    return render(
        request,
        'browse.html',
        {
            'collections': collections,
            'selected_collection': collection and collection or None,
            'collection_filter': collection_filter,
            'fields': fields,
            'selected_field': field,
            'values': values,
            'ivalues': ivalues,
            'column_split': len(values) // 2,
            'dummyvalues': dummyvalues,
        }
    )
Beispiel #13
0
def browse(request, id=None, name=None):
    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user,
                                                          collections)
    collections = collections.annotate(
        num_records=Count('records')).filter(
        num_records__gt=0).order_by('title')

    if not collections:
        return render_to_response(
            'browse.html',
            {},
            context_instance=RequestContext(request))

    if 'c' in request.GET:
        collection = get_object_or_404(collections, name=request.GET['c'])
        return HttpResponseRedirect(reverse(
            'solr-browse-collection',
            kwargs={'id': collection.id, 'name': collection.name}))

    fields = None
    if id:
        collection = get_object_or_404(collections, id=id)
        if collection:
            fields = _get_browse_fields(collection.id)
            if not fields:
                return HttpResponseRedirect(reverse('solr-browse'))

    for ic in collections:
        if fields:
            break
        collection = ic
        fields = _get_browse_fields(collection.id)

    if not fields:
        raise Http404()

    if 'f' in request.GET:
        try:
            field = get_object_or_404(Field, id=request.GET['f'],
                                      id__in=(f.id for f in fields))
        except ValueError:
            # GET['f'] was text previously and external links exist
            # that are no longer valid
            return HttpResponseRedirect(
                reverse('solr-browse-collection',
                        kwargs={'id': collection.id, 'name': collection.name}))
    else:
        field = fields[0]

    values = FieldValue.objects.filter(
        field=field,
        record__collection=collection).values('value').annotate(
        freq=Count('value', distinct=False)).order_by('value')

    if 's' in request.GET:
        start = values.filter(value__lt=request.GET['s']).count() / 50 + 1
        return HttpResponseRedirect(reverse(
            'solr-browse-collection',
            kwargs={'id': collection.id, 'name': collection.name}) +
            "?f=%s&page=%s" % (field.id, start))

    return render_to_response(
        'browse.html',
        {
            'collections': collections,
            'selected_collection': collection and collection or None,
            'fields': fields,
            'selected_field': field,
            'values': values,
        },
        context_instance=RequestContext(request))
Beispiel #14
0
def browse(request, id=None, name=None):

    browse_children = getattr(settings, 'BROWSE_CHILDREN', False)

    collections = filter_by_access(request.user, Collection)
    collections = apply_collection_visibility_preferences(request.user,
                                                          collections)

    if not browse_children:
        # Filter out empty collections.  When browsing child collections
        # as well, we don't do this
        collections = collections.annotate(
            num_records=Count('records')).filter(
            num_records__gt=0)

    collections = collections.order_by('order', 'title')

    if not collections:
        return render_to_response(
            'browse.html',
            {},
            context_instance=RequestContext(request))

    if 'c' in request.GET:
        collection = get_object_or_404(collections, name=request.GET['c'])
        return HttpResponseRedirect(reverse(
            'solr-browse-collection',
            kwargs={'id': collection.id, 'name': collection.name}))

    fields = None
    if id:
        collection = get_object_or_404(collections, id=id)
        if collection:
            fields = _get_browse_fields(collection.id)
            if not fields:
                return HttpResponseRedirect(reverse('solr-browse'))

    for ic in collections:
        if fields:
            break
        collection = ic
        fields = _get_browse_fields(collection.id)

    if not fields:
        raise Http404()

    collection_and_children = \
        [collection] + list(collection.all_child_collections)

    if 'f' in request.GET:
        try:
            field = get_object_or_404(Field, id=request.GET['f'],
                                      id__in=(f.id for f in fields))
        except ValueError:
            # GET['f'] was text previously and external links exist
            # that are no longer valid
            return HttpResponseRedirect(
                reverse('solr-browse-collection',
                        kwargs={'id': collection.id, 'name': collection.name}))
    else:
        field = fields[0]

    ivalues = FieldValue.objects.filter(
        field=field,
        record__collection__in=collection_and_children,
    ).values('browse_value').distinct().order_by('browse_value')

    if 's' in request.GET:
        start = ivalues.filter(value__lt=request.GET['s']).count() / 50 + 1
        return HttpResponseRedirect(reverse(
            'solr-browse-collection',
            kwargs={'id': collection.id, 'name': collection.name}) +
            "?f=%s&page=%s" % (field.id, start))

    try:
        page = int(request.GET['page'])
    except:
        page = 1

    start = (page - 1) * 50
    ivalues_list = [
        row['browse_value']
        for row in ivalues[start:start + 50]
        if row['browse_value'] != ''
    ]

    ivalues_length = ivalues.count()

    values = list(FieldValue.objects.filter(
        field=field,
        record__collection__in=collection_and_children,
        browse_value__in=ivalues_list,
    ).values('value', 'browse_value').annotate(
        freq=Count('record', distinct=True)
    ).order_by('browse_value', 'value'))

    collection_filter = '|'.join(
        str(c.id) for c in collection_and_children
    )

    class DummyContent():
        def __init__(self, length):
            self.length = length
        def __len__(self):
            return self.length
        def __getitem__(self, index):
            if isinstance(index, slice):
                return range(index.start or 0, index.stop, index.step or 1)
            else:
                return None

    return render_to_response(
        'browse.html',
        {
            'collections': collections,
            'selected_collection': collection and collection or None,
            'collection_filter': collection_filter,
            'fields': fields,
            'selected_field': field,
            'values': values,
            'ivalues': ivalues,
            'column_split': len(values) / 2,
            'dummyvalues': DummyContent(ivalues_length),
        },
        context_instance=RequestContext(request))