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

API: Récupérer des WasteActions avec leur ResourceActions (si l'utilisateur est connecté) #4544

Open
wants to merge 5 commits into
base: staging
Choose a base branch
from

Conversation

raphodn
Copy link
Member

@raphodn raphodn commented Oct 10, 2024

Quoi ?

Suite de #4500
On améliore l'endpoint API existant wasteActions/<int:pk>, en rajoutant un champ actions si l'utilisateur est connecté.
Ce champ contient une liste des ResourceAction existantes pour les cantines auxquelles l'utilisateur appartient.

Pour cela

  • on créé 2 nouveaux serializers : WasteActionWithActionsSerializer & ResourceActionWithCanteenSerializer
  • on a besoin de passer le user au serializer pour changer la queryset dynamiquement
  • nouveau Model queryset ResourceAction.for_user(user) pour simplifier un peu

@raphodn raphodn force-pushed the raphodn/backend-resource-action-create branch from e536a78 to 3f9d008 Compare October 10, 2024 15:11
@raphodn raphodn force-pushed the raphodn/backend-resource-get-with-actions branch from 69a04c9 to 5261f51 Compare October 10, 2024 15:17
Base automatically changed from raphodn/backend-resource-action-create to staging October 10, 2024 15:29
@raphodn raphodn force-pushed the raphodn/backend-resource-get-with-actions branch from 5261f51 to b38801c Compare October 10, 2024 15:30
@raphodn raphodn force-pushed the raphodn/backend-resource-get-with-actions branch from b38801c to 469dfe4 Compare October 10, 2024 15:33
@raphodn raphodn marked this pull request as ready for review October 10, 2024 15:34
@@ -39,3 +39,10 @@ class WasteActionView(RetrieveAPIView):
model = WasteAction
queryset = WasteAction.objects.all()
serializer_class = WasteActionSerializer

def get_serializer(self, *args, **kwargs):
if self.request.user.is_authenticated:
Copy link
Member Author

Choose a reason for hiding this comment

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

vu qu'on a besoin de passer le user au serializer, je n'ai pas trouvé mieux que les kwargs (j'ai vu que c'était déjà utilisé ailleurs (canteen, "action"))

Copy link
Collaborator

Choose a reason for hiding this comment

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

est-ce que c'est possible de passer actions directe ? C'est-à-dire mettre la logique de user et queryset qui est maintenant dans le serializer ici ?

Copy link
Member Author

@raphodn raphodn Oct 11, 2024

Choose a reason for hiding this comment

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

j'ai essayé mais je n'arrive pas à filtrer le queryset de la FK.
ici dans le vue ca permet seulement de filtrer le queryset de WasteAction, pas ResourceAction.

Copy link
Member Author

Choose a reason for hiding this comment

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

mais en fait ca bloque sur le fait que je n'arrive pas à passer un queryset custom à serializer custom 🤔
le queryset custom dans WasteActionWithActionsSerializer pour le champ actions est ignoré..

Copy link
Member Author

Choose a reason for hiding this comment

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

ok je crois que j'ai trouvé une solution avec SerializerMethodField

Copy link
Member Author

Choose a reason for hiding this comment

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

finally : cd8b85a

@@ -5,12 +5,19 @@
from .wasteaction import WasteAction


class ResourceActionQuerySet(models.QuerySet):
def for_user(self, user):
Copy link
Member Author

Choose a reason for hiding this comment

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

si vous trouvez un meilleur nom de for_user let me know ^^

Copy link
Collaborator

Choose a reason for hiding this comment

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

for_managed_canteens/for_user_canteens ?

Copy link
Member Author

Choose a reason for hiding this comment

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

renommé ici : 111a86d

self.assertTrue("actions" in response.data)
self.assertEqual(len(response.data["actions"]), 1)
self.assertEqual(response.data["actions"][0]["canteen_id"], self.canteen.id)
self.assertEqual(response.data["actions"][0]["canteen"]["id"], self.canteen.id)
Copy link
Collaborator

Choose a reason for hiding this comment

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

pourquoi avoir les deux canteen_id et canteen: {id} ?

Copy link
Member Author

Choose a reason for hiding this comment

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

le serializer ResourceActionWithCanteenSerializer hérite de ResourceActionSerializer du coup il a les deux (l'id, et l'objet). ca ne me choque pas ^^

@raphodn raphodn force-pushed the raphodn/backend-resource-get-with-actions branch from 7d0dd34 to cd8b85a Compare October 11, 2024 15:07
@raphodn raphodn requested a review from hfroot October 11, 2024 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants