def revision_result(user, subject_key): """ Save the result and calculate points """ subject = Utils.urlsafe_to_key(subject_key).get() post = Utils.parse_json(request) points_counter = 0 for result in post['result']: if result['marked'] == "read": concept = Utils.urlsafe_to_key(result['key']).get() data = UserConcept.query(UserConcept.concept == concept.key, ancestor=user.key).get() if not data: data = UserConcept(parent=user.key, subject=subject.key, concept=concept.key) data.read += 1 data.put() user.addPoints(1, "Read concept") points_counter += 1 return Respond.success({ "new_points": points_counter, "balance": user.getPoints() })
def test_result(user, subject_key): """ Save the result and calculate points """ # subject = Utils.urlsafe_to_key(subject_key).get() post = Utils.parse_json(request) points_counter = 0 for result in post['result']: if result['marked'] == "right": UserTests(concept=Utils.urlsafe_to_key(result['key']), right=True, parent=user.key).put() user.addPoints(1, "Answered correctly") points_counter += 1 if result['marked'] == "wrong": UserTests(concept=Utils.urlsafe_to_key(result['key']), right=False, parent=user.key).put() user.addPoints(1, "Answered incorrectly") points_counter += 1 return Respond.success({ "new_points": points_counter, "balance": user.getPoints() })
def mark_concept_important(user, concept_key): """ Mark a concept as important """ concept = Utils.urlsafe_to_key(concept_key).get() # Get the user data for the concept user_data = UserConcept.query(UserConcept.concept == concept.key, ancestor=user.key).get() if not user_data: subject = concept.key.parent().parent() user_data = UserConcept(subject=subject, concept=concept.key, important=True, parent=user.key) user_data.put() else: if user_data.important: return Respond.error('Concept already marked important') user_data.important = True user_data.put() return Respond.success('Concept marked important')
def payment_status(user, payment_key): payment_request = Utils.urlsafe_to_key(payment_key).get() return Respond.success({ 'status': payment_request.status, 'balance': user.getPoints() })
def subject_test(user, subject_key): """ Send test concepts """ subject_key = Utils.urlsafe_to_key(subject_key) session_data = user.getSession() if session_data['views'] < 5: return Respond.error("Not enough views left", error_code=400) # Find revised concepts concepts = UserConcept.query(UserConcept.subject == subject_key, ancestor=user.key).fetch() # select 5 randomly if len(concepts) < 5: return Respond.error("Less than 5 concepts read", error_code=400) # Unique indices random_nums = random.sample(range(1, len(concepts)), 5) test_concepts = [] for i in random_nums: test_concepts.append(concepts[i].concept.get().to_dict()) user.subtractSessionViews(5) return Respond.success({ "concepts": test_concepts, "session_data": user.getSession() })
def subject_index(user, subject_key): subject = Utils.urlsafe_to_key(subject_key).get() index = [] chapters = Chapter.query(ancestor=subject.key).order(Chapter.srno) # user_data = UserConcept.query( # UserConcept.subject == subject.key, # ancestor=user.key # ).fetch() for chapter in chapters: concepts = [] concepts_list = Concept.query(ancestor=chapter.key).order(Concept.srno) for concept in concepts_list: concepts.append({"key": concept.key, "name": concept.name}) for concept in concepts: # for data in user_data: # if data.concept == concept['key']: # concept['read'] = data.read # concept['important'] = data.important concept['key'] = concept['key'].urlsafe() index.append({ "key": chapter.key.urlsafe(), "name": chapter.name, "concepts": concepts }) return Respond.success({"index": index})
def get_chapters(user, subject_key): subject = Utils.urlsafe_to_key(subject_key).get() chapters = map( Chapter.for_list, Chapter.query(ancestor=subject.key).order(Chapter.srno).fetch()) return Respond.success({"chapters": chapters})
def read_concept(user, concept_key): """ Send the concept to the user """ if not user_has_views(user, 1): return Respond.error('Not enough views left', error_code=420) concept = Utils.urlsafe_to_key(concept_key).get() return Respond.success({"concept": concept.to_dict()})
def add_code_for_user(user, user_key): """ Add a code for a user to user """ post = Utils.parse_json(request) UserCodes(parent=Utils.urlsafe_to_key(user_key), code=post['code'], points=post['points']).put() return Respond.success("Code added")
def subscribe(user, course_key): """ Subscribe a course :param user: :param course_key: :return: Response """ post = Utils.parse_json(request) user.course = Utils.urlsafe_to_key(course_key) user.college = post['college'] user.put() return Respond.success("Course subscribed by user")
def right_concept(user, concept_key): """ Mark a concept as right """ # get the concept data entity concept_data = UserConceptData.query( UserConceptData.concept == Utils.urlsafe_to_key(concept_key), ancestor=user.key).get() if not concept_data: return Respond.error(error="No data of user for this concept") # increase right count concept_data.right = concept_data.right + 1 concept_data.put() # return return Respond.success("Marked right")
def wrong_concept(user, concept_key): """ Mark a concept as wrong """ # get the concept data entity concept_data = UserConceptData.query( UserConceptData.concept == Utils.urlsafe_to_key(concept_key), ancestor=user.key).get() if not concept_data: return Respond.error(error="No data of user for this concept") # mark done as false concept_data.done = False concept_data.put() # return return Respond.success("Marked wrong")
def done_concept(user, concept_key): """ Mark a concept as done """ # get the concept data entity concept_data = UserConceptData.query( UserConceptData.concept == Utils.urlsafe_to_key(concept_key), ancestor=user.key).get() if not concept_data: return Respond.error(error="No data of user for this concept") # mark it as understood concept_data.done = True concept_data.put() # return return Respond.success("Marked done")
def subject_revise(user, subject_key): """ Send revision concepts """ # Find not read concepts # Fetch the first 5 from them # Send concepts subject = Utils.urlsafe_to_key(subject_key).get() session_data = user.getSession() if session_data['views'] < 5: return Respond.error("Not enough views left", error_code=420) user_concepts = UserConcept.query(UserConcept.subject == subject.key, ancestor=user.key).fetch() chapters = Chapter.query(ancestor=subject.key).order(Chapter.srno) revision_concepts = [] for chapter in chapters: concepts = Concept.query(ancestor=chapter.key).order( Concept.srno).fetch() for concept in concepts: if len(revision_concepts) < 5: if any(x.concept == concept.key for x in user_concepts): pass else: revision_concepts.append(concept.to_dict()) else: break if len(revision_concepts) is 0: return Respond.error("No concepts left to revise", error_code=400) user.subtractSessionViews(len(revision_concepts)) return Respond.success({ "concepts": revision_concepts, "session_data": user.getSession() })
def get_concepts(user, chapter_key): """ Get the concepts of a chapter :param chapter_key: :param user: :return: """ chapter = ndb.Key(urlsafe=chapter_key).get().single_dic() chapter['concepts'] = [] concepts = Concept.query(ancestor=Utils.urlsafe_to_key(chapter_key)).order( Concept.srno).fetch() for concept in concepts: chapter['concepts'].append(concept.to_dict()) return Respond.success({'chapter': chapter})
def save_data_offline(user, subject_key): """ Send a json file to download all concept data of the subject """ if not user.pro: return Respond.error('User not a pro', error_code=410) subject = Utils.urlsafe_to_key(subject_key).get() index = [] user_data_list = UserConcept.query(UserConcept.subject == subject.key, ancestor=user.key).fetch() user_data = {} for data in user_data_list: user_data[data.concept.urlsafe()] = { 'important': data.important, 'read': data.read } chapters = Chapter.query(ancestor=subject.key).order(Chapter.srno) for chapter in chapters: concepts = [] concept_list = Concept.query(ancestor=chapter.key).order(Concept.srno) for concept in concept_list: concept_data = concept.to_dict() key = concept_data['key'] if key in user_data: concept_data.update(user_data[key]) concepts.append(concept_data) index.append({ "name": chapter.name, "key": chapter.key.urlsafe(), "concepts": concepts }) return Respond.success(index)
def get_batch_concepts(user): """ Send concepts required by in the request :param user: :return: """ post = Utils.parse_json(request) concepts = post['concepts'] if not user_has_views(user, len(concepts)): return Respond.error('Not enough views left', error_code=420) response = [] for concept in concepts: entity = Utils.urlsafe_to_key(concept).get() response.append(entity.to_dict()) return Respond.success({"concepts": response})
def save_result(user): """ Save the result for the user :param user: :return: """ post = Utils.parse_json(request) points_counter = 0 for result in post['result']: if result['marked'] == "read": concept = Utils.urlsafe_to_key(result['key']).get() data = UserConcept.query(UserConcept.concept == concept.key, ancestor=user.key).get() if not data: data = UserConcept(parent=user.key, concept=concept.key) data.read += 1 data.put() user.addPoints(1, "Read concept") points_counter += 1 user.subtractSessionViews(len(post['result'])) return Respond.success({ "new_points": points_counter, "balance": user.getPoints(), "session": user.getSession() })