Exemple #1
0
 def delete(self, request, *args, **kwargs):
     stu_id = kwargs.get('id')
     if stu_id:
         try:
             stu_obj = Student.objects.get(pk=stu_id)
             stu_ser = StudentSerializer(stu_obj).data
         except:
             return Response({'status': 400, 'message': '此学生不存在,删除失败'})
         try:
             stu_obj.delete()
             return Response({
                 'status': 200,
                 'message': '删除单个学生成功',
                 'result': stu_ser
             })
         except:
             return Response({'status': 400, 'message': '删除单个学生失败'})
     else:
         stu_obj = Student.objects.all()
         stu_ser = StudentSerializer(stu_obj, many=True).data
         try:
             stu_obj.delete()
             return Response({
                 'status': 200,
                 'message': '删除所有学生成功',
                 'result': stu_ser
             })
         except:
             return Response({'status': 400, 'message': '删除所有学生失败'})
Exemple #2
0
def student_add(request):
    # add student
    req = JSONParser().parse(request)
    student_serializer = StudentSerializer(data=req)

    if not student_serializer.is_valid():
        return JsonResponse(student_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    student_serializer.save()
    return JsonResponse(student_serializer.data,
                        status=status.HTTP_201_CREATED)
Exemple #3
0
def student_detail(request, pk):
    """
    Retrieve, update or delete a student.
    """
    try:
        cause = Student.nodes.get(uid=pk)
    
    except Student.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = StudentSerializer(cause)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = StudentSerializer(cause, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(cause.errors, status=400)

    elif request.method == 'DELETE':
        cause.delete()
        return HttpResponse(status=204)
Exemple #4
0
def student(request, id):
    # get student detail by id
    try:
        student = Student.objects.get(pk=id)
    except Student.DoesNotExist:
        return JsonResponse({'message': 'The student does not exist'},
                            status=status.HTTP_404_NOT_FOUND)

    # query detail by id
    if request.method == 'GET':
        student_serializer = StudentSerializer(student)
        return JsonResponse(student_serializer.data, status=status.HTTP_200_OK)

    # query update by id
    elif request.method == 'PUT':
        req = JSONParser().parse(request)
        student_serializer = StudentSerializer(student, data=req)
        if not student_serializer.is_valid():
            return JsonResponse(student_serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)

        student_serializer.save()
        return JsonResponse(student_serializer.data, status=status.HTTP_200_OK)

    # query delete by id
    elif request.method == 'DELETE':
        student.delete()
        return JsonResponse({'message': 'The student succesfully delete'},
                            status=status.HTTP_200_OK)
Exemple #5
0
def get_student(request, id):
    try:
        student = Student.objects.get(student_number=id)
    except Student.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == "GET":
        serializer = StudentSerializer(student)
        return JSONResponse(serializer.data)
Exemple #6
0
 def get(self, request, *args, **kwargs):
     stu_id = kwargs.get('id')
     if stu_id:
         try:
             stu_obj = Student.objects.get(pk=stu_id)
             stu_serializer = StudentSerializer(stu_obj).data
             return Response({
                 'status': 200,
                 'message': '查询单个学生成功',
                 'results': stu_serializer,
             })
         except:
             return Response({'status': 400, 'message': '此学生不存在,查询失败'})
     else:
         stu_obj = Student.objects.all()
         stu_serializer = StudentSerializer(stu_obj, many=True).data
         return Response({
             'status': 200,
             'message': '查询所有学生成功',
             'results': stu_serializer,
         })
Exemple #7
0
def student(request, nim):
    # get student detail by id
    try:
        student = Student.objects.get(nim=nim)
    except Student.DoesNotExist:
        return JsonResponse({'message': 'The student does not exist'},
                            status=status.HTTP_404_NOT_FOUND)

    # query detail by id
    if request.method == 'GET':
        student_serializer = StudentSerializer(student)
        return JsonResponse(student_serializer.data, status=status.HTTP_200_OK)

    return JsonResponse({}, status=status.HTTP_200_OK)
Exemple #8
0
def student_list(request):
    """
    List all students, or create a student.
    """
    if request.method == 'GET':
        students = Student.nodes.all()
        serializer = StudentSerializer(students, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        serializer = StudentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(created=timezone.now())
            return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #9
0
def student_list(request):
    # get student list
    req = JSONParser().parse(request)
    offset, limit, order_by, order_dir = req['offset'], req['limit'], req[
        'order_by'], req['order_dir']

    if not limit or not order_by or not order_dir:
        return JsonResponse({'message': 'invalid param'},
                            status=status.HTTP_400_BAD_REQUEST)

    students = Student.objects.all().order_by(
        Lower(order_by).desc() if order_dir ==
        'desc' else Lower(order_by).asc())[offset:offset + limit]
    student_serializer = StudentSerializer(students, many=True)

    return JsonResponse(student_serializer.data,
                        safe=False,
                        status=status.HTTP_200_OK)
Exemple #10
0
    def post(self, request, *args, **kwargs):

        request_data = request.data
        if not isinstance(request_data, dict) or request_data == {}:
            return Response({"status": 400, "message": "参数有误"})
        stu_de_ser = StudentDeSerializer(data=request_data)

        if stu_de_ser.is_valid():
            stu_ser = stu_de_ser.save()
            return Response({
                'status': 200,
                'message': '学生添加成功',
                'results': StudentSerializer(stu_ser).data
            })
        else:
            return Response({
                'status': 400,
                'message': '学生添加失败',
                'result': stu_de_ser.errors
            })
Exemple #11
0
def student_list(request):
    if request.method == 'GET':
        students = Student.objects.all()
        serializer = StudentSerializer(students, many=True)
        return JSONResponse(serializer.data)
    def test_create_serializer(self):

        student = factory.build(dict, FACTORY_CLASS=StudentFactory)
        serializer = StudentSerializer(data=student)
        assert serializer.is_valid() == True