Ejemplo n.º 1
0
 def put(self, request, pk, format=None):
     student = self.get_object(pk)
     serializer = StudentSerializer(student, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
def createStudent(request):
    # product = Product.objects.get(id=pk)

    serializer = StudentSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
    return Response(serializer.data)
Ejemplo n.º 3
0
    def test_gpa_decimals(self):
        matric_no = "1234567"
        data = [{
            "matricNo": matric_no,
            "givenNames": "Philip J",
            "surname": "Fry",
            "academicPlan": "F100-2208",
            "gradYear": "19-20",
            "finalAward3": 1.200
        }]
        serializer = StudentSerializer(data=data, many=True)
        if serializer.is_valid():
            serializer.save()

        student = Student.objects.get(matricNo=matric_no)
        student.save()

        self.assertEqual(str(student.finalAward1), '1.2')
        self.assertEqual(str(student.finalAward2), '1.20')
        self.assertEqual(str(student.finalAward3), '1.200')

        student.finalAward1 = 1.23
        student.save()

        self.decimal_assertion()

        student.finalAward1 = 1.232
        student.save()

        self.decimal_assertion()

        final_award3_dec = str(student.finalAward3).partition('.')[-1]
        self.assertEqual(len(final_award3_dec), 3)
Ejemplo n.º 4
0
def updateStudent(request,pk):
    student = Student_model.objects.get(id=pk)
    serializer = StudentSerializer(instance=student,data=request.data)
    
    if serializer.is_valid():
        serializer.save()

    return Response(serializer.data)
Ejemplo n.º 5
0
 def post(self, request):
     print(self.request.user)
     serializer = StudentSerializer(data=request.data)
     if serializer.is_valid():
         print(self.request.user)
         serializer.save(user=self.request.user)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         return Response({'status': 'error'})
Ejemplo n.º 6
0
    def list(self, request):
        queryset = Student.objects.all()

        serializer = StudentSerializer(queryset, many=True)
        json = JSONRenderer().render(serializer.data)
        print(json)

        print('Reversing')
        stream = io.BytesIO(json)
        data = JSONParser().parse(stream)

        json_list = StudentSerializer(data=data, many=True)
        data.reverse()
        json_list.is_valid()
        json = JSONRenderer().render(json_list.validated_data)
        print(json)

        return Response(json_list.validated_data)
Ejemplo n.º 7
0
def student_api(request):
    if request.method == "GET":
        json_data = request.body
        stream = io.BytesIO(json_data)
        pythondata = JSONParser().parse(stream)
        id = pythondata.get('id', None)
        if id is not None:
            stu = Student.objects.get(id=id)
            serializer = StudentSerializer(stu)
            json_data = JSONRenderer().render(serializer.data)
            return HttpResponse(json_data, content_type="application/json")
        stu = Student.objects.all()
        serializer = StudentSerializer(stu, many=True)
        json_data = JSONRenderer().render(serializer.data)
        return HttpResponse(json_data, content_type="application/json")

    if request.method == "POST":
        json_data = request.body
        stream = io.BytesIO(json_data)
        pythondata = JSONParser().parse(stream)
        serializer = StudentSerializer(data=pythondata)
        if serializer.is_valid():
            serializer.save()
            res = {"msg": "Data Created"}
            json_data = JSONRenderer().render(res)
            return HttpResponse(json_data, content_type="application/json")
        json_data = JSONRenderer().render(serializer.errors)
        return HttpResponse(json_data, content_type="application/json")

    if request.method == "PUT":
        json_data = request.body
        stream = io.BytesIO(json_data)
        pythondata = JSONParser().parse(stream)
        id = pythondata.get('id')
        stu = Student.objects.get(id=id)
        serializer = StudentSerializer(stu, data=pythondata, partial=True)
        if serializer.is_valid():
            serializer.save()
            res = {"msg": "Partial Data Created"}
            json_data = JSONRenderer().render(res)
            return HttpResponse(json_data, content_type="application/json")
        json_data = JSONRenderer().render(serializer.errors)
        return HttpResponse(json_data, content_type="application/json")
Ejemplo n.º 8
0
def student_list(request):
    if request.method == 'GET':
        students = Student.objects.all()
        serializer = StudentSerializer(students, many=True)
        return JsonResponse(serializer.data, safe=False)
    if request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = StudentSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Ejemplo n.º 9
0
 def post(self, request, *args, **kwargs):
     json_data = request.body
     stream = io.BytesIO(json_data)
     pythondata = JSONParser().parse(stream)
     serializer = StudentSerializer(data=pythondata)
     if serializer.is_valid():
         serializer.save()
         res = {"msg": "Data Created"}
         json_data = JSONRenderer().render(res)
         return HttpResponse(json_data, content_type="application/json")
     json_data = JSONRenderer().render(serializer.errors)
     return HttpResponse(json_data, content_type="application/json")
Ejemplo n.º 10
0
    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = StudentSerializer(data=data, many=True)
        if serializer.is_valid():
            try:
                serializer.save()
                return Response(serializer.data, status=status.HTTP_201_CREATED)
            except IntegrityError:
                return Response("Data already exists", status=status.HTTP_200_OK)

        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 11
0
 def put(self, request, *args, **kwargs):
     json_data = request.body
     stream = io.BytesIO(json_data)
     pythondata = JSONParser().parse(stream)
     id = pythondata.get('id')
     stu = Student.objects.get(id=id)
     serializer = StudentSerializer(stu, data=pythondata, partial=True)
     if serializer.is_valid():
         serializer.save()
         res = {"msg": "Partial Data Created"}
         json_data = JSONRenderer().render(res)
         return HttpResponse(json_data, content_type="application/json")
     json_data = JSONRenderer().render(serializer.errors)
     return HttpResponse(json_data, content_type="application/json")
Ejemplo n.º 12
0
 def post(self, request):
     serializer = StudentSerializer(data=request.data)
     if serializer.is_valid():
         if request.user.is_student():
             if request.user.student.id == serializer.validated_data['id']:
                 serializer.save()
                 return Response(serializer.data)
             else:
                 return Response(status=status.HTTP_401_UNAUTHORIZED)
         else:
             return Response(status=status.HTTP_404_NOT_FOUND)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 13
0
def studentApi(request, id=0):
    if request.method == 'GET':
        students = Students.objects.all()
        student_serializer = StudentSerializer(students, many=True)
        return JsonResponse(student_serializer.data, safe=False)
    elif request.method == 'POST':
        student_data = JSONParser().parse(request)
        student_serializer = StudentSerializer(data=student_data)
        if student_serializer.is_valid():
            student_serializer.save()
            return JsonResponse("Added Successfully!", safe=False)
        return JsonResponse("Failed to add", safe=False)
    elif request.method == 'PUT':
        student_data = JSONParser().parse(request)
        student = Students.objects.get(StudentId=student_data['StudentId'])
        student_serializer = StudentSerializer(student, data=student_data)
        if student_serializer.is_valid():
            student_serializer.save()
            return JsonResponse("Updated Successfully!", safe=False)
        return JsonResponse("Failed to update", safe=False)
    elif request.method == 'DELETE':
        student = Students.objects.get(StudentId=id)
        student.delete()
        return JsonResponse("Deleted Successfully!", safe=False)
Ejemplo n.º 14
0
 def test_student_data_stored(self):
     data = [{
         "matricNo": "1234567",
         "givenNames": "Philip J",
         "surname": "Fry",
         "academicPlan": "F100-2208",
         "gradYear": "19-20",
         "finalAward1": 0.0,
         "finalAward2": 0.00,
         "finalAward3": 0.000
     }, {
         "matricNo": "7654321",
         "givenNames": "Hermes",
         "surname": "Conrad",
         "academicPlan": "F100-2208",
         "gradYear": "19-20",
         "finalAward1": 0.0,
         "finalAward2": 0.00,
         "finalAward3": 0.000
     }]
     serializer = StudentSerializer(data=data, many=True)
     if serializer.is_valid():
         serializer.save()
     student1 = Student.objects.filter(
         matricNo="1234567",
         givenNames="Philip J",
         surname="Fry",
         academicPlan="F100-2208",
         gradYear=GraduationYear.objects.get(gradYear="19-20"),
         finalAward1=0.0,
         finalAward2=0.00,
         finalAward3=0.000).exists()
     student2 = Student.objects.filter(
         matricNo="7654321",
         givenNames="Hermes",
         surname="Conrad",
         academicPlan="F100-2208",
         gradYear=GraduationYear.objects.get(gradYear="19-20"),
         finalAward1=0.0,
         finalAward2=0.00,
         finalAward3=0.000).exists()
     self.assertTrue(student1)
     self.assertTrue(student2)
Ejemplo n.º 15
0
 def post(self, request):
     """
     Enroll in a labgroup.
     """
     # ensure all parameters are present in the request data
     for param in ('wwuid', 'labgroup', 'enroll_key'):
         if param not in request.data.keys():
             return Response(status=status.HTTP_400_BAD_REQUEST)
     # load the labgroup from the database
     try:
         labgroup = LabGroup.objects.get(id=request.data['labgroup'])
     except LabGroup.DoesNotExist:
         return Response(status=status.HTTP_404_NOT_FOUND)
     # check enroll key
     if request.data['enroll_key'] != labgroup.enroll_key:
         return Response(status=status.HTTP_403_FORBIDDEN)
     # delete student if it exists
     try:
         student = Student.objects.get(user=request.user)
         student.delete()
     except Student.DoesNotExist:
         pass
     # validate student data in request
     student_data = {
         'user': request.user.id,
         'labgroup': labgroup.id,
         'wwuid': request.data['wwuid'],
     }
     serializer = StudentSerializer(data=student_data)
     if not serializer.is_valid():
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     # create the student
     student = Student(user=request.user,
                       labgroup=labgroup,
                       wwuid=request.data['wwuid'])
     student.save()
     # add new student to the student group
     group = permissions.get_or_create_student_permissions()
     group.user_set.add(request.user)
     # return successful response
     return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 16
0
def student_detail(request, pk):
    try:
        student = Student.objects.get(pk=pk)
    except Student.DoesNotExist:
        return HttpResponse(status=404)

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

    if request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = StudentSerializer(student, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)

    if request.method == 'DELETE':
        student.delete()
        return HttpResponse(status=204)
 def create(self, request):
     serializer = StudentSerializer(data=request.data, context={'request': request})
     serializer.is_valid(raise_exception=True)
     serializer.save(user=request.user)
     return Response(serializer.data, status=status.HTTP_201_CREATED)
Ejemplo n.º 18
0
 def post(self, request):
     lserializer = StudentSerializer(data=request.DATA.get('student'))
     if lserializer.is_valid():
         lserializer.save()
         return Response(lserializer.data, status=status.HTTP_201_CREATED)
     return Response(lserializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 19
0
 def create(self, request, *args, **kwargs):
     serializer = StudentSerializer(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)