-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: staging
Are you sure you want to change the base?
Changes from 3 commits
e2467d1
60a4a0b
469dfe4
cd8b85a
111a86d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
from rest_framework.generics import ListAPIView, RetrieveAPIView | ||
from rest_framework.pagination import LimitOffsetPagination | ||
|
||
from api.serializers import WasteActionSerializer | ||
from api.serializers import WasteActionSerializer, WasteActionWithActionsSerializer | ||
from data.models import WasteAction | ||
|
||
from .utils import UnaccentSearchFilter | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok je crois que j'ai trouvé une solution avec There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. finally : cd8b85a |
||
kwargs["context"] = self.get_serializer_context() | ||
kwargs["context"]["user"] = self.request.user | ||
return WasteActionWithActionsSerializer(*args, **kwargs) | ||
return super().get_serializer(*args, **kwargs) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,19 @@ | |
from .wasteaction import WasteAction | ||
|
||
|
||
class ResourceActionQuerySet(models.QuerySet): | ||
def for_user(self, user): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. si vous trouvez un meilleur nom de There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renommé ici : 111a86d |
||
return self.filter(canteen__in=user.canteens.all()) | ||
|
||
|
||
class ResourceAction(models.Model): | ||
class Meta: | ||
unique_together = ("resource", "canteen") | ||
verbose_name = "ressource : action (cantine)" | ||
verbose_name_plural = "ressources : actions (cantines)" | ||
|
||
objects = models.Manager.from_queryset(ResourceActionQuerySet)() | ||
|
||
creation_date = models.DateTimeField(auto_now_add=True) | ||
modification_date = models.DateTimeField(auto_now=True) | ||
history = HistoricalRecords() | ||
|
There was a problem hiding this comment.
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
etcanteen: {id}
?There was a problem hiding this comment.
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 ^^