Require shifts to be approved first

This commit is contained in:
Tanner Collin 2018-04-18 17:33:04 +00:00
parent a245a51724
commit 7fef870719
4 changed files with 51 additions and 5 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2018-04-02 22:46
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='shift',
name='approved',
field=models.NullBooleanField(),
),
]

View File

@ -65,4 +65,5 @@ class Shift(models.Model):
actual_end = models.DateTimeField(null=True)
description = models.CharField(max_length=100, null=True)
chart = models.TextField(max_length=1000, null=True)
approved = models.NullBooleanField(blank=True)
deleted = models.BooleanField(default=False)

View File

@ -196,8 +196,8 @@ class CShiftSerializer(ShiftSerializer):
class Meta:
model = Shift
fields = ('uuid', 'chart', 'get_price_uuid', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'amount', 'description', 'deleted')
read_only_fields = ('chart', 'price', 'set_price', 'set_date', 'actual_start', 'actual_end', 'deleted')
fields = ('uuid', 'chart', 'get_price_uuid', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'amount', 'description', 'approved', 'deleted')
read_only_fields = ('chart', 'price', 'set_price', 'set_date', 'actual_start', 'actual_end', 'approved', 'deleted')
def create(self, validated_data):
user = self.context['request'].user
@ -234,21 +234,46 @@ class CShiftSerializer(ShiftSerializer):
validated_data['price'] = price
return serializers.ModelSerializer.create(self, validated_data)
def update(self, instance, validated_data):
if instance.actual_start:
raise serializers.ValidationError("Can't update after check in.")
# Reset approval on update
instance.approved = None
return super().update(instance, validated_data)
class PShiftSerializer(ShiftSerializer):
action = serializers.ChoiceField(write_only=True, choices=['checkin', 'checkout'])
action = serializers.ChoiceField(write_only=True, allow_blank=True, choices=['checkin', 'checkout'])
class Meta:
model = Shift
fields = ('uuid', 'action', 'chart', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'amount', 'description', 'deleted')
fields = ('uuid', 'action', 'chart', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'amount', 'description', 'approved', 'deleted')
read_only_fields = ('uuid', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'description', 'deleted')
def update(self, instance, validated_data):
approved = validated_data['approved']
action = validated_data['action']
chart = validated_data['chart']
if approved == True:
if instance.deleted:
raise serializers.ValidationError("Shift was deleted.")
instance.approved = True
elif approved == False:
if instance.actual_start:
raise serializers.ValidationError("Can't disapprove after check in.")
instance.approved = False
if action == 'checkin':
if instance.deleted:
raise serializers.ValidationError("Shift was deleted.")
if not instance.approved:
raise serializers.ValidationError("Shift hasn't been approved.")
if instance.actual_start:
raise serializers.ValidationError("Already checked in.")
if chart:

View File

@ -116,7 +116,7 @@ class CShiftViewSet(viewsets.ModelViewSet):
instance = self.get_object()
if instance.actual_start:
return Response("Shift already started.", status=status.HTTP_400_BAD_REQUEST)
return Response("Can't delete after check in.", status=status.HTTP_400_BAD_REQUEST)
instance.deleted = True
instance.save()