Refer to Work Type, Price, Management, and Shift by uuid
This commit is contained in:
parent
3823c887b6
commit
9817a4ebba
|
@ -1,3 +1,4 @@
|
|||
import uuid
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
from django.core.validators import RegexValidator
|
||||
|
@ -26,6 +27,7 @@ class Provider(models.Model):
|
|||
return 'Provider: ' + self.user.username
|
||||
|
||||
class WorkType(models.Model):
|
||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
|
||||
client = models.ForeignKey(Client, related_name='work_types', on_delete=models.CASCADE)
|
||||
color = models.CharField(max_length=16)
|
||||
label = models.CharField(max_length=100)
|
||||
|
@ -35,6 +37,7 @@ class WorkType(models.Model):
|
|||
return 'WorkType: ' + self.label + ' of ' + self.client.user.username
|
||||
|
||||
class Manage(models.Model):
|
||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
|
||||
client = models.ForeignKey(Client, related_name='employees', on_delete=models.CASCADE)
|
||||
provider = models.ForeignKey(Provider, related_name='employers', on_delete=models.CASCADE)
|
||||
note = models.CharField(max_length=500, blank=True)
|
||||
|
@ -45,12 +48,14 @@ class Manage(models.Model):
|
|||
return self.client.user.username + ' manages ' + self.provider.user.username
|
||||
|
||||
class Price(models.Model):
|
||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
|
||||
management = models.ForeignKey(Manage, related_name='prices', on_delete=models.CASCADE)
|
||||
work_type = models.ForeignKey(WorkType, on_delete=models.CASCADE)
|
||||
amount = models.DecimalField(max_digits=8, decimal_places=2)
|
||||
deleted = models.BooleanField(default=False)
|
||||
|
||||
class Shift(models.Model):
|
||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
|
||||
price = models.ForeignKey(Price, on_delete=models.CASCADE)
|
||||
set_price = models.DecimalField(max_digits=8, decimal_places=2, null=True)
|
||||
set_date = models.DateField()
|
||||
|
|
|
@ -17,7 +17,7 @@ class UserInfoSerializer(serializers.ModelSerializer):
|
|||
class WorkTypeSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = WorkType
|
||||
fields = ('id', 'color', 'label', 'deleted')
|
||||
fields = ('uuid', 'color', 'label', 'deleted')
|
||||
read_only_fields =('deleted',)
|
||||
|
||||
def create(self, validated_data):
|
||||
|
@ -25,13 +25,13 @@ class WorkTypeSerializer(serializers.ModelSerializer):
|
|||
return serializers.ModelSerializer.create(self, validated_data)
|
||||
|
||||
class PriceSerializer(serializers.ModelSerializer):
|
||||
get_employee_id = serializers.IntegerField(write_only=True)
|
||||
get_work_type_id = serializers.IntegerField(write_only=True)
|
||||
get_employee_uuid = serializers.UUIDField(write_only=True)
|
||||
get_work_type_uuid = serializers.UUIDField(write_only=True)
|
||||
work_type = WorkTypeSerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Price
|
||||
fields = ('id', 'get_employee_id', 'get_work_type_id', 'work_type', 'amount', 'deleted')
|
||||
fields = ('uuid', 'get_employee_uuid', 'get_work_type_uuid', 'work_type', 'amount', 'deleted')
|
||||
read_only_fields =('deleted',)
|
||||
depth = 1
|
||||
|
||||
|
@ -43,11 +43,11 @@ class PriceSerializer(serializers.ModelSerializer):
|
|||
|
||||
client = user.client
|
||||
manage_objs = Manage.objects.filter(client=client).filter(deleted=False)
|
||||
employee_id = validated_data.pop('get_employee_id')
|
||||
employee = get_object_or_404(manage_objs, id=employee_id)
|
||||
employee_uuid = validated_data.pop('get_employee_uuid')
|
||||
employee = get_object_or_404(manage_objs, uuid=employee_uuid)
|
||||
worktype_objs = WorkType.objects.filter(client=client).filter(deleted=False)
|
||||
work_type_id = validated_data.pop('get_work_type_id')
|
||||
work_type = get_object_or_404(worktype_objs, id=work_type_id)
|
||||
work_type_uuid = validated_data.pop('get_work_type_uuid')
|
||||
work_type = get_object_or_404(worktype_objs, uuid=work_type_uuid)
|
||||
|
||||
if not employee.approved:
|
||||
raise serializers.ValidationError("Employee hasn't approved employment yet.")
|
||||
|
@ -76,7 +76,7 @@ class EmployeeSerializer(serializers.ModelSerializer):
|
|||
|
||||
class Meta:
|
||||
model = Manage
|
||||
fields = ('id', 'provider', 'provider_email', 'note', 'approved', 'prices', 'deleted')
|
||||
fields = ('uuid', 'provider', 'provider_email', 'note', 'approved', 'prices', 'deleted')
|
||||
read_only_fields = ('approved', 'deleted')
|
||||
|
||||
def get_provider(self, obj):
|
||||
|
@ -136,7 +136,7 @@ class EmployerSerializer(serializers.ModelSerializer):
|
|||
|
||||
class Meta:
|
||||
model = Manage
|
||||
fields = ('id', 'client', 'note', 'approved', 'prices')
|
||||
fields = ('uuid', 'client', 'note', 'approved', 'prices')
|
||||
read_only_fields = ('note',)
|
||||
|
||||
def get_client(self, obj):
|
||||
|
@ -173,11 +173,11 @@ class UserSerializer(serializers.ModelSerializer):
|
|||
depth = 1
|
||||
|
||||
class CShiftSerializer(serializers.ModelSerializer):
|
||||
get_price_id = serializers.IntegerField(write_only=True)
|
||||
get_price_uuid = serializers.UUIDField(write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Shift
|
||||
fields = ('id', 'chart', 'get_price_id', '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', 'deleted')
|
||||
read_only_fields = ('chart', 'price', 'set_price', 'set_date', 'actual_start', 'actual_end', 'amount', 'deleted')
|
||||
|
||||
def create(self, validated_data):
|
||||
|
@ -188,8 +188,8 @@ class CShiftSerializer(serializers.ModelSerializer):
|
|||
|
||||
client = user.client
|
||||
price_objs = Price.objects.filter(management__client=client).filter(deleted=False)
|
||||
price_id = validated_data.pop('get_price_id')
|
||||
price = get_object_or_404(price_objs, id=price_id)
|
||||
price_uuid = validated_data.pop('get_price_uuid')
|
||||
price = get_object_or_404(price_objs, uuid=price_uuid)
|
||||
|
||||
if price.management.deleted:
|
||||
raise serializers.ValidationError("Employment relationship was deleted.")
|
||||
|
@ -220,8 +220,8 @@ class PShiftSerializer(serializers.ModelSerializer):
|
|||
|
||||
class Meta:
|
||||
model = Shift
|
||||
fields = ('id', 'action', 'chart', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'amount', 'description', 'deleted')
|
||||
read_only_fields = ('id', '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', 'deleted')
|
||||
read_only_fields = ('uuid', 'price', 'set_price', 'set_date', 'set_start', 'set_end', 'actual_start', 'actual_end', 'amount', 'description', 'deleted')
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
action = validated_data['action']
|
||||
|
|
|
@ -48,6 +48,7 @@ class ProviderViewSet(viewsets.ModelViewSet):
|
|||
return Provider.objects.filter(user__username=self.request.user)
|
||||
|
||||
class WorkTypeViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "uuid"
|
||||
serializer_class = WorkTypeSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
|
@ -60,6 +61,7 @@ class WorkTypeViewSet(viewsets.ModelViewSet):
|
|||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
class EmployeeViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "uuid"
|
||||
serializer_class = EmployeeSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
|
@ -72,6 +74,7 @@ class EmployeeViewSet(viewsets.ModelViewSet):
|
|||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
class EmployerViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "uuid"
|
||||
serializer_class = EmployerSerializer
|
||||
|
||||
# Disallow creation and deletions of relationships
|
||||
|
@ -81,6 +84,7 @@ class EmployerViewSet(viewsets.ModelViewSet):
|
|||
return Manage.objects.filter(provider__user__username=self.request.user)
|
||||
|
||||
class PriceViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "uuid"
|
||||
serializer_class = PriceSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
|
@ -93,6 +97,7 @@ class PriceViewSet(viewsets.ModelViewSet):
|
|||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
class CShiftViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "uuid"
|
||||
serializer_class = CShiftSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
|
@ -111,6 +116,7 @@ class CShiftViewSet(viewsets.ModelViewSet):
|
|||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
class PShiftViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "uuid"
|
||||
serializer_class = PShiftSerializer
|
||||
|
||||
# Disallow creation and deletions of relationships
|
||||
|
|
Loading…
Reference in New Issue
Block a user