def get_homepage_stats(): """Get any stats that are displayed on the homepage and return them as a dict """ r = make_redis_interface('STATS') ten_days_ago = make_aware(datetime.today() - timedelta(days=10), utc) last_ten_days = [ 'api:v3.d:%s.count' % (date.today() - timedelta(days=x)).isoformat() for x in range(0, 10) ] homepage_data = { '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'], 'opinions_in_last_ten': Opinion.objects.filter(date_created__gte=ten_days_ago).count(), 'oral_arguments_in_last_ten': Audio.objects.filter(date_created__gte=ten_days_ago).count(), 'api_in_last_ten': sum([ int(result) for result in r.mget(*last_ten_days) if result is not None ]), 'users_in_last_ten': User.objects.filter(date_joined__gte=ten_days_ago).count(), 'days_of_oa': naturalduration( Audio.objects.aggregate(Sum('duration'))['duration__sum'], as_dict=True, )['d'], 'viz_in_last_ten': SCOTUSMap.objects.filter( date_published__gte=ten_days_ago, published=True, ).count(), 'visualizations': SCOTUSMap.objects.filter( published=True, deleted=False, ).annotate(Count('clusters'), ).filter( # Ensures that we only show good stuff on homepage clusters__count__gt=10, ).order_by( '-date_published', '-date_modified', '-date_created', )[:1], 'private': False, # VERY IMPORTANT! } return homepage_data
def get_homepage_stats(): """Get any stats that are displayed on the homepage and return them as a dict """ r = make_redis_interface("STATS") ten_days_ago = make_aware(datetime.today() - timedelta(days=10), utc) last_ten_days = [ "api:v3.d:%s.count" % (date.today() - timedelta(days=x)).isoformat() for x in range(0, 10) ] homepage_data = { "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"], "opinions_in_last_ten": Opinion.objects.filter(date_created__gte=ten_days_ago).count(), "oral_arguments_in_last_ten": Audio.objects.filter(date_created__gte=ten_days_ago).count(), "api_in_last_ten": sum([ int(result) for result in r.mget(*last_ten_days) if result is not None ]), "users_in_last_ten": User.objects.filter(date_joined__gte=ten_days_ago).count(), "days_of_oa": naturalduration( Audio.objects.aggregate(Sum("duration"))["duration__sum"], as_dict=True, )["d"], "viz_in_last_ten": SCOTUSMap.objects.filter( date_published__gte=ten_days_ago, published=True, ).count(), "visualizations": SCOTUSMap.objects.filter( published=True, deleted=False, ).annotate(Count("clusters"), ).filter( # Ensures that we only show good stuff on homepage clusters__count__gt=10, ).order_by( "-date_published", "-date_modified", "-date_created", )[:1], "private": False, # VERY IMPORTANT! } return homepage_data
def test_input_as_int_or_str(self) -> None: """Can we take input as either an int or a str?""" test_cases = ( ("62", "1:02"), (62, "1:02"), ) for test, result in test_cases: self.assertEqual(naturalduration(test), result)
def test_conversion_to_strings(self) -> None: """Can we get the str output right?""" test_cases = ( ("01", "1"), ("61", "1:01"), ("3601", "1:00:01"), ("86401", "1:00:00:01"), ("90061", "1:01:01:01"), ) for test, result in test_cases: self.assertEqual(naturalduration(test), result)
def test_input_as_int_or_str(self): """Can we take input as either an int or a str?""" test_cases = ( ('62', '1:02'), (62, '1:02'), ) for test, result in test_cases: self.assertEqual( naturalduration(test), result, )
def test_conversion_to_strings(self): """Can we get the str output right?""" test_cases = ( ('01', '1'), ('61', '1:01'), ('3601', '1:00:01'), ('86401', '1:00:00:01'), ('90061', '1:01:01:01'), ) for test, result in test_cases: self.assertEqual( naturalduration(test), result, )
def test_weird_values(self): test_cases = ( (None, '0'), # None (0, '0'), # Zero (22.2, '22'), # Float ) for test, expected_result in test_cases: actual_result = naturalduration(test) self.assertEqual(actual_result, expected_result, msg="Error with weird value: %s.\n" " Got: %s\n" " Expected: %s" % (test, actual_result, expected_result))
def test_weird_values(self): test_cases = ( (None, '0'), # None (0, '0'), # Zero (22.2, '22'), # Float ) for test, expected_result in test_cases: actual_result = naturalduration(test) self.assertEqual( actual_result, expected_result, msg="Error with weird value: %s.\n" " Got: %s\n" " Expected: %s" % (test, actual_result, expected_result) )
def test_conversion_to_dict(self) -> None: """Can we get the numbers right when it's a dict?""" test_cases = ( ("01", {"d": 0, "h": 0, "m": 0, "s": 1}), ("61", {"d": 0, "h": 0, "m": 1, "s": 1}), ("3601", {"d": 0, "h": 1, "m": 0, "s": 1}), ("86401", {"d": 1, "h": 0, "m": 0, "s": 1}), ("90061", {"d": 1, "h": 1, "m": 1, "s": 1}), ) for test, expected_result in test_cases: actual_result = naturalduration(test, as_dict=True) self.assertEqual( actual_result, expected_result, msg="Could not convert %s to dict.\n" " Got: %s\n" " Expected: %s" % (test, actual_result, expected_result), )
def test_conversion_to_dict(self): """Can we get the numbers right when it's a dict?""" test_cases = ( ('01', {'d': 0, 'h': 0, 'm': 0, 's': 1}), ('61', {'d': 0, 'h': 0, 'm': 1, 's': 1}), ('3601', {'d': 0, 'h': 1, 'm': 0, 's': 1}), ('86401', {'d': 1, 'h': 0, 'm': 0, 's': 1}), ('90061', {'d': 1, 'h': 1, 'm': 1, 's': 1}), ) for test, expected_result in test_cases: actual_result = naturalduration(test, as_dict=True) self.assertEqual( actual_result, expected_result, msg="Could not convert %s to dict.\n" " Got: %s\n" " Expected: %s" % (test, actual_result, expected_result) )
def faq(request): """Loads the FAQ page""" template_data = { 'scraped_court_count': Court.objects.filter(in_use=True, has_opinion_scraper=True).count(), 'total_opinion_count': OpinionCluster.objects.all().count(), 'total_recap_count': RECAPDocument.objects.filter(is_available=True).count(), 'total_oa_minutes': naturalduration(Audio.objects.aggregate( Sum('duration'))['duration__sum'], as_dict=True)['m'], 'total_judge_count': Person.objects.all().count(), } return contact( request, template_path='faq.html', template_data=template_data, initial={'subject': 'FAQs'}, )
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'), user=request.user, ) 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(commit=False) alert.user = request.user alert.save() action = "created" messages.add_message(request, messages.SUCCESS, 'Your alert was %s successfully.' % action) # and redirect to the alerts page return HttpResponseRedirect(reverse("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.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'] r = redis.StrictRedis( host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DATABASES['STATS'], ) last_ten_days = [ 'api:v3.d:%s.count' % (date.today() - timedelta(days=x)).isoformat() for x in range(0, 10) ] api_in_last_ten = sum([ int(result) for result in r.mget(*last_ten_days) if result is not None ]) users_in_last_ten = User.objects.filter( date_joined__gte=ten_days_ago).count() opinions_in_last_ten = Opinion.objects.filter( date_created__gte=ten_days_ago).count() oral_arguments_in_last_ten = Audio.objects.filter( date_created__gte=ten_days_ago).count() days_of_oa = naturalduration( Audio.objects.aggregate(Sum('duration'))['duration__sum'], as_dict=True, )['d'] viz_in_last_ten = SCOTUSMap.objects.filter( date_published__gte=ten_days_ago, published=True, ).count() visualizations = SCOTUSMap.objects.filter( published=True, deleted=False, ).annotate(Count('clusters'), ).filter( # Ensures that we only show good stuff on homepage clusters__count__gt=10, ).order_by( '-date_published', '-date_modified', '-date_created', )[:1] 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, 'viz_in_last_ten': viz_in_last_ten, 'visualizations': visualizations, 'private': False, # VERY IMPORTANT! }) 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 if request.user.is_anonymous(): return HttpResponseRedirect( "{path}?next={next}{encoded_params}".format( path=reverse('sign-in'), next=request.path, encoded_params=quote("?" + request.GET.urlencode()))) else: alert = get_object_or_404(Alert, pk=request.GET.get('edit_alert'), user=request.user) 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.html', render_dict, RequestContext(request), )
def get_homepage_stats(): """Get any stats that are displayed on the homepage and return them as a dict """ 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'] r = redis.StrictRedis( host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DATABASES['STATS'], ) last_ten_days = ['api:v3.d:%s.count' % (date.today() - timedelta(days=x)).isoformat() for x in range(0, 10)] api_in_last_ten = sum( [int(result) for result in r.mget(*last_ten_days) if result is not None] ) users_in_last_ten = User.objects.filter( date_joined__gte=ten_days_ago ).count() opinions_in_last_ten = Opinion.objects.filter( date_created__gte=ten_days_ago ).count() oral_arguments_in_last_ten = Audio.objects.filter( date_created__gte=ten_days_ago ).count() days_of_oa = naturalduration( Audio.objects.aggregate( Sum('duration') )['duration__sum'], as_dict=True, )['d'] viz_in_last_ten = SCOTUSMap.objects.filter( date_published__gte=ten_days_ago, published=True, ).count() visualizations = SCOTUSMap.objects.filter( published=True, deleted=False, ).annotate( Count('clusters'), ).filter( # Ensures that we only show good stuff on homepage clusters__count__gt=10, ).order_by( '-date_published', '-date_modified', '-date_created', )[:1] return { '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, 'viz_in_last_ten': viz_in_last_ten, 'visualizations': visualizations, 'private': False, # VERY IMPORTANT! }
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'), user=request.user, ) 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(commit=False) alert.user = request.user alert.save() action = "created" messages.add_message(request, messages.SUCCESS, 'Your alert was %s successfully.' % action) # and redirect to the alerts page return HttpResponseRedirect(reverse("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.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'] r = redis.StrictRedis( host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DATABASES['STATS'], ) last_ten_days = ['api:v3.d:%s.count' % (date.today() - timedelta(days=x)).isoformat() for x in range(0, 10)] api_in_last_ten = sum( [int(result) for result in r.mget(*last_ten_days) if result is not None] ) users_in_last_ten = User.objects.filter( date_joined__gte=ten_days_ago ).count() opinions_in_last_ten = Opinion.objects.filter( date_created__gte=ten_days_ago ).count() oral_arguments_in_last_ten = Audio.objects.filter( date_created__gte=ten_days_ago ).count() days_of_oa = naturalduration( Audio.objects.aggregate( Sum('duration') )['duration__sum'], as_dict=True, )['d'] viz_in_last_ten = SCOTUSMap.objects.filter( date_published__gte=ten_days_ago, published=True, ).count() visualizations = SCOTUSMap.objects.filter( published=True, deleted=False, ).annotate( Count('clusters'), ).filter( # Ensures that we only show good stuff on homepage clusters__count__gt=10, ).order_by( '-date_published', '-date_modified', '-date_created', )[:1] 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, 'viz_in_last_ten': viz_in_last_ten, 'visualizations': visualizations, 'private': False, # VERY IMPORTANT! }) 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 if request.user.is_anonymous(): return HttpResponseRedirect( "{path}?next={next}{encoded_params}".format( path=reverse('sign-in'), next=request.path, encoded_params=quote("?" + request.GET.urlencode()) )) else: alert = get_object_or_404( Alert, pk=request.GET.get('edit_alert'), user=request.user ) 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.html', render_dict, RequestContext(request), )