Skip to content

Commit e1148dc

Browse files
fixup! ✨(backend) add agreement batch order viewset and serializer
1 parent c5bae9e commit e1148dc

File tree

6 files changed

+45
-52
lines changed

6 files changed

+45
-52
lines changed

src/backend/joanie/core/api/client/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,16 +1661,16 @@ class GenericAgreementViewSet(
16611661
filterset_class = filters.AgreementFilter
16621662
ordering = ["-student_signed_on", "-created_on"]
16631663
queryset = (
1664-
models.Contract.objects.exclude(batch_orders__state=enums.ORDER_STATE_CANCELED)
1664+
models.Contract.objects.exclude(batch_order__state=enums.ORDER_STATE_CANCELED)
16651665
.select_related(
16661666
"definition",
16671667
"organization_signatory",
16681668
)
16691669
.prefetch_related(
1670-
"batch_orders__organization",
1671-
"batch_orders__offering__course",
1672-
"batch_orders__owner",
1673-
"batch_orders__offering__product",
1670+
"batch_order__organization",
1671+
"batch_order__offering__course",
1672+
"batch_order__owner",
1673+
"batch_order__offering__product",
16741674
)
16751675
)
16761676

@@ -1693,7 +1693,7 @@ class NestedOrganizationAgreementViewSet(NestedGenericViewSet, GenericAgreementV
16931693
offering id.
16941694
"""
16951695

1696-
lookup_fields = ["batch_orders__organization__pk", "pk"]
1696+
lookup_fields = ["batch_order__organization__pk", "pk"]
16971697
lookup_url_kwargs = ["organization_id", "pk"]
16981698

16991699
def _lookup_by_organization_code_or_pk(self):
@@ -1704,7 +1704,7 @@ def _lookup_by_organization_code_or_pk(self):
17041704
try:
17051705
uuid.UUID(self.kwargs["organization_id"])
17061706
except ValueError:
1707-
self.lookup_fields[0] = "batch_orders__organization__code__iexact"
1707+
self.lookup_fields[0] = "batch_order__organization__code__iexact"
17081708

17091709
def initial(self, request, *args, **kwargs):
17101710
"""
@@ -1726,7 +1726,7 @@ def get_queryset(self):
17261726
)
17271727

17281728
additional_filter = {
1729-
"batch_orders__organization__accesses__user__username": username,
1729+
"batch_order__organization__accesses__user__username": username,
17301730
}
17311731

17321732
return queryset.filter(**additional_filter)

src/backend/joanie/core/filters/client/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class AgreementFilter(filters.FilterSet):
184184
method="filter_signature_state",
185185
choices=enums.CONTRACT_SIGNATURE_STATE_FILTER_CHOICES,
186186
)
187-
organization_id = filters.UUIDFilter(field_name="batch_orders__organization__id")
187+
organization_id = filters.UUIDFilter(field_name="batch_order__organization__id")
188188
offering_id = filters.UUIDFilter(method="filter_offering_id")
189189

190190
class Meta:
@@ -219,11 +219,11 @@ def filter_offering_id(self, queryset, _name, value):
219219
return queryset.none()
220220

221221
return queryset.filter(
222-
batch_orders__relation__course_id=offering.course_id,
223-
batch_orders__relation__product_id=offering.product_id,
224-
batch_orders__organization__in=offering.organizations.only(
225-
"pk"
226-
).values_list("pk", flat=True),
222+
batch_order__relation__course_id=offering.course_id,
223+
batch_order__relation__product_id=offering.product_id,
224+
batch_order__organization__in=offering.organizations.only("pk").values_list(
225+
"pk", flat=True
226+
),
227227
)
228228

229229

src/backend/joanie/core/serializers/client.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ def get_total_currency(self, *args, **kwargs) -> str:
549549
class AgreementBatchOrderSerializer(AbilitiesModelSerializer):
550550
"""Small serializer for Contracts models related to Batch Orders (agreements)"""
551551

552-
batch_order = serializers.SerializerMethodField(read_only=True)
552+
batch_order = BatchOrderLightSerializer(read_only=True)
553553

554554
class Meta:
555555
model = models.Contract
@@ -559,13 +559,6 @@ class Meta:
559559
]
560560
read_only_fields = fields
561561

562-
def get_batch_order(self, instance):
563-
"""
564-
Returns batch order's information through the related name of the contract model
565-
"""
566-
batch_order = instance.batch_orders.first()
567-
return BatchOrderLightSerializer(batch_order, context=self.context).data
568-
569562

570563
class QuoteDefinitionSerializer(serializers.ModelSerializer):
571564
"""Read only serializer for QuoteDefinition model."""

src/backend/joanie/tests/core/api/organizations/test_api_organizations_agreements.perf.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
OrganizationAgreementApiTest.test_api_organizations_agreement_list_by_signature_state:
22
- db: SELECT DISTINCT "joanie_contract"."id" FROM "joanie_contract" ORDER BY "joanie_contract"."id" ASC
3-
- db: 'SELECT COUNT(*) AS "__count" FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."id" = "joanie_batch_order"."contract_id") INNER JOIN "joanie_batch_order" T4 ON ("joanie_contract"."id" = T4."contract_id") INNER JOIN "joanie_organization" T5 ON (T4."organization_id" = T5."id") INNER JOIN "joanie_organization_access" ON (T5."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") WHERE (NOT (EXISTS(SELECT # AS "a" FROM "joanie_batch_order" U1 WHERE (U1."state" = # AND U1."contract_id" = ("joanie_contract"."id")) LIMIT #)) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND "joanie_contract"."organization_signed_on" IS # AND "joanie_contract"."student_signed_on" IS #)'
4-
- db: 'SELECT ... FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."id" = "joanie_batch_order"."contract_id") INNER JOIN "joanie_batch_order" T4 ON ("joanie_contract"."id" = T4."contract_id") INNER JOIN "joanie_organization" T5 ON (T4."organization_id" = T5."id") INNER JOIN "joanie_organization_access" ON (T5."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_contract_definition" ON ("joanie_contract"."definition_id" = "joanie_contract_definition"."id") LEFT OUTER JOIN "joanie_user" T9 ON ("joanie_contract"."organization_signatory_id" = T9."id") WHERE (NOT (EXISTS(SELECT # AS "a" FROM "joanie_batch_order" U1 WHERE (U1."state" = # AND U1."contract_id" = ("joanie_contract"."id")) LIMIT #)) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND "joanie_contract"."organization_signed_on" IS # AND "joanie_contract"."student_signed_on" IS #) ORDER BY "joanie_contract"."created_on" DESC LIMIT #'
5-
- db: SELECT ... FROM "joanie_batch_order" WHERE "joanie_batch_order"."contract_id" IN (#::uuid) ORDER BY "joanie_batch_order"."created_on" ASC
3+
- db: 'SELECT COUNT(*) AS "__count" FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."batch_order_id" = "joanie_batch_order"."id") INNER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") INNER JOIN "joanie_organization_access" ON ("joanie_organization"."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") WHERE (NOT ("joanie_batch_order"."state" = # AND "joanie_batch_order"."state" IS NOT NULL) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND "joanie_contract"."organization_signed_on" IS # AND "joanie_contract"."student_signed_on" IS #)'
4+
- db: 'SELECT ... FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."batch_order_id" = "joanie_batch_order"."id") INNER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") INNER JOIN "joanie_organization_access" ON ("joanie_organization"."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_contract_definition" ON ("joanie_contract"."definition_id" = "joanie_contract_definition"."id") LEFT OUTER JOIN "joanie_user" T7 ON ("joanie_contract"."organization_signatory_id" = T7."id") WHERE (NOT ("joanie_batch_order"."state" = # AND "joanie_batch_order"."state" IS NOT NULL) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND "joanie_contract"."organization_signed_on" IS # AND "joanie_contract"."student_signed_on" IS #) ORDER BY "joanie_contract"."created_on" DESC LIMIT #'
5+
- db: SELECT ... FROM "joanie_batch_order" WHERE "joanie_batch_order"."id" IN (#::uuid) ORDER BY "joanie_batch_order"."created_on" ASC
66
- db: SELECT ... FROM "joanie_organization" WHERE "joanie_organization"."id" IN (#::uuid) ORDER BY "joanie_organization"."created_on" DESC
77
- db: 'SELECT ... FROM "joanie_course_product_relation" WHERE "joanie_course_product_relation"."id" = #::uuid LIMIT #'
88
- db: SELECT ... FROM "joanie_course" WHERE "joanie_course"."id" IN (#::uuid) ORDER BY "joanie_course"."code" ASC
@@ -126,9 +126,9 @@ OrganizationAgreementApiTest.test_api_organizations_agreement_retrieve_with_acce
126126
OrganizationAgreementApiTest.test_api_organizations_agreements_list_by_offering:
127127
- db: SELECT DISTINCT "joanie_contract"."id" FROM "joanie_contract" ORDER BY "joanie_contract"."id" ASC
128128
- db: 'SELECT ... FROM "joanie_course_product_relation" INNER JOIN "joanie_course_product_relation_organizations" ON ("joanie_course_product_relation"."id" = "joanie_course_product_relation_organizations"."courseproductrelation_id") WHERE ("joanie_course_product_relation"."id" = #::uuid AND "joanie_course_product_relation_organizations"."organization_id" IN (#::uuid)) LIMIT #'
129-
- db: 'SELECT COUNT(*) AS "__count" FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."id" = "joanie_batch_order"."contract_id") INNER JOIN "joanie_batch_order" T4 ON ("joanie_contract"."id" = T4."contract_id") INNER JOIN "joanie_organization" T5 ON (T4."organization_id" = T5."id") INNER JOIN "joanie_organization_access" ON (T5."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_batch_order" T8 ON ("joanie_contract"."id" = T8."contract_id") INNER JOIN "joanie_course_product_relation" ON (T8."relation_id" = "joanie_course_product_relation"."id") WHERE (NOT (EXISTS(SELECT # AS "a" FROM "joanie_batch_order" U1 WHERE (U1."state" = # AND U1."contract_id" = ("joanie_contract"."id")) LIMIT #)) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND T8."organization_id" IN (SELECT U0."id" FROM "joanie_organization" U0 INNER JOIN "joanie_course_product_relation_organizations" U1 ON (U0."id" = U1."organization_id") WHERE U1."courseproductrelation_id" = #::uuid) AND "joanie_course_product_relation"."course_id" = #::uuid AND "joanie_course_product_relation"."product_id" = #::uuid)'
130-
- db: 'SELECT ... FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."id" = "joanie_batch_order"."contract_id") INNER JOIN "joanie_batch_order" T4 ON ("joanie_contract"."id" = T4."contract_id") INNER JOIN "joanie_organization" T5 ON (T4."organization_id" = T5."id") INNER JOIN "joanie_organization_access" ON (T5."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_batch_order" T8 ON ("joanie_contract"."id" = T8."contract_id") INNER JOIN "joanie_course_product_relation" ON (T8."relation_id" = "joanie_course_product_relation"."id") INNER JOIN "joanie_contract_definition" ON ("joanie_contract"."definition_id" = "joanie_contract_definition"."id") LEFT OUTER JOIN "joanie_user" T14 ON ("joanie_contract"."organization_signatory_id" = T14."id") WHERE (NOT (EXISTS(SELECT # AS "a" FROM "joanie_batch_order" U1 WHERE (U1."state" = # AND U1."contract_id" = ("joanie_contract"."id")) LIMIT #)) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND T8."organization_id" IN (SELECT U0."id" FROM "joanie_organization" U0 INNER JOIN "joanie_course_product_relation_organizations" U1 ON (U0."id" = U1."organization_id") WHERE U1."courseproductrelation_id" = #::uuid) AND "joanie_course_product_relation"."course_id" = #::uuid AND "joanie_course_product_relation"."product_id" = #::uuid) ORDER BY "joanie_contract"."created_on" DESC LIMIT #'
131-
- db: SELECT ... FROM "joanie_batch_order" WHERE "joanie_batch_order"."contract_id" IN (#::uuid) ORDER BY "joanie_batch_order"."created_on" ASC
129+
- db: 'SELECT COUNT(*) AS "__count" FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."batch_order_id" = "joanie_batch_order"."id") INNER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") INNER JOIN "joanie_organization_access" ON ("joanie_organization"."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_course_product_relation" ON ("joanie_batch_order"."relation_id" = "joanie_course_product_relation"."id") WHERE (NOT ("joanie_batch_order"."state" = # AND "joanie_batch_order"."state" IS NOT NULL) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND "joanie_batch_order"."organization_id" IN (SELECT U0."id" FROM "joanie_organization" U0 INNER JOIN "joanie_course_product_relation_organizations" U1 ON (U0."id" = U1."organization_id") WHERE U1."courseproductrelation_id" = #::uuid) AND "joanie_course_product_relation"."course_id" = #::uuid AND "joanie_course_product_relation"."product_id" = #::uuid)'
130+
- db: 'SELECT ... FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."batch_order_id" = "joanie_batch_order"."id") INNER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") INNER JOIN "joanie_organization_access" ON ("joanie_organization"."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_course_product_relation" ON ("joanie_batch_order"."relation_id" = "joanie_course_product_relation"."id") INNER JOIN "joanie_contract_definition" ON ("joanie_contract"."definition_id" = "joanie_contract_definition"."id") LEFT OUTER JOIN "joanie_user" T10 ON ("joanie_contract"."organization_signatory_id" = T10."id") WHERE (NOT ("joanie_batch_order"."state" = # AND "joanie_batch_order"."state" IS NOT NULL) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = # AND "joanie_batch_order"."organization_id" IN (SELECT U0."id" FROM "joanie_organization" U0 INNER JOIN "joanie_course_product_relation_organizations" U1 ON (U0."id" = U1."organization_id") WHERE U1."courseproductrelation_id" = #::uuid) AND "joanie_course_product_relation"."course_id" = #::uuid AND "joanie_course_product_relation"."product_id" = #::uuid) ORDER BY "joanie_contract"."created_on" DESC LIMIT #'
131+
- db: SELECT ... FROM "joanie_batch_order" WHERE "joanie_batch_order"."id" IN (#::uuid) ORDER BY "joanie_batch_order"."created_on" ASC
132132
- db: SELECT ... FROM "joanie_organization" WHERE "joanie_organization"."id" IN (#::uuid) ORDER BY "joanie_organization"."created_on" DESC
133133
- db: 'SELECT ... FROM "joanie_course_product_relation" WHERE "joanie_course_product_relation"."id" = #::uuid LIMIT #'
134134
- db: SELECT ... FROM "joanie_course" WHERE "joanie_course"."id" IN (#::uuid) ORDER BY "joanie_course"."code" ASC
@@ -355,9 +355,9 @@ OrganizationAgreementApiTest.test_api_organizations_agreements_list_filter_by_wi
355355
- db: 'SELECT "joanie_organization_access"."role" FROM "joanie_organization_access" WHERE ("joanie_organization_access"."organization_id" = #::uuid AND "joanie_organization_access"."user_id" = #::uuid) ORDER BY "joanie_organization_access"."created_on" DESC LIMIT #'
356356
OrganizationAgreementApiTest.test_api_organizations_agreements_list_with_accesses:
357357
- db: SELECT DISTINCT "joanie_contract"."id" FROM "joanie_contract" ORDER BY "joanie_contract"."id" ASC
358-
- db: 'SELECT COUNT(*) AS "__count" FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."id" = "joanie_batch_order"."contract_id") INNER JOIN "joanie_batch_order" T4 ON ("joanie_contract"."id" = T4."contract_id") INNER JOIN "joanie_organization" T5 ON (T4."organization_id" = T5."id") INNER JOIN "joanie_organization_access" ON (T5."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") WHERE (NOT (EXISTS(SELECT # AS "a" FROM "joanie_batch_order" U1 WHERE (U1."state" = # AND U1."contract_id" = ("joanie_contract"."id")) LIMIT #)) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = #)'
359-
- db: 'SELECT ... FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."id" = "joanie_batch_order"."contract_id") INNER JOIN "joanie_batch_order" T4 ON ("joanie_contract"."id" = T4."contract_id") INNER JOIN "joanie_organization" T5 ON (T4."organization_id" = T5."id") INNER JOIN "joanie_organization_access" ON (T5."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_contract_definition" ON ("joanie_contract"."definition_id" = "joanie_contract_definition"."id") LEFT OUTER JOIN "joanie_user" T9 ON ("joanie_contract"."organization_signatory_id" = T9."id") WHERE (NOT (EXISTS(SELECT # AS "a" FROM "joanie_batch_order" U1 WHERE (U1."state" = # AND U1."contract_id" = ("joanie_contract"."id")) LIMIT #)) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = #) ORDER BY "joanie_contract"."created_on" DESC LIMIT #'
360-
- db: SELECT ... FROM "joanie_batch_order" WHERE "joanie_batch_order"."contract_id" IN (...) ORDER BY "joanie_batch_order"."created_on" ASC
358+
- db: 'SELECT COUNT(*) AS "__count" FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."batch_order_id" = "joanie_batch_order"."id") INNER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") INNER JOIN "joanie_organization_access" ON ("joanie_organization"."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") WHERE (NOT ("joanie_batch_order"."state" = # AND "joanie_batch_order"."state" IS NOT NULL) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = #)'
359+
- db: 'SELECT ... FROM "joanie_contract" INNER JOIN "joanie_batch_order" ON ("joanie_contract"."batch_order_id" = "joanie_batch_order"."id") INNER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") INNER JOIN "joanie_organization_access" ON ("joanie_organization"."id" = "joanie_organization_access"."organization_id") INNER JOIN "joanie_user" ON ("joanie_organization_access"."user_id" = "joanie_user"."id") INNER JOIN "joanie_contract_definition" ON ("joanie_contract"."definition_id" = "joanie_contract_definition"."id") LEFT OUTER JOIN "joanie_user" T7 ON ("joanie_contract"."organization_signatory_id" = T7."id") WHERE (NOT ("joanie_batch_order"."state" = # AND "joanie_batch_order"."state" IS NOT NULL) AND "joanie_batch_order"."organization_id" = #::uuid AND "joanie_user"."username" = #) ORDER BY "joanie_contract"."created_on" DESC LIMIT #'
360+
- db: SELECT ... FROM "joanie_batch_order" WHERE "joanie_batch_order"."id" IN (...) ORDER BY "joanie_batch_order"."created_on" ASC
361361
- db: SELECT ... FROM "joanie_organization" WHERE "joanie_organization"."id" IN (#::uuid) ORDER BY "joanie_organization"."created_on" DESC
362362
- db: 'SELECT ... FROM "joanie_course_product_relation" WHERE "joanie_course_product_relation"."id" = #::uuid LIMIT #'
363363
- db: 'SELECT ... FROM "joanie_course_product_relation" WHERE "joanie_course_product_relation"."id" = #::uuid LIMIT #'

0 commit comments

Comments
 (0)