def put(self, request): employee = self.get_object(request.data['id']) 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)
def get(self, request, pk=None): if pk: employee = self.get_object(pk) serializer = EmployeeSerializer(employee) else: employee = Employee.objects.all().select_related( 'type').prefetch_related('product') serializer = EmployeeSerializer(employee, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
class CompanySerializer(ModelSerializer): employees = EmployeeSerializer(many=True, required=False) class Meta: model = CompanyModel # exclude = ['user'] fields = ['id', 'name', 'country', 'city', 'user', 'employees']
class ListRequestSerializer(serializers.ModelSerializer): # returns item and employee dicts in response item = ItemSerializer(read_only=True) employee = EmployeeSerializer(read_only=True) class Meta: model = Request fields = ("id", "employee", "item", "quantity", "date", "active")
def setUp(self): departament = Departament.objects.create(departament='Architecture') self.employee = Employee.objects.create(**{ 'name': 'Arnaldo Pereira', 'email': '*****@*****.**', 'departament': departament }) self.data = EmployeeSerializer(self.employee).data
def retrieve(self, request, pk): try: item = Employee.objects.get(pk=pk) except ObjectDoesNotExist as err: raise NotFound except MultipleObjectsReturned as err: item = Employee.objects.filter(pk=pk)[:1] serialized = EmployeeSerializer(item) return Response(serialized.data)
class PollSearchSerializer(serializers.Serializer): id = serializers.IntegerField() title = serializers.CharField() status = serializers.CharField() created_by = EmployeeSerializer() created_at = serializers.DateTimeField() class Meta: model = Question
class CompanySerializer(ModelSerializer): employees = EmployeeSerializer(many=True, required=False) class Meta: model = CompanyModel fields = ['id', 'name', 'country', 'city', 'employees', 'owner'] read_only_fields = ['employees'] def create(self, validated_data): company = CompanyModel.objects.create(**validated_data) return company
def perform_destroy(self, instance): employees_all = instance.employees.all() print(employees_all) data = {'is_employed': False} for employee in employees_all: serializer = EmployeeSerializer(employee, data=data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() instance.delete() return Response(1, status=status.HTTP_204_NO_CONTENT)
def employee_list(request, format=None): """ List all code snippets, or create new snippet. """ if request.method == 'GET': snippet = Employee.objects.all() serializer = EmployeeSerializer(snippet, many=True) return Response(serializer.data) elif request.method == 'POST': #data = JSONParser().parse(request) 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)
class LaborSerializer(serializers.ModelSerializer): employee = EmployeeSerializer() labor_type = LaborTypeSerializer() class Meta: model = Labor fields = ( "id", "invoice", "employee", "labor_type", "estimated_rate", "actual_rate", "estimated_time", "actual_time", "labor_date", )
def employee_list(request): """ List all Employee instances or creates a new one """ if request.method == 'GET': employees = Employee.objects.all() serializer = EmployeeSerializer(employees, many=True) return Response(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = EmployeeSerializer(data=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)
def employeeApi(request): if request.method=='GET': employees = Employees.objects.all() employees_serializer = EmployeeSerializer(employees, many=True) return JsonResponse(employees_serializer.data, safe=False) elif request.method=='POST': employee_data=JSONParser().parse(request) employee_serializer = EmployeeSerializer(data=employee_data) if employee_serializer.is_valid(): employee_serializer.save() return JsonResponse("Added Successfully!!" , safe=False) return JsonResponse("Failed to Add.",safe=False)
def parse_xml_and_save_employee_data(csv_file): error_items = [] message = 'Import Success' class_alert = 'alert-info' df = pd.read_csv(csv_file, header=0) for index, row in df.iterrows(): data = dict(row) department = data.pop('department') join_date_str = data.pop('join_date') serializer = EmployeeSerializer(data=data, partial=True) if serializer.is_valid(): department_obj, created = Department.objects.get_or_create( name=department) join_date = parser.parse(join_date_str).date() data['department'] = department_obj.id data['join_date'] = join_date new_serializer = EmployeeSerializer(data=data, partial=True) if new_serializer.is_valid(): new_serializer.save() else: error_items.append(data) message = 'Error' class_alert = 'alert-danger' return { 'message': message, 'error_items': error_items, 'class': class_alert } # if __name__ == '__main__': # parse_xml_and_save_employee_data()
def employee_details(request, pk, format=None): """ Retrieve, update and delete code snippet. """ try: snippet = Employee.objects.get(pk=pk) except Employee.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = EmployeeSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': #data = JSONParser().parse(request) serializer = EmployeeSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def employee_detail(request, pk): """ Get, update or remove an employee instance """ try: employee = Employee.objects.get(pk=pk) except Employee.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = EmployeeSerializer(employee) return Response(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = EmployeeSerializer(employee, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': employee.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def test_required_fields(self): #noqa for field in self.REQUIRED_FIELDS: data = {k: v for k, v in self.TEST_DATA.items() if k != field} serializer = EmployeeSerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertIn(field, serializer.errors)
class CompanySerializer(ModelSerializer): employee = EmployeeSerializer(many=True, required=False) class Meta: model = CompanyModel exclude = ['user']
def get(self, request): employee = self.get_object(request.GET['id']) serializer = EmployeeSerializer(employee) return Response(serializer.data)
def post(request): 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)
def test_create(self): #noqa data = self.TEST_DATA.copy() serializer = EmployeeSerializer(data=data) self.assertTrue(serializer.is_valid()) serializer.save() self.assertTrue(Employee.objects.count() is 1)
class CompanySerializer(ModelSerializer): employee = EmployeeSerializer(many=True, required=False) class Meta: model = CompanyModel fields = '__all__'
def get(self, request, pk, format=None): employee = self.get_object(pk) serializer = EmployeeSerializer(employee) return Response(serializer.data)