def put(self, request, pk, format=None): employee = self.get_object(pk) serializer = EmployeeSerializer(employee, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
class ContractSerializer(serializers.ModelSerializer): employee = EmployeeSerializer(read_only=True) client = ClientSerializer(read_only=True) car = CarSerializer(read_only=True) payment_type_name = serializers.CharField(source='payment_type.name', read_only=True) class Meta: model = Contract fields = [ 'id', 'employee', 'client', 'car', 'date', 'payment_type', 'payment_type_name', 'price' ] def create(self, validated_data): employee = get_object_or_404(Employee.objects.all(), pk=self.context.get('employee_id')) car = get_object_or_404(Car.objects.all(), pk=self.context.get('car_id')) payment_type = get_object_or_404( PaymentType.objects.all(), pk=self.context.get('payment_type_id')) client = get_object_or_404(Client.objects.all(), pk=self.context.get('client_id')) contract = Contract.objects.create(price=validated_data.get('price'), car=car, employee=employee, payment_type=payment_type, client=client) return contract
class EventParticipantSerializer(serializers.ModelSerializer): employee = EmployeeSerializer(many=False) supplier = SupplierSerializer(many=False) customer = CustomerSerializer(many=False) class Meta: model = models.EventParticipant fields = "__all__"
def employees_detail(request, pk): try: emp = employee.objects.get(pk=pk) except employee.DoesNotExist: return JsonResponse({'message': 'The employee does not exist'}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': employees_serializer = EmployeeSerializer(emp) return JsonResponse(employees_serializer.data) elif request.method == 'PUT': employee_data = JSONParser().parse(request) employees_serializer = EmployeeSerializer(emp, data=employee_data) if employees_serializer.is_valid(): employees_serializer.save() return JsonResponse(employees_serializer.data) return JsonResponse(employees_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': emp.delete() return JsonResponse({'message': 'Tutorial was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT)
class TourSerializer(serializers.ModelSerializer): approving_manager_id = serializers.IntegerField(write_only=True, required=False) created_by_id = serializers.IntegerField(write_only=True) financial_manager_id = serializers.IntegerField(write_only=True, required=False) approving_manager = EmployeeSerializer(read_only=True) created_by = EmployeeSerializer(read_only=True) financial_manager = EmployeeSerializer(read_only=True) class Meta: model = Tour fields = [ "id", "purpose", "start_date", "end_date", "mode_of_travel", "ticket_cost", "origin_cab_fare", "destination_cab_fare", "hotel_cost", "hotel_receipt", "conveyance", "approving_manager_id", "approving_manager", "status", "additional_information", "modified_date", "feedback_date", "created_by_id", "created_by", "created_date", "financial_manager", "financial_manager_id", "remarks" ]
def employees_list(request): if request.method == 'GET': employees = employee.objects.all() firstname = request.GET.get('firstname', None) if firstname is not None: employees = employees.filter(firstname__icontains=firstname) employees_serializer = EmployeeSerializer(employees, many=True) return JsonResponse(employees_serializer.data, safe=False) elif request.method == 'POST': employee_data = JSONParser().parse(request) employees_serializer = EmployeeSerializer(data=employee_data) if employees_serializer.is_valid(): employees_serializer.save() return JsonResponse(employees_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(employees_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def employees_list_permament(request): employees = employee.objects.filter(ispermanent=false) if request.method == 'GET': employees_serializer = EmployeeSerializer(employees, many=True) return JsonResponse(employees_serializer.data, safe=False)
def get_employee(self, obj): self.context.update({"job": obj}) return EmployeeSerializer(obj.employee, many=False, read_only=True, context=self.context).data
def list(self, request, *args, **kwargs): queryset = self.get_queryset() serializer = EmployeeSerializer(queryset, many=True) return Response(serializer.data)
def put(self, request, *args, **kwargs): serializer = EmployeeSerializer(self.get_object(), data=request.data) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): serializer = EmployeeSerializer(data=request.data) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def get(self, request): queryset = Employees.objects.all() serializer=EmployeeSerializer(queryset, many=True) return Response(serializer.data)
def to_representation(self, obj): data = super().to_representation(obj) fulldata = OrderedDict() fulldata['client'] = data if hasattr(obj, 'otherdetails'): fulldata['otherdetails'] = OtherDetailsSerializer( obj.otherdetails).data if hasattr(obj, 'address'): fulldata['billing_address'] = AddressSerializer(obj.address).data if hasattr(obj.address, 'country'): fulldata['billing_address']['country'] = { 'id': obj.address.country.id, 'name_ascii': obj.address.country.name_ascii } if hasattr(obj.address, 'state'): fulldata['billing_address']['state'] = { 'id': obj.address.state.id, 'name_ascii': obj.address.state.name_ascii } if hasattr(obj.address, 'city'): fulldata['billing_address']['city'] = { 'id': obj.address.city.id, 'name_ascii': obj.address.city.name_ascii } if hasattr(obj, 'shippingaddress'): fulldata['shipping_address'] = ShippingAddressSerializer( obj.shippingaddress).data if hasattr(obj, 'remark'): fulldata['remark'] = RemarkSerializer(obj.remark).data if hasattr(obj, 'contactperson_set'): fulldata['contact_persons'] = ContactPersonSerializer( obj.contactperson_set.all(), many=True).data if hasattr(obj, 'project_set'): projects = [] for i in obj.project_set.all(): resources = [] for res_obj in i.financialinfo.resource_set.all().filter( resource_billing_type='billable'): resources_assigned = EmployeeSerializer( res_obj.resource_assigned).data res = ResourceSerializer(res_obj).data res['resource_assigned'] = resources_assigned contracts = [] for cont in res_obj.resource_assigned.get_contract.all(): contracts.append( ContractAttachmentsSerializer(cont).data) res['contracts'] = contracts terms = [] for term in i.financialinfo.termattachment_set.filter( resource=res_obj.resource_assigned): terms.append(TermAttachmentSerializer(term).data) res['terms'] = terms resources.append(res) projects.append({ 'project_name': i.project_name, 'slug': i.slug, 'resources': resources }) fulldata['projects'] = projects return fulldata
def get(self, request, pk, format=None): employees = self.request.user.enterprise.employee.filter(id=pk) serializer = EmployeeSerializer(employees, many=True) return Response(serializer.data)
def get(self, request, format=None): employee = self.request.user.enterprise.employee.all() serializer = EmployeeSerializer(employee, many=True) return Response(serializer.data)
def post(self, request, format=None): serializer = EmployeeSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status = status.HTTP_201_CREATED) return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)