Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ webpack-stats-prod.json

# Cache
.cache/
.pytest_cache/

# OSX
.DS_Store
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ django-taggit = "*"
python-dateutil = "*"
"psycopg2" = "*"
"psycopg2-binary" = "*"
djangorestframework = "*"

[dev-packages]
coverage = "*"
Expand Down
72 changes: 39 additions & 33 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added clock/contracts/api/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions clock/contracts/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from clock.contracts.models import Contract


class ContractEndDateSerializer(serializers.ModelSerializer):
class Meta:
model = Contract
fields = ('end_date', )
9 changes: 9 additions & 0 deletions clock/contracts/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import include, path
from rest_framework import routers

from clock.contracts.api import views

router = routers.DefaultRouter()
router.register('end_date', views.ContractEndDateViewSet, base_name='end_date')

urlpatterns = [path('', include(router.urls))]
15 changes: 15 additions & 0 deletions clock/contracts/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.shortcuts import get_object_or_404
from rest_framework import permissions, viewsets
from rest_framework.response import Response

from clock.contracts.api.serializers import ContractEndDateSerializer
from clock.contracts.models import Contract


class ContractEndDateViewSet(viewsets.ViewSet):
permission_classes = (permissions.IsAuthenticated, )

def retrieve(self, request, pk=None):
contract = get_object_or_404(Contract, pk=pk, employee=request.user)
serializer = ContractEndDateSerializer(contract)
return Response(serializer.data)
Empty file added clock/shifts/api/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions clock/shifts/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from clock.shifts.models import Shift


class ShiftSerializer(serializers.ModelSerializer):
class Meta:
model = Shift
fields = ('started', 'finished', 'employee', 'contract')
Empty file.
102 changes: 102 additions & 0 deletions clock/shifts/api/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from collections import OrderedDict

import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient

from clock.shifts.factories import UserFactory


@pytest.fixture
def client(user):
client = APIClient()
client.force_authenticate(user=user)
return client


@pytest.fixture
def user():
return UserFactory()


@pytest.mark.django_db
def test_shift_overlap_api_start_after_finish(client):
"""Fail when the finished date is smaller than the started."""
url = reverse(
'api:overlap-list',
kwargs={
'started': '2018-05-02',
'finished': '2018-05-01',
'contract': 1,
'reoccuring': 'DAILY',
'pk': 0
}
)

response = client.get(url)
assert response.status_code == status.HTTP_400_BAD_REQUEST


@pytest.mark.django_db
@pytest.mark.parametrize(
['reoccurence', 'status_code'], [
('ONCE', status.HTTP_400_BAD_REQUEST), ('DAILY', status.HTTP_200_OK),
('WEEKLY', status.HTTP_200_OK), ('MONTHLY', status.HTTP_200_OK)
]
)
def test_shift_overlap_api_reoccurence_string(
client, reoccurence, status_code
):
"""Check the response depending on the reoccurence string."""
url = reverse(
'api:overlap-list',
kwargs={
'started': '2018-05-01',
'finished': '2018-05-02',
'contract': 1,
'reoccuring': reoccurence,
'pk': 0
}
)

response = client.get(url)
assert response.status_code == status_code


@pytest.mark.django_db
def test_shift_overlap_api_generate_new_shifts(client):
"""Test we can create new shifts, when not overlapping with anything."""
url = reverse(
'api:overlap-list',
kwargs={
'started': '2018-05-01 05:00',
'finished': '2018-05-02 08:00',
'contract': 1,
'reoccuring': 'DAILY',
'pk': 0
}
)

response = client.get(url)
assert response.status_code == status.HTTP_200_OK

shifts = [
{
'started': '2018-05-01T05:00:00+02:00',
'finished': '2018-05-01T08:00:00+02:00',
'employee': 6,
'contract': None
}, {
'started': '2018-05-02T05:00:00+02:00',
'finished': '2018-05-02T08:00:00+02:00',
'employee': 6,
'contract': None
}
]
shifts = [OrderedDict(shift) for shift in shifts]

without_overlaps = response.data['without_overlap']
with_overlaps = response.data['with_overlap']
assert with_overlaps == []
assert without_overlaps == shifts
20 changes: 20 additions & 0 deletions clock/shifts/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# from django.urls import include, path
# from rest_framework import routers
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns

from clock.shifts.api import views

# router = routers.DefaultRouter()
# router.register('shifts', views.ShiftOverlapView, base_name='test')

urlpatterns = [
path('', views.api_root),
path(
'overlap/<str:started>/<str:finished>/<int:contract>/<str:reoccuring>/<int:pk>/',
views.ShiftOverlapView.as_view(),
name='overlap-list'
)
]

urlpatterns = format_suffix_patterns(urlpatterns)
Loading