Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/1977 - Update transaction Manager to have report code label in order to sort via Report Type #900

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion django-backend/fecfiler/reports/form_3x/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .serializers import Form3XSerializer
import structlog
from rest_framework.response import Response
from fecfiler.reports.report_code_label import report_code_label_mapping

logger = structlog.get_logger(__name__)

Expand Down Expand Up @@ -36,9 +37,13 @@ def coverage_dates(self, request):
)
return JsonResponse(data, safe=False)

@action(detail=False)
def report_code_map(self, request):
return JsonResponse(report_code_label_mapping, safe=False)

@action(detail=False, methods=["get"], url_path=r"future")
def future_form3x_reports(self, request):
json_date_string = request.GET.get('after', '')
json_date_string = request.GET.get("after", "")
data = list(
self.get_queryset().filter(coverage_through_date__gt=json_date_string)
)
Expand Down
38 changes: 38 additions & 0 deletions django-backend/fecfiler/reports/report_code_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.db.models import Case, When, Value

report_code_label_mapping = {
"Q1": "APRIL 15 QUARTERLY REPORT (Q1)",
"Q2": "JULY 15 QUARTERLY REPORT (Q2)",
"Q3": "OCTOBER 15 QUARTERLY REPORT (Q3)",
"YE": "JANUARY 31 YEAR-END (YE)",
"TER": "TERMINATION REPORT (TER)",
"MY": "JULY 31 MID-YEAR REPORT (MY)",
"12G": "12-DAY PRE-GENERAL (12G)",
"12P": "12-DAY PRE-PRIMARY (12P)",
"12R": "12-DAY PRE-RUNOFF (12R)",
"12S": "12-DAY PRE-SPECIAL (12S)",
"12C": "12-DAY PRE-CONVENTION (12C)",
"30G": "30-DAY POST-GENERAL (30G)",
"30R": "30-DAY POST-RUNOFF (30R)",
"30S": "30-DAY POST-SPECIAL (30S)",
"M2": "FEBRUARY 20 MONTHLY REPORT (M2)",
"M3": "MARCH 20 MONTHLY REPORT (M3)",
"M4": "APRIL 20 MONTHLY REPORT (M4)",
"M5": "MAY 20 MONTHLY REPORT (M5)",
"M6": "JUNE 20 MONTHLY REPORT (M6)",
"M7": "JULY 20 MONTHLY REPORT (M7)",
"M8": "AUGUST 20 MONTHLY REPORT (M8)",
"M9": "SEPTEMBER 20 MONTHLY REPORT (M9)",
"M10": "OCTOBER 20 MONTHLY REPORT (M10)",
"M11": "NOVEMBER 20 MONTHLY REPORT (M11)",
"M12": "DECEMBER 20 MONTHLY REPORT (M12)",
}

# Generate the Case object
report_code_label_case = Case(
*[When(report_code=k, then=Value(v)) for k, v in report_code_label_mapping.items()],
When(form_24__report_type_24_48=24, then=Value("24 HOUR")),
When(form_24__report_type_24_48=48, then=Value("48 HOUR")),
When(form_99__isnull=False, then=Value("")),
When(form_1m__isnull=False, then=Value("")),
)
31 changes: 2 additions & 29 deletions django-backend/fecfiler/reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.viewsets import GenericViewSet, ModelViewSet
from fecfiler.committee_accounts.views import CommitteeOwnedViewMixin
from .models import Report
from .report_code_label import report_code_label_case
from fecfiler.transactions.models import Transaction
from fecfiler.memo_text.models import MemoText
from fecfiler.web_services.models import DotFEC, UploadSubmission, WebPrintSubmission
Expand All @@ -14,34 +15,6 @@

logger = structlog.get_logger(__name__)

report_code_label_mapping = Case(
When(report_code="Q1", then=Value("APRIL 15 (Q1)")),
When(report_code="Q2", then=Value("JULY 15 (Q2)")),
When(report_code="Q3", then=Value("OCTOBER 15 (Q3)")),
When(report_code="YE", then=Value("JANUARY 31 (YE)")),
When(report_code="TER", then=Value("TERMINATION (TER)")),
When(report_code="MY", then=Value("JULY 31 (MY)")),
When(report_code="12G", then=Value("GENERAL (12G)")),
When(report_code="12P", then=Value("PRIMARY (12P)")),
When(report_code="12R", then=Value("RUNOFF (12R)")),
When(report_code="12S", then=Value("SPECIAL (12S)")),
When(report_code="12C", then=Value("CONVENTION (12C)")),
When(report_code="30G", then=Value("GENERAL (30G)")),
When(report_code="30R", then=Value("RUNOFF (30R)")),
When(report_code="30S", then=Value("SPECIAL (30S)")),
When(report_code="M2", then=Value("FEBRUARY 20 (M2)")),
When(report_code="M3", then=Value("MARCH 30 (M3)")),
When(report_code="M4", then=Value("APRIL 20 (M4)")),
When(report_code="M5", then=Value("MAY 20 (M5)")),
When(report_code="M6", then=Value("JUNE 20 (M6)")),
When(report_code="M7", then=Value("JULY 20 (M7)")),
When(report_code="M8", then=Value("AUGUST 20 (M8)")),
When(report_code="M9", then=Value("SEPTEMBER 20 (M9)")),
When(report_code="M10", then=Value("OCTOBER 20 (M10)")),
When(report_code="M11", then=Value("NOVEMBER 20 (M11)")),
When(report_code="M12", then=Value("DECEMBER 20 (M12)")),
)


version_labels = {
"F3XN": "Original",
Expand Down Expand Up @@ -73,7 +46,7 @@ class ReportViewSet(CommitteeOwnedViewMixin, ModelViewSet):
whens = [When(form_type=k, then=Value(v)) for k, v in version_labels.items()]

queryset = (
Report.objects.annotate(report_code_label=report_code_label_mapping)
Report.objects.annotate(report_code_label=report_code_label_case)
# alias fields used by the version_label annotation only. not part of payload
.alias(
form_type_label=Case(
Expand Down
13 changes: 10 additions & 3 deletions django-backend/fecfiler/transactions/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from decimal import Decimal
from enum import Enum
from .schedule_b.managers import refunds as schedule_b_refunds
from ..reports.models import Report
from fecfiler.reports.report_code_label import report_code_label_case

"""Manager to deterimine fields that are used the same way across transactions,
but are called different names"""
Expand Down Expand Up @@ -330,6 +332,12 @@ def transaction_view(self):

class TransactionViewManager(Manager):
def get_queryset(self):
REPORT_CODE_LABEL_CLAUSE = Subquery( # noqa: N806
Report.objects.filter(transactions=OuterRef("pk"))
.annotate(report_code_label=report_code_label_case)
.values("report_code_label")[:1]
)

return (
super()
.get_queryset()
Expand Down Expand Up @@ -376,6 +384,7 @@ def get_queryset(self):
"_calendar_ytd_per_election_office",
),
line_label=self.LINE_LABEL_CLAUSE(),
report_code_label=REPORT_CODE_LABEL_CLAUSE,
)
.alias(order_key=self.ORDER_KEY_CLAUSE())
.order_by("order_key")
Expand Down Expand Up @@ -404,9 +413,7 @@ def ORDER_KEY_CLAUSE(self): # noqa: N802
output_field=TextField(),
),
),
default=Concat(
"schedule", "_form_type", "created", output_field=TextField()
),
default=Concat("schedule", "_form_type", "created", output_field=TextField()),
output_field=TextField(),
)

Expand Down
26 changes: 13 additions & 13 deletions django-backend/fecfiler/transactions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@

logger = structlog.get_logger(__name__)

MISSING_SCHEMA_NAME_ERROR = ValidationError(
{"schema_name": ["No schema_name provided"]}
)
MISSING_SCHEMA_NAME_ERROR = ValidationError({"schema_name": ["No schema_name provided"]})

SCHEDULE_SERIALIZERS = dict(
A=ScheduleASerializer,
Expand Down Expand Up @@ -76,9 +74,7 @@ class TransactionSerializer(
back_reference_tran_id_number = CharField(
required=False, allow_null=True, read_only=True
)
back_reference_sched_name = CharField(
required=False, allow_null=True, read_only=True
)
back_reference_sched_name = CharField(required=False, allow_null=True, read_only=True)
form_type = CharField(required=False, allow_null=True)
itemized = BooleanField(read_only=True)
name = CharField(read_only=True)
Expand All @@ -98,6 +94,7 @@ class TransactionSerializer(
max_digits=11, decimal_places=2, read_only=True
) # debt payments
line_label = CharField(read_only=True)
report_code_label = CharField(read_only=True)

class Meta:
model = Transaction
Expand Down Expand Up @@ -144,6 +141,7 @@ def get_fields():
"payment_amount",
"balance_at_close",
"line_label",
"report_code_label",
]

fields = get_fields()
Expand Down Expand Up @@ -261,13 +259,15 @@ def to_representation(self, instance):
representation["report_ids"] = []
for report in instance.reports.all():
representation["report_ids"].append(report.id)
representation["reports"].append({
"id": report.id,
"coverage_from_date": report.coverage_from_date,
"coverage_through_date": report.coverage_through_date,
"report_code": report.report_code,
"report_type": report.report_type
})
representation["reports"].append(
{
"id": report.id,
"coverage_from_date": report.coverage_from_date,
"coverage_through_date": report.coverage_through_date,
"report_code": report.report_code,
"report_type": report.report_type,
}
)

return representation

Expand Down
2 changes: 2 additions & 0 deletions django-backend/fecfiler/transactions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class TransactionViewSet(CommitteeOwnedViewMixin, ModelViewSet):
"aggregate",
"balance",
"back_reference_tran_id_number",
"form_type",
"report_code_label",
]
ordering = ["-created"]
queryset = Transaction.objects
Expand Down