Ejemplo n.º 1
0
def register():
  # If user is already login then redirect user to the profile page
  if g.user is not None and g.user.is_authenticated():
    return redirect(url_for('users.home'))
  # Otherwise bring up the register form  
  form = RegisterForm(request.form)
  if form.validate_on_submit():
    # create an user instance not yet stored in the database
    user = User(nickname=form.nickname.data, fullname=form.fullname.data, email=form.email.data,\
      password=generate_password_hash(form.password.data), badges=categorizeEmail(form.email.data))
    # Insert the record in our database and commit it
    user.status =0
    try:
      db.session.add(user)
      db.session.commit()
    except:
      db.session.rollback()
      flash('Email hoac nickname bi trung')
      return render_template("users/register.html", form=form)

    # Log the user in, as he now has an id and name---> allow login when verify already, so not set the session
    #session['user_id'] = user.id
    #session['username'] = user.nickname
    #send email to verify
    
    follower_notification(form, 'follower_email.html')
    # flash will display a message to the user
    # flash('Thanks for registering')
    # redirect user to the 'home' method of the user module.
    flash('chuyentay.com has been send email co your email adress, please visit to verify your account')
    return redirect(url_for('users.home'))
  return render_template("users/register.html", form=form)
Ejemplo n.º 2
0
    def test_invalid_password_is_rejected(self):
        User.create(name="Joe", email="*****@*****.**", password="******")

        with self.client:
            self.client.post("/login/", data={"name": "Joe", "password": "******"})

            self.assertTrue(current_user.is_anonymous())
Ejemplo n.º 3
0
def get_user(user_id):
    # Set initial values to be modified as we read through blocks
    name = None
    balance = 0
    transactions = []

    # Loop through all blocks in the blockchain
    for block in blockchain.chain:
        # Loop through all transactions in each block
        for transaction in block["transactions"]:
            if transaction["type"] == "new_user" and transaction[
                    "user_id"] == user_id:
                # Save initial user data
                user_id = transaction["user_id"]
                name = transaction["name"]
                balance += transaction["balance"]
            elif transaction["type"] == "transaction":
                if transaction["recipient"] == user_id:
                    # Add to the balance if this user received coins
                    balance += transaction["amount"]
                    transactions.append(transaction)
                elif transaction["sender"] == user_id:
                    # Subtract from the balance if this user spent coins
                    balance -= transaction["amount"]
                    transactions.append(transaction)

    # Create the resulting user object
    user = User(user_id, name, balance, transactions)
    return jsonify(user.serialize())
Ejemplo n.º 4
0
    def test_users_can_logout(self):
        User.create(name="Joe", email="*****@*****.**", password="******")

        with self.client:
            self.client.post("/login/", data={"name": "Joe", "password": "******"})
            self.client.get("/logout/")

            self.assertTrue(current_user.is_anonymous())
Ejemplo n.º 5
0
    def test_users_can_login(self):
        User.create(name="Joe", email="*****@*****.**", password="******")

        with self.client:
            response = self.client.post("/login/", data={"name": "Joe", "password": "******"})

            # self.assert_redirects(response, url_for("index"))
            self.assertTrue(current_user.name == "Joe")
            self.assertFalse(current_user.is_anonymous())
 def test_login_success(self):
     pw_hash = bcrypt.generate_password_hash('testpass')
     user = User(username='******', pw_hash=pw_hash)
     db.session.add(user)
     db.session.commit()
     res = self.login('testuser', 'testpass')
     soup = BeautifulSoup(res.data)
     d = soup.find('span', 'flashdata')
     assert d.text == u'Logged in successfully'
Ejemplo n.º 7
0
def register_view():
    form = RegistrationForm(request.form)
    if form.validate_on_submit():
        user = User(username=form.name.data, email=form.email.data, password=form.password.data)
        # form.populate_obj(user)
        db.session.add(user)
        db.session.commit()
        login_user(user)
        return redirect(url_for('index'))
    return render_template('users/register.html', form=form)
Ejemplo n.º 8
0
def before_api_request():
    if request.json is None:
        return errors.bad_request('Invalid JSON in body.')
    token = request.json.get('token')
    if not token:
        return errors.unauthorized('Authentication token not provided.')
    user = User.validate_api_token(token)
    if not user:
        return errors.unauthorized('Invalid authentication token.')
    g.current_user = user
Ejemplo n.º 9
0
def create():
    form = SignUpForm(request.form)
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if not user:
            user = User(form.first_name.data, form.last_name.data,
                        form.username.data,
                        generate_password_hash(form.password.data))
            db.session.add(user)
            db.session.commit()
            flash('Created account. Please signin')
            return redirect(url_for('auth.signin'))
        flash('Username exists', 'error-message')
    return render_template("auth/signup.html", form=form)
Ejemplo n.º 10
0
def adduser(email, username, admin=False):
    """Register a new user."""
    from getpass import getpass

    password = getpass()
    password2 = getpass(prompt='Confirm: ')
    if password != password2:
        import sys

        sys.exit('Error: passwords do not match.')
    db.create_all()
    user = User(email=email,
                username=username,
                password=password,
                is_admin=admin)
    db.session.add(user)
    db.session.commit()
    print('User {0} was registered successfully.'.format(username))
Ejemplo n.º 11
0
def create():
    form = RegistrationForm()
    if form.validate():
        if form.password.data == form.password_confirmation.data:
            user = User(form.first_name.data, form.last_name.data,
                        form.email.data, form.phone.data,
                        generate_password_hash(form.password.data))
            db.session.add(user)
            db.session.commit()
            if user:
                session['user_id'] = user.id
                flash(f'Welcome back {user.first_name}!')
                return redirect(url_for('pages.about'))
            else:
                flash('Database error', 'main_warning')
        else:
            flash('Password and password confirmation do not match',
                  'form_errors')
    else:
        flash(form.errors, 'form_errors')

    return render_template('users/new.html', form=form)