Skip to content

Commit

Permalink
refactor(opinion_page): Remove class methods from dataclasses
Browse files Browse the repository at this point in the history
Streamline code organization and remove unnecessary complexity by removing the RECAPDocCitationRecord dataclass and the associated class methods from the ViewAuthority dataclass. Additionally, enhance the AuthoritiesContext dataclass by introducing two init-only fields and replacing the construct method with the post_init() method, simplifying object initialization.
  • Loading branch information
ERosendo committed Nov 30, 2023
1 parent 4454e2b commit c7efdef
Showing 1 changed file with 21 additions and 106 deletions.
127 changes: 21 additions & 106 deletions cl/opinion_page/types.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
from dataclasses import dataclass
from typing import List, Literal
from dataclasses import InitVar, dataclass, field
from typing import Literal

from django.urls import reverse

from cl.search.models import OpinionCluster, OpinionsCitedByRECAPDocument


@dataclass
class RECAPDocCitationRecord:
total_citation_count: int
cit_records: List[OpinionsCitedByRECAPDocument]
from cl.search.models import OpinionCluster, RECAPDocument


@dataclass
Expand All @@ -18,109 +10,32 @@ class ViewAuthority:
count: int
url: str

@classmethod
def from_cluster_authority(cls, auth: OpinionCluster, query_string: str):
opinion_url = f"{auth.get_absolute_url()}?{query_string}"
citation_count = auth.citation_depth
opinion_caption = auth.caption
return cls(
caption=opinion_caption, count=citation_count, url=opinion_url
)

@classmethod
def from_recap_cit_record(
cls, record: OpinionsCitedByRECAPDocument, query_string: str
):
opinion_caption = record.cited_opinion.cluster.caption
citation_count = record.depth
opinion_url = (
f"{record.cited_opinion.cluster.get_absolute_url()}?{query_string}"
)
return cls(
caption=opinion_caption, count=citation_count, url=opinion_url
)


@dataclass
class AuthoritiesContext:
citation_record: InitVar[OpinionCluster | RECAPDocument]
query_string: InitVar[str]
top_authorities: list[ViewAuthority] = field(init=False)
total_authorities_count: int
top_authorities: List[ViewAuthority]
view_all_url: str
doc_type: Literal["opinion", "document"]

@classmethod
def construct(
cls,
citation_record: OpinionCluster | RECAPDocCitationRecord,
request_query_string: str,
):
doc_type: Literal["opinion", "document"]
if isinstance(citation_record, RECAPDocCitationRecord):
top_authorities = [
ViewAuthority.from_recap_cit_record(
cit_record, request_query_string
def __post_init__(self, citation_record, query_string):
if isinstance(citation_record, RECAPDocument):
self.top_authorities = [
ViewAuthority(
caption=record.cited_opinion.cluster.caption,
count=record.depth,
url=f"{record.cited_opinion.cluster.get_absolute_url()}?{query_string}",
)
for cit_record in citation_record.cit_records
for record in citation_record.authorities_with_data[:5]
]
view_all_url_base = "" # TODO
total_authorities_count = citation_record.total_citation_count
doc_type = "document"

else:
top_authorities = [
ViewAuthority.from_cluster_authority(ca, request_query_string)
for ca in citation_record.authorities_with_data[:5]
self.top_authorities = [
ViewAuthority(
caption=record.caption,
count=record.citation_depth,
url=f"{record.get_absolute_url()}?{query_string}",
)
for record in citation_record.authorities_with_data[:5]
]
view_all_url_base = reverse(
"view_authorities",
args=[citation_record.pk, citation_record.slug],
)
total_authorities_count = len(
citation_record.authorities_with_data
)
doc_type = "opinion"

return cls(
top_authorities=top_authorities,
total_authorities_count=total_authorities_count,
view_all_url=view_all_url_base,
doc_type=doc_type,
)

# @classmethod
# def from_opinion_cluster(
# cls, cluster: OpinionCluster, request_query_string: str
# ):
# top_authorities = [
# ViewAuthority.from_cluster_authority(ca, request_query_string)
# for ca in cluster.authorities_with_data[:5]
# ]
# view_all_url_base = reverse(
# "view_authorities", args=[cluster.pk, cluster.slug]
# )
# return cls(
# top_authorities=top_authorities,
# view_all_url=f"{view_all_url_base}?{request_query_string}",
# total_authorities_count=len(cluster.authorities_with_data),
# )

# @classmethod
# def from_recap_document_cits(
# cls,
# total_cit_count: int,
# cit_records: List[OpinionsCitedByRECAPDocument],
# request_query_string: str,
# ):
# top_authorities = [
# ViewAuthority.from_recap_cit_record(
# r, query_string=request_query_string
# )
# for r in cit_records
# ]
# # TODO
# view_all_url_base = ""
# return cls(
# top_authorities=top_authorities,
# total_authorities_count=total_cit_count,
# view_all_url=view_all_url_base,
# )

0 comments on commit c7efdef

Please sign in to comment.