def update(user, concept_key): """ Update a concept :param user: :param concept_key: :return: Updated concept """ concept = ndb.Key(urlsafe=concept_key).get() post = Utils.parse_json(request) if 'name' in post: concept.name = post['name'] if 'explanation' in post: concept.explanation = post['explanation'] if 'references' in post: references = [] for ref in post['references']: reference = References(title=ref['title'], source=ref['source']) references.append(reference) concept.references = references if 'tips' in post: concept.tips = post['tips'] if 'questions' in post: concept.questions = post['questions'] concept.put() return Respond.success({'concept': concept.to_dict()})
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 admin_login(): """ Login a user :return: """ # Pass the post details post = Utils.parse_json(request) # Check if email and password in the post if 'email' not in post or 'password' not in post: return Respond.error("Email password not found", error_code=422) # Find the user with that email user = User.query(User.email == post['email']).get() # If user not found if user is None: return Respond.error("User not found with the provided email", error_code=404) if user.type != "Admin" and user.type != "Creator": return Respond.error("Login with password not allowed", error_code=422) # If password not correct if not user.verify_password(post['password']): return Respond.error("Password incorrect") # Make token token = user.make_token() # Respond with user and token return Respond.success({"token": token, "user": user.as_dict()})
def activate_pro(user): """ Activate pro usage for the user :param user: :return: Response """ # Perform checks if user.getPoints() < PRO_COST: return Respond.error('Low balance', error_code=420) if user.pro: return Respond.error('User already a pro', error_code=410) post = Utils.parse_json(request) users_with_device = User.query(User.device == post['device']).fetch() if len(users_with_device) > 0: return Respond.error('Device already registered by another user') # Activate Pro for user on this device user.subtractPoints(PRO_COST, 'Activated Pro') user.pro = True user.device = post['device'] user.put() return Respond.success('Activated pro for user')
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 store(user): """ Store a course. :param user: :return: """ post = Utils.parse_json(request) course = Course(name=post['name']) course.put() return Respond.success(Course.as_dict(course))
def store(user): """ Store a subject. :param user: :return: """ post = Utils.parse_json(request) subject = Subject(name=post['name'], parent=user.course) subject.put() return Respond.success(subject.dict_for_list())
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 social_login(): """ Login via google With the id token get user details, if the user does not exist then create it. If the user is coming back then just save the id token Then make a token and send it to the client """ post = Utils.parse_json(request) id_token = post['id_token'] url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=%s' % id_token try: result = urlfetch.fetch(url) if result.status_code == 200: result = json.loads(result.content) name = result['name'] email = result['email'] picture = result['picture'] else: error = 'Status code: {} , Response: {}'.format( result.status_code, result.content) logging.error(error) return Respond.error('Error getting user info from google.') except urlfetch.Error: logging.exception('Caught exception fetching url') return Respond.error('Error getting user info from google.') users = User.query(User.email == email).fetch() if len(users) > 1: return Respond.error("There was an error", error_code=422) elif len(users) == 1: # User already exists. Just make a token and return user = users[0] # Make token token = user.make_token() # Respond with user and token return Respond.success({"token": token, "user": user.as_dict()}) else: # New User. Create and send token user = User(name=name, email=email, picture_uri=picture) user.put() # Make token token = user.make_token() # Respond with user and token return Respond.success({"token": token, "user": user.as_dict()})
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 edit_chapter(user, chapter_key): """ Edit the chapter :param user: :param chapter_key: :return: """ chapter = ndb.Key(urlsafe=chapter_key).get() post = Utils.parse_json(request) chapter.name = post['name'] chapter.put() return Respond.success(chapter.for_list())
def assign_rep(user): """ Assign rep to list of codes """ post = Utils.parse_json(request) codes = UserCodes.query(UserCodes.srno >= post['start'], UserCodes.srno <= post['end']) for code in codes: code.repName = post['repName'] code.put() return Respond.success('yo')
def store(user): """ Store a chapter :param user: :return: """ post = Utils.parse_json(request) subject = ndb.Key(urlsafe=post['subject_key']) srno = Chapter.query(ancestor=subject).count() chapter = Chapter(name=post['name'], srno=srno, parent=subject) chapter.put() return Respond.success(chapter.single_dic())
def store(user): """ Store a concept. :param user: :return: """ post = Utils.parse_json(request) if 'name' not in post or 'chapter_key' not in post: return Respond.error("Input not valid", error_code=422) chapter_key = ndb.Key(urlsafe=post['chapter_key']) srno = Concept.query(ancestor=chapter_key).count() concept = Concept(name=post['name'], srno=srno, parent=chapter_key) concept.put() return Respond.success({'concept': concept.to_dict()})
def admin_register(user): """ Get the post variables and register the user :return: User token """ # parse the json request post = Utils.parse_json(request) # Check if basic account info is in the post if 'name' not in post or 'email' not in post or 'password' not in post: return Respond.error("Validation Error", error_code=422) # Check if email has not registered before if User.query(User.email == post['email']).count() > 0: return Respond.error("User account with this email already registered", 401) # Create a user model user = User(name=post['name'], email=post['email'], password=User.hash_password(post['password']), type=post['type']) # Add other properties of the user if sent if 'year' in post: user.year = post['year'] if 'course' in post: user.course = post['course'] if 'college' in post: user.college = post['college'] if 'picture_uri' in post: user.picture_uri = post['picture_uri'] # Save the user user.put() # Make a token for the user token = user.make_token() # Respond with the token return Respond.success({"token": token, "user": user.as_dict()})
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 code_redeem(user): """ Check code for user and give points accordingly """ post = Utils.parse_json(request) code_data = UserCodes.query(UserCodes.code == post['code'], UserCodes.activated == False).get() if not code_data: promo_data = UserCodes.query(UserCodes.code == post['code'], ancestor=user.key).get() if not promo_data: return Respond.error('No code found', error_code=400) user.addPoints(promo_data.points, "Used code: " + promo_data.code) return Respond.success({ "new_points": promo_data.points, "balance": user.getPoints() }) code_data.activated = True code_data.activatedBy = user.key code_data.put() user.addPoints(code_data.points, "Used code: " + code_data.code) return Respond.success({ "new_points": code_data.points, "balance": user.getPoints() })
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() })