diff --git a/Pipfile b/Pipfile index 02d1aaa9..6c028281 100644 --- a/Pipfile +++ b/Pipfile @@ -5,3 +5,4 @@ url = "https://pypi.python.org/simple" [packages] django = "*" djangorestframework = "*" +django-filter = "*" diff --git a/Pipfile.lock b/Pipfile.lock index 2590657f..8d6b5c62 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "cf364d603966857cd128557bc679634e0e6754c0ee8fe7d80af6b38d5e2dc745" + "sha256": "a0ef885217179eed1565af7ec6a732e8b141fb42c156f152380a65feac7cb1fe" }, "requires": {}, "sources": [ @@ -15,6 +15,9 @@ "django": { "version": "==1.11.4" }, + "django-filter": { + "version": "==1.0.4" + }, "djangorestframework": { "version": "==3.6.3" }, diff --git a/api/views.py b/api/views.py index eda0ff57..59a1dd07 100644 --- a/api/views.py +++ b/api/views.py @@ -14,26 +14,76 @@ class BabyViewSet(viewsets.ModelViewSet): queryset = Baby.objects.all() serializer_class = BabySerializer + def get_queryset(self): + queryset = Baby.objects.all() + + for param in ['first_name', 'last_name']: + value = self.request.query_params.get(param, None) + if value is not None: + queryset = queryset.filter(**{param: value}) + + return queryset + class DiaperChangeViewSet(viewsets.ModelViewSet): queryset = DiaperChange.objects.all() serializer_class = DiaperChangeSerializer + def get_queryset(self): + queryset = DiaperChange.objects.all() + + for param in ['baby__last_name', 'wet', 'solid', 'color']: + value = self.request.query_params.get(param, None) + if value is not None: + queryset = queryset.filter(**{param: value}) + + return queryset + class FeedingViewSet(viewsets.ModelViewSet): queryset = Feeding.objects.all() serializer_class = FeedingSerializer + def get_queryset(self): + queryset = Feeding.objects.all() + + for param in ['baby__last_name', 'type', 'method']: + value = self.request.query_params.get(param, None) + if value is not None: + queryset = queryset.filter(**{param: value}) + + return queryset + class SleepViewSet(viewsets.ModelViewSet): queryset = Sleep.objects.all() serializer_class = SleepSerializer + def get_queryset(self): + queryset = Sleep.objects.all() + + for param in ['baby__last_name']: + value = self.request.query_params.get(param, None) + if value is not None: + queryset = queryset.filter(**{param: value}) + + return queryset + class TummyTimeViewSet(viewsets.ModelViewSet): queryset = TummyTime.objects.all() serializer_class = TummyTimeSerializer + def get_queryset(self): + queryset = TummyTime.objects.all() + + for param in ['baby__last_name']: + value = self.request.query_params.get(param, None) + if value is not None: + queryset = queryset.filter(**{param: value}) + + return queryset + class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() diff --git a/core/admin.py b/core/admin.py index 2638375d..701415e9 100644 --- a/core/admin.py +++ b/core/admin.py @@ -9,28 +9,33 @@ from .models import Baby, DiaperChange, Feeding, Sleep, TummyTime @admin.register(Baby) class BabyAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'birth_date',) + list_filter = ('last_name',) search_fields = ('first_name', 'last_name', 'birth_date',) @admin.register(DiaperChange) class DiaperChangeAdmin(admin.ModelAdmin): list_display = ('baby', 'time', 'wet', 'solid', 'color') + list_filter = ('baby', 'wet', 'solid', 'color') search_fields = ('baby__first_name', 'baby__last_name',) @admin.register(Feeding) class FeedingAdmin(admin.ModelAdmin): list_display = ('start', 'end', 'duration', 'baby', 'type', 'method',) + list_filter = ('baby', 'type', 'method',) search_fields = ('baby__first_name', 'baby__last_name', 'type', 'method',) @admin.register(Sleep) class SleepAdmin(admin.ModelAdmin): list_display = ('start', 'end', 'duration', 'baby',) + list_filter = ('baby',) search_fields = ('baby__first_name', 'baby__last_name',) @admin.register(TummyTime) class TummyTimeAdmin(admin.ModelAdmin): list_display = ('start', 'end', 'duration', 'baby', 'milestone',) + list_filter = ('baby',) search_fields = ('baby__first_name', 'baby__last_name', 'milestone',)