def EnrollCourseByPayment(request, pk, upk): course = Course.objects.get(id=pk) student = StudentProfile.objects.get(user_id=upk) print("before before") enrollment = Enrollment.objects.filter(student=student, course=course).first() print("before running") if not enrollment: print("running") c = Coupon.objects.create(course=course, coupon_key="", isValid=False, isIssued=True) print(c.id) serializer = CouponSerializer(instance=c, data=request.data) if serializer.is_valid(): coupon = str(c.id) + ":" + str(c.course.id) coupon_key = hashlib.shake_256(coupon.encode()).hexdigest(5) serializer.save(coupon_key=coupon_key) enroll = Enrollment(course=course, student=student, enroll_key=coupon_key, is_payment=True) enroll_serializer = CourseEnrollSerializer(enroll, data=request.data) if enroll_serializer.is_valid(): enroll_serializer.save() return Response(enroll_serializer.data) return Response(enroll_serializer.errors) return Response(serializer.errors) else: return Response({'message': 'You have already enrolled'}, status=403)
def EnrollCourse(request, pk, upk): course = Course.objects.get(id=pk) student = StudentProfile.objects.get(user_id=upk) couponList = Coupon.objects.filter(course=course) for c in couponList: if str(request.data['coupon_key']) == str(c.coupon_key): enroll = Enrollment(course=course, student=student, enroll_key=request.data['coupon_key']) condition = c.isValid == True and c.isIssued == True if request.method == "POST": if condition: e = Enrollment.objects.filter(course=course, student=student).first() if not e: serializer = CourseEnrollSerializer(enroll, data=request.data) if serializer.is_valid(): serializer.save() couponSerializer = CouponSerializer( instance=c, data=request.data) if couponSerializer.is_valid(): couponSerializer.save(isValid=False) return Response(serializer.data) return Response(serializer.errors, status=403) return Response( { "message": "You have already enrolled this course..." }, status=403) return Response({"message": "Coupon is not valid"}, status=403) return Response({"message": "coupon is not found"}, status=404)
def FreeEnroll(request, cid, sid): course = Course.objects.get(id=cid) student = StudentProfile.objects.get(user_id=sid) enrollment = Enrollment.objects.filter(student=student, course=course).first() if not enrollment: enroll = Enrollment(course=course, student=student, enroll_key="Free") enroll_serializer = CourseEnrollSerializer(enroll, data=request.data) if enroll_serializer.is_valid(): enroll_serializer.save() return Response(enroll_serializer.data) return Response(enroll_serializer.errors) else: return Response({'message': 'You have already enrolled'}, status=403)
def EnrollCourseByTeacher(request, pk): course = Course.objects.get(id=pk) res = [] for username in request.data['students']: student = StudentProfile.objects.filter( user__username=username).first() if student: e = Enrollment.objects.filter(course=course, student=student).first() if not e: enroll = Enrollment(course=course, student=student, enroll_key="Enrolled by teacher") serializer = CourseEnrollSerializer(enroll, data=request.data) if serializer.is_valid(): serializer.save() res.append({ "username": student.user.username, "email": student.user.email, "status": "enrolled successfully", "success": True }) else: res.append({ "username": student.user.username, "email": student.user.email, "status": "something is wrong", "success": False }) else: res.append({ "username": student.user.username, "email": student.user.email, "status": "already enrolled for this course", "success": False }) else: res.append({ "username": username, "email": "", "status": "student not found", "success": False }) return Response(res, status=200)