def add_language(lang): # Authenticate the user user_profile = verify_auth_token() if not user_profile: return json_result({ 'success': 0, 'error' : 'authentication failed', }) # Get user properties email = user_profile['email'] print lang print user_profile['langs'] if lang not in user_profile['langs']: # Append the language to the array user_profile['langs'].append(lang) user_profile['langs'] = f7(user_profile['langs']) # Perform the upsert mongo.db.user_profiles.update( { 'email': email }, user_profile, upsert = True, ) # Return success return json_result({ 'success': 1, })
def update_progress(): # Authenticate the user user_profile = verify_auth_token() if user_profile is None: return json_result({ 'success': 0, 'error' : 'authentication failed', }) # Get user properties email = user_profile['email'] try: # Read the parameters card_updates_json = request.form['progress_updates'] deck_updates_json = request.form['deck_updates'] lang = request.form['lang'] # Decode JSON parameter card_updates = json.loads(card_updates_json) deck_updates = json.loads(deck_updates_json) except KeyError: # Return failure if the arguments don't exist return json_result({ 'success': 0, 'error' : 'invalid parameters', }) if deck_updates != {}: for deck_id, phrase_ids in deck_updates.iteritems(): deck = mongo.db.excerpts.update( { '_id': ObjectId(deck_id) }, { '$set': { 'phrase_ids': [ObjectId(p) for p in phrase_ids] } }, ) if card_updates != {}: # Get user progress, or initialize an empty progress history if none exists user_progress = mongo.db.user_progress.find_one({ 'email': email, 'lang': lang }) for studyMode in card_updates.keys(): # Iterate over the phrase ids and the new progress values, altering values for phrase_id, progress in card_updates[studyMode].iteritems(): user_progress['progress'][studyMode][phrase_id] = progress # Perform the upsert mongo.db.user_progress.update( { 'email': email, 'lang': lang }, user_progress, upsert = True ) # Return success return json_result({ 'success': 1, })
def get_dictionary(lang): # Authenticate the user user_profile = verify_auth_token() if not user_profile: return json_result({ 'success': 0, 'error' : 'authentication failed', }) # Return success return send_from_directory(os.path.join(app.appconfig.template_folder, 'dictionaries'), "%s.json" % lang)
def get_plan(): # Authenticate the user user_profile = verify_auth_token() if user_profile is None: return json_result({ 'success': 0, 'error' : 'authentication failed', }) try: # Read the parameters lang = request.form['lang'] planMode = request.form['plan_mode'] except KeyError: # Return failure if the arguments don't exist return json_result({ 'success': 0, 'error' : 'invalid parameters', }) email = user_profile['email'] # Create the documents dcitionary docs_cursor = mongo.db.documents.find({ 'lang': lang, 'email': email }) doc_dict = { str(d['_id']): d for d in docs_cursor } # Create the excerpts dictionary excerpt_cursor = mongo.db.excerpts.find({ 'lang': lang, 'email': email }) excerpt_dict = { str(e['_id']): e for e in excerpt_cursor } plan = {} coll = mongo.db["phrases_%s" % lang] for document_id in doc_dict.keys(): excerpt_cursor = mongo.db.excerpts.find({ 'document_id': ObjectId(document_id) }).sort('_id', pymongo.ASCENDING) plan[document_id] = [str(e['_id']) for e in excerpt_cursor] # Return success return json_result({ 'success': 1, 'result' : { 'docs': doc_dict, 'excerpts': excerpt_dict, 'plan': plan, }, })
def fitbit_connect(): """Redirect handler for fitbit oauth2, accepts code and exchanges for token""" token = request.cookies.get("token") user = auth.verify_auth_token(token) if token else None if not user: raise Exception("Need a user") code = request.args.get("code") token = exchange_code_for_access_token(code) user.fit_access_token = token["access_token"] user.fit_refresh_token = token["refresh_token"] user.save() return redirect("/#/home")
def home_page(): # Authentication user_profile = auth.verify_auth_token() if user_profile == None: return flask.redirect(flask.url_for('unauthorized_page')) # Get permissions permissions = auth.get_permissions(user_profile) # Render the page if permissions == None: return flask.redirect(flask.url_for('unauthorized_page')) else: return flask.render_template('phrase.html', permissions = permissions, )
def get_excerpt_dictionary(): # Authenticate the user user_profile = verify_auth_token() if user_profile is None: return json_result({ 'success': 0, 'error' : 'authentication failed', }) try: # Read the parameters lang = request.form['lang'] except KeyError: # Return failure if the arguments don't exist return json_result({ 'success': 0, 'error' : 'invalid parameters', }) email = user_profile['email'] excerpts = mongo.db.excerpts.find({ 'lang': lang, 'email': email }) phrase_ids = [] for excerpt in excerpts: phrase_ids += excerpt['phrase_ids'] print len(set(phrase_ids)) coll = mongo.db["phrases_%s" % lang] d = {} for phrase_id_chunk in dbutils.chunk_list(phrase_ids, 1000): cursor = coll.find({ '_id': { '$in': phrase_id_chunk } }) d.update(dictionary.create_dictionary_from_cursor(lang, cursor)) print len(set(d.keys())) return json_result({ 'success': 1, 'result' : d, })
def phrase_page(lang, base): # Authentication user_profile = auth.verify_auth_token() if user_profile == None or not auth.has_permission(user_profile, 'MANAGE_DICTIONARY'): return flask.redirect(flask.url_for('unauthorized_page')) # Get the phrase from the database phrase = mongo.db.phrases.find_one({ 'lang': lang, 'base': base }) section = dbutils.get_section_for_phrase(mongo, phrase) if phrase and flask.request.method == 'POST': # Apply changes from the form for k, txs in phrase['txs'].iteritems(): for tx in txs: orig_rank = int(tx['rank']) tx['deleted'] = flask.request.form["tx-deleted-%s-%d" % (k, orig_rank)] == '1' tx['text'] = flask.request.form["tx-%s-%d" % (k, orig_rank)] tx['rank'] = int(flask.request.form["tx-rank-%s-%d" % (k, orig_rank)]) phrase['txs'][k] = sorted(txs, key=lambda tx: tx['rank']) # Perform the update mongo.db.phrases.update( { 'lang': lang, 'base': base }, phrase, ) # Parse parameters show_deleted = int(flask.request.args.get('show-deleted', 0)) # Pass the translation to the template return flask.render_template('phrase.html', lang = lang, phrase = phrase, section = section, show_deleted = show_deleted )
def get_progress(): # Authenticate the user user_profile = verify_auth_token() if user_profile is None: return json_result({ 'success': 0, 'error' : 'authentication failed', }) # Get user properties email = user_profile['email'] try: # Read the parameters lang = request.form['lang'] except KeyError: # Return failure if the arguments don't exist return json_result({ 'success': 0, 'error' : 'invalid parameters', }) # Get user progress, or initialize an empty progress history if none exists user_progress = mongo.db.user_progress.find_one({ 'email': email, 'lang': lang }) if user_progress == None: user_progress = { 'email' : email, 'lang' : lang, 'progress': { 'defs': { }, 'pron': { }, }, } mongo.db.user_progress.insert(user_progress) # Return success return json_result({ 'success': 1, 'result' : user_progress, })
def phrases_page(lang): # Authentication user_profile = auth.verify_auth_token() if user_profile == None or not auth.has_permission(user_profile, 'manage_dictionary'): return flask.redirect(flask.url_for('unauthorized_page')) min_phrase = flask.request.args.get('min-phrase') max_phrase = flask.request.args.get('max-phrase') if min_phrase and max_phrase: min_phrase = int(min_phrase) max_phrase = int(max_phrase) else: min_phrase = 0 max_phrase = 99 phrases = mongo.db.phrases.find({ 'lang': lang, 'rank': { '$gt': min_phrase-1, '$lt': max_phrase+1 } }).sort('rank', 1) processed_phrases = [] for phrase in phrases: if dbutils.get_section_for_phrase(mongo, phrase): phrase['has_section'] = 1 if 'txs' in phrase: phrase['has_txs'] = 1 processed_phrases.append(phrase) # Render the template by passing the total phrase counts return flask.render_template('phrases.html', lang = lang, min_phrase = min_phrase, max_phrase = max_phrase, phrase_counts = processed_phrases, )
def manage_page(): user_profile = auth.verify_auth_token() if user_profile == None: return flask.redirect(flask.url_for('login_page')) else: return flask.redirect(flask.url_for('phrases_page', lang='fr'))
def add_document(): # Authenticate the user user_profile = verify_auth_token() if user_profile is None: return json_result({ 'success': 0, 'error' : 'authentication failed', }) # Get user properties and params email = user_profile['email'] try: title = request.form['title'] text = request.form['text'] lang = request.form['lang'] except KeyError: # Return failure if the arguments don't exist return json_result({ 'success': 0, 'error' : 'invalid parameters', }) # 1. segment the document into excerpts given the language excerpts = api_utils.segment_doc(text, lang) # Insert the document document_id = mongo.db.documents.insert({ 'title': title, 'text': text, 'lang': lang, 'email': email }) # 2. Get the set of unique phrases to be looked up phrases_list, unique_phrases = api_utils.get_phrases_from_excerpts(excerpts, lang) # 3. Look up all phrases and get phrase-to-ID mapping phrase_id_map = api_utils.get_phrase_ids(unique_phrases, lang) # 3.5 TODO(kgu) handle phrases not found # 4. Insert each excerpt with corresponding phrase ids num_ph = sum([len(phrases) for phrases in phrases_list]) num_hit = 0 missed_phrases = set() new_excerpts = [] bulk = mongo.db.excerpts.initialize_unordered_bulk_op() for excerpt, phrases in zip(excerpts, phrases_list): phrase_ids = [phrase_id_map[ph] for ph in phrases if phrase_id_map[ph] != None] bulk.insert({ 'email': email, 'lang': lang, 'excerpt': excerpt, 'phrase_ids': phrase_ids, 'document_id': document_id}) num_hit += len([1 for ph in phrases if phrase_id_map[ph] != None]) missed_phrases |= set([ph for ph in phrases if phrase_id_map[ph] == None]) bulk.execute() # Return success, stats for analysis, and missing phrases. return json_result({ 'success': 1, 'document_id': document_id, # excerpts stats 'num_excerpts': len(excerpts), 'total_phrase_count': num_ph, 'total_hit_count': num_hit, 'total_hit_rate': num_hit / float(num_ph), # missed phrases 'missed_phrases': list(missed_phrases) })