def post(self, request, format=None): #buy new plan userId = request.query_params.get('userId') # get parameters token = request.query_params.get('token') planId = request.query_params.get('planId') if not checkUserToken(userId, token): #check permission raise Exception("userid or token is invalid") instance_userplan = UserPlan.objects.filter(user_id=userId).exclude( status=1) #get records which status!=expire if 0 < len(instance_userplan) < 2: #if user has 1 active plan if planId is not None: instance_plan = Plan.objects.get(id=planId) #get selected plan latest_userplan = expireDate(userId) #get latest user plan new_row = createNewUserPlan( userId, planId, instance_plan.days, latest_userplan.expire_at) # create new plan instance_serializer = UserPlanSerializer( new_row) #serialize new plan return Response( instance_serializer.data, status=status.HTTP_201_CREATED) #return new plan elif len(instance_userplan) == 0: #if user dosen't have any plan instance_plan = Plan.objects.get(id=planId) # get selected plan new_row = createNewUserPlan(userId, planId, instance_plan.days, datetime.today()) # create new plan instance_serializer = UserPlanSerializer( new_row) #serialize new plan return Response(instance_serializer.data, status=status.HTTP_201_CREATED) #return new plan else: return Response("you have two active plan", status=status.HTTP_400_BAD_REQUEST ) #we couldn't add more than 2 plan
def resolve_questions(self, info, **kwargs): userId = kwargs.get('userId') # get kwargs token = kwargs.get('token') if not checkUserToken(userId, token): # check user token raise Exception("Your user_id or token is invalid") instance = Question.objects.all() # get all records return instance # return all records
def resolve_competitions(self,info,**kwargs): # get all records userId=kwargs.get('userId') token=kwargs.get('token') if not checkUserToken(userId,token): # check permission raise Exception("userid or token is invalid") instance=Competition.objects.order_by('-id') for item in instance: # for each competition get all questions item.question=Question.objects.filter(competition_id=item.id) return instance
def get(self, request, format=None): userId = request.query_params.get('userId') #get parameters token = request.query_params.get('token') if not checkUserToken(userId, token): #check permission raise Exception("userid or token is invalid") instance_plan = Plan.objects.all() #get list of plans instance_plan_serializer = PlanSerializer(instance_plan, many=True) #serialize list return Response(instance_plan_serializer.data, status=status.HTTP_200_OK) #return list
def resolve_competition_detail(self,info,id,**kwargs): # get single record userId=kwargs.get('userId') token=kwargs.get('token') if not checkUserToken(userId,token): # check permission raise Exception("userid or token is invalid") try: instance=Competition.objects.get(pk=id) # get record except Exception: raise Exception("competition Id is invalid") return instance
def get(self, request, format=None): userId = request.headers.get('userId') #get user token = request.headers.get('token') # get token if not checkUserToken(userId, token): # check if ok return Response('Your user_id or token is invalid', status=status.HTTP_401_UNAUTHORIZED) instance = Video.objects.all() # get all records serializer_instance = VideoSerializer( instance, many=True) # serialize all record return Response(serializer_instance.data, status=status.HTTP_200_OK) # return all records
def resolve_question_detail(self, info, id, **kwargs): userId = kwargs.get('userId') # get kwargs token = kwargs.get('token') if not checkUserToken(userId, token): # check user token raise Exception("Your user_id or token is invalid") if id is not None: # check id question try: instance = Question.objects.get(pk=id) # get record except Question.DoesNotExist: raise Exception("ID is invalid") return instance # return specific record
def get(self, request, format=None): #get all records userid = request.headers.get('userId') #get user id token = request.headers.get('token') #get token if not checkUserToken(userid, token): # check token return Response('Your user_id or token is invalid', status=status.HTTP_401_UNAUTHORIZED) instance = Competition.objects.order_by( '-id') # get all records order by id serializer_instance = CompetitionSerializer( instance, many=True) # serialize data return Response(serializer_instance.data, status=status.HTTP_200_OK) # return all records
def get(self, request, id, format=None): userId = request.headers.get('userId') token = request.headers.get('token') if not checkUserToken(userId, token): return Response('Your user_id or token is invalid', status=status.HTTP_401_UNAUTHORIZED) if id is not None: try: instance = Question.objects.get(pk=id) serializer_instance = QuestionSerializer(instance) return Response(serializer_instance.data, status=status.HTTP_200_OK) except Question.DoesNotExist: pass return Response("Question ID is invalid", status=status.HTTP_400_BAD_REQUEST)
def get(self, request, id, format=None): # get one record userid = request.headers.get('userId') #get user id token = request.headers.get('token') # get token if not checkUserToken(userid, token): # check token return Response('Your user_id or token is invalid', status=status.HTTP_401_UNAUTHORIZED) if id is not None: try: instance = Competition.objects.get( pk=id) # try to get specific record serializer_instance = CompetitionSerializer( instance) # serialize record return Response(serializer_instance.data, status=status.HTTP_200_OK) # return record except Competition.DoesNotExist: pass return Response("Competition ID is invalid", status=status.HTTP_400_BAD_REQUEST) # id invalid
def get( self, request, format=None ): # get latest expire date(if we have two expite date give the latest one) userId = request.query_params.get('userId') #get parameters token = request.query_params.get('token') if not checkUserToken(userId, token): #check permissions raise Exception("userid or token is invalid") instance = expireDate(userId) #get latest expire date is available if instance: instance_serializer = UserPlanSerializer( instance) # serialize latest plan return Response(instance_serializer.data, status=status.HTTP_200_OK) #return latest plan else: return Response( "Expire Pleae buy new plan") #if coudn't find any plan
def post(self, request, format=None): # get answers jason #try: userScore = 0 # define local score userId = request.headers.get('userId') token = request.headers.get('token') if not checkUserToken(userId, token): # check permission raise Exception("user id or token is invalid") data = request.data['answers'] #split json #print(data['answers']) for item in data: comptId = item['cycle_question_id'] try: userAnswer = UserAnswer.objects.filter(competition_id=comptId, user_id=userId) # check if save before or not # yani faghat yekbar mitavanad javab dahad print('useridddddd', userAnswer[0].user_id) break # if save before exit compelatly of for except IndexError: for x in item['data']: questionId = x['question'] answerId = x['answer'] try: #check is compId and questionId are related question = Question.objects.get(pk=questionId, competition_id=comptId) except Exception: raise Exception( "competition info is not matched with question info" ) try: # try to add record userAnswer = UserAnswer( competition_id=comptId, # if is not save before add question_id=questionId, answer=answerId, user_id=userId) userAnswer.save() # save userScore = calculateScore( userId, questionId, answerId) # calculate user score except Exception: raise Exception("couldnt import,wrong information") return Response(userScore, status=status.HTTP_201_CREATED) # return user score
def mutate(self, info, userId, token, answer): if not checkUserToken(userId, token): raise Exception("user id or token is invalid") answer = answer['answers'] userScore = 0 for item in answer: print('item=', item) comptId = item['cycle_question_id'] try: userAnswer_instance = UserAnswer.objects.filter( competition_id=comptId, user_id=userId) # check if save before or not # yani faghat yekbar mitavanad javab dahad print('useridddddd', userAnswer_instance[0].user_id) break # if save before exit compelatly of for except IndexError: for x in item['data']: print('x=', x) questionId = x['question'] answerId = x['answer'] try: #check is compId and questionId are related question = Question.objects.get(pk=questionId, competition_id=comptId) except Exception: raise Exception( "competition info is not matched with question info" ) try: # try to add record userAnswer_instance = UserAnswer( competition_id=comptId, # if is not save before add question_id=questionId, answer=answerId, user_id=userId) userAnswer_instance.save() # save userScore = calculateScore( userId, questionId, answerId) # calculate user score userAnswer_instance.message = str(userScore) print('userScore ', userScore) return RegisterAnswer(userAnswer=userAnswer_instance) except Exception: raise Exception("couldnt import,wrong information")
def resolve_video_detail(self, info, **kwargs): try: id = kwargs.get('id') # get parameters from kwargs userId = kwargs.get('userId') token = kwargs.get('token') except Exception: raise Exception("Your parameters are wrong") if not checkUserToken(userId, token): #check user token raise Exception("Your user_id or token is invalid") if id is not None: # chech id try: instance = Video.objects.get(pk=id) # get record if videoViewIncreament( userId, instance.id ): #check is user view this video befor or not instance.view += 1 # add number og view instance.save() # save updated record instance.message = "Increament Done" instance.message = "ok" # set message except Video.DoesNotExist: raise Exception("Video ID is invalid") return instance # return video
def get(self, request, id, format=None): userId = request.headers.get('userId') # get user token = request.headers.get('token') # get token if not checkUserToken(userId, token): # check if ok return Response('Your user_id or token is invalid', status=status.HTTP_400_BAD_REQUEST) if id is not None: try: instance = News.objects.get(pk=id) # get record if newsViewIncreament( userId, instance.id ): #check if user view this news befor or not instance.view += 1 # add number og view instance.save() # save updated record serializer_instance = NewsSerializer(instance) # serialize return Response(serializer_instance.data, status=status.HTTP_200_OK) # return record except News.DoesNotExist: pass return Response( "News Id is not valid", status=status.HTTP_400_BAD_REQUEST) # if news id is invalid
def resolve_videos(self, info, userId, token): if not checkUserToken(userId, token): # check user token raise Exception("Your user_id or token is invalid") instance = Video.objects.all() # get all videos return instance # return all videos