Require shifts to be approved first
This commit is contained in:
parent
a245a51724
commit
7fef870719
20
caremyway/api/migrations/0002_shift_approved.py
Normal file
20
caremyway/api/migrations/0002_shift_approved.py
Normal 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(),
|
||||||
|
),
|
||||||
|
]
|
|
@ -65,4 +65,5 @@ class Shift(models.Model):
|
||||||
actual_end = models.DateTimeField(null=True)
|
actual_end = models.DateTimeField(null=True)
|
||||||
description = models.CharField(max_length=100, null=True)
|
description = models.CharField(max_length=100, null=True)
|
||||||
chart = models.TextField(max_length=1000, null=True)
|
chart = models.TextField(max_length=1000, null=True)
|
||||||
|
approved = models.NullBooleanField(blank=True)
|
||||||
deleted = models.BooleanField(default=False)
|
deleted = models.BooleanField(default=False)
|
||||||
|
|
|
@ -196,8 +196,8 @@ class CShiftSerializer(ShiftSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Shift
|
model = Shift
|
||||||
fields = ('uuid', 'chart', 'get_price_uuid', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'amount', 'description', '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', 'deleted')
|
read_only_fields = ('chart', 'price', 'set_price', 'set_date', 'actual_start', 'actual_end', 'approved', 'deleted')
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
user = self.context['request'].user
|
user = self.context['request'].user
|
||||||
|
@ -234,21 +234,46 @@ class CShiftSerializer(ShiftSerializer):
|
||||||
validated_data['price'] = price
|
validated_data['price'] = price
|
||||||
return serializers.ModelSerializer.create(self, validated_data)
|
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):
|
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:
|
class Meta:
|
||||||
model = Shift
|
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')
|
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):
|
def update(self, instance, validated_data):
|
||||||
|
approved = validated_data['approved']
|
||||||
action = validated_data['action']
|
action = validated_data['action']
|
||||||
chart = validated_data['chart']
|
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 action == 'checkin':
|
||||||
if instance.deleted:
|
if instance.deleted:
|
||||||
raise serializers.ValidationError("Shift was deleted.")
|
raise serializers.ValidationError("Shift was deleted.")
|
||||||
|
if not instance.approved:
|
||||||
|
raise serializers.ValidationError("Shift hasn't been approved.")
|
||||||
if instance.actual_start:
|
if instance.actual_start:
|
||||||
raise serializers.ValidationError("Already checked in.")
|
raise serializers.ValidationError("Already checked in.")
|
||||||
if chart:
|
if chart:
|
||||||
|
|
|
@ -116,7 +116,7 @@ class CShiftViewSet(viewsets.ModelViewSet):
|
||||||
instance = self.get_object()
|
instance = self.get_object()
|
||||||
|
|
||||||
if instance.actual_start:
|
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.deleted = True
|
||||||
instance.save()
|
instance.save()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user