本文整理汇总了Python中models.User.select方法的典型用法代码示例。如果您正苦于以下问题:Python User.select方法的具体用法?Python User.select怎么用?Python User.select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.User
的用法示例。
在下文中一共展示了User.select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_select
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_select(self):
User.create(name='jack', email='[email protected]')
User.create(name='amy', email='[email protected]')
User.create(name='tom', email='[email protected]')
### select all fields
query = User.select()
result = query.execute()
assert result.count == 3
for user in result.all():
assert '%[email protected]' % user.name == user.email
### select part fields
query = User.select(User.name)
result = query.execute()
assert result.count == 3
jack = result.one()
assert jack.name == 'jack'
amy = result.one()
assert amy.name == 'amy'
tom = result.one()
assert tom.name == 'tom'
assert result.one() is None
### select with where
query = User.where(name='jack').select()
result = query.execute()
assert result.count == 1
assert result.one().name == 'jack'
示例2: populate_with_defaults
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def populate_with_defaults():
'''Create user admin and grant him all permission
If the admin user already exists the function will simply return
'''
logging.getLogger(__name__).debug("Populating with default users")
if not User.select().where(User.name == 'admin').exists():
admin = User.create(name='admin', password='admin')
admins = Group.create(name='admins')
starCap = Capability.create(domain='.+',
action=(Action.CREATE |
Action.READ |
Action.UPDATE |
Action.DELETE))
admins.capabilities.add(starCap)
admin.groups.add(admins)
admin.save()
if not User.select().where(User.name == 'anonymous').exists():
anon = User.create(name='anonymous', password='')
anons = Group.create(name='anonymous')
readCap = Capability.create(domain=Capability.simToReg('/volumes/*'),
action=Action.READ)
anons.capabilities.add(readCap)
anon.groups.add(anons)
anon.save()
示例3: test_database
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_database(self):
with test_database(TEST_DB, (User,)):
self.create_users()
self.assertEqual(User.select().count(), 2)
self.assertNotEqual(
User.select().get().password,
'password 1'
)
示例4: test_lcase_ucase
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_lcase_ucase(self):
self.create_data(4, table=1)
query = User.select(fn.ucase(User.name), User.name)
for user, func in query:
assert user.name.upper() == func.ucase
query = User.select(fn.lcase(User.name), User.name)
for user, func in query:
assert user.name.lower() == func.lcase
示例5: main
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def main():
user_sq = User.select().where(
User.member_type != 'Expired',
User.expire_at <= date.today()
)
for user in user_sq:
user.member_type = 'Expired'
user.save()
update_mailing_list(
[x.itsc for x in User.select(User.itsc).where(
User.member_type != 'Expired')])
示例6: test_bitwise_operator
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_bitwise_operator(self):
assert User.create(name='jack', email='[email protected]')
if db_type != 'sqlite': # sqlite3 dosen't know operator '^'
query = User.select(User.id.op('^')(1))
result = query.execute()
assert result.tuples()[0][0] == 0
query = User.select(User.id.op('&')(0))
result = query.execute()
assert result.tuples()[0][0] == 0
query = User.select(sql('').op('~-')(User.id))
result = query.execute()
assert result.tuples()[0][0] == 0
示例7: test_tuples
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_tuples(self):
assert User.create(name='jack', email='[email protected]')
assert User.create(name='amy', email='[email protected]')
assert User.create(name='tom', email='[email protected]')
query = User.select(User.id)
result = query.execute()
assert result.tuples() == ((1,), (2,), (3,))
示例8: get
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def get(self):
if User.select().count() != 0:
user = User.get(User.id == 1)
return render_template('profile/user.html',user=user)
else:
content = 'There are no users, please create a user'
return render_template('info.html', content=content)
示例9: test_bad_username
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_bad_username(self):
"""Test username error through our POST route"""
self.data['username'] = 'non*ascii*character'
with test_database(self.TEST_DB, (User,)):
rv = self.app.post('/register', data=self.data)
self.assertEqual(User.select().count(), 0)
self.assertEqual(rv.status_code, 200)
示例10: test_distinct
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_distinct(self):
assert User.create(name='jack', email='[email protected]')
assert User.create(name='jack', email='[email protected]')
assert User.create(name='wangchao', email='[email protected]')
assert User.create(name='hit9', email='[email protected]')
query = User.select(fn.count(distinct(User.name)))
result = query.execute()
count = result.tuples()[0][0]
assert count == 3
query = User.orderby(User.id).select(distinct(User.email))
result = query.execute()
assert result.tuples() == (
('[email protected]',), ('[email protected]',), ('[email protected]', )
)
emails = [user.email for user in result.all()]
assert len(emails) == len(set(emails))
query = User.orderby(User.id).select(distinct(User.name, User.email))
try:
result = query.execute()
except Exception: # should raise an error
pass
else:
raise Exception
query = User.orderby(User.id).select(distinct(User.name), User.email)
result = query.execute()
assert result.count == 4
示例11: login
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def login(self):
error = None
login_form_cls = LoginForm
if request.method == 'POST':
form = login_form_cls(request.form)
next_url = request.form.get('next') or "/"
if form.validate():
username = form.username.data
password = form.password.data
active = User.select().where(User.active == True) # noqa
try:
authed_user = active.where(User.username == username).get()
except User.DoesNotExist:
error = True
else:
if not authed_user.check_password(password):
error = True
if not error and authed_user:
self.login_user(authed_user)
return redirect(next_url)
else:
flash('Incorrect username or password')
else:
form = login_form_cls()
next_url = request.args.get('next') or "/"
return render_template(
'login.html',
form=form,
next=next_url)
示例12: test_bad_email
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_bad_email(self):
"""Test email error through our POST route"""
self.data['email'] = 'noatsymbolordomain'
with test_database(self.TEST_DB, (User,)):
rv = self.app.post('/register', data=self.data)
self.assertEqual(User.select().count(), 0)
self.assertEqual(rv.status_code, 200)
示例13: save_message
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def save_message(user_id, message):
type_name = message.get('type_name')
args = message.get('args')
file = message.get('file', '')
conversation_id = message.get('conversation_id')
mt = MessageType.select().where(MessageType.name == type_name).first()
u = User.select().where(User.id == user_id).first()
cps = ConversationParty.select().where(ConversationParty.conversation == conversation_id)
number_of_conversationees = cps.count()
if not mt or not u or not cps or not number_of_conversationees:
raise InvalidMessageDataException('Couldn\'t save message: invalid message data')
m = Message()
with database.transaction():
m.conversation = conversation_id
m.message_type = mt
m.sender_id = user_id
m.ts = datetime.now()
m.file = file
m.content, m.display_content = __save_content(args, mt, user_id, cps)
m.save()
update_conversation(conversation_id=conversation_id,
last_message=m)
mark_message_as_read(user_id=user_id,
message=m,
conversation_id=conversation_id)
message_object = get_message_json(message=m)
message_object['recipient_ids'] = [cp.user.id for cp in cps]
return json.dumps(message_object)
示例14: test_taco_create
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def test_taco_create(self):
taco_data = {
'protein': 'chicken',
'shell': 'flour',
'cheese': False,
'extras': 'Gimme some guac.'
}
with test_database(TEST_DB, (User, Taco)):
UserModelTestCase.create_users(1)
self.app.post('/login', data=USER_DATA)
taco_data['user'] = User.select().get()
rv = self.app.post('/taco', data=taco_data)
self.assertEqual(rv.status_code, 302)
self.assertEqual(rv.location, 'http://localhost/')
self.assertEqual(Taco.select().count(), 1)
def test_taco_list(self):
taco_data = {
'protein': 'chicken',
'shell': 'flour',
'cheese': False,
'extras': 'Gimme some guac.'
}
with test_database(TEST_DB, (User, Taco)):
UserModelTestCase.create_users(1)
taco_data['user'] = User.select().get()
Taco.create(**taco_data)
rv = self.app.get('/')
self.assertNotIn('no tacos yet', rv.get_data(as_text=True))
self.assertIn(taco_data['extras'], rv.get_data(as_text=True))
示例15: mainpage
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import select [as 别名]
def mainpage():
form = Jform()
print request.args
print form.is_submitted()
form.spec.data = '2' # pre-select choice
# user_q = User.select()
user_q = User.select(User.id, User.username).distinct().where(User.username != 'admin')
user_q = user_q.order_by(User.username.asc())
# form.ulist.choices = [ (u.id, u.username) for u in user_q]
form.ulist.choices = [ (str(u.id), u.username) for u in user_q]
print form.ulist.choices
if form.validate_on_submit():
tt = Testing(name=form.name.data,
number=form.number.data,
spec=form.spec.data,
ulist=form.ulist.data,
author='xxxx') #author=users.get_current_user())
tt.save()
flash('Thanks, babe!')
return redirect(url_for('mainpage'))
return render_template('welcome.html', form=form)