def core_docket_data( request: HttpRequest, pk: int, ) -> Tuple[Docket, Dict[str, Union[bool, str, Docket, FavoriteForm]]]: """Gather the core data for a docket, party, or IDB page.""" docket = get_object_or_404(Docket, pk=pk) title = make_docket_title(docket) try: fave = Favorite.objects.get(docket_id=docket.pk, user=request.user) except (ObjectDoesNotExist, TypeError): # Not favorited or anonymous user favorite_form = FavoriteForm( initial={ "docket_id": docket.pk, "name": trunc(best_case_name(docket), 100, ellipsis="..."), } ) else: favorite_form = FavoriteForm(instance=fave) has_alert = user_has_alert(request.user, docket) return ( docket, { "docket": docket, "title": title, "favorite_form": favorite_form, "has_alert": has_alert, "timezone": COURT_TIMEZONES.get(docket.court_id, "US/Eastern"), "private": docket.blocked, }, )
def alert_on_staleness(current_build_date, court_id, url): """Send an alert email if a feed goes stale on a weekday, according to its timezone. :param current_build_date: When the feed was updated :param court_id: The CL ID of the court :param url: The URL for the feed """ _now = now() court_tz = timezone(COURT_TIMEZONES.get(court_id, "US/Pacific")) court_now = _now.astimezone(court_tz) if court_now.weekday() in [SATURDAY, SUNDAY]: # Maintenance is frequently done on weekends, causing staleness. Don't # send alerts on weekends. return staleness_limit = timedelta(minutes=2 * 60) staleness = _now - current_build_date if staleness > staleness_limit: email = emails["stale_feed"] send_mail( email["subject"] % court_id, email["body"] % (court_id, round(staleness.total_seconds() / 60, 2), url), email["from"], email["to"], )
def core_docket_data(request, pk): """Gather the core data for a docket, party, or IDB page.""" docket = get_object_or_404(Docket, pk=pk) title = ', '.join([ s for s in [ trunc(best_case_name(docket), 100, ellipsis="..."), docket.docket_number, ] if s.strip() ]) try: fave = Favorite.objects.get(docket_id=docket.pk, user=request.user) except (ObjectDoesNotExist, TypeError): # Not favorited or anonymous user favorite_form = FavoriteForm( initial={ 'docket_id': docket.pk, 'name': trunc(best_case_name(docket), 100, ellipsis='...'), }) else: favorite_form = FavoriteForm(instance=fave) has_alert = False if request.user.is_authenticated: has_alert = DocketAlert.objects.filter(docket=docket, user=request.user).exists() return docket, { 'docket': docket, 'title': title, 'favorite_form': favorite_form, 'has_alert': has_alert, 'timezone': COURT_TIMEZONES.get(docket.court_id, 'US/Eastern'), 'private': docket.blocked, }
def core_docket_data(request, pk): """Gather the core data for a docket, party, or IDB page.""" docket = get_object_or_404(Docket, pk=pk) title = ', '.join([s for s in [ trunc(best_case_name(docket), 100, ellipsis="..."), docket.docket_number, ] if s.strip()]) try: fave = Favorite.objects.get(docket_id=docket.pk, user=request.user) except (ObjectDoesNotExist, TypeError): # Not favorited or anonymous user favorite_form = FavoriteForm(initial={ 'docket_id': docket.pk, 'name': trunc(best_case_name(docket), 100, ellipsis='...'), }) else: favorite_form = FavoriteForm(instance=fave) has_alert = False if request.user.is_authenticated: has_alert = DocketAlert.objects.filter(docket=docket, user=request.user).exists() return docket, { 'docket': docket, 'title': title, 'favorite_form': favorite_form, 'has_alert': has_alert, 'timezone': COURT_TIMEZONES.get(docket.court_id, 'US/Eastern'), 'private': docket.blocked, }
def view_docket(request, pk, slug): docket = get_object_or_404(Docket, pk=pk) if not is_bot(request): with suppress_autotime(docket, ['date_modified']): cached_count = docket.view_count docket.view_count = F('view_count') + 1 docket.save() docket.view_count = cached_count + 1 try: fave = Favorite.objects.get(docket_id=docket.pk, user=request.user) except (ObjectDoesNotExist, TypeError): # Not favorited or anonymous user favorite_form = FavoriteForm( initial={ 'docket_id': docket.pk, 'name': trunc(best_case_name(docket), 100, ellipsis='...'), }) else: favorite_form = FavoriteForm(instance=fave) de_list = docket.docket_entries.all().prefetch_related('recap_documents') form = DocketEntryFilterForm(request.GET) if form.is_valid(): cd = form.cleaned_data if cd.get('entry_gte'): de_list = de_list.filter(entry_number__gte=cd['entry_gte']) if cd.get('entry_lte'): de_list = de_list.filter(entry_number__lte=cd['entry_lte']) if cd.get('filed_after'): de_list = de_list.filter(date_filed__gte=cd['filed_after']) if cd.get('filed_before'): de_list = de_list.filter(date_filed__lte=cd['filed_before']) if cd.get('order_by') == DocketEntryFilterForm.DESCENDING: de_list = de_list.order_by('-entry_number') paginator = Paginator(de_list, 100, orphans=5) page = request.GET.get('page') try: docket_entries = paginator.page(page) except PageNotAnInteger: docket_entries = paginator.page(1) except EmptyPage: docket_entries = paginator.page(paginator.num_pages) return render( request, 'view_docket.html', { 'docket': docket, 'parties': docket.parties.exists(), # Needed to show/hide parties tab. 'docket_entries': docket_entries, 'form': form, 'favorite_form': favorite_form, 'get_string': make_get_string(request), 'timezone': COURT_TIMEZONES.get(docket.court_id, 'US/Eastern'), 'private': docket.blocked, })
def view_parties(request, docket_id, slug): """Show the parties and attorneys tab on the docket.""" docket = get_object_or_404(Docket, pk=docket_id) title = ', '.join([ s for s in [ trunc(best_case_name(docket), 100, ellipsis="..."), docket.docket_number, ] if s.strip() ]) try: fave = Favorite.objects.get(docket_id=docket.pk, user=request.user) except (ObjectDoesNotExist, TypeError): # Not favorited or anonymous user favorite_form = FavoriteForm( initial={ 'docket_id': docket.pk, 'name': trunc(best_case_name(docket), 100, ellipsis='...'), }) else: favorite_form = FavoriteForm(instance=fave) # We work with this data at the level of party_types so that we can group # the parties by this field. From there, we do a whole mess of prefetching, # which reduces the number of queries needed for this down to four instead # of potentially thousands (good times!) party_types = docket.party_types.select_related('party').prefetch_related( Prefetch( 'party__roles', queryset=Role.objects.filter(docket=docket).order_by( 'attorney_id', 'role', 'date_action').select_related('attorney').prefetch_related( Prefetch( 'attorney__organizations', queryset=AttorneyOrganization.objects.filter( attorney_organization_associations__docket=docket). distinct(), to_attr='firms_in_docket', )))).order_by('name', 'party__name') parties = [] for party_type_name, party_types in groupby(party_types, lambda x: x.name): party_types = list(party_types) parties.append({ 'party_type_name': party_type_name, 'party_type_objects': party_types }) return render( request, 'docket_parties.html', { 'docket': docket, 'title': title, 'parties': parties, 'favorite_form': favorite_form, 'timezone': COURT_TIMEZONES.get(docket.court_id, 'US/Eastern'), 'private': docket.blocked, })
def alert_on_staleness( current_build_date: datetime, court_id: str, url: str, ) -> None: """Send an alert email if a feed goes stale on a weekday, according to its timezone. Slow down after the first day. :param current_build_date: When the feed was updated :param court_id: The CL ID of the court :param url: The URL for the feed """ _now = now() staleness = _now - current_build_date # If a feed is not stale, do not send an alert. staleness_limit = timedelta(minutes=2 * 60) if staleness < staleness_limit: return # Maintenance is frequently done on weekends, causing staleness. Don't # send alerts on weekends. court_tz = timezone(COURT_TIMEZONES.get(court_id, "US/Pacific")) court_now = _now.astimezone(court_tz) if court_now.weekday() in [SATURDAY, SUNDAY]: return # If it's really stale, don't send alerts except during hours evenly # divisible by six to slow down alerts. on_a_sixth = _now.hour % 6 == 0 really_stale = timedelta(minutes=60 * 60 * 24) if (staleness > really_stale) and not on_a_sixth: return # All limits have passed; send an alert email: EmailType = emails["stale_feed"] send_mail( email["subject"] % court_id, email["body"] % (court_id, round(staleness.total_seconds() / 60, 2), url), email["from_email"], email["to"], )