-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1186 from fecgov/release/sprint-50
Release/sprint 50
- Loading branch information
Showing
62 changed files
with
2,725 additions
and
671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
51 changes: 51 additions & 0 deletions
51
django-backend/fecfiler/cash_on_hand/migrations/0001_initial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Generated by Django 4.2.7 on 2024-01-16 20:15 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
("committee_accounts", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="CashOnHandYearly", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
unique=True, | ||
), | ||
), | ||
( | ||
"cash_on_hand", | ||
models.DecimalField( | ||
blank=True, decimal_places=2, max_digits=11, null=True | ||
), | ||
), | ||
("year", models.TextField(blank=True, null=True)), | ||
("created", models.DateTimeField(auto_now_add=True)), | ||
("updated", models.DateTimeField(auto_now=True)), | ||
( | ||
"committee_account", | ||
models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="committee_accounts.committeeaccount", | ||
), | ||
), | ||
], | ||
options={ | ||
"db_table": "cash_on_hand_yearly", | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from django.db import models, transaction as db_transaction | ||
from fecfiler.reports.models import Report | ||
from fecfiler.committee_accounts.models import CommitteeOwnedModel | ||
import uuid | ||
|
||
|
||
class CashOnHandYearly(CommitteeOwnedModel): | ||
|
||
id = models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
unique=True, | ||
) | ||
|
||
year = models.TextField(null=False, blank=False, unique=True) | ||
cash_on_hand = models.DecimalField( | ||
null=False, blank=False, max_digits=11, decimal_places=2 | ||
) | ||
created = models.DateTimeField(auto_now_add=True) | ||
updated = models.DateTimeField(auto_now=True) | ||
|
||
def save(self, *args, **kwargs): | ||
with db_transaction.atomic(): | ||
super(CashOnHandYearly, self).save(*args, **kwargs) | ||
Report.objects.filter( | ||
committee_account=self.committee_account, | ||
).update(calculation_status=None) | ||
|
||
class Meta: | ||
db_table = "cash_on_hand_yearly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from .models import CashOnHandYearly | ||
from fecfiler.committee_accounts.serializers import CommitteeOwnedSerializer | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CashOnHandYearlySerializer(CommitteeOwnedSerializer): | ||
class Meta: | ||
model = CashOnHandYearly | ||
fields = [f.name for f in CashOnHandYearly._meta.get_fields()] | ||
read_only_fields = [ | ||
"id", | ||
"created", | ||
"updated", | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.test import TestCase | ||
from ..models import CashOnHandYearly | ||
from fecfiler.reports.tests.utils import create_form3x | ||
from fecfiler.committee_accounts.models import CommitteeAccount | ||
from fecfiler.committee_accounts.utils import create_committee_view | ||
from fecfiler.web_services.summary.tasks import CalculationState | ||
from fecfiler.reports.models import Report | ||
|
||
|
||
class CashOnHandYearlyTestCase(TestCase): | ||
|
||
def setUp(self): | ||
self.committee = CommitteeAccount.objects.create(committee_id="C00000000") | ||
create_committee_view(self.committee.id) | ||
|
||
def test_calcualtion_status_flag(self): | ||
f3x1 = create_form3x( | ||
self.committee, | ||
"2005-01-01", | ||
"2005-01-30", | ||
) | ||
f3x2 = create_form3x( | ||
self.committee, | ||
"2005-02-01", | ||
"2005-02-28", | ||
) | ||
Report.objects.update(calculation_status=CalculationState.SUCCEEDED) | ||
|
||
CashOnHandYearly.objects.create( | ||
committee_account=self.committee, | ||
year="2016", | ||
cash_on_hand=100, | ||
) | ||
|
||
self.assertIsNone(Report.objects.get(pk=f3x1.id).calculation_status) | ||
self.assertIsNone(Report.objects.get(pk=f3x2.id).calculation_status) | ||
|
||
def test_permissions(self): | ||
CashOnHandYearly.objects.create( | ||
committee_account=self.committee, | ||
year="2016", | ||
cash_on_hand=100, | ||
) | ||
other_committee = CommitteeAccount.objects.create(committee_id="C00000001") | ||
other_committees_coh = CashOnHandYearly.objects.filter( | ||
committee_account=other_committee | ||
).first() | ||
self.assertIsNone(other_committees_coh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from django.test import RequestFactory, TestCase | ||
from ..views import CashOnHandYearlyViewSet | ||
from ..models import CashOnHandYearly | ||
from fecfiler.committee_accounts.models import CommitteeAccount | ||
from .utils import create_cash_on_hand_yearly | ||
from rest_framework.test import force_authenticate | ||
|
||
from fecfiler.user.models import User | ||
|
||
|
||
class CashOnHandYearlyViewSetTest(TestCase): | ||
fixtures = ["C01234567_user_and_committee"] | ||
|
||
def setUp(self): | ||
self.user = User.objects.get(id="12345678-aaaa-bbbb-cccc-111122223333") | ||
|
||
self.committee = CommitteeAccount.objects.get(committee_id="C01234567") | ||
self.factory = RequestFactory() | ||
|
||
def test_no_override(self): | ||
request = self.factory.get("/api/v1/cash_on_hand/year/2024/") | ||
request.user = self.user | ||
request.session = { | ||
"committee_uuid": str(self.committee.id), | ||
"committee_id": str(self.committee.committee_id), | ||
} | ||
other_committee = CommitteeAccount.objects.create(committee_id="C00000001") | ||
create_cash_on_hand_yearly(other_committee, "2024", 1) | ||
response = CashOnHandYearlyViewSet.as_view({"get": "cash_on_hand_for_year"})( | ||
request, year="2024" | ||
) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_get_override(self): | ||
request = self.factory.get("/api/v1/cash_on_hand/year/2024/") | ||
request.user = self.user | ||
request.session = { | ||
"committee_uuid": str(self.committee.id), | ||
"committee_id": str(self.committee.committee_id), | ||
} | ||
create_cash_on_hand_yearly(self.committee, "2024", 1) | ||
response = CashOnHandYearlyViewSet.as_view({"get": "cash_on_hand_for_year"})( | ||
request, year="2024" | ||
) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data["cash_on_hand"], "1.00") | ||
|
||
def test_set_override(self): | ||
request = self.factory.post( | ||
"/api/v1/cash_on_hand/year/2024/", {"cash_on_hand": 2} | ||
) | ||
request.user = self.user | ||
request.session = { | ||
"committee_uuid": str(self.committee.id), | ||
"committee_id": str(self.committee.committee_id), | ||
} | ||
force_authenticate(request, self.user) | ||
response = CashOnHandYearlyViewSet.as_view({"post": "cash_on_hand_for_year"})( | ||
request, year="2024" | ||
) | ||
self.assertEqual(response.status_code, 200) | ||
cash_on_hand = CashOnHandYearly.objects.get( | ||
committee_account=self.committee, year="2024" | ||
) | ||
self.assertEqual(cash_on_hand.cash_on_hand, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from ..models import CashOnHandYearly | ||
|
||
|
||
def create_cash_on_hand_yearly(committee_account, year, cash_on_hand): | ||
return CashOnHandYearly.objects.create( | ||
committee_account=committee_account, year=year, cash_on_hand=cash_on_hand | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
from .views import CashOnHandYearlyViewSet | ||
|
||
# Create a router and register our viewsets with it. | ||
router = DefaultRouter() | ||
router.register(r"cash_on_hand", CashOnHandYearlyViewSet, basename="cash_on_hand") | ||
|
||
# The API URLs are now determined automatically by the router. | ||
urlpatterns = [ | ||
path("", include(router.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from rest_framework.response import Response | ||
from rest_framework.decorators import action | ||
from fecfiler.committee_accounts.views import CommitteeOwnedViewMixin | ||
from .serializers import CashOnHandYearlySerializer | ||
from .models import CashOnHandYearly | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CashOnHandYearlyViewSet(CommitteeOwnedViewMixin): | ||
""" | ||
Note that this ViewSet inherits from CommitteeOwnedViewMixin | ||
The queryset will be further limited by the user's committee | ||
in CommitteeOwnedViewMixin's implementation of get_queryset() | ||
""" | ||
|
||
queryset = CashOnHandYearly.objects | ||
serializer_class = CashOnHandYearlySerializer | ||
pagination_class = None | ||
|
||
@action(detail=False, methods=["get", "post"], url_path=r"year/(?P<year>\d+)") | ||
def cash_on_hand_for_year(self, request, year): | ||
if request.method == "GET": | ||
return self.get_cash_on_hand_for_year(request, year) | ||
elif request.method == "POST": | ||
return self.set_cash_on_hand_for_year(request, year) | ||
|
||
def get_cash_on_hand_for_year(self, request, year): | ||
cash_on_hand = self.get_queryset().filter(year=year).first() | ||
if cash_on_hand is None: | ||
return Response(status=404) | ||
serializer = self.get_serializer(cash_on_hand) | ||
return Response(serializer.data) | ||
|
||
def set_cash_on_hand_for_year(self, request, year): | ||
new_cash_on_hand = request.data.get("cash_on_hand") | ||
committee_uuid = self.get_committee_uuid() | ||
if new_cash_on_hand is None: | ||
return Response("cash_on_hand is required", status=400) | ||
cash_on_hand_record = self.get_queryset().filter(year=year).first() | ||
if cash_on_hand_record is None: | ||
cash_on_hand_record = CashOnHandYearly.objects.create( | ||
committee_account_id=committee_uuid, | ||
year=year, | ||
cash_on_hand=new_cash_on_hand, | ||
) | ||
else: | ||
cash_on_hand_record.cash_on_hand = new_cash_on_hand | ||
cash_on_hand_record.save() | ||
|
||
serializer = self.get_serializer(cash_on_hand_record) | ||
return Response(serializer.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.