Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
class CompanySerializer(ModelSerializer):
    employees = EmployeeSerializer(many=True, required=False)

    class Meta:
        model = CompanyModel
        # exclude = ['user']
        fields = ['id', 'name', 'country', 'city', 'user', 'employees']
Beispiel #4
0
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")
Beispiel #5
0
 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
Beispiel #6
0
 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)
Beispiel #7
0
class PollSearchSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    title = serializers.CharField()
    status = serializers.CharField()
    created_by = EmployeeSerializer()
    created_at = serializers.DateTimeField()

    class Meta:
        model = Question
Beispiel #8
0
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)
Beispiel #10
0
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)
Beispiel #11
0
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",
        )
Beispiel #12
0
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)
Beispiel #13
0
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)
Beispiel #14
0
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()
Beispiel #15
0
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)
Beispiel #16
0
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)
Beispiel #17
0
 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)
Beispiel #18
0
class CompanySerializer(ModelSerializer):
    employee = EmployeeSerializer(many=True, required=False)

    class Meta:
        model = CompanyModel
        exclude = ['user']
Beispiel #19
0
 def get(self, request):
     employee = self.get_object(request.GET['id'])
     serializer = EmployeeSerializer(employee)
     return Response(serializer.data)
Beispiel #20
0
 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)
Beispiel #21
0
 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)
Beispiel #22
0
class CompanySerializer(ModelSerializer):
    employee = EmployeeSerializer(many=True, required=False)

    class Meta:
        model = CompanyModel
        fields = '__all__'
Beispiel #23
0
 def get(self, request, pk, format=None):
     employee = self.get_object(pk)
     serializer = EmployeeSerializer(employee)
     return Response(serializer.data)