本文整理汇总了Python中pyramid_sqlalchemy.Session.query方法的典型用法代码示例。如果您正苦于以下问题:Python Session.query方法的具体用法?Python Session.query怎么用?Python Session.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid_sqlalchemy.Session
的用法示例。
在下文中一共展示了Session.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_register_new_user_email_not_verified
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_register_new_user_email_not_verified(self):
self.testapp.post('/__session', {
'next_url': 'http://localhost/foo/bar',
'user_info__provider': 'twitter',
'user_info__external_id': '1234',
'user_info__screen_name': 'John Doe',
'user_info__first_name': 'John',
'user_info__last_name': 'Doe',
'user_info__email': '[email protected]',
}, status=302)
# if no email is provided at registration, the email is
# not verified
res = self.testapp.post('/register', {
'first_name': 'John2',
'last_name': 'Doe2',
'email': '',
'submit': 'Register into Yith Library',
}, status=302)
self.assertEqual(res.status, '302 Found')
self.assertEqual(res.location, 'http://localhost/foo/bar')
self.assertEqual(Session.query(User).count(), 1)
user = Session.query(User).filter(User.first_name == 'John2').one()
self.assertEqual(user.first_name, 'John2')
self.assertEqual(user.last_name, 'Doe2')
self.assertEqual(user.email, '')
self.assertEqual(user.email_verified, False)
self.assertEqual(user.send_passwords_periodically, False)
identity = Session.query(ExternalIdentity).filter(
ExternalIdentity.external_id == '1234',
ExternalIdentity.provider == 'twitter',
).one()
self.assertEqual(identity.user, user)
示例2: test_register_new_user_wants_analytics_cookie
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_register_new_user_wants_analytics_cookie(self):
self.testapp.post('/__session', {
'next_url': 'http://localhost/foo/bar',
'user_info__provider': 'google',
'user_info__external_id': '1234',
'user_info__screen_name': 'John Doe',
'user_info__first_name': 'John',
'user_info__last_name': 'Doe',
'user_info__email': '',
USER_ATTR: True,
}, status=302)
# The user want the Google Analytics cookie
res = self.testapp.post('/register', {
'first_name': 'John3',
'last_name': 'Doe3',
'email': '[email protected]',
'submit': 'Register into Yith Library',
}, status=302)
self.assertEqual(res.status, '302 Found')
self.assertEqual(res.location, 'http://localhost/foo/bar')
self.assertEqual(Session.query(User).count(), 1)
user = Session.query(User).filter(User.first_name == 'John3').one()
self.assertFalse(user is None)
self.assertEqual(user.first_name, 'John3')
self.assertEqual(user.last_name, 'Doe3')
self.assertEqual(user.email, '[email protected]')
self.assertEqual(user.email_verified, False)
self.assertEqual(user.allow_google_analytics, True)
self.assertEqual(user.send_passwords_periodically, False)
identity = Session.query(ExternalIdentity).filter(
ExternalIdentity.external_id == '1234',
ExternalIdentity.provider == 'google',
).one()
self.assertEqual(identity.user, user)
示例3: test_password_delete_found
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_password_delete_found(self):
password = Password(service='myservice',
secret='s3cr3t',
user_id=self.user_id)
with transaction.manager:
Session.add(password)
Session.flush()
password_id = password.id
count_before = Session.query(Password).count()
self.assertEqual(count_before, 1)
res = self.testapp.delete('/passwords/%s' % str(password_id),
headers=self.auth_header)
self.assertEqual(res.status, '200 OK')
self.assertEqual(res.body, (b'{"password": {"id": "'
+ text_type(password_id).encode('ascii')
+ b'"}}'))
count_after = Session.query(Password).count()
self.assertEqual(count_after, 0)
try:
password = Session.query(Password).filter(
Password.id == password_id
).one()
except NoResultFound:
password = None
self.assertEqual(password, None)
示例4: test_clean_expired_access_codes
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_clean_expired_access_codes(self):
# add some access codes
with transaction.manager:
user1 = User(first_name='John1', last_name='Doe')
user2 = User(first_name='John2', last_name='Doe')
user3 = User(first_name='John3', last_name='Doe')
app = Application(name='Test Application')
user1.applications.append(app)
# access code that expires yesterday
ac1 = AccessCode(
code='123',
expiration=datetime.datetime(2015, 7, 13, 10, 0, 0),
application=app,
user=user1,
)
# access code that expires one second ago
ac2 = AccessCode(
code='456',
expiration=datetime.datetime(2015, 7, 14, 19, 59, 59),
application=app,
user=user2,
)
# access code that expires in 2 hours
ac3 = AccessCode(
code='789',
expiration=datetime.datetime(2015, 7, 14, 22, 0, 0),
application=app,
user=user3,
)
Session.add(user1)
Session.add(user2)
Session.add(user3)
Session.add(app)
Session.add(ac1)
Session.add(ac2)
Session.add(ac3)
self.assertEqual(Session.query(AccessCode).count(), 3)
sys.argv = ['notused', self.conf_file_path]
sys.stdout = StringIO()
result = clean_access_codes()
self.assertEqual(result, None)
stdout = sys.stdout.getvalue()
expected_output = """2 access codes were cleaned
"""
self.assertEqual(stdout, expected_output)
self.assertEqual(Session.query(AccessCode).count(), 1)
try:
access_code = Session.query(AccessCode).filter(
AccessCode.code == '789').one()
except NoResultFound:
access_code = None
self.assertNotEqual(access_code, None)
示例5: revoke_application
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def revoke_application(request):
app_id = request.matchdict['app']
try:
uuid.UUID(app_id)
except ValueError:
return HTTPBadRequest()
try:
app = Session.query(Application).filter(Application.id == app_id).one()
except NoResultFound:
return HTTPNotFound()
assert_authenticated_user_is_registered(request)
if 'submit' in request.POST:
authorized_apps = Session.query(AuthorizedApplication).filter(
AuthorizedApplication.application == app,
AuthorizedApplication.user == request.user
).all()
for authorized_app in authorized_apps:
Session.delete(authorized_app)
request.session.flash(
_('The access to application ${app} has been revoked',
mapping={'app': app.name}),
'success',
)
return HTTPFound(
location=request.route_path('oauth2_authorized_applications'))
return {'app': app}
示例6: empty
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def empty(self):
"""
Empty the collection (delete all items)
"""
entity_class = self.context.model
Session.query(entity_class).delete()
return Response(status=204)
示例7: test_merge_with_itself
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_merge_with_itself(self):
user = User()
Session.add(user)
Session.flush()
self.assertEqual(1, Session.query(User).count())
self.assertEqual(0, merge_accounts(user, [user.id]))
self.assertEqual(1, Session.query(User).count())
示例8: test_valid_request
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_valid_request(self):
_, user_id = create_and_login_user(self.testapp)
_, application_id, application_secret = create_client()
# First authorize the app
res = self.testapp.get('/oauth2/endpoints/authorization', {
'response_type': 'code',
'client_id': application_id,
'redirect_uri': 'https://example.com/callback',
})
self.assertEqual(res.status, '200 OK')
res = self.testapp.post('/oauth2/endpoints/authorization', {
'submit': 'Authorize',
'response_type': 'code',
'client_id': application_id,
'redirect_uri': 'https://example.com/callback',
'scope': 'read-passwords',
})
self.assertEqual(res.status, '302 Found')
grant = Session.query(AuthorizationCode).filter(
AuthorizationCode.application_id == application_id,
AuthorizationCode.user_id == user_id,
).one()
code = grant.code
# now send the token request
headers = {
'Authorization': auth_basic_encode(application_id,
application_secret),
}
res = self.testapp.post('/oauth2/endpoints/token', {
'grant_type': 'authorization_code',
'code': code,
}, headers=headers)
self.assertEqual(res.status, '200 OK')
self.assertEqual(res.headers['Cache-Control'], 'no-store')
self.assertEqual(res.headers['Pragma'], 'no-cache')
# the grant code should be removed
try:
grant = Session.query(AuthorizationCode).filter(
AuthorizationCode.application_id == application_id,
AuthorizationCode.user_id == user_id,
).one()
except NoResultFound:
grant = None
self.assertEqual(grant, None)
# and an access token should be created
self.assertEqual(res.json['token_type'], 'Bearer')
self.assertEqual(res.json['expires_in'], 3600)
access_code = Session.query(AccessCode).filter(
AccessCode.code == res.json['access_token'],
).one()
self.assertNotEqual(access_code, None)
示例9: test_revoke_application_app
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_revoke_application_app(self):
administrator = User(screen_name='Alice doe',
first_name='Alice',
last_name='Doe',
email='[email protected]')
user = User(screen_name='John Doe',
first_name='John',
last_name='Doe',
email='[email protected]')
app = Application(name='Test Application',
main_url='http://example.com',
callback_url='http://example.com/callback',
user=administrator)
auth_app = AuthorizedApplication(
scope=['read-passwords'],
response_type='code',
redirect_uri='http://example.com/callback',
application=app,
user=user,
)
with transaction.manager:
Session.add(user)
Session.add(app)
Session.add(auth_app)
Session.flush()
user_id = user.id
app_id = app.id
self.testapp.get('/__login/' + str(user_id))
self.assertEqual(Session.query(Application).count(), 1)
self.assertEqual(Session.query(AuthorizedApplication).count(), 1)
res = self.testapp.get('/oauth2/applications/%s/revoke' % str(app_id))
self.assertEqual(res.status, '200 OK')
res.mustcontain('Revoke authorization to application <span>Test Application</span>')
res = self.testapp.post('/oauth2/applications/%s/revoke' % str(app_id), {
'submit': 'Yes, I am sure',
})
self.assertEqual(res.status, '302 Found')
self.assertEqual(res.location, 'http://localhost/oauth2/authorized-applications')
try:
auth_app = Session.query(AuthorizedApplication).filter(
AuthorizedApplication.application_id == app_id,
AuthorizedApplication.user_id == user_id,
).one()
except NoResultFound:
auth_app = None
self.assertEqual(auth_app, None)
# the application should not be removed on cascade
self.assertEqual(Session.query(Application).count(), 1)
示例10: test_merge_with_invented_users
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_merge_with_invented_users(self):
user = User()
Session.add(user)
Session.flush()
fake_id = '00000000-0000-0000-0000-000000000000'
self.assertEqual(1, Session.query(User).count())
self.assertEqual(0, merge_accounts(user, [fake_id]))
self.assertEqual(1, Session.query(User).count())
示例11: test_already_authorized_app
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_already_authorized_app(self):
_, user_id = create_and_login_user(self.testapp)
_, application_id, _ = create_client()
count = Session.query(AuthorizedApplication).filter(
AuthorizedApplication.user_id == user_id,
).count()
self.assertEqual(count, 0)
# do an initial authorization
res = self.testapp.get('/oauth2/endpoints/authorization', {
'response_type': 'code',
'client_id': application_id,
'redirect_uri': 'https://example.com/callback',
})
self.assertEqual(res.status, '200 OK')
res = self.testapp.post('/oauth2/endpoints/authorization', {
'submit': 'Authorize',
'response_type': 'code',
'client_id': application_id,
'redirect_uri': 'https://example.com/callback',
'scope': 'read-passwords',
})
self.assertEqual(res.status, '302 Found')
count = Session.query(AuthorizedApplication).filter(
AuthorizedApplication.user_id == user_id,
).count()
self.assertEqual(count, 1)
# Now do a second authorization
res = self.testapp.get('/oauth2/endpoints/authorization', {
'response_type': 'code',
'client_id': application_id,
'redirect_uri': 'https://example.com/callback',
})
self.assertEqual(res.status, '302 Found')
count = Session.query(AuthorizedApplication).filter(
AuthorizedApplication.user_id == user_id,
).count()
self.assertEqual(count, 1)
grants = Session.query(AuthorizationCode).filter(
AuthorizationCode.application_id == application_id,
AuthorizationCode.user_id == user_id,
)
# There are two grants now
self.assertEqual(grants.count(), 2)
code = grants.all()[1].code
location = 'https://example.com/callback?code=%s' % code
self.assertEqual(res.location, location)
示例12: test_non_authorized_app_yet
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_non_authorized_app_yet(self):
_, user_id = create_and_login_user(self.testapp)
_, application_id, _ = create_client()
count = Session.query(AuthorizedApplication).filter(
AuthorizedApplication.user_id == user_id,
).count()
self.assertEqual(count, 0)
res = self.testapp.get('/oauth2/endpoints/authorization', {
'response_type': 'code',
'client_id': application_id,
'redirect_uri': 'https://example.com/callback',
})
self.assertEqual(res.status, '200 OK')
res.mustcontain('Authorize Application')
res.mustcontain('Permissions:')
res.mustcontain('Access your passwords')
res.mustcontain('Allow access')
res.mustcontain('No, thanks')
res.mustcontain('You can revoke this authorization in the future.')
res = self.testapp.post('/oauth2/endpoints/authorization', {
'submit': 'Authorize',
'response_type': 'code',
'client_id': application_id,
'redirect_uri': 'https://example.com/callback',
'scope': 'read-passwords',
})
self.assertEqual(res.status, '302 Found')
# Check that the app is authorized now
query = Session.query(AuthorizedApplication).filter(
AuthorizedApplication.user_id == user_id,
)
self.assertEqual(query.count(), 1)
auth = query[0]
self.assertEqual(auth.redirect_uri, 'https://example.com/callback')
self.assertEqual(auth.response_type, 'code')
self.assertEqual(auth.application.id, application_id)
self.assertEqual(auth.scope, ['read-passwords'])
self.assertEqual(auth.user_id, user_id)
self.assertEqual(auth.application_id, application_id)
# Check the right redirect url
grant = Session.query(AuthorizationCode).filter(
AuthorizationCode.application_id == application_id,
AuthorizationCode.user_id == user_id,
).one()
self.assertEqual(grant.application.id, application_id)
location = 'https://example.com/callback?code=%s' % grant.code
self.assertEqual(res.location, location)
示例13: test_backups_import_good_file
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_backups_import_good_file(self):
user_id = create_and_login_user(self.testapp)
content = get_gzip_data(text_type('[{"secret": "password1"}, {"secret": "password2"}]'))
res = self.testapp.post(
'/backup/import', {},
upload_files=[('passwords-file', 'good.json', content)],
status=302)
self.assertEqual(res.status, '302 Found')
self.assertEqual(res.location, 'http://localhost/backup')
self.assertEqual(2, Session.query(Password).count())
user = Session.query(User).filter(User.id == user_id).one()
self.assertEqual(len(user.passwords), 2)
示例14: test_merge_valid_users
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_merge_valid_users(self):
master_user = User()
Session.add(master_user)
other_user = User()
Session.add(other_user)
Session.flush()
self.assertEqual(2, Session.query(User).count())
self.assertEqual(1, merge_accounts(master_user, [other_user.id]))
self.assertEqual(1, Session.query(User).count())
示例15: test_google_analytics_preference_auth_users
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import query [as 别名]
def test_google_analytics_preference_auth_users(self):
# Authenticated users save the preference in the database
user_id = create_and_login_user(self.testapp, email='[email protected]')
res = self.testapp.post('/google-analytics-preference', {'yes': 'Yes'})
self.assertEqual(res.status, '200 OK')
self.assertEqual(res.json, {'allow': True})
user_refreshed = Session.query(User).filter(User.id == user_id).one()
self.assertEqual(user_refreshed.allow_google_analytics, True)
res = self.testapp.post('/google-analytics-preference', {'no': 'No'})
self.assertEqual(res.status, '200 OK')
self.assertEqual(res.json, {'allow': False})
user_refreshed = Session.query(User).filter(User.id == user_id).one()
self.assertEqual(user_refreshed.allow_google_analytics, False)