def citation(request, citation_id):
    """
    View for individual citation record.
    """
    template = loader.get_template('isisdata/citation.html')
    citation = get_object_or_404(Citation, pk=citation_id)

    if not citation.public:
        return HttpResponseForbidden()

    # Some citations are deleted. These should be hidden from public view.
    if citation.status_of_record == 'DL':
        return Http404("No such Citation")

    authors = citation.acrelation_set.filter(type_controlled__in=['AU', 'CO', 'ED'], citation__public=True)

    subjects = citation.acrelation_set.filter(Q(type_controlled__in=['SU'], citation__public=True) & ~Q(authority__type_controlled__in=['GE', 'TI'], citation__public=True))
    persons = citation.acrelation_set.filter(type_broad_controlled__in=['PR'], citation__public=True)
    categories = citation.acrelation_set.filter(Q(type_controlled__in=['CA']), citation__public=True)

    query_time = Q(type_controlled__in=['TI'], citation__public=True) | (Q(type_controlled__in=['SU'], citation__public=True) & Q(authority__type_controlled__in=['TI'], citation__public=True))
    time_periods = citation.acrelation_set.filter(query_time)

    query_places = Q(type_controlled__in=['SU'], citation__public=True) & Q(authority__type_controlled__in=['GE'], citation__public=True)
    places = citation.acrelation_set.filter(query_places)

    related_citations_ic = CCRelation.objects.filter(subject_id=citation_id, type_controlled='IC', object__public=True)
    related_citations_inv_ic = CCRelation.objects.filter(object_id=citation_id, type_controlled='IC', subject__public=True)
    related_citations_isa = CCRelation.objects.filter(subject_id=citation_id, type_controlled='ISA', object__public=True)
    related_citations_inv_isa = CCRelation.objects.filter(object_id=citation_id, type_controlled='ISA', subject__public=True)

    query = Q(subject_id=citation_id, type_controlled='RO', object__public=True) | Q(object_id=citation_id, type_controlled='RB', subject__public=True)
    related_citations_ro = CCRelation.objects.filter(query)

    related_citations_rb = CCRelation.objects.filter(subject_id=citation_id, type_controlled='RB', object__public=True)
    related_citations_re = CCRelation.objects.filter(subject_id=citation_id, type_controlled='RE', object__public=True)
    related_citations_inv_re = CCRelation.objects.filter(object_id=citation_id, type_controlled='RE', subject__public=True)
    related_citations_as = CCRelation.objects.filter(subject_id=citation_id, type_controlled='AS', object__public=True)

    properties = citation.acrelation_set.exclude(type_controlled__in=['AU', 'ED', 'CO', 'SU', 'CA'])
    properties_map = defaultdict(list)
    for prop in properties:
        properties_map[prop.type_controlled] += [prop]

    # Location of citation in REST API
    api_view = reverse('citation-detail', args=[citation.id], request=request)

    # Provide progression through search results, if present.

    # make sure we have a session key
    if hasattr(request, 'session') and not request.session.session_key:
        request.session.save()
        request.session.modified = True

    session_id = request.session.session_key
    fromsearch = request.GET.get('fromsearch', False)
    #search_key = request.session.get('search_key', None)
    last_query = request.GET.get('last_query', None) #request.session.get('last_query', None)

    query_string = request.GET.get('query_string', None)

    if query_string:
        query_string = query_string.encode('ascii','ignore')
        search_key = base64.b64encode(last_query)
        # search_key = base64.b64encode(query_string) #request.session.get('search_key', None)
    else:
        search_key = None

    user_cache = caches['default']
    search_results = user_cache.get('search_results_citation_' + str(search_key))
    page_citation = user_cache.get(session_id + '_page_citation', None) #request.session.get('page_citation', None)

    if search_results and fromsearch and page_citation:

        search_count = search_results.count()

        prev_search_result = None
        # Only display the "previous" link if we are on page 2+.
        if page_citation > 1:
            prev_search_result = search_results[(page_citation - 1)*20 - 1]

        # If we got to the last result of the previous page we need to count
        #  down the page number.
        if prev_search_result == 'isisdata.citation.' + citation_id:
            page_citation = page_citation - 1
            user_cache.set(session_id + '_page_citation', page_citation)
        search_results_page = search_results[(page_citation - 1)*20:page_citation*20 + 2]
        try:
            search_index = search_results_page.index(citation_id) + 1   # +1 for display.
            if search_index == 21:
                user_cache.set(session_id + '_page_citation', page_citation+1)

        except (IndexError, ValueError):
            search_index = None
        try:
            search_next = search_results_page[search_index]
        except (IndexError, ValueError, __HOLE__):
            search_next = None
        try:
            search_previous = search_results_page[search_index - 2]
            if search_index - 2 == -1:
                search_previous = prev_search_result

        except (IndexError, ValueError, AssertionError, TypeError):
            search_previous = None
        if search_index:
            search_current = search_index + (20* (page_citation - 1))
        else:
            search_current = None
    else:
        search_index = None
        search_next = None
        search_previous = None
        search_current = None
        search_count = 0

    #last_query = request.session.get('last_query', None)

    context = RequestContext(request, {
        'citation_id': citation_id,
        'citation': citation,
        'authors': authors,
        'properties_map': properties,
        'subjects': subjects,
        'persons': persons,
        'categories': categories,
        'time_periods': time_periods,
        'places': places,
        'source_instance_id': citation_id,
        'source_content_type': ContentType.objects.get(model='citation').id,
        'related_citations_ic': related_citations_ic,
        'related_citations_inv_ic': related_citations_inv_ic,
        'related_citations_rb': related_citations_rb,
        'related_citations_isa': related_citations_isa,
        'related_citations_inv_isa': related_citations_inv_isa,
        'related_citations_ro': related_citations_ro,
        'related_citations_re': related_citations_re,
        'related_citations_inv_re': related_citations_inv_re,
        'related_citations_as': related_citations_as,
        'api_view': api_view,
        'search_results': search_results,
        'search_index': search_index,
        'search_next': search_next,
        'search_previous': search_previous,
        'search_current': search_current,
        'search_count': search_count,
        'fromsearch': fromsearch,
        'last_query': last_query,
        'query_string': query_string,
    })
    return HttpResponse(template.render(context))
dataset/ETHPy150Open upconsulting/IsisCB/isiscb/isisdata/views.py/citation