コード例 #1
0
def login():
    regform = RegisterForm()
    error = None
    loginerror = None
    loginform = LoginForm(request.form)
    if request.method == 'POST':
        if loginform.validate_on_submit():
            user = User.query.filter_by(
                username=loginform.username.data).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, loginform.password.data):
                login_user(user, remember=True)
                if 'url' in session:
                    return redirect(session['url'])
                return redirect(url_for('home.home'))
            else:
                login_email = User.query.filter_by(
                    email=loginform.username.data).first()
                if login_email is not None and bcrypt.check_password_hash(
                        login_email.password, loginform.password.data):
                    login_user(login_email)
                    if 'url' in session:
                        return redirect(session['url'])
                    return redirect(url_for('users.profile'))
                else:
                    loginerror = 'invalid credentials! please try again.'
        else:
            return "not validated"
    return render_template('auth.html',
                           regform=regform,
                           loginform=loginform,
                           error=error,
                           loginerror=loginerror)
コード例 #2
0
ファイル: views.py プロジェクト: Alisao/masterlywriters
def admin_login():
    regform = AdminRegForm()
    error = None
    loginerror = None
    loginform = AdminLoginForm(request.form)
    if request.method == 'POST':
        if loginform.validate_on_submit():
            admin_username = loginform.username.data
            admin_user = Admin.query.filter_by(username=admin_username).first()

            if admin_user is not None and bcrypt.check_password_hash(
                    admin_user.password, loginform.password.data):
                session["admin_session_active"] = True
                return redirect(url_for('admin.dashboard'))
            else:
                admin_email = Admin.query.filter_by(
                    email=admin_username).first()

                if admin_email is not None and bcrypt.check_password_hash(
                        admin_email.password, loginform.password.data):
                    session["admin_session_active"] = True
                    return redirect(url_for('admin.dashboard'))
                else:
                    loginerror = 'invalid credentials! please try again.'
        else:
            return "not validated"
    return render_template('admin_auth.html',
                           regform=regform,
                           loginform=loginform,
                           error=error,
                           loginerror=loginerror)
コード例 #3
0
ファイル: views.py プロジェクト: xiaofji/project_539-
def login():
    formr = RegisterForm(request.form)
    forms = LoginForm(request.form)

    error = None

    if request.method == 'POST':
            if request.form['submit'] == 'login':
                if forms.validate_on_submit():
                    user = User.query.filter_by(username=request.form['username']).first()
                    print user
                    if user is not None and bcrypt.check_password_hash(
                        user.password, request.form['password']
                    ):
                        login_user(user)
                        return redirect(url_for('home.personalpage'))
                    else:
                        error = 'Invalid username or password.'
            elif request.form['submit'] == 'signup':
                if formr.validate_on_submit():
                    user = User(
                        username=formr.username.data,
                        password=formr.password.data
                    )
                    db.session.add(user)
                    db.session.commit()
                    login_user(user)
                    return redirect(url_for('home.default'))

    return render_template('register.html', formr=formr, form=forms, user=current_user)
コード例 #4
0
ファイル: views.py プロジェクト: xiaofji/project_539-
def default():
    form = LoginForm(request.form)
    error = None
    if request.method == 'POST':
        if request.form['submit'] == 'login':
            if form.validate_on_submit():
                user = User.query.filter_by(
                    username=request.form['username']).first()
                print user
                if user is not None and bcrypt.check_password_hash(
                        user.password, request.form['password']):
                    login_user(user)
                else:
                    error = 'Invalid username or password.'
        elif request.form['submit'] == 'signup':
            return redirect(url_for('users.login'))
        elif request.form['submit'] == 'logout':
            logout_user()
            return render_template("index.html",
                                   name="index",
                                   title="Chinese Cooking",
                                   form=form,
                                   user=current_user)

    return render_template("index.html",
                           name="index",
                           title="Chinese Cooking",
                           error=error,
                           form=form,
                           user=current_user)
コード例 #5
0
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']):
                # session['logged_in'] = True
                login_user(user)
                flash('You were logged in.')
                return redirect(url_for('home.home'))
            else:
                error = 'Invalid Credentials. Please try again.'

    # TO MAKE THE LOGIN AND REGISTER PAGE ON THE SAME PAGE
    form = RegisterForm()
    if form.validate_on_submit():
        user = User(name=form.username.data,
                    email=form.email.data,
                    password=form.password.data)
        db.session.add(user)
        db.session.commit()
        login_user(user)
        return redirect(url_for('home.home'))

    return render_template('login.html', form=form, error=error)
コード例 #6
0
def login():
    formr = RegisterForm(request.form)
    forms = LoginForm(request.form)

    error = None

    if request.method == 'POST':
        if request.form['submit'] == 'login':
            if forms.validate_on_submit():
                user = User.query.filter_by(
                    username=request.form['username']).first()
                print user
                if user is not None and bcrypt.check_password_hash(
                        user.password, request.form['password']):
                    login_user(user)
                    return redirect(url_for('home.personalpage'))
                else:
                    error = 'Invalid username or password.'
        elif request.form['submit'] == 'signup':
            if formr.validate_on_submit():
                user = User(username=formr.username.data,
                            password=formr.password.data)
                db.session.add(user)
                db.session.commit()
                login_user(user)
                return redirect(url_for('home.default'))

    return render_template('register.html',
                           formr=formr,
                           form=forms,
                           user=current_user)
コード例 #7
0
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        user = User.query.filter(User.name == request.form['username']).first()
        if user is not None and bcrypt.check_password_hash(user.password, request.form['password']):
            login_user(user)
            flash('You were logged in. You gat it Booy!')
            return redirect(url_for('home.home'))
        else:
            error = 'Invalid username or password.'
    return render_template('login.html', form=form, error=error)
コード例 #8
0
ファイル: views.py プロジェクト: xiaofji/project_539-
def yuecai(LearningRecipeName):
    error = None
    ingresobj = Ingre.query.filter(Ingre.recipes.any(recipename=LearningRecipeName)).all()
    ingres = []
    ingresurl = []
    ingresorder = Recipe.query.filter_by(recipename=LearningRecipeName).first().ingresorder.split(" ")
    instruction = Recipe.query.filter_by(recipename=LearningRecipeName).first().instruction.split("|")
    print instruction

    for i in ingresorder:
        for obj in ingresobj:

            if obj.id == int(i):
                ingres.append(obj.ingrename)
                ingresurl.append(obj.url)


    form = LoginForm(request.form)
    formR = AddRecipeForm(request.form)
    if request.method == 'POST':
        if request.form['submit'] == 'login':
            if form.validate_on_submit():
                user = User.query.filter_by(username=request.form['username']).first()
                print user
                if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']
                ):
                    login_user(user)

                else:
                    error = 'Invalid username or password.'
        elif request.form['submit'] == 'signup':
            return redirect(url_for('users.login'))
        elif request.form['submit'] == 'logout':
            logout_user()
            return redirect(url_for("home.default"))
        else:
            user=User.query.filter_by(username=current_user.username).first()
            recipe = Recipe.query.filter_by(recipename=request.form['submit']).first()
            if recipe is not None:
                recipes = Recipe.query.filter(Recipe.users.any(username=current_user.username)).all()

                if recipe not in recipes:
                    user.add_recipes([recipe])

                    # commit the changes
                    db.session.commit()
            return redirect(url_for("home.personalpage"))
    if LearningRecipeName == "sliced_cold_chicken" or LearningRecipeName == "bitter_shrimp_ball" or LearningRecipeName == "sichuan_fish":
        return render_template("yuecai.html", title = "Cooking", form=form, instruction=instruction, user=current_user, error=error, LearningRecipeName = LearningRecipeName, ingres=ingres, ingresurl=ingresurl)
    else:
        return render_template("coming.html", title = "Cooking", form=form, user=current_user, error=error, LearningRecipeName = LearningRecipeName)
コード例 #9
0
ファイル: views.py プロジェクト: cjohns38/flask-intro
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(user.password, request.form['password']):
                #session['logged_in'] = True
                login_user(user)
                flash('You were logged in.')
                return redirect(url_for('home.home'))
            else:
                error = 'Invalid Creditionals. Please try again.'
    return render_template('login.html', form=form, error=error)
コード例 #10
0
ファイル: views.py プロジェクト: kevlab/discover_flask
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(
               user.password, request.form['password']):
                login_user(user)
                flash('You were just logged in')
                return redirect(url_for('home.home'))  # blueprint.viewfuntion
            else:
                error = 'Invalid credentials, Please try again.'
    return render_template('login.html', form=form, error=error)
コード例 #11
0
ファイル: views.py プロジェクト: jreiher2003/flask-heroku
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(user.password, request.form['password']):
                login_user(user)
                flash('You were logged in. Go Crazy.', 'bg-success')
                return redirect(url_for('home.home'))

            else:
                error = 'Invalid username or password.'
    return render_template('login.html', form=form, error=error)
コード例 #12
0
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']):
                login_user(user)
                flash('You were signed in. Go crazy.')
                return redirect(url_for('home.home'))
            else:
                error = 'Invalid Credentials. Please try again.'
    return render_template('login.html', form=form, error=error)
コード例 #13
0
def home():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(email=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']):
                login_user(user)
                flash("You were just logged in!")
                return redirect(url_for('account.account'))
            else:
                error = "Invalid credentials. Please try again."
    return render_template("index.html", form=form, error=error)
コード例 #14
0
ファイル: views.py プロジェクト: fcs23800/flask-intro
def login():
	error = None
	form = LoginForm(request.form)
	if request.method == 'POST':
		if form.validate_on_submit():
			user = User.query.filter_by(name=request.form['username']).first()
			if user is not None and bcrypt.check_password_hash(user.password, request.form['password']):
				#session['logged_in'] = True
				login_user(user)
				flash('You were logged in.')
				return redirect(url_for('home.home'))
			else:
				error = 'Invalid username or password.'
	return render_template('login.html', form=form, error=error)
コード例 #15
0
ファイル: views.py プロジェクト: jpirih/Flask-Blog
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(user.password, request.form['password']):
                login_user(user)
                flash("Prijava uspela pijavljen is kot {}".format(current_user.name))
                return redirect(url_for('blog.blog_home'))
            error = 'Napaka: Nepravilen uporabnik oziroma geslo. Prosim poskusi znova'
        else:
            return render_template('login.html', form=form, error=error)

    return render_template('login.html', form=form, error=error)
コード例 #16
0
ファイル: views.py プロジェクト: TroveLem/MiniBlog
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = Users.query.filter_by(username=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']
            ):
                login_user(user)
                flash('You successfully logged in!')
                # return redirect(url_for('.profile', user=user))
                return redirect(url_for('.home'))
            else:
                error = 'Invalid username or password.'
    return render_template('login.html', error=error, form=form)
コード例 #17
0
def login():
    error = None
    form = LoginForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            user = db.session.query(User).filter_by(
                name=form.username.data).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, form.password.data):
                # session['logged_in'] = True
                login_user(user)
                flash("You were just logged in")
                return redirect(url_for("home.home"))
            else:
                error = "Invalid Crediantial. Please try again"
    return render_template("login.html", form=form, error=error)
コード例 #18
0
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        user = User.query.filter(
            User.username == request.form['username']).first()
        if user is not None and bcrypt.check_password_hash(
                user.password, request.form['password']):
            login_user(user)
            session['logged_in'] = True
            flash('You are now logged in', 'success')
            return redirect(url_for('dashboard.dashboard'))
        else:
            error = 'Invalid username or password.'

    return render_template('login.html', form=form, error=error)
コード例 #19
0
ファイル: views.py プロジェクト: wilcoxky/flask-intro
def login():
  error = None
  form = LoginForm(request.form)
  if request.method == 'POST':
      if form.validate_on_submit():
          # Flask login does not handle authentication so we do this
          # Using Bcrypt to check
          user = User.query.filter_by(name=request.form['username']).first()
          if user is not None and bcrypt.check_password_hash(
            user.password, request.form['password']):
              login_user(user)
              # session['logged_in'] = True
              flash("You were just logged in")
              return redirect(url_for('home.home'))
          else:
            error = 'Invalid Credentials. Please try again.'
  return render_template('login.html',form=form, error=error)
コード例 #20
0
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']):
                login_user(user)
                flash("Prijava uspela pijavljen is kot {}".format(
                    current_user.name))
                return redirect(url_for('blog.blog_home'))
            error = 'Napaka: Nepravilen uporabnik oziroma geslo. Prosim poskusi znova'
        else:
            return render_template('login.html', form=form, error=error)

    return render_template('login.html', form=form, error=error)
コード例 #21
0
ファイル: views.py プロジェクト: eashan/IOTA_1
def login():
    error = None

    form = LoginForm(request.form)

    if request.method == 'POST':
        if form.validate_on_submit():
            user = NGO.query.filter_by(name=request.form['username']).first()
            if (user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password'].encode('utf-8'))):
                session['logged_in'] = True
                flash('You were logged in.')
                return redirect(url_for('home.home'))
            else:
                error = 'Invalid Credentials. Please try again.'

    return render_template('login.html', form=form, error=error)
コード例 #22
0
ファイル: views.py プロジェクト: xiaofji/project_539-
def personalpage():
    error = None
    recipes = None
    form = LoginForm(request.form)
    if current_user is None:
        title = 'personalpage'
    else:
        title = current_user.username
    if request.method == 'POST':
        if request.form['submit'] == 'login':
            if form.validate_on_submit():
                user = User.query.filter_by(
                    username=request.form['username']).first()
                print user
                if user is not None and bcrypt.check_password_hash(
                        user.password, request.form['password']):
                    login_user(user)
                    title = user

                else:
                    error = 'Invalid username or password.'
        elif request.form['submit'] == 'signup':
            return redirect(url_for('users.login'))
        elif request.form['submit'] == 'logout':
            logout_user()
            return redirect(url_for('home.default'))

    recipes = Recipe.query.filter(
        Recipe.users.any(username=current_user.username)).all()
    recipesNotLearnt = Recipe.query.filter(~Recipe.users.any(
        username=current_user.username)).all()
    recipesname = []
    recipesNotLearntname = []
    for recipe in recipes:
        recipesname.append(recipe.recipename)
    for recipe in recipesNotLearnt:
        recipesNotLearntname.append(recipe.recipename)
    # recipes = db.session.query(Recipe).all()
    # return render_template('personalpage.html', recipes=recipes, recipesNotLearnt = recipesNotLearnt, form=form, user=current_user)  # render a template

    return render_template('personalpage.html',
                           title=title,
                           recipes=recipesname,
                           recipesNotLearnt=recipesNotLearntname,
                           form=form,
                           user=current_user)  # render a template
コード例 #23
0
 def post(self):
     # get the post data
     post_data = request.get_json()
     if post_data:
         email = post_data.get("email")
         password = post_data.get("password")
     else:
         email = request.form["email"]  # "*****@*****.**"
         password = request.form["password"]  # "pwd"  #
     rd = request.args.get("rd")
     try:
         # fetch the user data
         user = User.query.filter_by(email=email).first()
         if user and bcrypt.check_password_hash(user.password, password):
             auth_token = user.encode_auth_token(user.id)
             if auth_token:
                 if rd:
                     redir = redirect(rd)
                 else:
                     redir = "You are logged in!"
                 response = make_response(redir)
                 response.set_cookie(
                     "Authorization",
                     "Bearer {}".format(auth_token.decode()))
                 return response
                 # return make_response(jsonify(responseObject)), 200
         else:
             if user is None:
                 response_object = {
                     'status': 'fail',
                     'message': 'User {} does not exist.'.format(email)
                 }
             else:
                 response_object = {
                     'status': 'fail',
                     'message': 'Please enter correct password.'
                 }
             return make_response(jsonify(response_object), 404)
     except Exception as e:
         print(e)
         response_object = {
             'status': 'fail',
             'message': 'Error:{}'.format(str(e))
         }
         return make_response(jsonify(response_object), 500)
コード例 #24
0
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(
                username=request.form['username']).first()
            remember_me = form.remember.data
            if user is not None and bcrypt.check_password_hash(
                user.password, request.form['password']
            ):
                login_user(user, remember=remember_me)
                flash('Login successful', 'success')
                return redirect(url_for('home.home'))

            else:
                error = 'Invalid username or password.'
    return render_template('login.html', form=form, error=error, now=datetime.utcnow())
コード例 #25
0
ファイル: views.py プロジェクト: sindani254/flask_api
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']):
                session["logged_in"] = True
                login_user(user)
                flash("login successful!")
                return redirect(url_for(request.args.get('next',
                                                         "home.index")))
            else:
                error = "invalid credentials. please try again!"
        else:
            error = "invalid credentials. please try again!"
    return render_template('login.html', form=form, error=error)
コード例 #26
0
ファイル: views.py プロジェクト: xiaofji/project_539-
def default():
    form = LoginForm(request.form)
    error = None
    if request.method == 'POST':
        if request.form['submit'] == 'login':
            if form.validate_on_submit():
                user = User.query.filter_by(username=request.form['username']).first()
                print user
                if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']
                ):
                    login_user(user)
                else:
                    error = 'Invalid username or password.'
        elif request.form['submit'] == 'signup':
            return redirect(url_for('users.login'))
        elif request.form['submit'] == 'logout':
            logout_user()
            return render_template("index.html", name = "index", title = "Chinese Cooking", form=form, user=current_user)


    return render_template("index.html", name = "index", title = "Chinese Cooking", error = error, form=form, user=current_user)
コード例 #27
0
ファイル: views.py プロジェクト: xiaofji/project_539-
def personalpage():
    error = None
    recipes = None
    form = LoginForm(request.form)
    if current_user is None:
        title = 'personalpage'
    else:
        title = current_user.username
    if request.method == 'POST':
        if request.form['submit'] == 'login':
            if form.validate_on_submit():
                user = User.query.filter_by(username=request.form['username']).first()
                print user
                if user is not None and bcrypt.check_password_hash(
                    user.password, request.form['password']
                ):
                    login_user(user)
                    title = user

                else:
                    error = 'Invalid username or password.'
        elif request.form['submit'] == 'signup':
            return redirect(url_for('users.login'))
        elif request.form['submit'] == 'logout':
            logout_user()
            return redirect(url_for('home.default'))

    recipes = Recipe.query.filter(Recipe.users.any(username=current_user.username)).all()
    recipesNotLearnt = Recipe.query.filter(~Recipe.users.any(username=current_user.username)).all()
    recipesname = []
    recipesNotLearntname = []
    for recipe in recipes:
        recipesname.append(recipe.recipename)
    for recipe in recipesNotLearnt:
        recipesNotLearntname.append(recipe.recipename)
    # recipes = db.session.query(Recipe).all()
    # return render_template('personalpage.html', recipes=recipes, recipesNotLearnt = recipesNotLearnt, form=form, user=current_user)  # render a template

    return render_template('personalpage.html', title=title, recipes=recipesname, recipesNotLearnt = recipesNotLearntname, form=form, user=current_user)  # render a template
コード例 #28
0
ファイル: views.py プロジェクト: sznote/flask
def login():
	error = None
	form = LoginForm(request.form)

	if request.method ==  'POST':
		if form.validate_on_submit():
		 	user = User.query.filter_by(name=request.form['username']).first()
		 	if user is not None and bcrypt.check_password_hash(user.password,request.form['password'] ):
			# if request.form['username'] != 'admin' or  request.form['password'] != 'admin':
			# 	error = 'Invalid credentials Plases try again.'
			# 	print request.form['username']
			# else:
			#	session['logined_in'] = True
				login_user(user)
				flash('You are login Success!!')
				return redirect(url_for('home.home'))
			else:
				error = "Invalid credentials Plases try again."
		else:
			return render_template("login.html",form=form, error=error)
					
	return render_template("login.html",form=form, error=error)
コード例 #29
0
ファイル: views.py プロジェクト: Melvie/BeerShed
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form['username']).first()
            if user is not None and bcrypt.check_password_hash(user.password, request.form['password']):
                #session['logged_in'] = True
                login_user(user)
                flash('You were logged in.')
                identity_changed.send(current_app._get_current_object(), identity=Identity(user.id))
                if user.role == "admin":
                    print user.role
                    flash('Welcome Admin!')
                    return redirect(url_for('home.home'))

                else:
                    #render_template('guest.html',form=form, error=error)
                    return redirect(url_for('home.guest'))

            else:
                error = 'Invalid username or password.'
    return render_template('login.html', form=form, error=error)
コード例 #30
0
ファイル: views.py プロジェクト: Alisao/masterlywriters
def change_account_password():
    profile_form = ProfileForm(request.form)
    change_pswd_form = ChangePasswordForm(request.form)
    error = None
    invalid_pswd_msg = None
    duplicate_phone_error = None

    # get all orders
    all_orders = get_client_order(current_user.id)
    posts_count = len(all_orders)

    try:
        if request.method == 'POST' and change_pswd_form.validate_on_submit():
            old_pswd = change_pswd_form.old_password.data
            new_hashed_pswd = bcrypt.generate_password_hash(
                change_pswd_form.new_password.data).decode('utf-8')
            user = User.query.filter_by(id=current_user.id).first()

            if user is not None and bcrypt.check_password_hash(
                    user.password, old_pswd):
                user.password = new_hashed_pswd
                db.session.commit()
                flash('password successfully updated')
                return redirect(url_for('users.profile'))
            else:
                error = 'invalid password'
                invalid_pswd_msg = 'invalid password'
        return render_template("profile.html",
                               error=error,
                               all_orders=all_orders,
                               posts_count=posts_count,
                               profile_form=profile_form,
                               change_pswd_form=change_pswd_form,
                               duplicate_phone_error=duplicate_phone_error,
                               invalid_pswd_msg=invalid_pswd_msg)
    except Exception as e:
        return (str(e))
コード例 #31
0
ファイル: views.py プロジェクト: tanayjha/webdevel
def login():
    error = None
    form = LoginForm(request.form)
    if request.method == "POST":
        if form.validate_on_submit():
            user = User.query.filter_by(name=request.form["username"]).first()
            if user is not None and bcrypt.check_password_hash(user.password, request.form["password"]):
                # session['logged_in'] = True
                login_user(user)
                flash("You were logged in.")
                return redirect(url_for("home.home"))
            else:
                error = "Invalid Credentials. Please try again."

    # TO MAKE THE LOGIN AND REGISTER PAGE ON THE SAME PAGE
    form = RegisterForm()
    if form.validate_on_submit():
        user = User(name=form.username.data, email=form.email.data, password=form.password.data)
        db.session.add(user)
        db.session.commit()
        login_user(user)
        return redirect(url_for("home.home"))

    return render_template("login.html", form=form, error=error)
コード例 #32
0
ファイル: views.py プロジェクト: xiaofji/project_539-
def yuecai(LearningRecipeName):
    error = None
    ingresobj = Ingre.query.filter(
        Ingre.recipes.any(recipename=LearningRecipeName)).all()
    ingres = []
    ingresurl = []
    ingresorder = Recipe.query.filter_by(
        recipename=LearningRecipeName).first().ingresorder.split(" ")
    instruction = Recipe.query.filter_by(
        recipename=LearningRecipeName).first().instruction.split("|")
    print instruction

    for i in ingresorder:
        for obj in ingresobj:

            if obj.id == int(i):
                ingres.append(obj.ingrename)
                ingresurl.append(obj.url)

    form = LoginForm(request.form)
    formR = AddRecipeForm(request.form)
    if request.method == 'POST':
        if request.form['submit'] == 'login':
            if form.validate_on_submit():
                user = User.query.filter_by(
                    username=request.form['username']).first()
                print user
                if user is not None and bcrypt.check_password_hash(
                        user.password, request.form['password']):
                    login_user(user)

                else:
                    error = 'Invalid username or password.'
        elif request.form['submit'] == 'signup':
            return redirect(url_for('users.login'))
        elif request.form['submit'] == 'logout':
            logout_user()
            return redirect(url_for("home.default"))
        else:
            user = User.query.filter_by(username=current_user.username).first()
            recipe = Recipe.query.filter_by(
                recipename=request.form['submit']).first()
            if recipe is not None:
                recipes = Recipe.query.filter(
                    Recipe.users.any(username=current_user.username)).all()

                if recipe not in recipes:
                    user.add_recipes([recipe])

                    # commit the changes
                    db.session.commit()
            return redirect(url_for("home.personalpage"))
    if LearningRecipeName == "sliced_cold_chicken" or LearningRecipeName == "bitter_shrimp_ball" or LearningRecipeName == "sichuan_fish":
        return render_template("yuecai.html",
                               title="Cooking",
                               form=form,
                               instruction=instruction,
                               user=current_user,
                               error=error,
                               LearningRecipeName=LearningRecipeName,
                               ingres=ingres,
                               ingresurl=ingresurl)
    else:
        return render_template("coming.html",
                               title="Cooking",
                               form=form,
                               user=current_user,
                               error=error,
                               LearningRecipeName=LearningRecipeName)