def validate_coupon(request): if request.user.is_authenticated(): if request.method == 'GET': coupon_code = request.GET.get('coupon_code', '') exam_code = request.GET.get('exam_code', '') coupon_obj = Coupon() coupon = coupon_obj.validate_coupon(coupon_code) if coupon is not None: coupon_obj.change_used_status_of_coupon( coupon_code, request.user.id, exam_code) return HttpResponse( json.dumps({ 'status': 'ok', 'coupon_code': coupon_code, 'subscription_type': coupon['subscription_type'] })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'Invalid Coupon' })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'Not a valid request' }))
def is_subscribed(self, request): if request.user.is_authenticated(): coupon_obj = Coupon() exam_code = request.POST.get('exam_code', '') user_id = request.user.id if exam_code.strip() == 'sample': return HttpResponse( json.dumps({ 'status': 'error', 'message': 'not subscribed' })) else: if coupon_obj.check_subscried(exam_code, user_id): return HttpResponse( json.dumps({ 'status': 'ok', 'url': '/honorcode/' + exam_code + '/' })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'not subscribed' })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'You are not authorized to perform this action.' }))
def change_subscription_plan(self, user_name, coupon_code): from apps.mainapp.classes.Coupon import Coupon coupon_obj = Coupon() coupon = coupon_obj.get_coupon_by_coupon_code(coupon_code) user = self.db_object.get_one(self.table_name, {'username':user_name}) subscription_type = list(user['subscription_type']) if coupon['subscription_type'] not in subscription_type and coupon['subscription_type'] not in ['DPS', 'CPS']: subscription_type.append(coupon['subscription_type']) return self.db_object.update_upsert(self.table_name, {'username':user_name}, {'subscription_type':subscription_type})
def get_coupons(request, subscription_type): coupon_obj = Coupon() if subscription_type == 'beioe': subscription_type = 'BE-IOE' elif subscription_type == 'mbbsiom': subscription_type = 'MBBS-IOM' subscription_type = subscription_type.upper() coupons = coupon_obj.get_coupons(subscription_type) # coupon_obj.update_coupons(subscription_type) return HttpResponse(json.dumps({'status':'ok', 'coupons':coupons}))
def generate_coupon(request): # 1. DPS (Daily Practice Set) # 2. CPS (Competitive Pracice Set) # 3. MBBS-IOM # 4. BE-IOE # 5. IDP (Inter Disciplinary Plan) coupon = Coupon() coupon.generate_coupons('IDP') coupon.generate_coupons('DPS') coupon.generate_coupons('CPS') coupon.generate_coupons('BE-IOE') coupon.generate_coupons('MBBS-IOM') return HttpResponse(json.dumps({'status':'success'}))
def get_question_set(request, exam_code): ''' the function returns the api of a model question ''' if request.user.is_authenticated(): coupon_code = request.POST.get('coupon') user_obj = UserProfile() from apps.mainapp.classes.Exams import Exam exam_obj = Exam() up_exm = exam_obj.get_exam_detail(int(exam_code)) if up_exm['exam_family'] == 'CPS': if time.mktime( datetime.datetime.now().timetuple()) < up_exm['exam_date']: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'Exam has not begin yet, please check back later.' })) coupon_obj = Coupon() user_profile_obj = UserProfile() subscription_status = user_obj.check_subscribed( request.user.username, exam_code) user = user_profile_obj.get_user_by_username(request.user.username) '''Validation for subscription here''' if subscription_status or int(exam_code) in user['valid_exam']: exam_handler = ExamHandler() model_question_set = exam_handler.get_questionset_from_database( exam_code) if len(model_question_set) > 0: return HttpResponse( json.dumps({ 'status': 'ok', 'result': model_question_set })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'No question in this exam right now.' })) if coupon_obj.validate_coupon(coupon_code, up_exm['exam_category'], up_exm['exam_family']) is True: user_profile_obj = UserProfile() user_profile_obj.change_subscription_plan(request.user.username, coupon_code) #save the coupon code in user's couponcode array coupon_obj.change_used_status_of_coupon(coupon_code, request.user.username) user_profile_obj.save_coupon(request.user.username, coupon_code) subscription_status = user_obj.check_subscribed( request.user.username, exam_code) if subscription_status: '''Add Validation for subscription here''' exam_handler = ExamHandler() model_question_set = exam_handler.\ get_questionset_from_database(exam_code) response = HttpResponse( json.dumps({ 'status': 'ok', 'result': model_question_set })) else: response = HttpResponse( json.dumps({ 'status': 'error', 'message': 'You are not subscribed for this exam' })) else: response = HttpResponse( json.dumps({ 'status': 'error', 'message': 'Invalid Coupon Code.' })) else: response = HttpResponse( json.dumps({ 'status': 'error', 'message': 'Not a valid request' })) return response
def get_coupons(request, subscription_type): coupon_obj = Coupon() coupons = coupon_obj.get_coupons(subscription_type) return HttpResponse(json.dumps({'status':'ok', 'coupons':coupons}))
def validate_coupon(self, request): if request.user.is_authenticated(): coupon_obj = Coupon() exam_code = request.POST.get('exam_code', '') coupon_code = request.POST.get('coupon_code', '') user_profile_obj = UserProfile() user = user_profile_obj.get_user_by_username(request.user.username) from apps.mainapp.classes.Exams import Exam exam_obj = Exam() if exam_code != 'subs': up_exm = exam_obj.get_exam_detail(int(exam_code)) else: if coupon_obj.has_susbcription_plan_in_coupon(coupon_code): coupon_obj.change_used_status_of_coupon( coupon_code, request.user.username) user_profile_obj.change_subscription_plan( request.user.username, coupon_code) user_profile_obj.save_coupon(request.user.username, coupon_code) return HttpResponse( json.dumps({ 'status': 'ok', 'url': '/' })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'Invalid Coupon code.' })) if coupon_obj.validate_coupon(coupon_code, up_exm['exam_category'], up_exm['exam_family']) == True: #save the coupon code in user's couponcode array coupon_obj.change_used_status_of_coupon( coupon_code, request.user.username) user_profile_obj.change_subscription_plan( request.user.username, coupon_code) user_profile_obj.save_coupon(request.user.username, coupon_code) #Refreshment of user user = user_profile_obj.get_user_by_username( request.user.username) subscription_type = user['subscription_type'] #if coupon_code != 'IDP' or 'BE-IOE' or 'MBBS-IOM' then save the exam code in the valid exams if 'IDP' not in subscription_type and 'BE-IOE' not in subscription_type and 'MBBS-IOM' not in subscription_type: user_profile_obj.save_valid_exam(request.user.username, exam_code) if 'IDP' in subscription_type: return HttpResponse( json.dumps({ 'status': 'ok', 'url': '/honorcode/' + exam_code })) elif up_exm['exam_category'] in subscription_type: return HttpResponse( json.dumps({ 'status': 'ok', 'url': '/honorcode/' + exam_code })) else: subscribed_exams = user_profile_obj.get_subscribed_exams( request.user.username) if int(exam_code) in subscribed_exams: #Check if exam is cps or dps #if exam is cps then return url '/cps/exam_code' #else return url '/dps/exam_code/' exm_dtls = exam_obj.get_exam_detail(int(exam_code)) return HttpResponse( json.dumps({ 'status': 'ok', 'url': '/' + exm_dtls['exam_family'].lower() + '/' + exam_code })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'Invalid Coupon code.' })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'Invalid Coupon code.' })) else: return HttpResponse( json.dumps({ 'status': 'error', 'message': 'You are not authorized to perform this action.' }))