Skip to content

Commit

Permalink
models changes
Browse files Browse the repository at this point in the history
black

black

migrations, drop generic foreign keys usage

black

fixture fix

ut, migrations

migrations merge fix

ut

migration merge fix
  • Loading branch information
marekbiczysko committed Nov 6, 2024
1 parent 3e1b2a7 commit c3f031f
Show file tree
Hide file tree
Showing 117 changed files with 1,655 additions and 1,459 deletions.
3 changes: 3 additions & 0 deletions src/hct_mis_api/apps/core/hope_redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def _get_business_area_slug_from_obj(self) -> str:


class HopeRedirectCashPlan(HopeRedirect):
# TODO TP what is this?
def url(self) -> str:
business_area_slug = self.get_business_area_slug()

Expand All @@ -108,6 +109,7 @@ def _get_cash_plan(self) -> Optional[CashPlan]:


class HopeRedirectPayment(HopeRedirect):
# TODO TP what is this?
def url(self) -> str:
business_area_slug = self.get_business_area_slug()

Expand All @@ -128,6 +130,7 @@ def _get_payment_verification(self) -> Optional[PaymentVerification]:


class HopeRedirectTargetPopulation(HopeRedirect):
# TODO TP what is this?
def url(self) -> str:
business_area_slug = self.get_business_area_slug()

Expand Down
21 changes: 10 additions & 11 deletions src/hct_mis_api/apps/core/management/commands/generatefixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
)
from hct_mis_api.apps.household.models import DocumentType
from hct_mis_api.apps.payment.fixtures import (
CashPlanFactory,
PaymentRecordFactory,
PaymentFactory,
PaymentPlanFactory,
PaymentVerificationFactory,
PaymentVerificationPlanFactory,
)
Expand Down Expand Up @@ -124,11 +124,10 @@ def _generate_program_with_dependencies(options: Dict, business_area_index: int)
business_area=business_area,
)
for _ in range(cash_plans_amount):
cash_plan = CashPlanFactory(
payment_plan = PaymentPlanFactory(
program=program,
business_area=business_area,
)
cash_plan.save()
for _ in range(payment_record_amount):
registration_data_import = RegistrationDataImportFactory(imported_by=user, business_area=business_area)
household, individuals = create_household_for_fixtures(
Expand All @@ -146,17 +145,17 @@ def _generate_program_with_dependencies(options: Dict, business_area_index: int)
if household.admin_area:
program.admin_areas.add(household.admin_area)

payment_record = PaymentRecordFactory(
parent=cash_plan,
payment = PaymentFactory(
parent=payment_plan,
household=household,
target_population=target_population,
delivered_quantity_usd=None,
business_area=business_area,
)
payment_record.delivered_quantity_usd = Decimal(
cash_plan.exchange_rate * payment_record.delivered_quantity
payment.delivered_quantity_usd = Decimal(
payment_plan.exchange_rate * payment.delivered_quantity
).quantize(Decimal(".01"))
payment_record.save()
payment.save()

should_create_grievance = random.choice((True, False))
if should_create_grievance:
Expand All @@ -174,13 +173,13 @@ def _generate_program_with_dependencies(options: Dict, business_area_index: int)
SensitiveGrievanceTicketWithoutExtrasFactory,
household=household,
individual=random.choice(individuals),
payment_record=payment_record if should_contain_payment_record else None,
payment=payment if should_contain_payment_record else None,
),
"complaint": partial(
GrievanceComplaintTicketWithoutExtrasFactory,
household=household,
individual=random.choice(individuals),
payment_record=payment_record if should_contain_payment_record else None,
payment=payment if should_contain_payment_record else None,
),
}

Expand Down
2 changes: 0 additions & 2 deletions src/hct_mis_api/apps/core/management/commands/initdemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
from hct_mis_api.apps.payment.fixtures import (
generate_delivery_mechanisms,
generate_payment_plan,
generate_real_cash_plans,
generate_reconciled_payment_plan,
update_fsps,
)
Expand Down Expand Up @@ -149,7 +148,6 @@ def handle(self, *args: Any, **options: Any) -> None:
self.stdout.write("Generating payment plan...")
generate_payment_plan()
self.stdout.write("Generating real cash plans...")
generate_real_cash_plans()
self.stdout.write("Generating reconciled payment plan...")
generate_reconciled_payment_plan()
self.stdout.write("Updating FSPs...")
Expand Down
4 changes: 2 additions & 2 deletions src/hct_mis_api/apps/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ def is_valid_uuid(uuid_str: str) -> bool:


def decode_and_get_payment_object(encoded_id: str, required: bool) -> Optional[Any]:
from hct_mis_api.apps.payment.utils import get_payment_items_sequence_qs
from hct_mis_api.apps.payment.models import Payment

if required or encoded_id is not None:
decoded_id = decode_id_string(encoded_id)
qs = get_payment_items_sequence_qs()
qs = Payment.objects.filter(excluded=False, conflicted=False)
try:
return qs.get(id=decoded_id)
except Exception:
Expand Down
4 changes: 2 additions & 2 deletions src/hct_mis_api/apps/grievance/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class GrievanceTicketFilter(FilterSet):
fsp = CharFilter(method="fsp_filter")
cash_plan = CharFilter(
field_name="payment_verification_ticket_details",
lookup_expr="payment_verification__payment_verification_plan__payment_plan_object_id",
lookup_expr="payment_verification__payment_verification_plan__payment_plan_id",
)
created_at_range = DateTimeRangeFilter(field_name="created_at")
permissions = MultipleChoiceFilter(choices=Permissions.choices(), method="permissions_filter")
Expand Down Expand Up @@ -332,7 +332,7 @@ def prepare_ticket_filters(self, lookup: str, obj: GrievanceTicket) -> Q:
ticket_type in ("complaint_ticket_details", "sensitive_ticket_details")
and real_lookup == "payment_record"
):
q_obj |= Q(**{f"{ticket_type}__payment_object_id": str(obj.id)})
q_obj |= Q(**{f"{ticket_type}__payment_id": str(obj.id)})
elif real_lookup:
q_obj |= Q(**{f"{ticket_type}__{real_lookup}": obj})
return q_obj
Expand Down
10 changes: 3 additions & 7 deletions src/hct_mis_api/apps/grievance/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
TicketSystemFlaggingDetails,
)
from hct_mis_api.apps.household.fixtures import create_household
from hct_mis_api.apps.payment.fixtures import (
PaymentRecordFactory,
PaymentVerificationFactory,
)
from hct_mis_api.apps.payment.fixtures import PaymentFactory, PaymentVerificationFactory
from hct_mis_api.apps.payment.models import PaymentVerification


Expand Down Expand Up @@ -91,7 +88,7 @@ def create_extras(obj, create: bool, extracted: bool, **kwargs: Any) -> None:
)
obj.household = household
obj.individual = individuals[0]
obj.payment_obj = PaymentRecordFactory(household=household, currency="EUR")
obj.payment = PaymentFactory(household=household, currency="EUR")
obj.save()


Expand All @@ -118,8 +115,7 @@ def create_extras(obj, create: bool, extracted: bool, **kwargs: Any) -> None:
)
obj.household = household
obj.individual = individuals[0]
obj.payment_object_id = PaymentRecordFactory(household=household, currency="EUR").id
obj.payment_content_type_id = 80
obj.payment = PaymentFactory(household=household, currency="EUR")

obj.save()

Expand Down
62 changes: 62 additions & 0 deletions src/hct_mis_api/apps/grievance/migrations/0074_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Generated by Django 3.2.25 on 2024-09-25 17:16

from django.db import migrations, models


def migrate_payment_tickets_generic_foreign_key_to_onetoone(apps, schema_editor):
TicketComplaintDetails = apps.get_model("grievance", "TicketComplaintDetails")
TicketSensitiveDetails = apps.get_model("grievance", "TicketSensitiveDetails")

Payment = apps.get_model("payment", "Payment")
ContentType = apps.get_model("contenttypes", "ContentType")

ticket_complaint_details_to_update = []
ticket_sensitive_details_to_update = []

for model in [TicketComplaintDetails, TicketSensitiveDetails]:
for ticket_details in model.objects.exclude(payment_content_type__isnull=True, payment_object_id__isnull=True):
if ticket_details.payment_content_type.model == "payment":
related_instance = Payment.objects.get(id=ticket_details.payment_object_id)
ticket_details.payment = related_instance
if model == TicketComplaintDetails:
ticket_complaint_details_to_update.append(ticket_details)
else:
ticket_sensitive_details_to_update.append(ticket_details)

TicketComplaintDetails.objects.bulk_update(ticket_complaint_details_to_update, ["payment"])
TicketSensitiveDetails.objects.bulk_update(ticket_sensitive_details_to_update, ["payment"])


class Migration(migrations.Migration):

dependencies = [
("grievance", "0073_migration"),
("payment", "0149_migration"),
]

operations = [
migrations.AddField(
model_name="ticketcomplaintdetails",
name="payment",
field=models.OneToOneField(
to="payment.Payment",
null=True, # Temporarily allow nulls for migration purposes
on_delete=models.CASCADE,
related_name="ticket_complaint_details",
),
),
migrations.AddField(
model_name="ticketsensitivedetails",
name="payment",
field=models.OneToOneField(
to="payment.Payment",
null=True, # Temporarily allow nulls for migration purposes
on_delete=models.CASCADE,
related_name="ticket_sensitive_details",
),
),
migrations.RunPython(
migrate_payment_tickets_generic_foreign_key_to_onetoone,
migrations.RunPython.noop,
),
]
22 changes: 17 additions & 5 deletions src/hct_mis_api/apps/grievance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
URGENCY_NOT_SET,
)
from hct_mis_api.apps.household.models import Individual
from hct_mis_api.apps.payment.models import PaymentRecord, PaymentVerification
from hct_mis_api.apps.payment.models import PaymentVerification, Payment
from hct_mis_api.apps.utils.models import (
AdminUrlMixin,
ConcurrencyModel,
Expand Down Expand Up @@ -628,7 +628,7 @@ class TicketNote(TimeStampedUUIDModel):
}


class TicketComplaintDetails(GenericPaymentTicket):
class TicketComplaintDetails(GenericPaymentTicket): # TODO TP drop GenericPaymentTicket
STATUS_FLOW = GENERAL_STATUS_FLOW

ticket = models.OneToOneField(
Expand All @@ -648,12 +648,18 @@ class TicketComplaintDetails(GenericPaymentTicket):
on_delete=models.CASCADE,
null=True,
)
payment = models.OneToOneField(
Payment,
on_delete=models.CASCADE,
null=True,
related_name="ticket_complaint_details",
)

class Meta:
verbose_name_plural = "Ticket Complaint Details"


class TicketSensitiveDetails(GenericPaymentTicket):
class TicketSensitiveDetails(GenericPaymentTicket): # TODO TP drop GenericPaymentTicket
STATUS_FLOW = GENERAL_STATUS_FLOW

ticket = models.OneToOneField(
Expand All @@ -673,6 +679,12 @@ class TicketSensitiveDetails(GenericPaymentTicket):
on_delete=models.CASCADE,
null=True,
)
payment = models.OneToOneField(
Payment,
on_delete=models.CASCADE,
null=True,
related_name="ticket_sensitive_details",
)

class Meta:
verbose_name_plural = "Ticket Sensitive Details"
Expand Down Expand Up @@ -955,9 +967,9 @@ def individual(self) -> Optional["Individual"]:
return getattr(self.payment_record, "head_of_household", None)

@property
def payment_record(self) -> Optional["PaymentRecord"]:
def payment_record(self) -> Optional["Payment"]:
# TODO: need to double check this property sometimes return null ???
return getattr(self.payment_verification, "payment_obj", None)
return getattr(self.payment_verification, "payment", None)

class Meta:
verbose_name_plural = "Ticket Payment Verification Details"
Expand Down
8 changes: 4 additions & 4 deletions src/hct_mis_api/apps/grievance/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ def resolve_individual(grievance_ticket: GrievanceTicket, info: Any) -> Optional
@staticmethod
def resolve_payment_record(grievance_ticket: GrievanceTicket, info: Any) -> Optional[Any]:
payment_verification = getattr(grievance_ticket.ticket_details, "payment_verification", None)
payment_obj = getattr(grievance_ticket.ticket_details, "payment_obj", None)
return getattr(payment_verification, "payment_obj", None) if payment_verification else payment_obj
payment_obj = getattr(grievance_ticket.ticket_details, "payment", None)
return getattr(payment_verification, "payment", None) if payment_verification else payment_obj

@staticmethod
def resolve_admin(grievance_ticket: GrievanceTicket, info: Any) -> Optional[str]:
Expand Down Expand Up @@ -247,7 +247,7 @@ class Meta:
connection_class = ExtendedConnection

def resolve_payment_record(self, info: Any) -> Optional[Any]:
return getattr(self, "payment_obj", None)
return getattr(self, "payment", None)


class TicketSensitiveDetailsNode(DjangoObjectType):
Expand All @@ -260,7 +260,7 @@ class Meta:
connection_class = ExtendedConnection

def resolve_payment_record(self, info: Any) -> Optional[Any]:
return getattr(self, "payment_obj", None)
return getattr(self, "payment", None)


class TicketIndividualDataUpdateDetailsNode(DjangoObjectType):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def update_payment_verification_service(
if payment_verification_details.new_status == PaymentVerification.STATUS_NOT_RECEIVED:
status = payment_verification_details.new_status
elif (
payment_verification.payment_obj
and payment_verification_details.new_received_amount == payment_verification.payment_obj.delivered_quantity
payment_verification.payment
and payment_verification_details.new_received_amount == payment_verification.payment.delivered_quantity
):
status = PaymentVerification.STATUS_RECEIVED
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def create_tickets_based_on_payment_records_service(
model.objects.create(
individual=individual,
household=household,
payment_obj=payment_record,
payment=payment_record,
ticket=ticket,
)
ticket.refresh_from_db()
Expand Down Expand Up @@ -88,9 +88,7 @@ def update_ticket_based_on_payment_record_service(
if payment_record_id := input_data.get("payment_record"):
node_name, obj_id = b64decode(payment_record_id).decode().split(":")

payment_record: Union["Payment", "PaymentRecord"] = get_object_or_404( # type: ignore
Payment if node_name == "PaymentNode" else PaymentRecord, id=obj_id
)
ticket_details.payment_obj = payment_record
payment_record: "Payment" = get_object_or_404(Payment, id=obj_id)
ticket_details.payment = payment_record
ticket_details.save()
return grievance_ticket
Loading

0 comments on commit c3f031f

Please sign in to comment.