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

Hot fix : améliorer la perf de la page "mes cantines" #3955

Merged
merged 1 commit into from
May 30, 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
1 change: 1 addition & 0 deletions api/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SatelliteTeledeclarationSerializer,
ElectedCanteenSerializer,
MinimalCanteenSerializer,
CanteenSummarySerializer,
)
from .diagnostic import ( # noqa: F401
ManagerDiagnosticSerializer,
Expand Down
56 changes: 56 additions & 0 deletions api/serializers/canteen.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,62 @@ def get_central_kitchen_diagnostics(self, obj):
return None


class CanteenSummarySerializer(serializers.ModelSerializer):
images = MediaListSerializer(child=CanteenImageSerializer(), required=False)
diagnostics = FullDiagnosticSerializer(many=True, read_only=True, source="diagnostic_set")
central_kitchen_diagnostics = serializers.SerializerMethodField(read_only=True)

class Meta:
model = Canteen
my_fields = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
my_fields = (
read_only_fields = (
"id",
"name",
"city",
"city_insee_code",
"postal_code",
"sectors",
"daily_meal_count",
"yearly_meal_count",
"siret",
"management_type",
"production_type",
"department",
"region",
"publication_status",
"economic_model",
"is_central_cuisine",
"modification_date",
# the following can still be improved
"images", # can return the first image only
"diagnostics",
"central_kitchen_diagnostics", # can return a TD status instead of diagnostics
)

"id",
"name",
"city",
"city_insee_code",
"postal_code",
"sectors",
"daily_meal_count",
"yearly_meal_count",
"siret",
"management_type",
"production_type",
"department",
"region",
"publication_status",
"economic_model",
"is_central_cuisine",
"modification_date",
# the following can still be improved
"images", # can return the first image only
"diagnostics",
"central_kitchen_diagnostics", # can return a TD status instead of diagnostics
)
fields = my_fields
read_only_fields = my_fields
Copy link
Collaborator

@qloridant qloridant May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On ne peut pas directement écrire read_only_fields = (... ) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comme

fields = ("id", "name",...)
read_only_fields = ("id", "name",...)

?
Oui on peut, je n'aime pas la repetition


def get_central_kitchen_diagnostics(self, obj):
# Ideally we would also check the status of the satellite canteen and
# the central cuisine, for now we omit this check. For now it is the
# responsibility of the frontend to use this information.
if not obj.central_producer_siret or not obj.production_type == Canteen.ProductionType.ON_SITE_CENTRAL:
return None
try:
diagnostics = Diagnostic.objects.filter(
canteen__siret=obj.central_producer_siret,
central_kitchen_diagnostic_mode__in=[
Diagnostic.CentralKitchenDiagnosticMode.ALL,
Diagnostic.CentralKitchenDiagnosticMode.APPRO,
],
)

return CentralKitchenDiagnosticSerializer(diagnostics, many=True).data
except Canteen.DoesNotExist:
return None
except Canteen.MultipleObjectsReturned as e:
logger.exception(f"Multiple canteens returned when obtaining the central_producer_siret field {e}")
return None


class CanteenPreviewSerializer(serializers.ModelSerializer):
class Meta:
model = Canteen
Expand Down
3 changes: 2 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from api.views import ImportSimpleDiagnosticsView, ImportCompleteDiagnosticsView
from api.views import TeledeclarationCreateView, TeledeclarationCancelView, TeledeclarationPdfView
from api.views import PublishCanteenView, UnpublishCanteenView, SendCanteenNotFoundEmail
from api.views import UserCanteenPreviews, CanteenLocationsView
from api.views import UserCanteenPreviews, UserCanteenSummaries, CanteenLocationsView
from api.views import PartnerView, PartnersView, PartnerTypeListView
from api.views import ReservationExpeView, PurchaseListExportView, PurchaseOptionsView, ImportPurchasesView
from api.views import MessageCreateView, VegetarianExpeView, TeamJoinRequestView
Expand All @@ -58,6 +58,7 @@
),
path("publish/", PublishManyCanteensView.as_view(), name="publish_canteens"),
path("canteenPreviews/", UserCanteenPreviews.as_view(), name="user_canteen_previews"),
path("canteenSummaries/", UserCanteenSummaries.as_view(), name="user_canteens_summaries"),
path("canteens/", UserCanteensView.as_view(), name="user_canteens"),
path("canteens/<int:pk>", RetrieveUpdateUserCanteenView.as_view(), name="single_canteen"),
path(
Expand Down
1 change: 1 addition & 0 deletions api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ActionableCanteenRetrieveView,
CanteenStatusView,
TerritoryCanteensListView,
UserCanteenSummaries,
)
from .diagnostic import ( # noqa: F401
DiagnosticCreateView,
Expand Down
20 changes: 20 additions & 0 deletions api/views/canteen.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
CanteenStatusSerializer,
ElectedCanteenSerializer,
MinimalCanteenSerializer,
CanteenSummarySerializer,
)
from data.models import Canteen, ManagerInvitation, Sector, Diagnostic, Teledeclaration, Purchase
from data.region_choices import Region
Expand Down Expand Up @@ -377,6 +378,25 @@ def get_queryset(self):
return self.request.user.canteens.all()


class UserCanteenSummaries(ListAPIView):
model = Canteen
serializer_class = CanteenSummarySerializer
permission_classes = [IsAuthenticatedOrTokenHasResourceScope]
required_scopes = ["canteen"]
pagination_class = UserCanteensPagination
filter_backends = [
django_filters.DjangoFilterBackend,
UnaccentSearchFilter,
MaCantineOrderingFilter,
]
filterset_class = UserCanteensFilterSet
search_fields = ["name", "siret"]
ordering_fields = ["name", "creation_date", "modification_date", "daily_meal_count"]

def get_queryset(self):
return self.request.user.canteens.all()


@extend_schema_view(
get=extend_schema(
summary="Obtenir les détails d'une cantine.",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/ManagementPage/CanteensPagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default {
if (this.filterProductionType !== "all") queryParam += `&production_type=${this.filterProductionType}`
this.inProgress = true

return fetch(`/api/v1/canteens/?${queryParam}`)
return fetch(`/api/v1/canteenSummaries/?${queryParam}`)
.then((response) => {
if (response.status < 200 || response.status >= 400) throw new Error(`Error encountered : ${response}`)
return response.json()
Expand Down
Loading