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

Fixes to managers for mergable models #4528

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function ImportedHouseholdTableRow({
</TableCell>
<TableCell align="left">
<StyledLink onClick={() => handleClick()}>
{isMerged ? household.unicefId : household.importId}
{household.unicefId}
</StyledLink>
</TableCell>
<AnonTableCell>{household?.headOfHousehold?.fullName}</AnonTableCell>
Expand Down
7 changes: 5 additions & 2 deletions src/hct_mis_api/apps/account/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,12 @@ def check_node_permission(cls, info: Any, object_instance: Any) -> None:
raise PermissionDenied("Permission Denied")

@classmethod
def get_node(cls, info: Any, object_id: str) -> Optional[Model]:
def get_node(cls, info: Any, object_id: str, **kwargs: Any) -> Optional[Model]:
try:
object_instance = cls._meta.model.objects.get(pk=object_id)
if "get_object_queryset" in kwargs:
object_instance = kwargs.get("get_object_queryset").get(pk=object_id)
else:
object_instance = cls.get_queryset(cls._meta.model.objects, info).get(pk=object_id)
cls.check_node_permission(info, object_instance)
except cls._meta.model.DoesNotExist:
object_instance = None
Expand Down
32 changes: 21 additions & 11 deletions src/hct_mis_api/apps/household/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Case,
F,
Func,
Model,
OuterRef,
Prefetch,
Q,
Expand Down Expand Up @@ -264,13 +265,17 @@ class IndividualNode(BaseNodePermissionMixin, AdminUrlNodeMixin, DjangoObjectTyp
IndividualIdentityNode,
)

@classmethod
def get_node(cls, info: Any, object_id: str, **kwargs: Any) -> Optional[Model]:
return super().get_node(info, object_id, get_object_queryset=Individual.all_merge_status_objects)

@staticmethod
def resolve_documents(parent: Individual, info: Any) -> QuerySet[Document]:
return Document.objects.filter(pk__in=parent.documents.values("id"))
return parent.documents(manager="all_merge_status_objects")

@staticmethod
def resolve_identities(parent: Individual, info: Any) -> QuerySet[IndividualIdentity]:
return IndividualIdentity.objects.filter(pk__in=parent.identities.values("id"))
return parent.identities(manager="all_merge_status_objects")

@staticmethod
def resolve_import_id(parent: Individual, info: Any) -> str:
Expand All @@ -282,16 +287,16 @@ def resolve_preferred_language(parent: Individual, info: Any) -> Optional[str]:

@staticmethod
def resolve_payment_channels(parent: Individual, info: Any) -> QuerySet[BankAccountInfo]:
return BankAccountInfo.objects.filter(individual=parent).annotate(type=Value("BANK_TRANSFER"))
return BankAccountInfo.all_merge_status_objects.filter(individual=parent).annotate(type=Value("BANK_TRANSFER"))

def resolve_bank_account_info(parent, info: Any) -> Optional[BankAccountInfo]:
bank_account_info = parent.bank_account_info.first()
bank_account_info = parent.bank_account_info(manager="all_merge_status_objects").first() # type: ignore
if bank_account_info:
return bank_account_info
return None

def resolve_role(parent, info: Any) -> str:
role = parent.households_and_roles.first()
role = parent.households_and_roles(manager="all_merge_status_objects").first()
if role is not None:
return role.role
return ROLE_NO_ROLE
Expand Down Expand Up @@ -481,13 +486,14 @@ def resolve_selection(parent: Household, info: Any) -> HouseholdSelection:

@staticmethod
def resolve_individuals(parent: Household, info: Any, *arg: Any, **kwargs: Any) -> QuerySet:
individuals_ids = list(parent.individuals.values_list("id", flat=True))
collectors_ids = list(parent.representatives.values_list("id", flat=True))
individuals_ids = list(parent.individuals(manager="all_merge_status_objects").values_list("id", flat=True))

collectors_ids = list(parent.representatives(manager="all_merge_status_objects").values_list("id", flat=True))
ids = list(set(individuals_ids + collectors_ids))
return Individual.objects.filter(id__in=ids).prefetch_related(
return Individual.all_merge_status_objects.filter(id__in=ids).prefetch_related(
Prefetch(
"households_and_roles",
queryset=IndividualRoleInHousehold.objects.filter(household=parent.id),
queryset=IndividualRoleInHousehold.all_merge_status_objects.filter(household=parent.id),
)
)

Expand Down Expand Up @@ -578,6 +584,10 @@ def get_queryset(cls, queryset: QuerySet[Household], info: Any) -> QuerySet[Hous
qs = super().get_queryset(queryset, info)
return qs

@classmethod
def get_node(cls, info: Any, object_id: str, **kwargs: Any) -> Optional[Model]:
return super().get_node(info, object_id, get_object_queryset=Household.all_merge_status_objects)

class Meta:
model = Household
filter_fields = []
Expand Down Expand Up @@ -706,7 +716,7 @@ def resolve_all_individuals(self, info: Any, **kwargs: Any) -> QuerySet[Individu
if program and program.status == Program.DRAFT:
return Individual.objects.none()

queryset = Individual.objects.all()
queryset = Individual.all_merge_status_objects.all()
if does_path_exist_in_query("edges.node.household", info):
queryset = queryset.select_related("household")
if does_path_exist_in_query("edges.node.household.admin2", info):
Expand Down Expand Up @@ -762,7 +772,7 @@ def resolve_all_households(self, info: Any, **kwargs: Any) -> QuerySet:
if program and program.status == Program.DRAFT:
return Household.objects.none()

queryset = Household.objects.all()
queryset = Household.all_merge_status_objects.all()

if not user.partner.is_unicef: # Unicef partner has full access to all AdminAreas
business_area_id = BusinessArea.objects.get(slug=business_area_slug).id
Expand Down
4 changes: 1 addition & 3 deletions src/hct_mis_api/apps/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ class SoftDeletableRepresentationMergeStatusModel(MergeStatusModel):
class Meta:
abstract = True

# objects = SoftDeletableRepresentationMergedManager(_emit_deprecation_warnings=True)
# now we use 'rdi_merge_status' field for filtering
objects = SoftDeletableRepresentationManager()
objects = SoftDeletableRepresentationMergedManager(_emit_deprecation_warnings=True)
all_merge_status_objects = SoftDeletableRepresentationManager()
available_objects = SoftDeletableRepresentationMergedManager()
all_objects = models.Manager()
Expand Down
Loading