Skip to content

Commit

Permalink
Merge pull request #2772 from freelawproject/864-dates-in-search-resu…
Browse files Browse the repository at this point in the history
…lts-vary-from-those-in-database

864 Dates in search results vary from those in database
  • Loading branch information
mlissner authored May 25, 2023
2 parents 75cf83d + 395db9a commit e3ce412
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 38 deletions.
8 changes: 4 additions & 4 deletions cl/audio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.urls import NoReverseMatch, reverse

from cl.custom_filters.templatetags.text_filters import best_case_name
from cl.lib.date_time import midnight_pst
from cl.lib.date_time import midnight_pt
from cl.lib.model_helpers import make_upload_path
from cl.lib.models import AbstractDateTimeModel, s3_warning_note
from cl.lib.pghistory import AfterUpdateOrDeleteSnapshot
Expand Down Expand Up @@ -220,11 +220,11 @@ def as_search_dict(self) -> Dict[str, Union[int, List[int], str]]:
# Docket
docket = {"docketNumber": self.docket.docket_number}
if self.docket.date_argued is not None:
docket["dateArgued"] = midnight_pst(self.docket.date_argued)
docket["dateArgued"] = midnight_pt(self.docket.date_argued)
if self.docket.date_reargued is not None:
docket["dateReargued"] = midnight_pst(self.docket.date_reargued)
docket["dateReargued"] = midnight_pt(self.docket.date_reargued)
if self.docket.date_reargument_denied is not None:
docket["dateReargumentDenied"] = midnight_pst(
docket["dateReargumentDenied"] = midnight_pt(
self.docket.date_reargument_denied
)
out.update(docket)
Expand Down
9 changes: 6 additions & 3 deletions cl/lib/date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from django.utils.timezone import is_aware


def midnight_pst(d: date) -> datetime:
"""Cast a naive date object to midnight PST"""
def midnight_pt(d: date) -> datetime:
"""Cast a naive date object to midnight Pacific Time, PST or PDT according
to the date.
"""
pst = pytz.timezone("US/Pacific")
d = datetime.combine(d, time()).replace(tzinfo=pst)
d = datetime.combine(d, time())
d = pst.localize(d)
return d


Expand Down
4 changes: 2 additions & 2 deletions cl/lib/search_index_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import date

from cl.lib.date_time import midnight_pst
from cl.lib.date_time import midnight_pt


def solr_list(m2m_list, field):
Expand All @@ -10,7 +10,7 @@ def solr_list(m2m_list, field):
if obj is None:
continue
if isinstance(obj, date):
obj = midnight_pst(obj)
obj = midnight_pt(obj)
new_list.append(obj)
return new_list

Expand Down
16 changes: 16 additions & 0 deletions cl/lib/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.urls import reverse
from rest_framework.status import HTTP_200_OK, HTTP_503_SERVICE_UNAVAILABLE

from cl.lib.date_time import midnight_pt
from cl.lib.filesizes import convert_size_to_bytes
from cl.lib.mime_types import lookup_mime_type
from cl.lib.model_helpers import (
Expand Down Expand Up @@ -941,3 +942,18 @@ def test_related_factory_variable_list(self):
cluster_2.sub_opinions.all().order_by("type")[2].type,
"070rehearing",
)


class TestDateTimeHelpers(SimpleTestCase):
def test_midnight_pt(self) -> None:
# Date in PSD time -8 hours UTC offset
pst_date = datetime.date(2023, 1, 3)
pst_date_time = midnight_pt(pst_date)
pst_utc_offset_hours = pst_date_time.utcoffset().total_seconds() / 3600 # type: ignore
self.assertEqual(pst_utc_offset_hours, -8.0)

# Date in PDT time -7 hours UTC offset
pdt_date = datetime.date(2023, 5, 3)
pdt_date_time = midnight_pt(pdt_date)
pdt_utc_offset_hours = pdt_date_time.utcoffset().total_seconds() / 3600 # type: ignore
self.assertEqual(pdt_utc_offset_hours, -7.0)
4 changes: 2 additions & 2 deletions cl/opinion_page/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.utils.feedgenerator import Atom1Feed
from django.utils.safestring import SafeText, mark_safe

from cl.lib.date_time import midnight_pst
from cl.lib.date_time import midnight_pt
from cl.opinion_page.views import make_docket_title
from cl.search.models import Docket, DocketEntry, RECAPDocument

Expand Down Expand Up @@ -102,7 +102,7 @@ def item_link(self, item: DocketEntry) -> str:
return f"{item.docket.get_absolute_url()}?order_by=desc#{anchor}"

def item_pubdate(self, item: DocketEntry) -> datetime:
return midnight_pst(item.date_filed)
return midnight_pt(item.date_filed)

def item_enclosure_url(self, item: DocketEntry) -> Optional[str]:
if not item.entry_number:
Expand Down
6 changes: 3 additions & 3 deletions cl/people_db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)

from cl.custom_filters.templatetags.extras import granular_date
from cl.lib.date_time import midnight_pst
from cl.lib.date_time import midnight_pt
from cl.lib.model_helpers import (
make_choices_group_lookup,
validate_all_or_none,
Expand Down Expand Up @@ -301,9 +301,9 @@ def as_search_dict(self):

# Dates
if self.date_dob is not None:
out["dob"] = midnight_pst(self.date_dob)
out["dob"] = midnight_pt(self.date_dob)
if self.date_dod is not None:
out["dod"] = midnight_pst(self.date_dod)
out["dod"] = midnight_pt(self.date_dod)

# Joined Values. Brace yourself.
positions = self.positions.all()
Expand Down
6 changes: 3 additions & 3 deletions cl/search/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from requests import Session

from cl.lib import search_utils
from cl.lib.date_time import midnight_pst
from cl.lib.date_time import midnight_pt
from cl.lib.mime_types import lookup_mime_type
from cl.lib.scorched_utils import ExtraSolrInterface
from cl.search.forms import SearchForm
Expand Down Expand Up @@ -84,7 +84,7 @@ def item_author_name(self, item):
return get_item(item)["court"]

def item_pubdate(self, item):
return midnight_pst(get_item(item)["dateFiled"])
return midnight_pt(get_item(item)["dateFiled"])

def item_title(self, item):
return get_item(item)["caseName"]
Expand Down Expand Up @@ -132,7 +132,7 @@ def item_author_name(self, item):
return get_item(item)["court"]

def item_pubdate(self, item):
return midnight_pst(get_item(item)["dateFiled"])
return midnight_pt(get_item(item)["dateFiled"])

def item_title(self, item):
return get_item(item)["caseName"]
Expand Down
38 changes: 17 additions & 21 deletions cl/search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from cl.citations.utils import get_citation_depth_between_clusters
from cl.custom_filters.templatetags.text_filters import best_case_name
from cl.lib import fields
from cl.lib.date_time import midnight_pst
from cl.lib.date_time import midnight_pt
from cl.lib.model_helpers import (
make_docket_number_core,
make_recap_path,
Expand Down Expand Up @@ -899,11 +899,11 @@ def as_search_list(self):
"jurisdictionType": self.jurisdiction_type,
}
if self.date_argued is not None:
out["dateArgued"] = midnight_pst(self.date_argued)
out["dateArgued"] = midnight_pt(self.date_argued)
if self.date_filed is not None:
out["dateFiled"] = midnight_pst(self.date_filed)
out["dateFiled"] = midnight_pt(self.date_filed)
if self.date_terminated is not None:
out["dateTerminated"] = midnight_pst(self.date_terminated)
out["dateTerminated"] = midnight_pt(self.date_terminated)
try:
out["docket_absolute_url"] = self.get_absolute_url()
except NoReverseMatch:
Expand Down Expand Up @@ -960,7 +960,7 @@ def as_search_list(self):
if de.entry_number is not None:
de_out["entry_number"] = de.entry_number
if de.date_filed is not None:
de_out["entry_date_filed"] = midnight_pst(de.date_filed)
de_out["entry_date_filed"] = midnight_pt(de.date_filed)
rds = de.recap_documents.all()

if len(rds) == 0:
Expand Down Expand Up @@ -1488,11 +1488,11 @@ def get_docket_metadata(self):
}
)
if docket.date_argued is not None:
out["dateArgued"] = midnight_pst(docket.date_argued)
out["dateArgued"] = midnight_pt(docket.date_argued)
if docket.date_filed is not None:
out["dateFiled"] = midnight_pst(docket.date_filed)
out["dateFiled"] = midnight_pt(docket.date_filed)
if docket.date_terminated is not None:
out["dateTerminated"] = midnight_pst(docket.date_terminated)
out["dateTerminated"] = midnight_pt(docket.date_terminated)
try:
out["docket_absolute_url"] = docket.get_absolute_url()
except NoReverseMatch:
Expand Down Expand Up @@ -1586,9 +1586,7 @@ def as_search_dict(self, docket_metadata=None):
if self.docket_entry.entry_number is not None:
out["entry_number"] = self.docket_entry.entry_number
if self.docket_entry.date_filed is not None:
out["entry_date_filed"] = midnight_pst(
self.docket_entry.date_filed
)
out["entry_date_filed"] = midnight_pt(self.docket_entry.date_filed)

text_template = loader.get_template("indexes/dockets_text.txt")
out["text"] = text_template.render({"item": self}).translate(null_map)
Expand Down Expand Up @@ -2577,11 +2575,11 @@ def as_search_list(self):
"docketNumber": self.docket.docket_number,
}
if self.docket.date_argued is not None:
docket["dateArgued"] = midnight_pst(self.docket.date_argued)
docket["dateArgued"] = midnight_pt(self.docket.date_argued)
if self.docket.date_reargued is not None:
docket["dateReargued"] = midnight_pst(self.docket.date_reargued)
docket["dateReargued"] = midnight_pt(self.docket.date_reargued)
if self.docket.date_reargument_denied is not None:
docket["dateReargumentDenied"] = midnight_pst(
docket["dateReargumentDenied"] = midnight_pt(
self.docket.date_reargument_denied
)
out.update(docket)
Expand Down Expand Up @@ -2624,7 +2622,7 @@ def as_search_list(self):
pass

if self.date_filed is not None:
out["dateFiled"] = midnight_pst(self.date_filed)
out["dateFiled"] = midnight_pt(self.date_filed)
try:
out["absolute_url"] = self.get_absolute_url()
except NoReverseMatch:
Expand Down Expand Up @@ -3050,7 +3048,7 @@ def as_search_dict(self) -> Dict[str, Any]:
pass

if self.cluster.date_filed is not None:
out["dateFiled"] = midnight_pst(self.cluster.date_filed)
out["dateFiled"] = midnight_pt(self.cluster.date_filed)
try:
out["absolute_url"] = self.cluster.get_absolute_url()
except NoReverseMatch:
Expand All @@ -3063,15 +3061,13 @@ def as_search_dict(self) -> Dict[str, Any]:
# Docket
docket = {"docketNumber": self.cluster.docket.docket_number}
if self.cluster.docket.date_argued is not None:
docket["dateArgued"] = midnight_pst(
self.cluster.docket.date_argued
)
docket["dateArgued"] = midnight_pt(self.cluster.docket.date_argued)
if self.cluster.docket.date_reargued is not None:
docket["dateReargued"] = midnight_pst(
docket["dateReargued"] = midnight_pt(
self.cluster.docket.date_reargued
)
if self.cluster.docket.date_reargument_denied is not None:
docket["dateReargumentDenied"] = midnight_pst(
docket["dateReargumentDenied"] = midnight_pt(
self.cluster.docket.date_reargument_denied
)
out.update(docket)
Expand Down

0 comments on commit e3ce412

Please sign in to comment.