def login(): db.get_db() if current_user.is_authenticated: # URL may need to be altered to correct location return redirect(url_for('content.index')) form = LoginForm(request.form) if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user and global_bcrypt.check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) return redirect(url_for('content.index')) else: flash('Login unsuccessful. Please check email and password', 'danger') return render_template('auth/login.html', title='Log In', form=form)
def rateArticle(): try: mysql = db.get_db() # Establish db connection # Get user from database user = User.query.filter(User.id == current_user.id).first() # Get data from request body data = json.loads(request.data) content_id = data["content_id"] rating = data["rating"] # Get progress object and set new data progress = Progress(user_id=user.id, content_id=content_id, rating=rating, read_ct=1) # Add to session and commit mysql.session.add(progress) mysql.session.commit() # Return empty object with code 200 return response_wrapper({}, 200) except Exception as ex: return response_wrapper({"error": str(ex)}, 500) return response_wrapper({"error": "Something went wrong"}, 500)
def getArticlePages(): if current_user.is_authenticated: mysql = db.get_db() rows = mysql.session.query(Content).count() if rows: data = { "total_article_count" : rows} return response_wrapper(data, 200) return response_wrapper({"error": "Something went wrong"}, 500)
def getHistory(): try: history = [] rating_map = ["Just Right", "Too Hard", "Too Easy"] db.get_db() user = User.query.filter(User.id == current_user.id).first() progress = Progress.query.filter(Progress.user_id == user.id).all() for item in progress: obj = {} content = Content.query.filter(Content.id == item.content_id).first() obj["name"] = str(content.name.decode("utf-8")) obj["read_date"] = datetime.strftime(item.read_date,"%b %d %Y %H:%M:%S") obj["rating"] = rating_map[item.rating] history.append(obj) return response_wrapper(history, 200) except Exception as ex: return response_wrapper({"error": str(ex)}, 500) return response_wrapper({"error": "Something went wrong"}, 500)
def skill(user_id, lang_id): mysql = db.get_db() if user_id is not None and lang_id is not None: skill = request.form.get('skill') if skill is not None: language = Language.query.filter(Language.id == lang_id).first() user = User.query.filter(User.id == user_id).first() uls = UserLangSkill(user_id=user.id, language_id=language.id, skill=skill) mysql.session.add(uls) mysql.session.commit() return redirect(url_for('signup.interests', user_id=user_id)) return render_template('signup/skill.html')
def language(user_id): mysql = db.get_db() if user_id is not None: lang = request.form.get('lang') if lang is not None: language = Language.query.filter(Language.name == lang).first() return redirect( url_for('signup.skill', user_id=user_id, lang_id=language.id)) # For now, we'll just defaul to Chinese, English, and German. We'll need to update this route if we want to support all languages # default_countries = Country.query.filter((Country.alpha3code=='CHN') | (Country.alpha3code=='GBR') | (Country.alpha3code=='DEU')).all() default_countries = Country.query.filter(Country.alpha3code == 'GBR').all() languages = [{ 'name': c.languages[0].name, 'flag': c.flag_image } for c in default_countries] return render_template('signup/language.html', langs=languages)
def register(): form = RegistrationForm() mysql = db.get_db() '''if current_user.is_authenticated: return redirect(url_for('home.home'))''' if request.method == 'POST': flash('Test flash.', 'error') # Confirm username is unique username = request.form['username'] already_taken = User.query.filter_by(username=username).first() if already_taken: flash('Username already in use. Please choose a different one.', 'error') #reload form #return render_template('signup/signup.html', title='Sign Up') # Confirm email is unique email = request.form['inputEmail'] email_in_use = User.query.filter_by(email=email).first() if email_in_use: flash( 'Email already associated with an account. Please log in or use a different email address.', 'error') #return redirect(url_for('signup')) # Confirm registration password matches confirmation password # If password != confirmation password, flash error password = request.form['inputPassword'] if password != request.form['confirmPassword']: flash( 'Passwords entered do not match. Please correct and resubmit.', 'error') # redirect to signup form #return redirect(url_for('signup')) else: hashed_pwd = global_bcrypt.generate_password_hash(password).decode( 'utf-8') new_user = User(username=username, email=email, password=password) mysql.session.add(new_user) mysql.session.commit() #flash('Account created! Please set your preferences.', 'success') return redirect(url_for('signup.language')) return render_template('signup/signup.html', title='Sign Up', form=form)
def interests(user_id): mysql = db.get_db() if user_id is not None: interests = request.form.getlist('categories') if len(interests) > 0: for name in interests: category = Category.query.filter(Category.name == name).first() user = User.query.filter(User.id == user_id).first() user.categories.append(category) mysql.session.add(user) # Commit to db mysql.session.commit() return redirect(url_for("content.index")) # results = Category.query.all() results = Category.query.filter(Category.name == 'Sports').all() categories = [{'name': r.name, 'icon': r.icon_img} for r in results] return render_template('signup/interests.html', categories=categories)
def register(): if current_user.is_authenticated: # URL may need to be altered to correct location return redirect('/') form = RegistrationForm(request.form) mysql = db.get_db() if form.validate_on_submit(): # Hash password hashed_pw = global_bcrypt.generate_password_hash(form.password.data).decode('utf-8') # Create new user user = User(username=form.username.data, email=form.email.data, password=hashed_pw) mysql.session.add(user) mysql.session.commit() # Log in the user login_user(user) flash('Account has been created! You are now logged in. Please set your preferences now.', 'success') # Redirect to preferences dialogue return redirect(url_for('signup.language', user_id=user.id)) return render_template('signup/signup.html', title='Sign Up', form=form)
def getArticle(): try: db.get_db() # Establish database connection # Get param values page = request.args.get('page') language = request.args.get('language') category = request.args.get('category') last_id = request.args.get('last_id') direction = request.args.get('dir') # Get user user_info = {} user = User.query.filter(User.id == current_user.id).first() # Get user languages and categories user_languages = [] user_categories = [{"name": c.name, "selected": 0} for c in user.categories] # Get user level for selected language for l in iter(user.languages): selected = 0 l_item = Language.query.filter(Language.id == l.language_id).first() if language is not None: if Language.query.filter(l_item.iso639_2 == str(language)).first(): selected = 1 user_languages.append({"language_name": l_item.name, "iso": l_item.iso639_2, "skill": l.skill, "selected": selected}) # Finalize default language language = list(filter(lambda x: x["selected"] == 1, user_languages)) if len(language) == 0: language = user_languages[0] language["selected"] = 1 else: language = language[0] # Set Default category if category is None: user_categories[0]["selected"] = 1 category = user_categories[0] # Set user_info user_info["user"] = user user_info["language"] = language["iso"] user_info["category"] = category["name"] # return first 20 if int(page) == 1 and last_id is None and direction is None: contents = Content.query.filter(Content.language.has(Language.iso639_2 == language["iso"]), Content.level <= language["skill"]).order_by(Content.id.desc()).limit(20).all() return response_wrapper(contents_to_obj(contents, user_info, user_languages, user_categories), 200) # return next 20 based on last_id if direction == "next": contents = Content.query.filter(Content.language.has(Language.iso639_2 == language["iso"]), Content.level <= language["skill"], Content.id < last_id).order_by(Content.id.desc()).limit(20) # return prev 20 based on last_id if direction == "prev": contents = Content.query.filter(Content.language.has(Language.iso639_2 == language["iso"]), Content.level <= language["skill"], Content.id > last_id).order_by(Content.id.asc()).limit(20) contents = list(reversed(list(contents))) # reverse before returning to client. return response_wrapper(contents_to_obj(contents, user_info), 200) except Exception as ex: return response_wrapper({"error": str(ex)}, 500) return response_wrapper({"error": "Something went wrong"}, 500)