Beispiel #1
0
def view_audio_file(request, pk, _):
    """Using the ID, return the oral argument page.

    We also test if the item is a favorite and send data as such.
    """
    af = get_object_or_404(Audio, pk=pk)
    title = "Oral Argument for " + trunc(af.case_name, 100)
    get_string = search_utils.make_get_string(request)

    try:
        fave = Favorite.objects.get(audio_id=af.pk, users__user=request.user)
        favorite_form = FavoriteForm(instance=fave)
    except (ObjectDoesNotExist, TypeError):
        # Not favorited or anonymous user
        favorite_form = FavoriteForm(initial={
            'audio_id': af.pk,
            'name': af.docket.case_name
        })

    return render_to_response(
        'audio/oral_argument.html', {
            'title': title,
            'af': af,
            'favorite_form': favorite_form,
            'get_string': get_string,
            'private': af.blocked,
        }, RequestContext(request))
Beispiel #2
0
def view_audio_file(request, pk, _):
    """Using the ID, return the oral argument page.

    We also test if the item is a favorite and send data as such.
    """
    af = get_object_or_404(Audio, pk=pk)
    title = "Oral Argument for " + trunc(af.case_name, 100)
    get_string = search_utils.make_get_string(request)

    try:
        fave = Favorite.objects.get(audio_id=af.pk, users__user=request.user)
        favorite_form = FavoriteForm(instance=fave)
    except (ObjectDoesNotExist, TypeError):
        # Not favorited or anonymous user
        favorite_form = FavoriteForm(
            initial={
                'audio_id': af.pk,
                'name': trunc(af.docket.case_name, 100, ellipsis='...'),
            }
        )

    return render_to_response(
        'audio/oral_argument.html',
        {'title': title,
         'af': af,
         'favorite_form': favorite_form,
         'get_string': get_string,
         'private': af.blocked,
         },
        RequestContext(request)
    )
Beispiel #3
0
def view_opinion(request, pk, _):
    """Using the ID, return the document.

    We also test if the document ID is a favorite for the user, and send data
    as such. If it's a favorite, we send the bound form for the favorite so
    it can populate the form on the page. If it is not a favorite, we send the
    unbound form.
    """
    # Look up the court, document, title and favorite information
    doc = get_object_or_404(Document, pk=pk)
    citation_string = make_citation_string(doc)
    title = '%s, %s' % (trunc(doc.citation.case_name, 100), citation_string)
    get_string = search_utils.make_get_string(request)

    try:
        fave = Favorite.objects.get(doc_id=doc.pk, users__user=request.user)
        favorite_form = FavoriteForm(instance=fave)
    except (ObjectDoesNotExist, TypeError):
        # Not favorited or anonymous user
        favorite_form = FavoriteForm(
            initial={
                'doc_id': doc.pk,
                'name': trunc(doc.citation.case_name, 100, ellipsis='...'),
            }
        )

    # get most influential opinions that cite this opinion
    cited_by_trunc = doc.citation.citing_opinions.select_related(
        'citation').order_by('-citation_count', '-date_filed')[:5]

    authorities_trunc = doc.cases_cited.all().select_related(
        'document').order_by('case_name')[:5]
    authorities_count = doc.cases_cited.all().count()

    return render_to_response(
        'casepage/view_opinion.html',
        {'title': title,
         'citation_string': citation_string,
         'doc': doc,
         'favorite_form': favorite_form,
         'get_string': get_string,
         'private': doc.blocked,
         'cited_by_trunc': cited_by_trunc,
         'authorities_trunc': authorities_trunc,
         'authorities_count': authorities_count},
        RequestContext(request)
    )
Beispiel #4
0
def view_opinion(request, pk, _):
    """Using the ID, return the document.

    We also test if the document ID is a favorite for the user, and send data
    as such. If it's a favorite, we send the bound form for the favorite so
    it can populate the form on the page. If it is not a favorite, we send the
    unbound form.
    """
    # Look up the court, document, title and favorite information
    doc = get_object_or_404(Document, pk=pk)
    citation_string = make_citation_string(doc)
    title = "%s, %s" % (trunc(doc.citation.case_name, 100), citation_string)
    get_string = search_utils.make_get_string(request)

    try:
        fave = Favorite.objects.get(doc_id=doc.pk, users__user=request.user)
        favorite_form = FavoriteForm(instance=fave)
    except (ObjectDoesNotExist, TypeError):
        # Not favorited or anonymous user
        favorite_form = FavoriteForm(
            initial={"doc_id": doc.pk, "name": trunc(doc.citation.case_name, 100, ellipsis="...")}
        )

    # get most influential opinions that cite this opinion
    cited_by_trunc = doc.citation.citing_opinions.select_related("citation").order_by("-citation_count", "-date_filed")[
        :5
    ]

    authorities_trunc = doc.cases_cited.all().select_related("document").order_by("case_name")[:5]
    authorities_count = doc.cases_cited.all().count()

    return render_to_response(
        "casepage/view_opinion.html",
        {
            "title": title,
            "citation_string": citation_string,
            "doc": doc,
            "favorite_form": favorite_form,
            "get_string": get_string,
            "private": doc.blocked,
            "cited_by_trunc": cited_by_trunc,
            "authorities_trunc": authorities_trunc,
            "authorities_count": authorities_count,
        },
        RequestContext(request),
    )
Beispiel #5
0
def show_results(request):
    """
    This view can vary significantly, depending on how it is called:
     - In its most simple form, it is called via GET and without any
       parameters.
        --> This loads the homepage.
     - It might also be called with GET *with* parameters.
        --> This loads search results.
     - It might be called with a POST.
        --> This attempts to save an alert.

    It also has a few failure modes it needs to support:
     - It must react properly to an invalid alert form.
     - It must react properly to an invalid or failing search form.

    All of these paths have tests.
    """
    # Create a search string that does not contain the page numbers
    get_string = search_utils.make_get_string(request)
    get_string_sans_alert = search_utils.make_get_string(request, ['page', 'edit_alert'])
    render_dict = {
        'private': True,
        'get_string': get_string,
        'get_string_sans_alert': get_string_sans_alert,
    }

    if request.method == 'POST':
        # The user is trying to save an alert.
        alert_form = CreateAlertForm(request.POST, user=request.user)
        if alert_form.is_valid():
            cd = alert_form.cleaned_data

            # save the alert
            if request.POST.get('edit_alert'):
                # check if the user can edit this, or if they are url hacking
                alert = get_object_or_404(
                    Alert,
                    pk=request.POST.get('edit_alert'),
                    userprofile=request.user.profile
                )
                alert_form = CreateAlertForm(cd, instance=alert,
                                             user=request.user)
                alert_form.save()
                action = "edited"
            else:
                alert_form = CreateAlertForm(cd, user=request.user)
                alert = alert_form.save()

                # associate the user with the alert
                up = request.user.profile
                up.alert.add(alert)
                action = "created"
            messages.add_message(request, messages.SUCCESS,
                                 'Your alert was %s successfully.' % action)

            # and redirect to the alerts page
            return HttpResponseRedirect('/profile/alerts/')
        else:
            # Invalid form. Do the search again and show them the alert form
            # with the errors
            render_dict.update(do_search(request))
            render_dict.update({'alert_form': alert_form})
            return render_to_response(
                'search/search.html',
                render_dict,
                RequestContext(request),
            )

    else:
        # Either a search or the homepage
        if len(request.GET) == 0:
            # No parameters --> Homepage.
            if not is_bot(request):
                tally_stat('search.homepage_loaded')

            # Load the render_dict with good results that can be shown in the
            # "Latest Cases" section
            render_dict.update(do_search(request, rows=5,
                                         order_by='dateFiled desc'))
            # Get the results from the oral arguments as well
            oa_dict = do_search(request, rows=5, order_by='dateArgued desc',
                                type='oa')
            render_dict.update({'results_oa': oa_dict['results']})
            # But give it a fresh form for the advanced search section
            render_dict.update({'search_form': SearchForm(request.GET)})
            ten_days_ago = make_aware(datetime.today() - timedelta(days=10),
                                      utc)
            alerts_in_last_ten = Stat.objects\
                .filter(
                    name__contains='alerts.sent',
                    date_logged__gte=ten_days_ago)\
                .aggregate(Sum('count'))['count__sum']
            queries_in_last_ten = Stat.objects\
                .filter(
                    name='search.results',
                    date_logged__gte=ten_days_ago) \
                .aggregate(Sum('count'))['count__sum']
            bulk_in_last_ten = Stat.objects\
                .filter(
                    name__contains='bulk_data',
                    date_logged__gte=ten_days_ago)\
                .aggregate(Sum('count'))['count__sum']
            api_in_last_ten = Stat.objects \
                .filter(
                    name__contains='api',
                    date_logged__gte=ten_days_ago) \
                .aggregate(Sum('count'))['count__sum']
            users_in_last_ten = User.objects\
                .filter(date_joined__gte=ten_days_ago).count()
            opinions_in_last_ten = Document.objects\
                .filter(time_retrieved__gte=ten_days_ago).count()
            oral_arguments_in_last_ten = Audio.objects\
                .filter(time_retrieved__gte=ten_days_ago).count()
            days_of_oa = naturalduration(
                Audio.objects.aggregate(Sum('duration'))['duration__sum'],
                as_dict=True,
            )['d']
            render_dict.update({
                'alerts_in_last_ten': alerts_in_last_ten,
                'queries_in_last_ten': queries_in_last_ten,
                'opinions_in_last_ten': opinions_in_last_ten,
                'oral_arguments_in_last_ten': oral_arguments_in_last_ten,
                'bulk_in_last_ten': bulk_in_last_ten,
                'api_in_last_ten': api_in_last_ten,
                'users_in_last_ten': users_in_last_ten,
                'days_of_oa': days_of_oa,
                'private': False
            })
            return render_to_response(
                'homepage.html',
                render_dict,
                RequestContext(request)
            )

        else:
            # User placed a search or is trying to edit an alert
            if request.GET.get('edit_alert'):
                # They're editing an alert
                alert = get_object_or_404(
                    Alert,
                    pk=request.GET.get('edit_alert'),
                    userprofile=request.user.profile
                )
                alert_form = CreateAlertForm(
                    instance=alert,
                    initial={'query': get_string_sans_alert},
                    user=request.user,
                )
            else:
                # Just a regular search
                if not is_bot(request):
                    tally_stat('search.results')

                # Create bare-bones alert form.
                alert_form = CreateAlertForm(
                    initial={'query': get_string,
                             'rate': "dly"},
                    user=request.user
                )
            render_dict.update(do_search(request))
            render_dict.update({'alert_form': alert_form})
            return render_to_response(
                'search/search.html',
                render_dict,
                RequestContext(request),
            )
Beispiel #6
0
def show_results(request):
    """
    This view can vary significantly, depending on how it is called:
     - In its most simple form, it is called via GET and without any
       parameters.
        --> This loads the homepage.
     - It might also be called with GET *with* parameters.
        --> This loads search results.
     - It might be called with a POST.
        --> This attempts to save an alert.

    It also has a few failure modes it needs to support:
     - It must react properly to an invalid alert form.
     - It must react properly to an invalid or failing search form.

    All of these paths have tests.
    """
    # Create a search string that does not contain the page numbers
    get_string = search_utils.make_get_string(request)
    get_string_sans_alert = search_utils.make_get_string(
        request, ['page', 'edit_alert'])
    render_dict = {
        'private': True,
        'get_string': get_string,
        'get_string_sans_alert': get_string_sans_alert,
    }

    if request.method == 'POST':
        # The user is trying to save an alert.
        alert_form = CreateAlertForm(request.POST, user=request.user)
        if alert_form.is_valid():
            cd = alert_form.cleaned_data

            # save the alert
            if request.POST.get('edit_alert'):
                # check if the user can edit this, or if they are url hacking
                alert = get_object_or_404(Alert,
                                          pk=request.POST.get('edit_alert'),
                                          userprofile=request.user.profile)
                alert_form = CreateAlertForm(cd,
                                             instance=alert,
                                             user=request.user)
                alert_form.save()
                action = "edited"
            else:
                alert_form = CreateAlertForm(cd, user=request.user)
                alert = alert_form.save()

                # associate the user with the alert
                up = request.user.profile
                up.alert.add(alert)
                action = "created"
            messages.add_message(request, messages.SUCCESS,
                                 'Your alert was %s successfully.' % action)

            # and redirect to the alerts page
            return HttpResponseRedirect('/profile/alerts/')
        else:
            # Invalid form. Do the search again and show them the alert form
            # with the errors
            render_dict.update(do_search(request))
            render_dict.update({'alert_form': alert_form})
            return render_to_response(
                'search/search.html',
                render_dict,
                RequestContext(request),
            )

    else:
        # Either a search or the homepage
        if len(request.GET) == 0:
            # No parameters --> Homepage.
            if not is_bot(request):
                tally_stat('search.homepage_loaded')

            # Load the render_dict with good results that can be shown in the
            # "Latest Cases" section
            render_dict.update(
                do_search(request, rows=5, order_by='dateFiled desc'))
            # Get the results from the oral arguments as well
            oa_dict = do_search(request,
                                rows=5,
                                order_by='dateArgued desc',
                                type='oa')
            render_dict.update({'results_oa': oa_dict['results']})
            # But give it a fresh form for the advanced search section
            render_dict.update({'search_form': SearchForm(request.GET)})
            ten_days_ago = make_aware(datetime.today() - timedelta(days=10),
                                      utc)
            alerts_in_last_ten = Stat.objects\
                .filter(
                    name__contains='alerts.sent',
                    date_logged__gte=ten_days_ago)\
                .aggregate(Sum('count'))['count__sum']
            queries_in_last_ten = Stat.objects\
                .filter(
                    name='search.results',
                    date_logged__gte=ten_days_ago) \
                .aggregate(Sum('count'))['count__sum']
            bulk_in_last_ten = Stat.objects\
                .filter(
                    name__contains='bulk_data',
                    date_logged__gte=ten_days_ago)\
                .aggregate(Sum('count'))['count__sum']
            api_in_last_ten = Stat.objects \
                .filter(
                    name__contains='api',
                    date_logged__gte=ten_days_ago) \
                .aggregate(Sum('count'))['count__sum']
            users_in_last_ten = User.objects\
                .filter(date_joined__gte=ten_days_ago).count()
            opinions_in_last_ten = Document.objects\
                .filter(time_retrieved__gte=ten_days_ago).count()
            oral_arguments_in_last_ten = Audio.objects\
                .filter(time_retrieved__gte=ten_days_ago).count()
            days_of_oa = naturalduration(
                Audio.objects.aggregate(Sum('duration'))['duration__sum'],
                as_dict=True,
            )['d']
            render_dict.update({
                'alerts_in_last_ten': alerts_in_last_ten,
                'queries_in_last_ten': queries_in_last_ten,
                'opinions_in_last_ten': opinions_in_last_ten,
                'oral_arguments_in_last_ten': oral_arguments_in_last_ten,
                'bulk_in_last_ten': bulk_in_last_ten,
                'api_in_last_ten': api_in_last_ten,
                'users_in_last_ten': users_in_last_ten,
                'days_of_oa': days_of_oa,
                'private': False
            })
            return render_to_response('homepage.html', render_dict,
                                      RequestContext(request))

        else:
            # User placed a search or is trying to edit an alert
            if request.GET.get('edit_alert'):
                # They're editing an alert
                alert = get_object_or_404(Alert,
                                          pk=request.GET.get('edit_alert'),
                                          userprofile=request.user.profile)
                alert_form = CreateAlertForm(
                    instance=alert,
                    initial={'query': get_string_sans_alert},
                    user=request.user,
                )
            else:
                # Just a regular search
                if not is_bot(request):
                    tally_stat('search.results')

                # Create bare-bones alert form.
                alert_form = CreateAlertForm(initial={
                    'query': get_string,
                    'rate': "dly"
                },
                                             user=request.user)
            render_dict.update(do_search(request))
            render_dict.update({'alert_form': alert_form})
            return render_to_response(
                'search/search.html',
                render_dict,
                RequestContext(request),
            )
Beispiel #7
0
 def link(self, obj):
     return '/feed/search/?' + search_utils.make_get_string(obj)