Python app.models.User() Examples

The following are 30 code examples of app.models.User(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module app.models , or try the search function .
Example #1
Source File: webapp.py    From dash_on_flask with MIT License 6 votes vote down vote up
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))

    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            error = 'Invalid username or password'
            return render_template('login.html', form=form, error=error)

        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('main.index')
        return redirect(next_page)

    return render_template('login.html', title='Sign In', form=form) 
Example #2
Source File: test_api.py    From circleci-demo-python-flask with MIT License 6 votes vote down vote up
def test_users(self):
        # add two users
        r = Role.query.filter_by(name='User').first()
        self.assertIsNotNone(r)
        u1 = User(email='john@example.com', username='john',
                  password='cat', confirmed=True, role=r)
        u2 = User(email='susan@example.com', username='susan',
                  password='dog', confirmed=True, role=r)
        db.session.add_all([u1, u2])
        db.session.commit()

        # get users
        response = self.client.get(
            url_for('api.get_user', id=u1.id),
            headers=self.get_api_headers('susan@example.com', 'dog'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertTrue(json_response['username'] == 'john')
        response = self.client.get(
            url_for('api.get_user', id=u2.id),
            headers=self.get_api_headers('susan@example.com', 'dog'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertTrue(json_response['username'] == 'susan') 
Example #3
Source File: load_agagd_data.py    From online-ratings with MIT License 6 votes vote down vote up
def get_or_make_user(self, aga_id):
        """Gets or creates a fake User object for an AGA ID,
        along with an AGA player
        If the AGA ID has had one or more PIN changes, the most recent ID will
        be used.
        """
        while aga_id in self._pin_changes:
            aga_id = self._pin_changes[aga_id]
        if aga_id in self._users:
            return self._users[aga_id]
        else:
            user = User(aga_id=aga_id, email=uuid4(), fake=True)

            db.session.add(user)
            db.session.commit()

            player = Player(id=aga_id, name='', user_id=user.id, server_id=self.server_id, token=uuid4())
            db.session.add(player)

            self._users[aga_id] = user
            return user 
Example #4
Source File: test_users_dao.py    From notifications-api with MIT License 6 votes vote down vote up
def test_create_user(notify_db_session, phone_number):
    email = 'notify@digital.cabinet-office.gov.uk'
    data = {
        'name': 'Test User',
        'email_address': email,
        'password': 'password',
        'mobile_number': phone_number
    }
    user = User(**data)
    save_model_user(user, password='password', validated_email_access=True)
    assert User.query.count() == 1
    user_query = User.query.first()
    assert user_query.email_address == email
    assert user_query.id == user.id
    assert user_query.mobile_number == phone_number
    assert user_query.email_access_validated_at == datetime.utcnow()
    assert not user_query.platform_admin 
Example #5
Source File: test_user_model.py    From flask-pycon2014 with MIT License 6 votes vote down vote up
def test_gravatar(self):
        u = User(email='john@example.com', password='cat')
        with self.app.test_request_context('/'):
            gravatar = u.gravatar()
            gravatar_256 = u.gravatar(size=256)
            gravatar_pg = u.gravatar(rating='pg')
            gravatar_retro = u.gravatar(default='retro')
        with self.app.test_request_context('/',
                                           base_url='https://example.com'):
            gravatar_ssl = u.gravatar()
        self.assertTrue('http://www.gravatar.com/avatar/' +
                        'd4c74594d841139328695756648b6bd6'in gravatar)
        self.assertTrue('s=256' in gravatar_256)
        self.assertTrue('r=pg' in gravatar_pg)
        self.assertTrue('d=retro' in gravatar_retro)
        self.assertTrue('https://secure.gravatar.com/avatar/' +
                        'd4c74594d841139328695756648b6bd6' in gravatar_ssl) 
Example #6
Source File: test_user_model.py    From flask-pycon2014 with MIT License 6 votes vote down vote up
def test_moderation(self):
        db.create_all()
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat',
                  is_admin=True)
        t = Talk(title='t', description='d', author=u1)
        c1 = Comment(talk=t, body='c1', author_name='n',
                     author_email='e@e.com', approved=True)
        c2 = Comment(talk=t, body='c2', author_name='n',
                     author_email='e@e.com', approved=False)
        db.session.add_all([u1, u2, t, c1, c2])
        db.session.commit()
        for_mod1 = u1.for_moderation().all()
        for_mod1_admin = u1.for_moderation(True).all()
        for_mod2 = u2.for_moderation().all()
        for_mod2_admin = u2.for_moderation(True).all()
        self.assertTrue(len(for_mod1) == 1)
        self.assertTrue(for_mod1[0] == c2)
        self.assertTrue(for_mod1_admin == for_mod1)
        self.assertTrue(len(for_mod2) == 0)
        self.assertTrue(len(for_mod2_admin) == 1)
        self.assertTrue(for_mod2_admin[0] == c2) 
Example #7
Source File: test_talk_model.py    From flask-pycon2014 with MIT License 6 votes vote down vote up
def test_unsubscribe(self):
        db.create_all()
        u = User(email='john@example.com', username='john', password='cat')
        t = Talk(title='t', description='d', author=u)
        c1 = Comment(talk=t, body='c1', author_name='n',
                     author_email='e@e.com', approved=True, notify=True)
        c2 = Comment(talk=t, body='c2', author_name='n',
                     author_email='e2@e2.com', approved=False, notify=True)
        c3 = Comment(talk=t, body='c3', author_name='n',
                     author_email='e@e.com', approved=False, notify=True)
        db.session.add_all([u, t, c1, c2, c3])
        db.session.commit()
        token = t.get_unsubscribe_token(u'e@e.com')
        Talk.unsubscribe_user(token)
        comments = t.comments.all()
        for comment in comments:
            if comment.author_email == 'e@e.com':
                self.assertTrue(comment.notify == False)
            else:
                self.assertTrue(comment.notify == True) 
Example #8
Source File: views.py    From braindump with MIT License 6 votes vote down vote up
def password_reset_request():
    if not current_user.is_anonymous:
        return redirect(url_for('main.index'))
    form = PasswordResetRequestForm()
    if form.validate_on_submit():
        user = User.query.filter_by(
            email=form.email.data.lower().strip()).first()
        if user:
            token = user.generate_reset_token()
            send_email(user.email, 'Reset Your Password',
                       'auth/email/reset_password',
                       user=user, token=token,
                       next=request.args.get('next'))
        flash('An email with instructions to reset your password has been '
              'sent to you.')
        return redirect(url_for('auth.login'))
    return render_template('auth/reset_password.html', form=form) 
Example #9
Source File: test_user_model.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def test_password_salts_are_random(self):
        u = User(password='password')
        u2 = User(password='password')
        self.assertTrue(u.password_hash != u2.password_hash) 
Example #10
Source File: test_model_user.py    From braindump with MIT License 5 votes vote down vote up
def test_password_reset(self):
        u = User(password='password')
        old_password_hash = u.password_hash
        reset_token = u.generate_reset_token()
        u.reset_password(reset_token, 'new_password')
        self.assertTrue(u.password_hash != old_password_hash) 
Example #11
Source File: test_user_model.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def test_valid_confirmation_token(self):
        u = User(password='password')
        db.session.add(u)
        db.session.commit()
        token = u.generate_confirmation_token()
        self.assertTrue(u.confirm_account(token)) 
Example #12
Source File: test_model_user.py    From braindump with MIT License 5 votes vote down vote up
def test_no_password_getter(self):
        u = User(password='password')
        with self.assertRaises(AttributeError):
            u.password 
Example #13
Source File: views.py    From braindump with MIT License 5 votes vote down vote up
def password_reset(token):
    if not current_user.is_anonymous:
        return redirect(url_for('main.index'))
    form = PasswordResetForm()
    if form.validate_on_submit():
        user = User.query.filter_by(
            email=form.email.data.lower().strip()).first()
        if user is None:
            return redirect(url_for('main.index'))
        if user.reset_password(token, form.password.data):
            flash('Your password has been updated.')
            return redirect(url_for('auth.login'))
        else:
            return redirect(url_for('main.index'))
    return render_template('auth/reset_password.html', form=form) 
Example #14
Source File: manage.py    From braindump with MIT License 5 votes vote down vote up
def make_shell_context():
    return dict(
        app=app, db=db, User=User,
        Note=Note, Tag=Tag,
        Notebook=Notebook) 
Example #15
Source File: client_base.py    From braindump with MIT License 5 votes vote down vote up
def add_other_user(self):
        u = User(
            email='other@example.com', password='password', confirmed=True)
        app.db.session.add(u)
        app.db.session.commit()
        return u 
Example #16
Source File: test_model_user.py    From braindump with MIT License 5 votes vote down vote up
def test_password_setter(self):
        u = User(password='password')
        self.assertTrue(u.password_hash is not None) 
Example #17
Source File: test_model_user.py    From braindump with MIT License 5 votes vote down vote up
def test_password_salts_are_random(self):
        u = User(password='password')
        u2 = User(password='password')
        self.assertTrue(u.password_hash != u2.password_hash) 
Example #18
Source File: api_base.py    From braindump with MIT License 5 votes vote down vote up
def add_user(self):
        u = User(email='test@example.com', password='password', confirmed=True)
        db.session.add(u)
        db.session.commit()
        return u 
Example #19
Source File: test_user_model.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def test_valid_reset_token(self):
        u = User(password='password')
        db.session.add(u)
        db.session.commit()
        token = u.generate_password_reset_token()
        self.assertTrue(u.reset_password(token, 'notpassword'))
        self.assertTrue(u.verify_password('notpassword')) 
Example #20
Source File: test_user_model.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def test_password_verification(self):
        u = User(password='password')
        self.assertTrue(u.verify_password('password'))
        self.assertFalse(u.verify_password('notpassword')) 
Example #21
Source File: test_user_model.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def test_no_password_getter(self):
        u = User(password='password')
        with self.assertRaises(AttributeError):
            u.password() 
Example #22
Source File: test_user_model.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def test_password_setter(self):
        u = User(password='password')
        self.assertTrue(u.password_hash is not None) 
Example #23
Source File: test_pending_email_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_queue(self):
        db.create_all()
        u = User(email='john@example.com', username='john', password='cat')
        t1 = Talk(title='t1', description='d', author=u)
        t2 = Talk(title='t2', description='d', author=u)
        p = PendingEmail(name='n', email='e@e.com', subject='s',
                         body_text='t', body_html='h', talk=t1)
        db.session.add_all([u, t1, t2, p])
        db.session.commit()
        self.assertTrue(
            PendingEmail.already_in_queue('e@e.com', t1) == True)
        self.assertTrue(
            PendingEmail.already_in_queue('e2@e2.com', t1) == False)
        self.assertTrue(
            PendingEmail.already_in_queue('e@e.com', t2) == False) 
Example #24
Source File: test_comment_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_notification_list(self):
        db.create_all()
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat')
        t = Talk(title='t', description='d', author=u1)
        c1 = Comment(talk=t, body='c1', author_name='n1',
                     author_email='e@e.com', approved=True)
        c2 = Comment(talk=t, body='c2', author_name='n2',
                     author_email='e2@e2.com', approved=True, notify=False)
        c3 = Comment(talk=t, body='c3', author=u2, approved=True)
        c4 = Comment(talk=t, body='c4', author_name='n4',
                     author_email='e4@e4.com', approved=False)
        c5 = Comment(talk=t, body='c5', author=u2, approved=True)
        c6 = Comment(talk=t, body='c6', author_name='n6',
                     author_email='e6@e6.com', approved=True, notify=False)
        db.session.add_all([u1, u2, t, c1, c2, c3, c4, c5])
        db.session.commit()
        email_list = c4.notification_list()
        self.assertTrue(('e@e.com', 'n1') in email_list)
        self.assertFalse(('e2@e2.com', 'n2') in email_list)  # notify=False
        self.assertTrue(('susan@example.com', 'susan') in email_list)
        self.assertFalse(('e4@e4.com', 'n4') in email_list)  # comment author
        self.assertFalse(('e6@e6.com', 'n6') in email_list)
        email_list = c5.notification_list()
        self.assertFalse(('john@example.com', 'john') in email_list)
        self.assertTrue(('e4@e4.com', 'n4') in email_list)  # comment author 
Example #25
Source File: test_talk_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_bad_unsubscribe_token(self):
        talk, email = Talk.unsubscribe_user('an invalid token')
        self.assertIsNone(talk)
        self.assertIsNone(email)
        u = User(email='john@example.com', username='john', password='cat')
        t = Talk(title='t', description='d', author=u)
        token = t.get_unsubscribe_token('e@e.com')
        talk, email = Talk.unsubscribe_user(token)
        self.assertIsNone(talk)
        self.assertIsNone(email) 
Example #26
Source File: test_user_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_user_loader(self):
        db.create_all()
        u = User(email='john@example.com', username='john', password='cat')
        db.session.add(u)
        db.session.commit()
        self.assertTrue(load_user(u.id) == u) 
Example #27
Source File: test_user_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_password_salts_are_random(self):
        u = User(password='cat')
        u2 = User(password='cat')
        self.assertTrue(u.password_hash != u2.password_hash) 
Example #28
Source File: test_user_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_password_verification(self):
        u = User(password='cat')
        self.assertTrue(u.verify_password('cat'))
        self.assertFalse(u.verify_password('dog')) 
Example #29
Source File: test_user_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_password_setter(self):
        u = User(password='cat')
        self.assertTrue(u.password_hash is not None) 
Example #30
Source File: test_api.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_delete(self):
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat')
        t = Talk(title='t', description='d', author=u1)
        c = Comment(talk=t, body='c1', author_name='n',
                    author_email='e@e.com', approved=False)
        db.session.add_all([u1, u2, t, c])
        db.session.commit()

        # wrong user --> 403
        token = u2.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='DELETE',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 403)

        token = u1.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='DELETE',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 200)
            c = Comment.query.get(c.id)
            self.assertIsNone(c)