Skip to content

Commit

Permalink
Merge branch 'develop' into hotfix/125-slashless-routers
Browse files Browse the repository at this point in the history
  • Loading branch information
shayanealcantara authored Nov 22, 2019
2 parents a976685 + d6bbc21 commit 044a970
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<p align="center"> <img src="img/wordmark_1.svg" width="400"></p>

[![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

Expand Down Expand Up @@ -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)
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)
8 changes: 7 additions & 1 deletion src/acacia/urls.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -16,6 +16,12 @@
WeekHarvests.as_view({'get': 'list'}),
name='weekly_harvests'
),

path(
'monthly_harvest/<int:year>/<int:month>/',
MonthlyHarvests.as_view({'get': 'list'}),
name='monthly_harvest'
),
]

urlpatterns = list_all_endpoints(urlpatterns)
149 changes: 149 additions & 0 deletions src/harvest/tests/test_tree_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '[email protected]',
'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'
)
24 changes: 24 additions & 0 deletions src/harvest/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from . import serializers

import datetime
import calendar


class HarvestViewSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -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

0 comments on commit 044a970

Please sign in to comment.