From 0413dbdb886f1152f28074fb1d36d078da885ee9 Mon Sep 17 00:00:00 2001 From: Durval Carvalho Date: Tue, 19 Nov 2019 11:39:44 -0300 Subject: [PATCH 1/3] #188 Add route to harvests of specific month Co-authored-by: Leonardo da Silva Gomes --- src/acacia/urls.py | 8 +++++++- src/harvest/viewsets.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/acacia/urls.py b/src/acacia/urls.py index 8287cd0..c1ada09 100644 --- a/src/acacia/urls.py +++ b/src/acacia/urls.py @@ -1,7 +1,7 @@ from django.contrib import admin from django.urls import path, include from .helpers import list_all_endpoints -from harvest.viewsets import WeekHarvests +from harvest.viewsets import WeekHarvests, MonthlyHarvests urlpatterns = [ @@ -25,6 +25,12 @@ WeekHarvests.as_view({'get': 'list'}), name='weekly_harvests' ), + + path( + 'monthly_harvest///', + MonthlyHarvests.as_view({'get': 'list'}), + name='monthly_harvest' + ), ] urlpatterns = list_all_endpoints(urlpatterns) diff --git a/src/harvest/viewsets.py b/src/harvest/viewsets.py index 70ba824..1635872 100644 --- a/src/harvest/viewsets.py +++ b/src/harvest/viewsets.py @@ -7,6 +7,7 @@ from . import serializers import datetime +import calendar class HarvestViewSet(viewsets.ModelViewSet): @@ -74,3 +75,26 @@ def get_queryset(self): ) return queryset + + +class MonthlyHarvests(ListModelMixin, viewsets.GenericViewSet): + + queryset = Harvest.objects.all() + serializer_class = serializers.HarvestSerializer + permission_classes = (permissions.AllowAny,) + + def get_queryset(self): + + month = self.kwargs['month'] + year = self.kwargs['year'] + + _, last_day = calendar.monthrange(year, month) + + start_date = datetime.datetime(year, month, 1) + end_date = datetime.datetime(year, month, last_day) + + queryset = Harvest.objects.filter( + date__range=(start_date, end_date) + ) + + return queryset From 9385aaf012bfdf3f959789ea617f047268699cca Mon Sep 17 00:00:00 2001 From: Durval Carvalho Date: Thu, 21 Nov 2019 22:31:01 -0300 Subject: [PATCH 2/3] #188 Add tests to MonthlyHarvests viewset --- src/harvest/tests/test_tree_views.py | 149 +++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/src/harvest/tests/test_tree_views.py b/src/harvest/tests/test_tree_views.py index b2dcb05..b7e1ead 100644 --- a/src/harvest/tests/test_tree_views.py +++ b/src/harvest/tests/test_tree_views.py @@ -394,3 +394,152 @@ def test_delete_harvest(self): 0, msg='Failed to delete the harvest' ) + + +class MonthlyHarvestsTestCase(APITestCase): + + def create_authentication_tokens(self, user_credentials): + url_token = reverse('users:token_obtain_pair') + + response = self.client.post( + url_token, + user_credentials, + format='json' + ) + + self.assertEqual( + response.status_code, + 200, + msg='Failed to create user tokens credentials' + ) + + self.credentials = { + 'HTTP_AUTHORIZATION': 'Bearer ' + response.data['access'] + } + + def create_user(self): + user_data = { + "username": 'vitas', + 'email': 'vitas@vitas.com', + 'password': 'vitasIsNice', + 'confirm_password': 'vitasIsNice' + } + + url_user_signup = reverse('users:register') + + response = self.client.post( + url_user_signup, + user_data, + format='json' + ) + + self.assertEqual( + response.status_code, + 201, + msg='Failed during user creation' + ) + + user_data.pop('username') + user_data.pop('confirm_password') + + self.create_authentication_tokens(user_data) + + def create_property(self): + property_data = { + 'type_of_address': 'House', + 'BRZipCode': '73021498', + 'state': 'DF', + 'city': 'Gama', + 'district': 'Leste', + 'address': "Quadra 4", + } + + url_property_creation = reverse( + 'property:property-list', + ) + + response = self.client.post( + path=url_property_creation, + data=property_data, + format='json', + **self.credentials, + ) + + self.assertEqual( + response.status_code, + 201, + msg='Failed to create property' + ) + + self.property = Property.objects.get(pk=1) + + def setUp(self): + self.create_user() + self.create_property() + + self.harvest_data = { + 'date': '2019-11-21', + 'description': 'Apple Harvest', + 'status': 'Open', + 'max_volunteers': 10, + 'min_volunteers': 5, + } + + self.url_list = reverse( + 'property:harvest:harvest-list', + kwargs={'property_pk': self.property.pk} + ) + + self.url_detail = reverse( + 'property:harvest:harvest-detail', + kwargs={ + 'property_pk': self.property.pk, + 'pk': '1' + } + ) + + def tearDown(self): + Property.objects.all().delete() + User.objects.all().delete() + Harvest.objects.all().delete() + + def test_get_harvests_of_the_week(self): + + today = datetime.date(datetime(2020, 1, 1)) + + for i in range(0, 12): + date = today + timedelta(days=i*31) + + self.harvest_data['date'] = date + + response = self.client.post( + path=self.url_list, + data=self.harvest_data, + format='json', + **self.credentials, + ) + + self.assertEqual( + response.status_code, + 201, + msg='Failed to create a harvest' + ) + + for month in range(1, 13): + weekly_harvests_url = reverse( + 'monthly_harvest', + kwargs={ + 'year': date.year, + 'month': month + } + ) + + response = self.client.get( + path=weekly_harvests_url, + ) + + self.assertEqual( + len(response.data), + 1, + msg='Failed return only 1 harvest' + ) From d93f8be9ceb963b8f268466edf2500ba251cb9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fab=C3=ADola=20Fleury?= Date: Thu, 21 Nov 2019 23:45:02 -0300 Subject: [PATCH 3/3] Add maintainability badge to readme --- docs/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index c1ed842..adf7b8a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,7 @@

[![codecov](https://codecov.io/gh/fga-eps-mds/2019.2-Acacia/branch/develop/graph/badge.svg)](https://codecov.io/gh/fga-eps-mds/2019.2-Acacia) +[![Maintainability](https://api.codeclimate.com/v1/badges/9ceab9b0533182362c16/maintainability)](https://codeclimate.com/github/fga-eps-mds/2019.2-Acacia/maintainability) ## Visão geral @@ -68,4 +69,4 @@ Após esses passos a aplicação deverá estar acessível em: #### Front-end: -Para instalar a camada front-end da aplicação basta seguir os passos de instalação descritos [aqui](https://github.com/fga-eps-mds/2019.2-Acacia-Frontend) \ No newline at end of file +Para instalar a camada front-end da aplicação basta seguir os passos de instalação descritos [aqui](https://github.com/fga-eps-mds/2019.2-Acacia-Frontend)